Nexus3仅下载列表中的50个

时间:2019-06-18 20:27:05

标签: groovy nexus jenkins-groovy nexus3

因此,我有一个代码可以下载每个nuget软件包的版本nr,但是在列表中的50之后全部停止。

我使用带有时髦代码的jenkins并获取版本列表。

import groovy.json.JsonSlurperClassic 
import groovy.json.JsonBuilder
import wslite.rest.*


 def data = new URL("http://nexus.xx.xx.se:8081/service/rest/v1/search?repository=xx-sx-nuget&name=XXXFrontend").getText()  
 println data


/**
 * 'jsonString' is the input json you have shown
 * parse it and store it in collection
 */
 Map convertedJSONMap = new JsonSlurperClassic().parseText(data)


//If you have the nodes then fetch the first one only
if(convertedJSONMap."items"){

     println "Version : " + convertedJSONMap."items"[0]."version"
 }   

def list = convertedJSONMap.items.version
Collections.sort(list)


list

所以问题是它只能获得50个版本。如何获得50以上?我已经读过有关continuetoken的信息,但我不知道该如何使用?


更新

我已添加了此内容,但仍然无法正常工作

while(convertedJSONMap."continuesToken" != null){
def token =  convertedJSONMap."continuationToken"
def data2 = new URL("http://nexus.xxx.xxx.se:8081/service/rest/v1/search?repository=xxx-xx-nuget&name=xxxxxx&continuationToken=" +token).getText() 
 convertedJSONMap = JsonSlurperClassic().parseText(data2)

}

2 个答案:

答案 0 :(得分:0)

这就是我为我解决的方式。这只是我使用的代码的一部分

def json = sendRequest(url)

addResultToMap(map2, json, release) //I do something here with the received result

def continuationToken = json.continuationToken
if (continuationToken != null) {
    while (continuationToken != null) {
        json = sendRequest(url + "&continuationToken=" + continuationToken)
        addResultToMap(map2, json, release) //I do something here with the received result as above
        continuationToken = json.continuationToken
    }
}

我的sendRequest方法看起来像这样

def sendRequest(def url, String method = "GET") {
    String userPass = "${nexus.username}:${nexus.password}"
    String basicAuth = "Basic " + "${printBase64Binary(userPass.getBytes())}"

    def connection = new URL( url ).openConnection() as HttpURLConnection
    connection.setRequestProperty('Accept', 'application/json' )
    connection.setRequestProperty('Authorization', basicAuth)
    connection.setRequestMethod(method)

    try {
        if ( connection.responseCode <= 299 ) {
            if (connection.responseCode == 200) {
                return connection.inputStream.withCloseable { inStream -> new JsonSlurper().parse( inStream as InputStream ) }
            }
        } else {
            displayAndLogError(connection.responseCode + ": " + connection.inputStream.text, loglevel.DEBUG)
        }
    } catch(Exception exc) {
        displayAndLogError(exc.getMessage())
    }
}

答案 1 :(得分:0)

这里是替代方法:

import groovy.json.JsonSlurper

try {
    N_PAGES_MAX = 10
    List<String> versions = new ArrayList<String>()
    continuationToken = "123"
    artifactsUrl = "http://nexus.zzz.local/service/rest/v1/components?repository=releases-super-project"
    currentPage = 1

    while (true) {

        artifactsObjectRaw = ["curl", "-s", "-H", "accept: application/json", "-k", "--url", "${artifactsUrl}"].execute().text
        artifactsJsonObject = (new JsonSlurper()).parseText(artifactsObjectRaw)
        continuationToken = artifactsJsonObject.continuationToken

        if (continuationToken!=null && continuationToken!='123') {
            artifactsUrl = artifactsUrl + "&continuationToken=$continuationToken"
        }

        def items = artifactsJsonObject.items
        for(item in items){
            versions.add(item.name)
        }

        currentPage += 1
        if (continuationToken==null || currentPage>N_PAGES_MAX) break
    }

    return versions.sort().reverse()
}

catch (Exception e) {
    print "There was a problem fetching the versions"
}