简体   繁体   中英

How to sort and extract data from JFrog JSON response using groovy for Jenkins pipelining

I am using OS version of JFrog Artifactory for my CI-CD activities which run via the Jenkins pipeline. I am novice to groovy/java The REST APIs of OS JFrog Artifactory do not support the extraction of the latest build from a repository. With Jenkins pipeline in play, I was wondering if i could extract the data from the JSON response provided by Artifactory using Jenkins native groovy support(just to avoid external service which can be run via python/Java/Shell).

I am looking to put the extracted JSON response into a Map, sort the Map in descending order and extract the first Key-Value pair which contains the latest build info. I end up getting "-1" as the response when I try to extract the data.

import groovy.json.JsonSlurper

def response = httpRequest authentication: 'ArtifactoryAPIKey', consoleLogResponseBody: false, contentType: 'TEXT_PLAIN', httpMode: 'POST', requestBody: '''
    items.find({
    "$and": [
        {"repo": {"$match": "libs-snapshot-local"}},
        {"name": {"$match": "simple-integration*.jar"}}
            ]
     })''', url: 'http://<my-ip-and-port-info>/artifactory/api/search/aql'

def jsonParser = new JsonSlurper()
Map jsonOutput = jsonParser.parseText(response.content)
List resultsInfo = jsonOutput['results']
print(resultInfo[0].created)

def sortedResult = resultInfo.sort( {a, b -> b["created"] <=> a["created"] } )
sortedResult.each { 
 println it
}

The sample JSON to be parsed:

    {
"results" : [ {
  "repo" : "libs-snapshot-local",
  "path" : "simple-integration/2.5.150",
  "name" : "simple-integration-2.5.150.jar",
  "type" : "file",
  "size" : 1175,
  "created" : "2019-06-23T19:51:30.367+05:30",
  "created_by" : "admin",
  "modified" : "2019-06-23T19:51:30.364+05:30",
  "modified_by" : "admin",
  "updated" : "2019-06-23T19:51:30.368+05:30"
},{
  "repo" : "libs-snapshot-local",
  "path" : "simple-integration/2.5.140",
  "name" : "simple-integration-2.5.140.jar",
  "type" : "file",
  "size" : 1175,
  "created" : "2019-06-21T19:52:40.670+05:30",
  "created_by" : "admin",
  "modified" : "2019-06-21T19:52:40.659+05:30",
  "modified_by" : "admin",
  "updated" : "2019-06-21T19:52:40.671+05:30"
},{
  "repo" : "libs-snapshot-local",
  "path" : "simple-integration/2.5.150",
  "name" : "simple-integration-2.5.160.jar",
  "type" : "file",
  "size" : 1175,
  "created" : "2019-06-28T19:58:04.973+05:30",
  "created_by" : "admin",
  "modified" : "2019-06-28T19:58:04.970+05:30",
  "modified_by" : "admin",
  "updated" : "2019-06-28T19:58:04.973+05:30"
} ],
"range" : {
  "start_pos" : 0,
  "end_pos" : 3,
  "total" : 3
}
}






  //The output i am looking for: Latest build info with fields "created" and "name"
def jsonOutput = new groovy.json.JsonSlurper().parseText('''
{
"results" : [ {
  "repo" : "libs-snapshot-local",
  "path" : "simple-integration/2.5.150",
  "name" : "simple-integration-2.5.150.jar",
  "type" : "file",
  "size" : 1175,
  "created" : "2019-06-23T19:51:30.367+05:30",
  "created_by" : "admin",
  "modified" : "2019-06-23T19:51:30.364+05:30",
  "modified_by" : "admin",
  "updated" : "2019-06-23T19:51:30.368+05:30"
},{
  "repo" : "libs-snapshot-local",
  "path" : "simple-integration/2.5.140",
  "name" : "simple-integration-2.5.140.jar",
  "type" : "file",
  "size" : 1175,
  "created" : "2019-06-21T19:52:40.670+05:30",
  "created_by" : "admin",
  "modified" : "2019-06-21T19:52:40.659+05:30",
  "modified_by" : "admin",
  "updated" : "2019-06-21T19:52:40.671+05:30"
},{
  "repo" : "libs-snapshot-local",
  "path" : "simple-integration/2.5.150",
  "name" : "simple-integration-2.5.160.jar",
  "type" : "file",
  "size" : 1175,
  "created" : "2019-06-28T19:58:04.973+05:30",
  "created_by" : "admin",
  "modified" : "2019-06-28T19:58:04.970+05:30",
  "modified_by" : "admin",
  "updated" : "2019-06-28T19:58:04.973+05:30"
} ],
"range" : {
  "start_pos" : 0,
  "end_pos" : 3,
  "total" : 3
}
}
''')

def last = jsonOutput.results.sort{a, b -> b.created <=> a.created }[0]
println last.created
println last.name

The problem here is not with Groovy code but the Jenkins pipeline. This code as part of the question, and the solution provided by @daggett works charm on any Groovy IDE But, Fails when run via jenkins pipeline.

The issue URL: https://issues.jenkins-ci.org/browse/JENKINS-44924

I hope they fix it soon. Thanks for your help guys.

Here goes the link to full solution I was working on. FYI, It can help someone https://github.com/AravinthR/Py-JFrog-Fetch-Build

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM