Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Copy attachments between 2 issue on different projects

Claire Berry
Contributor
July 2, 2024

I have set up a global automation to copy attachments between 2 types of issues on 2 different projects.

When an attachment is added to the issue in project A, I want the attachment to copy over to the linked issue on project B.

The issue I am having with the automation is that it is duplicating the attachments.

For example, let's say I add one attachments image1.png to the issue in project A. This attachment then gets copied to the linked issue in project B (correct).
Then I add another attachment image2.png to the issue in project B.
But instead of just image2.png copying over to the issue in project A, both image1.png AND image2.png copy over.
So I end up with duplicate attachments on the issue in project A: image1.png, image1.png, image2.png.

Can anyone help configuring what I am trying to achieve here? I only want to copy over the new attachment added, not all existing attachments. 

Here is the automation:
attachment-automation.png

3 answers

1 accepted

0 votes
Answer accepted
Claire Berry
Contributor
July 4, 2024

Ended up getting this working with a custom script (thanks Scriptrunner!)

attachment-automation1.png

Full script here:

import com.atlassian.jira.component.ComponentAccessor

def attachmentManager = ComponentAccessor.attachmentManager

def user = Users.getByName('Automated-Jira-User')

def linkedIssues = ComponentAccessor.getIssueLinkManager().getLinkCollection(issue, user).getAllIssues()

def redirectedIssue

// Get the redirected linked issue

linkedIssues.each {

if(it.getLabels().toString().contains('ukisr-redirected-ticket') || it.getIssueType().name == 'Software Project Redirect v2' || it.getIssueType().name == 'Software Project Redirect - 1 approval'){

redirectedIssue = it

}

}

if(!redirectedIssue){ return }


// Get current attachments for both issues

def attachments = issue.attachments

def redirectedAttachments = redirectedIssue.attachments

def redirectedAttachmentsFileNames = []

// Get all file names for attachments in linked issue to compare later

redirectedAttachments.each{ redirectedAttachment ->

redirectedAttachmentsFileNames += redirectedAttachment.filename

}

attachments.each { attachment ->

// If attachment doesn't already exist on linked ticket, add it

if(!redirectedAttachmentsFileNames.contains(attachment.filename)){

// log.warn('attachment not on linked issue')

attachmentManager.copyAttachment(attachment, user, redirectedIssue.key)

}

}
0 votes
Rostam Kilgour
Contributor
December 12, 2024

I went with a simpler solution to this problem.

There is a delete all attachments function. So I just delete all the attachments and copy them back again to get round this whenever an issue has its attachment field edited.

0 votes
Bibek Behera
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 2, 2024

Watching this I have some suggestions on the cloud that I have checked on, not for the DC/Server.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events