Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Use ScriptRunner to un-watch an issue when created Edited

Davin Studer Community Leader Mar 21, 2019

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

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);
}

 

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);
}

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you