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

Groovy script to remove watcher

Patrick August 19, 2014

Hello,

I'm trying to remove a watcher from an issue when it's created. I use a custom listener with the 'Script Runner' plugin (I'm using JIRA v5.2.9 and Scipt Runner v2.1.15).

Here is my script listener :

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
 
class AutoRemoveWatcherListener {

	void workflowEvent(IssueEvent event) {
		def watcherManager = ComponentAccessor.getWatcherManager()
		def userManager = ComponentAccessor.getUserManager()
 
		def user = userManager.getUser('myUser')
		watcherManager.stopWatching(user, issue)
    }
}

Problem : it doesn't work !

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
JamieA
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.
August 19, 2014

You haven't defined "issue", you want somewhere:

def issue = event.issue

Patrick August 20, 2014

It still doesn't work. I tried to use that script as a postfunction and as a custom listener. I even tried to extend the class AbstractIssueEventListener but no results.

My script is directly placed at the atlassian-jira/WEB-INF/classes/ folder.

JamieA
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.
August 20, 2014

Decide on whether you want a postfunction or a listener, the scripts will be different.

Start with a postfunction because it's easiest.

If that doesn't work post your code and the errors from the log.

Patrick August 20, 2014

Yes the code in the postfunction is different from the listener, here it is :

import com.atlassian.jira.component.ComponentAccessor
 
def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager()
 
def user = userManager.getUser('myUser')
watcherManager.stopWatching(user, issue)

Here is the code with the log, but it appears that I have nothing in the logs file (catalina.out and atlassian-jira.log)

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

log.debug "--------------- REMOVE_WATCHER_SCRIPT --------------"
def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager()
 
def user = userManager.getUser('myUser')
watcherManager.stopWatching(user, issue)

2 votes
Alexey_Rjeutski__Polontech_
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.
August 20, 2014

1. You've choosen the wrong place to store listeners: please try to store under scripts folder under jira data folder g:\jiradata\6.3\scripts\com\polontech\listener\ExampleListener.groovy - see example

2. You should define the issue like

def issue = issueEvent.getIssue();

Patrick August 20, 2014

I'm using jira 5.2.9 and I can't find any folder that looks like your.

Alexey_Rjeutski__Polontech_
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.
August 20, 2014

do you see script listeners under script runner menu?

Patrick August 20, 2014

Yes I see it, then I choose 'Custom Listener' and when I provide the name of my script class, there are no errors so I suppose this is not a folder problem.

Alexey_Rjeutski__Polontech_
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.
August 20, 2014

Ok then, but you still need to get issue from the issue event to make this script workable

Patrick August 20, 2014

Even when I use event.getIssue(), it doesn't work.

JamieA
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.
August 21, 2014

What's in the log? Maybe a class-cast problem, with ApplicationUser and User.

Patrick August 21, 2014

Nothing appears in the log files, don't know what's the problem.

TAGS
AUG Leaders

Atlassian Community Events