0

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 ?

Abilash
  • 39
  • 5

1 Answers1

0

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!"
}

ycr
  • 12,828
  • 2
  • 25
  • 45
  • Is there is a way to use the function inside JSL ? not in jenkins file Like by putting the upload id's in a file and use the path inside the JSl and use the loop or use the upload list in the JSL and use the loop – Abilash Dec 02 '22 at 07:40
  • **I used the below script in the Jenkins pipeline,** ========================================================= `def idList = ['8','9'] idlist.each{ each_upload_id -> def delete_upload_url = server_url + "/api/v1/uploads/" + ${each_upload_id} stage('delete upload') { delete_operations.delete_upload(server_url, each_upload_id, authentication) }` **I'm getting this error**`error: groovy.lang.MissingPropertyException: No such property: idlist for class: groovy.lang.Binding` – Abilash Dec 07 '22 at 06:21
  • @Abilash I'm afraid I'm not familiar with JSL. – ycr Dec 07 '22 at 13:40