Hello.
I'd like to copy attachments from one Issue from one Jira instance to an other Issue in a other Jira instance. Both Jira instances are "Data Center". For this I use JIRA API to get attachments from the source instance and use the REST API to add attachments to the destination instance.
But I don't know how to pass attachments to the REST API. In my code I set body with the attachment filename, but of course is not correct. Any idea to pass the data (stream ?) of the attachment (with or without REST API) ? My code is a Groovy Scriptrunner.
Thanks for any help
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.jira.issue.AttachmentManager
import groovyx.net.http.*
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.URIBuilder
import javax.servlet.http.HttpServletRequest
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
import static groovyx.net.http.Method.*
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def sourceAttachmentManager = ComponentAccessor.getComponent(AttachmentManager)
def sourceIssue = ComponentAccessor.getIssueManager().getIssueByCurrentKey(sourceIssueKey)
def sourceAttachments = sourceAttachmentManager.getAttachments(sourceIssue)
def sourceBaseUrl = "URL SOURCE INSTANCE"
def sourceIssueKey = "SOURCE ISSUE"
def destinationBaseUrl = "URL DESTINATION INSTANCE"
def destinationIssueKey = "DESTINATION ISSUE"
def http = new HTTPBuilder(destinationBaseUrl, ContentType.JSON)
sourceAttachments.each { Attachment attachment ->
try {
http.request(POST, ContentType.JSON) {
headers.'Accept' = '*/*'
headers.'Content-Type' = 'multipart/form-data'
headers.'Authorization' = "Bearer " + "TOKEN"
headers.'X-Atlassian-Token' = 'nocheck'
uri.path = '/rest/api/2/issue/' + destinationIssueKey + '/attachment'
body = attachment.filename
response.success = { resp, reader ->
log.error(resp)
}
response.failure = { resp, reader ->
log.error(resp.statusLine)
}
}
}
catch (Exception e) {
log.error(e.getMessage())
}
}