JIRA: Scriptrunner custom listener: remove watcher after a comment is added to an issue

Sven Hessler June 29, 2017

Hi,

I have built a custom listener script in JIRA which listens on Events: Issue Commented, see below.

Apparently, it does not work, i.e. if an user comments an issue she is still added to the watchers.

Do you have any suggestion why this is not working?

 

Thanks for your help!

Sven

--- code ---

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.user.ApplicationUser

def watcherManager = ComponentAccessor.getWatcherManager()
def issueManager = ComponentAccessor.getIssueManager()
def userManager = (UserManager) ComponentAccessor.getUserManager()
def issue = event.issue as Issue

def user = event.getComment().getAuthorApplicationUser() as ApplicationUser
watcherManager.stopWatching(user, issue)


--- /code ---

P.S. JIRA v6.4.7, scriptrunner 4.1.3.24  

 

1 answer

1 accepted

2 votes
Answer accepted
Aidan Derossett _Adaptavist_
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 7, 2017

Hey Sven,

I looked into this and I think I know what's going on here. So all of your code looks fine and should have worked...but I'm almost certain that the watcher is simply being re-added by JIRA's autowatcher service after the Script Listener has already run. Basically, the Script Listener is catching and firing on the event before the autowatcher service is, so it is running first.

There are two ways that I currently know of to get around this. For the first option, you could use groovy to simply create a new thread and tell that thread to sleep for something like 0.1 second. This would be enough time to allow the JIRA autowatcher service to run, essentially making it execute first.

The Listener code to do this would look something like:

import com.atlassian.jira.component.ComponentAccessor

Thread.start {
    Thread.sleep(100)
    def watcherManager = ComponentAccessor.getWatcherManager()
    def issueManager = ComponentAccessor.getIssueManager()

    def user = event.getComment().getAuthorApplicationUser()
    watcherManager.stopWatching(user, event.issue)  
}

I've tested this myself and it does work as expected, you just need to reload the screen to view the changes. :)

The second option, is to simply disable the autowatch process by going to JIRA Admin > System > Default User Preferences.

Let me know if either of these solutions work for you!

Best wishes,                                                                                               Aidan

Sven Hessler July 10, 2017

Hi Aidan,

Thanks for your comment. Actually, I thought that this is timing issue of the firing/catching event.

I have solved it by applying your 2nd option, i.e. disabling the autowatch on a per-user basis.

 

Thanks again

Sven

 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events