Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner add active enabled accounts as watchers only

pg
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 3, 2022

Hi, 

We have a script runner in place, something which I have inherited, which already adds users as watchers, but is also including a few old disabled users who are members of a group, from when jira was initially setup using Jira Internal Directory, before it was integrated with Azure AD. The simple fix would be for me to remove the user from the group, but I cannot remove user from the groups, and encounter an error 

 

"You cannot remove a group from this user as it is not visible to you."

Therefore, I would like to modify the script to only add those watchers where the account is enabled or ignore disabled accounts if they exist in the group. ("techsupport")

 

The code is below, I can make sense of what its doing, but not being a coder, a little help on how to achieve this would be very appreciated. 

 

import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue

IssueManager issueManager = ComponentAccessor.getIssueManager();
def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager()

Issue issue = event.issue;
if (ComponentAccessor.getGroupManager().getUsersInGroup("support").contains(issue.reporter)) {
ComponentAccessor.getGroupManager().getUsersInGroup("techsupport").each {
def user = userManager.getUserByName(it.getName())
if (user) watcherManager.startWatching(user, issue)
}
}

Thanks in advance

 

1 answer

1 accepted

1 vote
Answer accepted
PD Sheehan
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.
November 3, 2022

If you want to include only active user, you can pass an additional parameter to the getUsersInGroup method to tell it to exclude inactive users.

But here is a simplified and somewhat cleaned-up version of your script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent

def watcherManager = ComponentAccessor.watcherManager
def groupManager = ComponentAccessor.groupManager
def issue = (event as IssueEvent).issue
if (groupManager.isUserInGroup(issue.reporter, "support")) {
groupManager.getUsersInGroup("techsupport", false).each {user->
watcherManager.startWatching(user, issue)
}
}
pg
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 8, 2022

Thanks for the guidance. 

Suggest an answer

Log in or Sign up to answer