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 Join to comment
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,220 views 14 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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot