Missed Team ’24? Catch up on announcements here.

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

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

Elena Oleksenko August 28, 2019

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

Suggest an answer

Log in or Sign up to answer
0 votes
Elena Oleksenko September 4, 2019

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);
}
}
}
TAGS
AUG Leaders

Atlassian Community Events