How to copy attachments to linked issue but not replacing existing attachements, just adding new?

Bojana Vasic October 8, 2018

Hi Community,

I am looking for a way to copy attachments to a linked issue through a post function, but without replacing the current attachments, just adding new ones. Basically , I am using ScriptRunner workflow function - clones an issue and links, and in that first step I get all attachments from the original issue (A issue) carried over to the new linked issue (B issue) which belongs to another project. At some point it can happen that after that B issue was resolved, the specific status of A issue will triggers re-opening of B issue, and at that point I need to copy only new attachements from A issue to re-opened B issue, and not replacing existing attachements in B issue.

This is what I have available, but does not work for me bacause it replaces old attachements; 
"Copy field value from linked issues (JMWE add-on) Set the value of a field to the value of the same field in linked issues (e.g. Epic)... replacing existing values."

Many thanks!

 

1 answer

1 accepted

0 votes
Answer accepted
David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 8, 2018

Hi Bojana,

the problem is that, since you are copying attachments between issues, they become different attachments on the destination issue, so it's not really easy to identify "new" attachments from already-copied ones. You could consider identically named attachments as being the same, but that's not exactly always right. And since Jira doesn't store attachment metadata like an MD5 that could uniquely identify attachments across issues, identifying copied attachments is difficult.

Of course, you could use the "Add field values" option, but then all attachments would be copied again, not just new attachments.

If you believe that comparing attachment names and sizes would be enough to avoid duplicates, then I could provide you with a simple Groovy script to be run in a Scripted (Groovy) post-function. Let me know.

David

Bojana Vasic October 11, 2018

Hi @David Fischer

Thanks a lot for your answer! I would appreciate if you could share a script so I can test the solution.

Many thanks!

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 11, 2018

Here's a script that would work as a Scripted (Groovy) post-function on your reopen transition of issue B. It will copy new attachments from issues linked to the current B issue through the "blocks" link type (adjust as needed) to the current B issue.

import com.atlassian.jira.issue.attachment.Attachment

//the current issue's existing attachments
def attachments = issue.get("attachment")

//iterate over the issues linked to the current issue through the "blocks" link type - change the link type as needed
for (Issue linkedIssue : issue.getLinkedIssues("blocks")) {
//add new attachments from linked issue
def linkedAttachments = linkedIssue.get("attachment")
for (Attachment linkedAttachment : linkedAttachments) {
//test if this attachment from the linked issue already exists on the current issue
if (!attachments.find{it.filename == linkedAttachment.filename && it.filesize == linkedAttachment.filesize && it.mimetype == linkedAttachment.mimetype})
//this attachment is new, copy it to the current issue
ComponentAccessor.attachmentManager.copyAttachment(linkedAttachment, currentUser, issue.key)
}
}
Bojana Vasic October 11, 2018

Thanks again, will test.

Bojana Vasic October 16, 2018

Hi again @David Fischer,

Will this keep attachements which already exist in B issue (which are not originally from A issue), or will those be replaced once attachements are compared and copied from A to B on the reopening? I need to keep all originally attached files in B not replacing them with attachements from A, and adding just the new ones from A but not replacing all from B as said...

Thanks again! :)

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 16, 2018

It will only add new attachments, never remove any (even those that are removed from issue A).

Swarna Radha August 20, 2019

Hi, 

I am getting errors in the script as from for (Issue linkedIssue : issue.getLinkedIssues("blocks")) { till the end of the code

 

import com.atlassian.jira.issue.attachment.Attachment

//the current issue's existing attachments
def attachments = issue.get("attachment")

//iterate over the issues linked to the current issue through the "blocks" link type - change the link type as needed
for (Issue linkedIssue : issue.getLinkedIssues("blocks")) {
//add new attachments from linked issue
def linkedAttachments = linkedIssue.get("attachment")
for (Attachment linkedAttachment : linkedAttachments) {
//test if this attachment from the linked issue already exists on the current issue
if (!attachments.find{it.filename == linkedAttachment.filename && it.filesize == linkedAttachment.filesize && it.mimetype == linkedAttachment.mimetype})
//this attachment is new, copy it to the current issue
ComponentAccessor.attachmentManager.copyAttachment(linkedAttachment, currentUser, issue.key)
}
}Capture 2.PNG
David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 21, 2019

Hi,

The code I shared is for JMWE, not ScriptRunner. That is why it is failing in ScriptRunner. 

Regards

David

Swarna Radha August 21, 2019

Hi,

Is there any code for script runner?

Thanks,

Swarna

Suggest an answer

Log in or Sign up to answer