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

How to copy comments with attachments to last linked issue - ScriptRunner

Tiffany Wortham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 31, 2018

Here's how to copy comments with attachments to the last linked issue:

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

def commentManager = ComponentAccessor.getCommentManager()
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def attachmentManager = ComponentAccessor.getAttachmentManager()
def user = ComponentAccessor.getJiraAuthenticationContext().loggedInUser

def issue = event.issue as Issue

lastComment = commentManager.getLastComment(issue)
body = lastComment.getBody()
author = lastComment.getAuthorApplicationUser()

def linkedIssue = issueLinkManager.getLinkCollection(issue, user).getAllIssues().last()

lastAttachment = attachmentManager.getAttachments(issue).last()

attachmentManager.copyAttachment(lastAttachment, user, linkedIssue.key)

commentManager.create(linkedIssue, author, body, false

 

1 comment

Comment

Log in or Sign up to comment
Neta Elyakim
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 1, 2018

Try this:

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

def commentManager = ComponentAccessor.getCommentManager()
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def attachmentManager = ComponentAccessor.getAttachmentManager()
def user = ComponentAccessor.getJiraAuthenticationContext().loggedInUser

def issue = event.issue as Issue

def lastComment = commentManager.getLastComment(issue)
def body = lastComment.getBody()
def author = lastComment.getAuthorApplicationUser()

Issue linkedIssue = issueLinkManager.getLinkCollection(issue, user).getAllIssues().last()
def linkedIssueLastComment = commentManager.getLastComment(linkedIssue).getBody()
//make sure that the listener won't go in a loop
if(linkedIssueLastComment != body){
List<Attachment> lastAttachment = attachmentManager.getAttachments(issue)

lastAttachment.each{ attachment ->
attachmentManager.copyAttachment(attachment, user, linkedIssue.key)
}

commentManager.create(linkedIssue, author, body, false)
}

TAGS
AUG Leaders

Atlassian Community Events