Groovy Sript Runner - Working from console but not as post-function

Hi Everybody,

I have a problem with a quite simple groovy script. The script shall remove all watchers from an issue after issue creation. I want to implement it as a post-function after the creation. The script runs fine from the Script Runner Console (with the commented line uncommented and the next one commented instead) but if I save it and append it to the create transission, I get a null pointer.

Here is the code:

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import java.util.Locale;

def watcherManager = ComponentManager.getInstance().getWatcherManager();

//MutableIssue issue = componentManager.getIssueManager().getIssueObject("UMWCC-2");
Issue issue = issue;

List<ApplicationUser> watchers = watcherManager.getWatchers(issue,Locale.getDefault());
for (ApplicationUser user:watchers){
        watcherManager.stopWatching(user,issue);
}

There is not even a StackTrace in the log. All there is is:

2014-10-23 19:23:42,898 http-bio-8080-exec-18 ERROR chris 1163x12866x1 135bdgr 192.168.195.54 /secure/QuickCreateIssue.jspa [groovy.canned.utils.ConditionUtils] javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method contains() on null object

 

I tried a bit of debugging, using the

import org.apache.log4j.Category
def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug "debug statements"

routine with some calls before and after the

watcherManager.stopWatching(user,issue);

But the loop seems to work well...

 

Can somebody help me here?

 

Thanks,

   Chris

6 answers

This widget could not be displayed.

It's not your script that's causing a problem, it's something else on the workflow. Does this error go away if you don;t add your script to the workflow at all?

This widget could not be displayed.

You are absolutly right! It looks like the problem is caused by a built-in Script Runner script, where I check an empty checkbox field for a containing value. I am not quite sure why this happens, but that's a different question. As well as my original problem, that is not solved yet. I hoped to work around the "reporter is a default watcher" with this script as a post-function after issue creation. Any idea why this does not work? Or should I open a seperate question for that?

This widget could not be displayed.

OK, I found a way. It looks like the object type of "issue" is not adequate for the stopWatching-method. By replacing: Issue issue = issue; with MutableIssue issue = ComponentManager.getInstance().getIssueManager().getIssueObject(issue.getKey()); it works like expected. Thanks Jamie! And I would appriciate if you would explain the solution a bit more in depth.

This widget could not be displayed.

Hi,

I need to use this feature in one of my listener.

I have written the code like below:

MutableIssue issue = event.getIssue();

List<ApplicationUser> watchers = watcherManager.getWatchers(issue,Locale.getDefault());
log.debug("watchers all:" + watchers);
for (ApplicationUser user:watchers){
log.debug ("watcher to remove:" +user);
watcherManager.stopWatching(user,issue);

log.debug ("watcher after remove:" +watchers);
}

 

 

log output as below:

watchers all: [user1, user2, user3]

watcher to remove: user1

watcher after remove: [user1, user2, user3]

watcher to remove: user2

watcher after remove: [user1, user2, user3]

watcher to remove: user3

watcher after remove: [user1, user2, user3]

 

Can anyone please let me know if I am doing anything wrong

This widget could not be displayed.

you need to fetch the watchers again, it's not going to change the list that you created at the beginning.

This widget could not be displayed.

Hi Jamie,

Thanks for the update. Can you please help me a bit further on how to fetch the watcher in the loop again & use that updated watcher list.

 

Regards,
Barnali 

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

99 views 1 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