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
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
}
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 :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Could you please post a sample how to call this method?
Thanks!!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Rus
Getting the error 'role' should not be empty.
Can some one tell how to call this?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.