scriptrunner - how to clear watchers when creating subtask

DI2E Licensing
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.
November 10, 2016

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
Answer accepted
adammarkham
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.
November 10, 2016

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)
}
DI2E Licensing
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.
November 11, 2016

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)
}
DI2E Licensing
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.
November 11, 2016

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

DI2E Licensing
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.
November 11, 2016

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!!!!

adammarkham
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.
November 11, 2016

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

DI2E Licensing
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.
November 11, 2016

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.

Alexander Bondarev
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.
February 1, 2019
watchers.each { watcher ->
    watcherManager.stopWatching(watcher, issue)

 

Hello! 

i have an error in this place: 

The script could not be compiled: <pre>org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script19.groovy: 6: expecting '}', found '-' @ line 6, column 25. watchers.each { watcher -&gt; ^ 1 error </pre>.

 

Added a Custom script post-function (scriptrunner).

Alexander Bondarev
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.
February 4, 2019

Solution: need to change "&gt"  to  ">". 

 

Thank`s a lot to cool guys from the telegram`s chat of Atlassian Community!

Suggest an answer

Log in or Sign up to answer