The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

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

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

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

0 votes
Tuncay Senturk _Snapbytes_
Community Champion
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 _Snapbytes_
Community Champion
August 22, 2024

This is the import statement you will need

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

TAGS
AUG Leaders

Atlassian Community Events