Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,361,480
Community Members
 
Community Events
168
Community Groups

Groovy Script to clone comments from Current issue to the Linked issues

Hello!

I'm trying to add a Script Post-Function [ScriptRunner] which will clone all the comments from the current issue to the linked issue (with the specific link type). 

I'm not good in groovy, so I found a couple of examples, changed and merged them into one script, which I suppose should do what I need.  

There is no errors displayed when I open the script in the post-function editor, it also says that there is no failures in the all executions, but the comments are not cloned to the linked issue. 

Please, help to understand why this script does not clone the comments from the current issue to the linked issue. 

 

import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.plugin.webfragment.model.JiraHelper;

IssueManager issueManager = ComponentAccessor.getIssueManager();
CommentManager commentMgr = ComponentAccessor.getCommentManager();
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
JiraHelper jiraHelper = new JiraHelper();
Issue currIssue = (Issue) jiraHelper.getContextParams().get("issue");

issueLinkManager.getOutwardLinks(issue.id)?.each {issueLink ->;
def linkedIssue = issueLink.destinationObject
String issueType = issueLink.destinationObject.getIssueType().toString()
if (issueLink.issueLinkType.name == "Implementation" ) {
Collection<Comment> comments = commentMgr.getComments(currIssue);
for (Comment comment : comments) {
String groupLevel = comment.getGroupLevel();
Long roleLevelId = comment.getRoleLevelId();
Date created = comment.getCreated();
Date updated = comment.getUpdated();
ApplicationUser author = comment.getAuthorApplicationUser();
ApplicationUser updateAuthor = comment.getUpdateAuthorApplicationUser();
commentMgr.create(linkedIssue, author, updateAuthor, comment.getBody(),groupLevel, roleLevelId, created, updated, false, false);
}
}
}

 

1 answer

I found a problem in the script, the problem was that I tried to use a variable

Issue currIssue = (Issue) jiraHelper.getContextParams().get("issue");

for accessing the current issue. 

here is the script that works for me: 

import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.plugin.webfragment.model.JiraHelper;

IssueManager issueManager = ComponentAccessor.getIssueManager();
CommentManager commentMgr = ComponentAccessor.getCommentManager();
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
issueLinkManager.getOutwardLinks(issue.id)?.each {issueLink ->;
def linkedIssue = issueLink.destinationObject
String issueType = issueLink.destinationObject.getIssueType().toString()
Collection<Comment> comments = commentMgr.getComments(linkedIssue);
if (issueLink.issueLinkType.name == "Implementation" ) {
for (Comment comment : comments) {
String groupLevel = comment.getGroupLevel();
Long roleLevelId = comment.getRoleLevelId();
Date created = comment.getCreated();
Date updated = comment.getUpdated();
ApplicationUser author = comment.getAuthorApplicationUser();
ApplicationUser updateAuthor = comment.getUpdateAuthorApplicationUser();
commentMgr.create(issue, author, updateAuthor, comment.getBody(),groupLevel, roleLevelId, created, updated, false, false);
}
}
}

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Online AMA this week: Your project management questions answered by Jira Design Lead James Rotanson

We know that great teams require amazing project management chops. It's no surprise that great teams who use Jira have strong project managers, effective workflows, and secrets that bring planning ...

208 views 1 6
Read article

Atlassian Community Events