简体   繁体   中英

How to make loop function using multiple input value to JSL in Groovy

I'm using the below function in Jenkins Shared Library.

/* The below function delete uploads that exist in the server. */

 

def delete_upload(server_url,each_upload_id,authentication){

 

    def delete_upload_url  = server_url + "/api/v1/uploads/" + each_upload_id

  

    def response =  httpRequest consoleLogResponseBody: true,

                    contentType: 'APPLICATION_JSON',

                    customHeaders: [[maskValue: false, name: 'id ', value: each_upload_id],

                    [maskValue: false, name: 'Authorization', value: authentication]],

                    httpMode: 'DELETE', ignoreSslErrors: true, responseHandle: 'NONE', url: delete_upload_url,

                    validResponseCodes: '100:599'

  

    if(response.status == 202){

    def result = readJSON text: """${response.content}"""

    return result['message'].toString()

    }

    else {

        throw new Exception("Incorrect upload id! Please give the correct upload id.")

    }

}

====================================================================================================

I'm getting below response ,

Response Code: HTTP/1.1 202 Accepted Response: {"code":202,"message":"Delete Job for file with id 2","type":"INFO"} Success: Status code 202 is in the accepted range: 100:599

====================================================================================================

Purpose : I'm using the above JSL function to delete a uploads in the web server using upload id.

Requirement :

I need to delete multiple uploads by using multiple upload id's (like each_upload_id in 1,2,3 etc) using this JSL delete function.

Need to pass the upload id's in loops and delete the uploads in the web server.

Any suggestions, please?

Are you looking for something like this?

def idList = ["1", "2", "3"]

try {
   idList.each{ id =>
     delete_upload(server_url,id,authentication)
   }
} catch(e) {
println "Error occurred!"
}

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