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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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 Statuspage

New feature: Slack notifications for Statuspage

We're excited to announce the release of a long-requested feature on Statuspage. Now visitors to your status page can subscribe to get notified in Slack when you report an incident or maintenance. Th...

81 views 0 9
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you