scriptrunner - how to clear watchers when creating subtask

I'm using scriptrunner to create a sub-task as a postfuction in one of my transitions. 

According to https://answers.atlassian.com/questions/123997 I should be able, in the sub-task create transition, set something to tell it not to inherit the parent's field values.  I am unable to find where to unset "Set Field Value from Parent".  All of the parent's fields that also exist in the sub-task are getting populated with the parents values.  I can live with this, but would REALLY like to clear all of the watchers out.

Here's what I have so far that's working like a charm.  The question is - what do I add to it to clear the watchers from the sub-task?  Thanks!

 

def cf = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Product Name'}
issue.summary = sourceIssue.getCustomFieldValue(cf)
issue.summary = issue.summary + " - License"
issue.description = "";
checkLink = {link -> false};

1 answer

1 accepted

2 votes

You can use this scripted post-function to remove all watchers from an issue:

import com.atlassian.jira.component.ComponentAccessor

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def locale = ComponentAccessor.getLocaleManager().getLocaleFor(user)

def watcherManager = ComponentAccessor.getWatcherManager()
def watchers = watcherManager.getWatchers(issue, locale)

watchers.each { watcher ->
    watcherManager.stopWatching(watcher, issue)
}

I put your scripted post-function as the last post-function in the sub-task create workflow.  Didn't work.  I'm assuming, perhaps incorrectly, that the sub-task create transition invoked from the parent workflow executes the post-functions that exist in the sub-task create workflow.  Or does it not?

I then tried putting it in the sub-task create transition in the parent workflow and received the following error:

[c.o.s.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: IS-10597, actionId: 911, file: null
java.lang.NullPointerException
	at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:210)
	at com.google.common.cache.LocalCache.get(LocalCache.java:3936)
	at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3941)
	at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4824)
	at com.google.common.cache.LocalCache$LocalLoadingCache.getUnchecked(LocalCache.java:4830)
	at com.atlassian.jira.issue.watchers.DefaultWatcherManager.getWatcherUserKeys(DefaultWatcherManager.java:159)
	at com.atlassian.jira.issue.watchers.DefaultWatcherManager.getWatchers(DefaultWatcherManager.java:136)
	at com.atlassian.jira.issue.watchers.WatcherManager$getWatchers$0.call(Unknown Source)
	at Script47.run(Script47.groovy:11)

The sub-task create workflow now looks like this:

def cf = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Product Name'}
issue.summary = sourceIssue.getCustomFieldValue(cf)
issue.summary = issue.summary + " - License"
issue.description = "";
checkLink = {link -> false};

import com.atlassian.jira.component.ComponentAccessor
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def locale = ComponentAccessor.getLocaleManager().getLocaleFor(user)
def watcherManager = ComponentAccessor.getWatcherManager()
def watchers = watcherManager.getWatchers(issue, locale)
watchers.each { watcher ->
    watcherManager.stopWatching(watcher, issue)
}

To be clear, I don't want to clear the watchers from the parent issue, but from the newly created sub-task.

In the sub-task, I added a "clear watchers" transition that only calls your code and it works like a charm.  It's a couple more steps (have to go into the sub-task, etc) but better than nothing!!!!

Great, glad its working for you. Yeah sticking it on the create transition for the sub-task will work.

Putting it in the create transition for the sub-task did not work. I just have a New -> New transition called "Clear Watchers" and all it does is run your script.

 

But it does exactly what I need. So thank you.

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
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

191 views 1 18
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