ScriptRunner: Clone issue with comments

Hi there! I want to run a custom post-function that clones a issue with their comments.

I use this code:

import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.WorkflowContext
CommentManager commentMgr = ComponentAccessor.getCommentManager();
Collection<Comment> comments = commentMgr.getComments(sourceIssue);
for (Comment comment : comments) {
    commentMgr.create(issue, comment.getAuthorApplicationUser(), comment.getBody(), false);
}

 

But when i run the script, i get this error:

2016-04-15 17:41:00,279 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2016-04-15 17:41:00,280 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: MDP-270, actionId: 31, file: null
java.lang.NullPointerException
	at com.atlassian.jira.issue.comments.DefaultCommentManager.create(DefaultCommentManager.java:258)
	at com.atlassian.jira.issue.comments.DefaultCommentManager.create(DefaultCommentManager.java:224)
	at com.atlassian.jira.issue.comments.DefaultCommentManager.create(DefaultCommentManager.java:212)
	at com.atlassian.jira.issue.comments.DefaultCommentManager.create(DefaultCommentManager.java:193)
	at com.atlassian.jira.issue.comments.DefaultCommentManager.create(DefaultCommentManager.java:180)
	at com.atlassian.jira.issue.comments.DefaultCommentManager.create(DefaultCommentManager.java:167)
	at com.atlassian.jira.issue.comments.DefaultCommentManager.create(DefaultCommentManager.java:173)
	at com.atlassian.jira.issue.comments.CommentManager$create$7.call(Unknown Source)
	at Script207.run(Script207.groovy:11)

Can you help me with this problem?

Thank you very much.

3 answers

0 vote

A stacktrace without providing the JIRA version is not that useful.

I'd guess that comment.getAuthorApplicationUser() is null in one case. Does it never work? Or does it only not work on some issues?

Hi! I forget to mention the version. The JIRA version is 6.4.13.

It always fail!

Steps:

  1. Assign this post function to a transition
  2. Create new issue
  3. Make the transition

I modify the code to this. Now, no longer fails, but the new issue does not contain any comment.

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.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.WorkflowContext
IssueManager issueManager = ComponentAccessor.getIssueManager();
CommentManager commentMgr = ComponentAccessor.getCommentManager();
Collection<Comment> comments = commentMgr.getComments(sourceIssue);
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);
}

Don't accept the answer unless it's answered the question.

What's in the logs?

No logs were found for this execution.

The code is success, but does not effect

Another comment:

To set that code i do this steps:

  1. Add Post Function to Transition
  2. Select --> Script Post-Function
  3. Select --> Clones an issue and links
  4. Insert code here --> Additional issue actions

I think the problem is the target issue doesn't really exist at that point. 

You could try this if you have the version just released. But it's not doc'd and the type checker will complain:

...
doAfterCreate = { ->
    for (Comment comment : comments) {
        ...
        commentMgr.create(...)
    }
}

No luck ;(, same as before. Type checker complains, the code run OK, but after cloning the task, the comments do not appear

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published 10 hours ago in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

34 views 0 3
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you