Groovy Script to list all groups and their respective members

I ran into a use case where I needed this query and didn't see it posted already. Hopefully it helps! This can be used in the script console as a part of the script runner add-on.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.group.search.GroupPickerSearchService
import com.atlassian.crowd.embedded.api.Group

def groupSearch = ComponentAccessor.getComponent(GroupPickerSearchService)
def groupList = groupSearch.findGroups("")
def groupManager = ComponentAccessor.getGroupManager()


for (Group group in groupList){
log.error group.getName().toString()//print group names to log
log.error groupManager.getUsersInGroup(group)//print group members to log
}

1 comment

Hi Cody,

I wanted to save the group members name in custom field and group name will be dynamic based user selection.

Please help me in this.

the code which i mentioned below is not working after the upgrade.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.crowd.embedded.api.User
import com.atlassian.crowd.embedded.api.Group;

Issue issue = issue;
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
def groupManager = ComponentAccessor.getGroupManager()
def userName = ""

CustomField releasersName = customFieldManager.getCustomFieldObjectByName( "Releasers Name" );
CustomField releaser = customFieldManager.getCustomFieldObjectByName( "Releaser" );

def userManager = ComponentAccessor.getUserManager()
//def group = userManager.getGroup((String) issue.getCustomFieldValue(releaser))
def cfgvalue =(List<Group>) issue.getCustomFieldValue(releaser);
for(Group group:cfgvalue){
Collection <ApplicationUser> usersInGroup = groupManager.getUsersInGroup(group)
for (User user : usersInGroup){
log.warn(user.getDisplayName())
if(userName == "") {
userName = user.getDisplayName()
} else {
userName = userName + "," +user.getDisplayName()
}
}
}
//log.warn(userName)
//issue.setCustomFieldValue(releasersName,userName)

releasersName.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(releasersName), userName),changeHolder);

Comment

Log in or Sign up to comment
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Apr 17, 2018 in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

767 views 2 19
Join discussion

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