Getting watcher-adder from IssueWatcherAddedEvent with ScriptRunner

Joshua DeClerck April 10, 2018

What I'm trying to do: only allow myself to add myself as a watcher to issues in JIRA.

Since it seems way too complicated to prevent being added as a watcher by other people, I decided to make a scripted listener that undoes it whenever it happens. Instant global unwatch, basically.

I got this far:

import com.atlassian.jira.event.issue.IssueWatcherAddedEvent
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor

def watcherEvent = event as IssueWatcherAddedEvent
def eventIssue = watcherEvent.getIssue() as Issue
def watcherUser = watcherEvent.getApplicationUser() as ApplicationUser
def watcherManager = ComponentAccessor.getWatcherManager()

// make this point to a directory group to de-jank and open up
def immuneUsers = ["meLDAP","meAdmin"]

if (immuneUsers.contains(watcherUser.getName())) {
watcherManager.stopWatching(watcherUser, eventIssue)
}

This works a little too well though, and removes immuneUsers even if they add themselves.

The problem is, I can't figure out how to grab the event initiator user. All of the event parameters I can find only seem to return the watcher user.

With access to the initiating user, I could tweak this into only stopWatching() if watcherUser != initiatingUser. Atlassian's API docs almost make this look impossible, though, which it might be. But am I missing something super obvious...?

1 answer

1 accepted

1 vote
Answer accepted
Alexey Matveev
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.
April 10, 2018
You can grab the event initiator like this

import com.atlassian.jira.component.ComponentAccessor
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
Joshua DeClerck April 11, 2018

Yuuup, that did it. Thanks! I learned.

Suggest an answer

Log in or Sign up to answer