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.
I found in another answer that you have to set the name of the body part to the string "file". This works for me.
I have this same issue. Very annoying, please fix.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Álvaro Jesús Jardón Cavero
It seems that the body is not valid for your API call and that is the reason why you are not to create the attachment. You will need to write your own custom script which gets the details of the attachment from the source issue and then calls the Add attachment API which is documented here to add each attachment to the required issue to copy it over.
Hope this will help you.
Thank you.
Kind Regards
Kate
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Kate,
Actually that's what the script I posted does, get the attachment and call the add attachment api...
Also, how is it I get a 200 status code if it's not valid?
Thanks, kind regards,
Alvaro.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It is unacceptable that Jira does not return an error status if it is not going to accept the request and act on it.
I'm having similar problems where .zip files are silently rejected by Jira.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.