How to Copy Attachments from the Latest Comment to Linked Issues in a ScriptRunner Script?

Sophie
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 22, 2024

Hi everyone,

I'm working on a ScriptRunner script in Jira that automatically copies the latest comment and its associated attachments from one issue to all linked issues in the SUP project.

I've successfully managed to filter out the relevant attachments that are mentioned in the latest comment, but I'm stuck on how to actually copy these attachments to the linked issues.
Here's the part of my script where I'm trying to figure out what to do next:

import com.atlassian.jira.component.ComponentAccessor

def attachmentManager = ComponentAccessor.attachmentManager
def commentManager = ComponentAccessor.commentManager

def comments = commentManager.getComments(issue)
def latestComment = comments.last()

def allAttachments = attachmentManager.getAttachments(issue)

def latestAttachments = allAttachments.findAll { attachment ->
    latestComment.body.contains(attachment.filename)
}

def issueLinkManager = ComponentAccessor.issueLinkManager
def linkedIssues = issueLinkManager.getOutwardLinks(issue.id).collect { it.destinationObject }

linkedIssues.each { linkedIssue ->
    if (linkedIssue.projectObject.key == "SUP") {
       
        latestAttachments.each { attachment ->
            // What should I add here to copy the attachments to the linked issues?
        }
        
        commentManager.create(linkedIssue, latestComment.authorApplicationUser, latestComment.body, true)
    }
}

Could someone guide me on what to write in the latestAttachments.each block to properly copy the attachments to the linked issues? Any help or examples would be greatly appreciated!

Thanks in advance!

 

1 answer

0 votes
Tuncay Senturk
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 22, 2024

Hi @Sophie 

Welcome to the Community!

Here is some code that may shed some light, though it hasn't been tested. I hope it helps you achieve your goal.

linkedIssues.each { linkedIssue ->
if (linkedIssue.projectObject.key == "SUP") {
latestAttachments.each { attachment ->
def file = attachmentManager.getAttachmentFile(attachment)
def inputStream = Files.newInputStream(file.toPath())
try {
CreateAttachmentParamsBean bean = new CreateAttachmentParamsBean.Builder().
file(file).
filename(attachment.filename.
issue(issue).
author(attachment.author).
build();
attachmentManager.createAttachment(bean);
} finally {
inputStream.close()
}
}

commentManager.create(linkedIssue, latestComment.authorApplicationUser, latestComment.body, true)
}
}

 

Tuncay Senturk
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 22, 2024

This is the import statement you will need

import com.atlassian.jira.issue.attachment.CreateAttachmentParamsBean;

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events