Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How can I duplicate an attachment to linked issue

Emre Ünal December 17, 2024

 

How can I duplicate an attachment to linked issue on the same project. I already created a workflow, screen and transition. I need a post-function for this. The code I currently use is like this and doesn't work:

 

 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.jira.user.ApplicationUser

def issue = issue
if (!issue) {
    log.error("Issue context is not available. This script must be run in the context of an issue.")
    return
}

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def attachmentManager = ComponentAccessor.attachmentManager

def existingAttachments = issue.getAttachments()

def linkedIssues = issue.getLinkedIssues("blocks")
linkedIssues.each { linkedIssue ->
    def linkedAttachments = linkedIssue.getAttachments()
    linkedAttachments.each { linkedAttachment ->
        def isDuplicate = existingAttachments.any { attachment ->
            attachment.filename == linkedAttachment.filename &&
            attachment.filesize == linkedAttachment.filesize &&
            attachment.mimetype == linkedAttachment.mimetype
        }

        if (!isDuplicate) {
            attachmentManager.copyAttachment(linkedAttachment, currentUser, issue.key)
        }
    }
}

1 answer

0 votes
Tuncay Senturk
Community Champion
December 18, 2024

Hi @Emre Ünal 

Could you try the below code?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.attachment.Attachment

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def attachmentManager = ComponentAccessor.attachmentManager
def issueLinkManager = ComponentAccessor.issueLinkManager

if (!issue) {
log.error("Issue context is not available. This script must be run in the context of an issue.")
return
}

def linkedIssues = issueLinkManager.getInwardLinks(issue.id).findAll { it.issueLinkType.name == "blocks" }.collect { it.sourceObject }

// Check if there are attachments to copy
linkedIssues.each { linkedIssue ->
linkedIssue.getAttachments().each { linkedAttachment ->
def isDuplicate = issue.getAttachments().any { attachment ->
attachment.filename == linkedAttachment.filename &&
attachment.filesize == linkedAttachment.filesize
}

if (!isDuplicate) {
// Copy the attachment
def file = attachmentManager.getAttachmentFile(linkedAttachment)
attachmentManager.createAttachment(file, linkedAttachment.filename, linkedAttachment.mimetype, currentUser, issue)
log.info("Copied attachment '${linkedAttachment.filename}' from issue '${linkedIssue.key}' to '${issue.key}'.")
}
}
}

 

Emre Ünal December 23, 2024

Hi @Tuncay Senturk ,

Thank you for your answer :)

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events