Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

ScriptRunner - Copy Attachments of the Project X -> Project Y

Bruno Mesquita dos Santos
Contributor
May 3, 2022

I created a code that when I change the status of a issue, this code run:

_______________________________________________________________________________________________

if (issue.fields.attachment) {
        issue.fields.attachment.collect { attachment ->
        def url = attachment.content as String
        def ind = url.indexOf("/secure")
        if (ind > -1){
            url = url.substring(url.indexOf("/secure"))
        }
        
        def fileBody = Unirest.get("${url}").asBinary().body
        for(def linkedItem in issue.fields.issuelinks) {
            def relatedIssueId = -1
            if (linkedItem.outwardIssue != null){
                relatedIssueId = linkedItem.outwardIssue.id
            }else if(linkedItem.inwardIssue != null){
                relatedIssueId = linkedItem.inwardIssue.id
            }else{
                println("both are missing")
                return
            }
            def relatedIssue = Unirest.get("/rest/api/2/issue/${relatedIssueId}")
                    .header("Content-Type", "application/json")
                    .asObject(Map).body
            //verify if the current attachment already exists in the destination
            if(!relatedIssue.fields.attachment.find{ it.filename == attachment['filename'] as String && it.size == attachment['size'] as int}){
              def resp = Unirest.post("/rest/api/2/issue/${relatedIssueId}/attachments")
                  .header("X-Atlassian-Token", "no-check")
                  .field("file", fileBody, ContentType.create(attachment['mimeType'] as String), attachment['filename'] as String)
                  .asObject(List)
              assert resp.status >= 200 && resp.status < 300
            }
        }
    }
}

_______________________________________________________________________________________________

But, sometimes I am reciving a error code 500:

2022-05-03 14:03:32.156 WARN - POST request to /rest/api/2/issue/87180/attachments returned an error code: 500, retrying...
2022-05-03 14:03:32.270 ERROR - org.apache.http.NoHttpResponseException: us.argon.polaris.connect.product.adaptavist.com:443 failed to respond
2022-05-03 14:03:32.292 ERROR - Class: com.adaptavist.sr.cloud.workflow.RunScript, Config: [className:com.adaptavist.sr.cloud.workflow.RunScript, uuid:21047bce-d12b-41ca-aca6-d9bbbb8bc7f0, enabled:true, executionUser:ADD_ON, 

 

Does anyone know why happening ?  

0 answers

Suggest an answer

Log in or Sign up to answer