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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Wednesday in Jira

Join our webinar: How 1B+ feature flag events helped us build the new Jira

Every time you release software, there's a bit of risk – that there's a bug, that something breaks, or that the feature doesn't resonate with customers. Feature flagging helps make high stakes s...

99 views 0 1
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