How to get all users in a Role from a particular project in Jira using Groovy?

lpopek August 17, 2018

Hello,
I would like to receive a List of people from the selected project by their roles.

This is my method, whats wrong?

List<ApplicationUser> getUsersForSpecifiedRolesInProject(String projectName, String role){
ErrorCollection errorCollections = new SimpleErrorCollection()
def roleInProject = ProjectRoleManager.getProjectRole(role) // role here
ProjectRoleService projectRoleServices = (ProjectRoleService) ComponentAccessor.getComponentOfType(ProjectRoleService.class)
ProjectRoleActors existingActors = projectRoleServices.getProjectRoleActors(roleInProject, projectName, errorCollections)

def usersInSpecifiedRole = existingActors.getUsers().toList()
return usersInSpecifiedRole

}

 

Thanks for response

2 answers

1 accepted

4 votes
Answer accepted
Mark Markov
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.
August 17, 2018

Hello @lpopek

Try this

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager

def getUsersForSpecifiedRolesInProject(String projectName, String role){
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager.class)
def projectRole = projectRoleManager.getProjectRole(role)
def project = ComponentAccessor.getProjectManager().getProjectObjByName(projectName)
def usersInRole = projectRoleManager.getProjectRoleActors(projectRole, project).getApplicationUsers().toList()
return usersInRole
}
lpopek August 17, 2018

Thanks a lot Mark! Works fine! Have a nice day :)

Mark Markov
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.
August 17, 2018

You re welcome! If it helps you, please mark answer as accepted. So that, other people will be able to find this answer easily for similar questions :)

Rus March 23, 2022

Thanks a lot Mark !!!

0 votes
Markus Pöhler
Contributor
July 28, 2020

Could you please post a sample how to call this method? 

 

Thanks!!

Rus March 23, 2022

I got it this way, in "project_test" i got a list of all users in role "Viewers" of project "test2":

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager

def getUsersForSpecifiedRolesInProject(String projectName, String role){
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager.class)
def projectRole = projectRoleManager.getProjectRole(role)
def project = ComponentAccessor.getProjectManager().getProjectObjByName(projectName)
def usersInRole = projectRoleManager.getProjectRoleActors(projectRole, project).getApplicationUsers().toList()
return usersInRole
}


def project_test = getUsersForSpecifiedRolesInProject("test2", "Viewers")

project_test

Harsh
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.
April 24, 2023

Hi @Rus 

Getting the error 'role' should not be empty.
Can some one tell how to call this? 


Rus April 24, 2023

Hi!

Most likely this means that this role is not in this project.

In the script, I renamed role "Viewers" to role "Viewers2" and got the same error:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager

def getUsersForSpecifiedRolesInProject(String projectName, String role){
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager.class)
def projectRole = projectRoleManager.getProjectRole(role)
def project = ComponentAccessor.getProjectManager().getProjectObjByName(projectName)
def usersInRole = projectRoleManager.getProjectRoleActors(projectRole, project).getApplicationUsers().toList()
return usersInRole
}


//def project_test = getUsersForSpecifiedRolesInProject("test2", "Viewers")
def project_test = getUsersForSpecifiedRolesInProject("test2", "Viewers1")
project_test

 

Response:

com.atlassian.jira.util.dbc.Assertions$NullArgumentException: ProjectRole should not be null! at com.atlassian.jira.util.dbc.Assertions.notNull(Assertions.java:25) at com.atlassian.jira.security.roles.DefaultProjectRoleManager.getProjectRoleActors(DefaultProjectRoleManager.java:100) at com.atlassian.jira.security.roles.ProjectRoleManager$getProjectRoleActors$2.call(Unknown Source) at Script1722.getUsersForSpecifiedRolesInProject(Script1722.groovy:110) at Script1722.run(Script1722.groovy:116)

Rus April 24, 2023

You can add to the script a check for the existence of a role in the project, if there is no role in the project, no error will be showed:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager

def getUsersForSpecifiedRolesInProject(String projectName, String role){
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager.class)
def projectRole = projectRoleManager.getProjectRole(role)
if (projectRole != null) {
def project = ComponentAccessor.getProjectManager().getProjectObjByName(projectName)
def usersInRole = projectRoleManager.getProjectRoleActors(projectRole, project).getApplicationUsers().toList()
return usersInRole
}
}


//def project_test = getUsersForSpecifiedRolesInProject("test2", "Viewers")
def project_test = getUsersForSpecifiedRolesInProject("test2", "Viewers1")
project_test

Harsh
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.
April 24, 2023

Hi @Rus 

Thanks for the reply. The role exists. 

Initially i replaced  the 'test2' with my project. 

And viewers to 'administrator'. But sadly same error which you mentioned. 

 

Do we have to change something here?

def getUsersForSpecifiedRolesInProject(String projectName, String role)

 

Thanks.

 

 


Suggest an answer

Log in or Sign up to answer