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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,559,498
Community Members
 
Community Events
184
Community Groups

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

Edited

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.
Aug 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
}

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.
Aug 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 :)

Thanks a lot Mark !!!

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

 

Thanks!!

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

Hi @Rus 

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


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)

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

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