Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Use ScriptRunner to un-watch an issue when created

Davin Studer
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.
March 21, 2019 edited

Our version: Jira 7.12.0

We have a Jira project that we use to manage our IT assets (servers, workstations, network gear, etc.) With most of our projects having a user auto-watch the issues they create makes sense ... it doesn't in this project. So, I am trying to write a script to un-watch the issue in a post function when the issue is created. The script runs fine ... no errors, but the issue still shows as being watched. Grrr! I also tried it as a script listener on the issue created event ... still no dice. If I stick it on another transition(other than Create) or use the issue updated event in a script listener it works just fine. I just can't seem to get it to not do it on issue created. Anybody have any thoughts on this. Below is the script ... it's pretty simple.

Post-function version

import com.atlassian.jira.component.ComponentAccessor;
import org.apache.log4j.Category
log.setLevel(org.apache.log4j.Level.DEBUG);

def watcherManager = ComponentAccessor.getWatcherManager(); def locale = new Locale("ENGLISH", "US");
//Kill all watchers ... well, not the watchers themselves.
def currentWatchers = watcherManager.getWatchers(issue, locale);
log.debug("Current watcher size: " + currentWatchers.size());
for(user in currentWatchers){
log.debug("Removing user as watcher: " + user); watcherManager.stopWatching(user, issue); }

//Do we still have watchers? Shouldn't.
def
newCurrentWatchers = watcherManager.getWatchers(issue, locale);

log
.debug("Current watcher size: " + newCurrentWatchers.size());
for(user in newCurrentWatchers){
log.debug("Grrr we still have a watcher: " + user);
}

 

Script listener version

import com.atlassian.jira.component.ComponentAccessor;
import org.apache.log4j.Category
log.setLevel(org.apache.log4j.Level.DEBUG);

def watcherManager = ComponentAccessor.getWatcherManager(); def locale = new Locale("ENGLISH", "US");
//Kill all watchers ... well, not the watchers themselves.
def currentWatchers = watcherManager.getWatchers(event.issue, locale);
log.debug("Current watcher size: " + currentWatchers.size());
for(user in currentWatchers){ log.debug("Removing user as watcher: " + user);
watcherManager.stopWatching(user, event.issue); }

//Do we still have watchers? Shouldn't.
def
newCurrentWatchers = watcherManager.getWatchers(event.issue, locale);

log
.debug("Current watcher size: " + newCurrentWatchers.size());
for(user in newCurrentWatchers){
log.debug("Grrr, we still have a watcher: " + user);
}

 

1 answer

1 accepted

1 vote
Answer accepted
Davin Studer
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.
March 21, 2019 edited

Found the answer here. The key is it needs to be a MutableIssue in the stopWatching method when the issue is created.

https://community.atlassian.com/t5/Jira-questions/Groovy-Sript-Runner-Working-from-console-but-not-as-post/qaq-p/347269

Here is my new script. It works now!!

Script Listener using the Issue Created event:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue;
import org.apache.log4j.Category

MutableIssue
issue = ComponentAccessor.getIssueManager().getIssueObject(event.issue.getKey());
def watcherManager = ComponentAccessor.getWatcherManager();
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
def locale = ComponentAccessor.getLocaleManager().getLocaleFor(user);
def currentWatchers = watcherManager.getWatchers(issue, locale);

for
(watcher in currentWatchers){
watcherManager.stopWatching(watcher, issue);
}

 

Christophe Heyman March 22, 2019

David, I am running into the same issue and have the following code that finds the watchers but doesn't remove any (see below). I'm using a MutableIssue so I can't figure out what I'm doing wrong.

This is running as the very last custom script post-function in the Create Issue transition.

Any ideas?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import org.apache.log4j.Category

log.setLevel(org.apache.log4j.Level.DEBUG);

MutableIssue myIssue = issue;

def watcherManager = ComponentAccessor.getWatcherManager();
def locale = new Locale("ENGLISH", "US");
def currentWatchers = watcherManager.getWatchers(myIssue, locale);

log.debug("Current watcher size: " + currentWatchers.size());
for(user in currentWatchers){
log.debug("Removing user as watcher: " + user);
watcherManager.stopWatching(user, myIssue);
}

//Do we still have watchers? Shouldn't.
def newCurrentWatchers = watcherManager.getWatchers(myIssue, locale);

log.debug("Current watcher size: " + newCurrentWatchers.size());
for(user in newCurrentWatchers){
log.debug("Grrr, we still have a watcher: " + user);
}
Juan José Marchal Gómez
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.
July 5, 2021

In my case works at the first time. Thanks!

Crs co September 18, 2022

@Davin Studer ,I have the same problem, as @Christophe Heyman had.

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, atlassian government cloud, fedramp, webinar, register for webinar, atlassian cloud webinar, fedramp moderate offering, work faster with cloud

Unlocking the future with Atlassian Government Cloud ☁️

Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.

Register Now
AUG Leaders

Atlassian Community Events