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

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

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

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Jul 10, 2018 in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

181 views 0 4
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you