How to copy Watchers and comments in script post-function

Hello,

I have a script post-function that will copy an issue to a new project on transition based on some variables. 

 

I added in some Additional Issue Actions in (copying the issue assign) using this code

issue.assignee = sourceIssue.assignee

 

Is there a way to copy over comment or more importantly watchers? I have tried issue.watchers but that is not a valld data source for issue

 

If there is not a way can I get a list of the functions for the issue (like a wiki)

 

 

I CANNOT install any additional add-ons we have a locked environment, I can only use scriptRunner or default JIRA functionality

1 answer

Here is a draft for you. Initilise varialbles issue and source issue properly

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.watchers.WatcherManager

Issue issue
Issue sourceIssue

WatcherManager watcherManager = ComponentAccessor.getWatcherManager();
watcherManager.startWatching(watcherManager.getWatchers(sourceIssue, null), issue);

CommentManager commentManager = ComponentAccessor.getCommentManager();

for(Comment comment: commentManager.getComments(sourceIssue)){
    commentManager.create(issue, comment.authorApplicationUser, comment.body, false)
}

I am having 2 issues with this code.

 

The first one is I am getting an error on this line

watcherManager.startWatching(watcherManager.getWatchers(sourceIssue, null), issue);

error.jpg

 

Also I tried to remove the watchers section and just use the comment section and I am not getting any errors in the code but the copy and everything is failing. Since I do not have any logging setup I do not know the reason for the fail. 

 

 

Also I am on JIRA v6.3.12 if that changes anything

Hm, try this one

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.watchers.WatcherManager
import com.atlassian.jira.user.ApplicationUser

Issue issue 
Issue sourceIssue

WatcherManager watcherManager = ComponentAccessor.getWatcherManager();
for(ApplicationUser user: watcherManager.getWatchers(issue, Locale.FRANCE))    
    watcherManager.startWatching(user, issue);

CommentManager commentManager = ComponentAccessor.getCommentManager();
for(Comment comment: commentManager.getComments(sourceIssue)){
    commentManager.create(issue, comment.authorApplicationUser, comment.body, false)
}

I have tried to use the following (Tested via Script Console)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.watchers.WatcherManager

//Issue issue
//Issue sourceIssue

Issue sourceIssue = ComponentAccessor.getIssueManager().getIssueObject("SUP_TR_NOTIFY-1")
Issue issue = ComponentAccessor.getIssueManager().getIssueObject("SUP_TR_NOTIFY-2")

WatcherManager watcherManager = ComponentAccessor.getWatcherManager();
watcherManager.startWatching(watcherManager.getWatchers(sourceIssue, null), issue);

And the error is : 

 2019-02-14 16:56:24,393 WARN [common.UserScriptEndpoint]: Script console script failed: java.lang.NullPointerException at com.atlassian.jira.issue.comparator.ApplicationUserBestNameComparator.<init>(ApplicationUserBestNameComparator.java:23) at com.atlassian.jira.issue.watchers.DefaultWatcherManager.getWatchers(DefaultWatcherManager.java:144) at com.atlassian.jira.issue.watchers.WatcherManager$getWatchers.call(Unknown Source) at Script148.run(Script148.groovy:14)

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Oct 09, 2018 in Jira Core

How to manage many similar workflows?

I have multiple projects that use variations of the same base workflow. The variations depend on the requirements of the project or issue type. The variations mostly come in the form of new statuses ...

397 views 6 0
Join discussion

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