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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,559,114
Community Members
 
Community Events
184
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
AUG Leaders

Atlassian Community Events