Add user group as watchers of an issue using ScriptRunner custom listener

Daniel Brvnišťan November 11, 2019

Hi, (especially @Kristian Walker _Adaptavist_ )

I am trying to create a custom listener that would add a specific user group, let's call it "INV" on a specific event.

The most similar use case I found is adding a project role as watchers.

I can't script this, could someone help me adjust the script to use a specific group?

Thanks a lot in advance,
Daniel

 

2 answers

1 accepted

4 votes
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 11, 2019

Here is the script from the other post, modified to get group members instead of project role members, I haven't actually tried it. But it should work.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.security.roles.ProjectRoleManager

Issue issue = event.issue
def watcherManager = ComponentAccessor.getWatcherManager()
def userUtil = ComponentAccessor.userUtil


userUtil.getAllUsersInGroupNames(['INV']).each {user ->
    watcherManager.startWatching(user, issue)
}
Daniel Brvnišťan November 12, 2019

It worked, amazing Peter, thanks so much :)

Matthew Frassetti April 25, 2023

Hi Peter, would it be possible to modify this to use it for users in a specific Project Role, as an "On Create" post function? I've been trying to get it to work for my use case but having a hard time as I am inexperienced. Thank you regardless.

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 25, 2023
Manoj Gangwar
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 14, 2024

Hi @Peter-Dave Sheehan 

The above script is also adding Inactive users as a watcher. How I can exclude the inactive users?

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 14, 2024

This has gotten a lot simpler with HAPI.

The same result can be achieved with

Groups.getByName('INV').members.findAll{it.active}.each{
    event.issue.addWatcher(it)
}

No imports needed

Like Manoj Gangwar likes this
Manoj Gangwar
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 15, 2024

Thanks @Peter-Dave Sheehan  One last thing - I want to add the condition based on the Issue Type & field value. currently, it applies on whole project.

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 15, 2024

Add something at the top to exit if the condition is not met.

if(event.issue.issueType.name != 'your issue type') return
if(event.issue.getCustomerFieldValue('some field') != 'your value') return

//conditions have been met, continue with adding watchers
Groups.getByName('INV').members.findAll{it.active}.each{
    event.issue.addWatcher(it)
}

 

Like Manoj Gangwar likes this
1 vote
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 12, 2019

Hi Daniel,

I have tested the code provided by Peter in the response above using the latest version of ScriptRunner and Jira 7.13.1 and can confirm this code worked when I configured as a script listener on the issue updated event.

The code added all users in the INV group as watchers on an issue when it is updated and I would advise using the code provided by Peter to achieve your requirement ensuring you set it to run on the event that you require.

Regards,

Kristian

Daniel Brvnišťan November 12, 2019

Thanks for testing Kristian, indeed it worked for me :)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events