Blog - Isos Technology

A Groovy Way to Quickly Find All Groups with a Certain Threshold of Members

Written by Amanda Kirk | Mar 21, 2023

Here at Isos, we often come across problems that could potentially take substantial effort, and ask ourselves how we can provide a solution for clients that takes less time, less effort, and less budget. One problem I keep coming across recently, is the need to find all groups containing a specific number of users, in an efficient and easily repeatable way. Imagine you're being asked by auditors or a manager to find candidates for a group cleanup or consolidation. Looking at groups with less than a certain threshold of users would be a great way to identify these candidates! 

In this blog post, I'll walkthrough how to quickly find all groups that have a specific threshold of members, by using a simple, groovy script that can be re-used time and time again! To run this script, just pull up a groovy console in Jira It's important to note that a groovy console comes with some typical add-ons like JMWE, and may not be available out-of-the-box. 

In our example below, we will find all groups with 3 or fewer members. You can change the threshold variable to adjust this amount.

Use Case: Get a List of Groups with 3 or fewer Members

import com.atlassian.jira.bc.group.search.GroupPickerSearchService
import com.atlassian.jira.component.ComponentAccessor
def groupSearch = ComponentAccessor.getComponent(GroupPickerSearchService);
def groupList = new ArrayList<>();
def groupManager = ComponentAccessor.getGroupManager()
def sb = new StringBuffer()
 
//define threshold (ex: groups with <= 3 members)
def threshold = 3
  
//get all groups
groupList.addAll(groupSearch.findGroups(""));
//keep track of how many groups we find within this threshold
def count = 0
  
//iterate over groups and output results
for (def gg : groupList){
    def users = groupManager.getUsersInGroup(gg.getName())
    if(users.size() <= threshold){
        sb.append(gg.getName() + " has " + users.size() + " users.\n")
    }
}
  
//sb.append("Total Number of Groups within Threshold: " + count)
return sb.toString()

As you can see from the above, we use the GroupPickerSearchService from the Jira API to iterate over all groups in Jira. For each group, we get its user list to check how many members it contains, and if its member size is within our threshold, we take note of the results. You can also format the output any way you'd like, so there is no need for exporting and formatting in an external tool. 

The only line that needs editing is line 8, where you define the threshold for your group. If you want to find all groups with 20 or fewer members, you would change that threshold variable to 20.

Here is some sample output to show you what the above script provides:

Example Output

Result type:
String
Result value:
asu-users has 0 users.
automated-tests has 1 users.
automation has 3 users.
bamboo-user has 2 users.
isos-conf-admin has 3 users.
isos-consulting-contractor has 2 users.
isos-pmo has 0 users.
isos-recruiting-contractor has 2 users.
isos-team-agile has 1 users.
isos-training has 2 users.
isos-ux-contractor has 3 users.
ms-jira-activity has 2 users.
nested-test has 2 users.
testme2 has 2 users.
timecard-approval-managers has 2 users.

And Voila! It is as simple as that!  I hope these script snippets can help speed your admin tasks or client needs. And as always, happy automating!