Greetings,
I'm trying to copy an attachment from a Jira-Software issue to a Jira-ServiceDesk issue (and the other direction as well) by using the ScriptRunner addon (script listener).
Basically I have the "linked issue key" on a text field, so when the attachment is added i get the linked issue by searching the issue with the key retrieved from this field and the attachments added from the event, create a multipartentity and use a /rest/api/3/issue/<issueKey>/attachments POST to upload them to the linked issue.
The issue here is, I get a 200 OK status response, but no attachment is added to the linked issue.
So far I'm testing this on a free instance, to be used on a client instance (I believe Standard plan if I'm not wrong)
The full code is as follows:
if(user.accountId == "557058:d2e5bd5c-dc49-41eb-a6f0-5e01093666c1"){return} //updated by a user different than the scriptrunner addon user
if(!changelog.items*.field.contains("Attachment")){return} //Only if there're attachments added.
import org.apache.http.entity.mime.MultipartEntityBuilder
import org.apache.http.entity.mime.HttpMultipartMode
import org.apache.http.entity.mime.content.ByteArrayBody
//ID of integrated projects
def jsmPrId = "10001" //Id of the JSM project
def jswPrId = "10000" //Id of the JSW project
//ID of customfields
def linkCfId = "customfield_10054" //id of the Linked Issue Key custom field
def linkedKey = get("/rest/api/3/issue/${issue.key}?fields=${linkCfId}")
.header('Content-Type', 'application/json')
.asObject(Map).body.fields[linkCfId]
def attachment
def fileBody
def multipart
for(i in changelog.items){
if(i.field == "Attachment" && i.to){
attachment = get("/rest/api/3/attachment/${i.to}")
.header('Content-Type', 'application/json')
.asObject(Map).body
fileBody = get("${attachment.content.substring(attachment.content.indexOf('/secure'))}").asBinary().body
multipart = MultipartEntityBuilder
.create()
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.addPart(
"file",
new ByteArrayBody(
new byte[fileBody.available()],
attachment.filename.toString()
)
)
.build()
post("/rest/api/3/issue/${linkedKey}/attachments")
.header('Content-Type', "${multipart.properties.contentType.value}")
.header('X-Atlassian-Token', 'no-check')
.body(multipart)
.asObject(Map)//.body
}
}
Any ideas of why this is not working are more than welcome.
Also, testing this on Script Console (for a single attachment) gives a statuscode 200 OK and no attachment is added to the linked issue.

Actually there's another thing about this, if the attachment is bigger than certain contentLength it gives a contentLength too high error (I didn't measure this, basically it did throw the error with a .png file, but not with a test .pdf and a test .txt files)
Thanks in advance, kind regards,
Alvaro.