Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Groovy Script to list all groups and their respective members

Cody Crandall November 29, 2017

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
}

2 comments

Comment

Log in or Sign up to comment
Raam S January 29, 2018

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.

Raam S January 29, 2018

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);

Chris Scheepers September 15, 2022

This one Works better to find all groups and members in scriptrunner script consol . (Thanks Mr G)

import org.apache.log4j.Level
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.crowd.embedded.api.Group
log.setLevel(Level.INFO)
def groupManager = ComponentAccessor.getGroupManager()

def result = "<table class='aui'>"
groupManager.allGroups.each{ group ->
def members = groupManager.getUserNamesInGroup(group)
log.info("${group.name} members $members")
result += "<tr> <td>${group.name}</td><td>$members</td></tr>"
}
result += "</table>"
return result
Like # people like this
Ajinkya Paradkar December 8, 2022

I want only to display 10 groups with their members.

Also , I am trying to fetch the Display names and not the usernames.

Can you please advise @Chris Scheepers 

 

Thanks,

Ajinkya

Graham Twine
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.
December 7, 2023
import org.apache.log4j.Level
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.crowd.embedded.api.Group

log.setLevel(Level.INFO)
def groupManager = ComponentAccessor.groupManager

def groups = ['group1', 'group2']
def result = "<table class='aui'>"
groups.each{ groupName ->

def group = groupManager.getGroup(groupName)
def members = groupManager.getUserNamesInGroup(group)
log.info("${group.name} members $members")
result += "<tr> <td>${group.name}</td><td>$members</td></tr>"
}
result += "</table>"
return result
Like Chris Scheepers likes this
SKAdmin February 27, 2024

I'm also trying to convert this same script to capture display name instead of UserName.

def members = groupManager.getUserNamesInGroup(group)
Does not work and the display name is needed at members not group. 

result += "<tr> <td>${group.displayName}</td><td>$members</td></tr>"
}

 

TAGS
AUG Leaders

Atlassian Community Events