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!
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)
}
}
This is the import statement you will need
import com.atlassian.jira.issue.attachment.CreateAttachmentParamsBean;
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.