Script Listener Clone Issue not copying comments

Anthony Cardone December 27, 2017

Hello,

I'm trying to use a script listener clone issue and links on the 'Issue Deleted' event to serve as a temporary recovery mechanism for deleted issues, but it's not copying over comments or attachments, even though it's set to copy all field and 'Copy Comments' is checked. 

Please advise? Am I doing something wrong?

Thanks!

-Anthony

 

Screenshot.png

3 answers

1 accepted

1 vote
Answer accepted
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 27, 2017

Hello,

I did not check my guess, but I think the problem is that the event fires after the issue was deleted and the issue object, which you reference, is just the "mirror" of the issue that was deleted. And this "mirror" issue does not contain comments but only issue fields. But I guess the listener takes comments from the real issue but the real issue is already gone and there are no comments available. I think the copy comment option works only for issues that still exist.

That is my guess. I did not check it.

Anthony Cardone December 27, 2017

Hm, I'm concerned it's something like that. I tried wiring it to IssuePreDeleteEvent but it doesn't seem to trigger when i delete something (script listener doesn't fire and doesn't error when I delete a ticket)

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 27, 2017

It triggers but it has an error

.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'IssuePreDeleteEvent{time=Wed Dec 27 20:39:33 UTC 2017, params={}, issue=BP-6, user=admin(admin)}' with class 'com.atlassian.jira.event.issue.IssuePreDeleteEvent' to class 'com.atlassian.jira.event.issue.IssueEvent'
    at com.onresolve.scriptrunner.canned.jira.utils.AbstractCloneIssue.doScript(AbstractCloneIssue.groovy:193)
    at com.onresolve.scriptrunner.canned.jira.utils.CopyIssueWithAttachments.super$2$doScript(CopyIssueWithAttachments.groovy)
    at com.onresolve.scriptrunner.canned.jira.utils.CopyIssueWithAttachments.doScript(CopyIssueWithAttachments.groovy:13)
    at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CloneIssue.super$3$doScript(CloneIssue.groovy)
    at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CloneIssue.doScript(CloneIssue.groovy:114)

I guess this listener can not work for this event

Anthony Cardone December 27, 2017

Thank you though for trying...  before calling this closed I'll still try a few more things, see if there's any alternate way or event I'm not thinking of

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 27, 2017

I just checked everything. I think I was right about the issue deleted event. The issue is not available already and you can not get comments. That is why you should use the IssuePreDeleteEvent. The issue still exists. You can create a custom listener and  write some code to clone an issue.

import com.atlassian.jira.issue.IssueFactory
import com.atlassian.jira.component.ComponentAccessor
def issueFactory = ComponentAccessor.getComponent(IssueFactory.class)
def iss = issueFactory.cloneIssueWithAllFields(event.issue)

Also you need to handle comments, attachments yourself.

Anthony Cardone December 27, 2017

Thank you

0 votes
Gaston Valente
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 27, 2017

Ok Anthony, try with something like this:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.comments.Comment;

def commentManager = ComponentAccessor.getCommentManager();
def comments = commentManager.getComments(issue);

if(comments)
{
for(Comment comment : comments)
{
commentManager.create(newIssue, comment.getAuthorApplicationUser(), comment.body?: "", true);
}

}
Anthony Cardone December 27, 2017

Tried this, but it was showing an error. Saved and ran, but it doesn't clone the issue. Screen Shot 2017-12-27 at 1.56.00 PM.png

Gaston Valente
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 27, 2017

You need to replace newIssue with the variable that represents the newly created issue, i do not remember the exact var name

Anthony Cardone December 27, 2017

I'll do some digging.... I found something indicating it was event.Issue, which made the code pass, but the listener failed with this message:

Time (on server): Wed Dec 27 2017 13:55:23 GMT-0500 (EST)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2017-12-27 13:55:23,913 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2017-12-27 13:55:23,914 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, script: com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CloneIssue
java.lang.NullPointerException
 at com.atlassian.jira.issue.managers.DefaultIssueManager.getEntitiesByIssue(DefaultIssueManager.java:395)
 at com.atlassian.jira.issue.managers.DefaultIssueManager.getEntitiesByIssueObject(DefaultIssueManager.java:410)
 at com.atlassian.jira.issue.managers.RequestCachingIssueManager.getEntitiesByIssueObject(RequestCachingIssueManager.java:155)
 at com.atlassian.jira.issue.comments.CommentSearchManager.getComments(CommentSearchManager.java:106)
 at com.atlassian.jira.issue.comments.DefaultCommentManager.getComments(DefaultCommentManager.java:176)
 at com.atlassian.jira.issue.comments.CommentManager$getComments.call(Unknown Source)
 at Script125.run(Script125.groovy:5)  

 

Anthony Cardone December 27, 2017

No luck so far, but digging... event.Issue stops the code error but the script listener fails to run.

Daniel Caloto January 13, 2018

HI Anthony.

Try with this code

def commentManager = ComponentAccessor.getCommentManager()
List<Comment> comments = commentManager.getComments(issue)
if (comments != null && comments.size() > 0) {
for (Comment comment : comments) {
comment.authorApplicationUser
commentManager.create(newIssue, comment.authorApplicationUser, comment.getBody(), true)
}
}
0 votes
Gaston Valente
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 27, 2017

Anthony, 

Does the user configured in the field "As User" have comment permissions?

Let me know, if that's ok maybe we can try by adding some code at the additional issue actions to copy the comments

Anthony Cardone December 27, 2017

Hi Gaston!

Confirmed, the 'As User' is part of a small admin group that has been granted every permission in the permission scheme, including add comment.

I welcome and/all code! :)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events