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