Search code examples
groovyjenkins-pipelineartifactory

How to get list of directories from Artifactory repo using groovy


I have the below groovy script in one of my Jenkins Active choice parameters:

import groovy.json.JsonSlurper
import jenkins.model.Jenkins
versions = getArtifactsVersions()
def getArtifactsVersions(){
   def responseJson = "curl -k -X GET https://{Artifatory_URL}/storage/my-repo/".execute().text
   def projectList = (new groovy.json.JsonSlurper().parseText(responseJson)).children
   return projectList
}

Its supposes to return list of Folders which exists in this path (without subdirctories), however, the result I gets from it is:

{uri=/TEST_FOR_YONI, folder=true}
{uri=/TEST_FOR_PKMLODEL_V2, folder=true}

how can I change it to return:

TEST_FOR_YONI
TEST_FOR_PKMLODEL_V2

For debug, I ran the below:

import groovy.json.JsonSlurper
import jenkins.model.Jenkins
def responseJson = "curl -k -X GET https://{Artifatory_URL}/storage/my-repo/".execute().text
print responseJson

and it reruns the below:

{
  "repo" : "my-repo",
  "path" : "/",
  "created" : "2020-11-29T18:00:42.635+02:00",
  "lastModified" : "2020-11-29T18:00:42.635+02:00",
  "lastUpdated" : "2020-11-29T18:00:42.635+02:00",
  "children" : [ {
    "uri" : "/TEST_FOR_YONI",
    "folder" : true
  }, {
    "uri" : "/TEST_FOR_PKLMODEL_V2",
    "folder" : true
  }
  ],
  "uri" : "https://{Artifatory_URL}/storage/my-repo/"
}

Solution

  • def responseJson='''
    {
      "repo" : "my-repo",
      "path" : "/",
      "created" : "2020-11-29T18:00:42.635+02:00",
      "lastModified" : "2020-11-29T18:00:42.635+02:00",
      "lastUpdated" : "2020-11-29T18:00:42.635+02:00",
      "children" : [ {
        "uri" : "/TEST_FOR_YONI",
        "folder" : true
      }, {
        "uri" : "/TEST_FOR_PKLMODEL_V2",
        "folder" : true
      }
      ],
      "uri" : "https://{Artifatory_URL}/storage/my-repo/"
    }
    '''
    
    
    def projectList = new groovy.json.JsonSlurper().parseText(responseJson).children.collect{ it.uri.substring(1) }