How to get groups, which are assigned to a specific role in a project?

Finn Bökenkamp January 22, 2018

I want to get the names of groups, which are assigned to a role in a project, but only found a way to get all usernames that are assigned to a role, with the following:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.security.PermissionManager;
import com.atlassian.jira.security.roles.ProjectRole;
import com.atlassian.jira.security.roles.ProjectRoleActors;
import com.atlassian.jira.security.roles.ProjectRoleManager;
import com.atlassian.jira.user.ApplicationUser;


Collection<Project> projects = permissionManager.getProjects(new ProjectPermissionKey("BROWSE_PROJECTS"), appUser);
ProjectRoleManager projectRoleManager = ComponentAccessor.getComponentOfType(ProjectRoleManager.class);
ProjectRole projectRole = projectRoleManager.getProjectRole("Role Name");

for(Project project : projects){
ProjectRoleActors projectRoleActors = projectRoleManager.getProjectRoleActors(projectRole, project);
System.out.println(projectRoleActors.getUsers().toString());
}

 Is there any way to get the groupnames instead of all usernames?

1 answer

0 votes
miikhy
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.
January 22, 2018

Hi,

You might want to use the ProjectRoleManager.getProjectIdsContainingRoleActorByNameAndType() method, you can define which type of actor you want to get, in your case it would be ProjectRoleActor.GROUP_ROLE_ACTOR_TYPE.

More details here: https://docs.atlassian.com/software/jira/docs/api/7.0.2/com/atlassian/jira/security/roles/ProjectRoleManager.html#getProjectIdsContainingRoleActorByNameAndType-java.lang.String-java.lang.String-

Hope this helps!

Cheers

miikhy
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.
January 22, 2018

This is based on knowing the group name. If you don't want to check all groups individually, you might want to search for an alternative solution. Will keep you posted if I can find one!

Cheers

Finn Bökenkamp January 22, 2018

Hi,

thanks for this, but you already say it. It would be more comfortable if I get the groupnames directly by the project, instead of run through all groupnames individually and compare each projectId with the result list of the function.

Would be great if there is any solution! Otherwise I go on with this one...

miikhy
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.
January 23, 2018

Sure! Please keep us posted if you find any better solution, very interesting question :)

Santwana Sarangi {Appfire}
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 28, 2019

Hi @Finn Bökenkamp ,

Here is the working solution to get the groups assigned to a particular project role in a project.

        Set<String> projGroupNames =new HashSet<String>();

        ProjectRoleManager projectRoleManager = ComponentAccessor.getComponentOfType(ProjectRoleManager.class);

        ProjectRole projectRole = projectRoleManager.getProjectRole("Administrators");

        ProjectRoleActors projectRoleActors = projectRoleManager.getProjectRoleActors(projectRole, project);

        for(RoleActor actor : projectRoleActors.getRoleActorsByType(ProjectRoleActor.GROUP_ROLE_ACTOR_TYPE)) {

            projGroupNames.add(actor.getParameter());

        }

Hope this helps.

Thanks,

Santwana

Ankush Kumar February 22, 2022

Hi @Santwana Sarangi {Appfire} 

you have working script, I am trying to fetch group based on role.

Santwana Sarangi {Appfire}
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.
February 22, 2022

Hi @Ankush Kumar ~ I have already provided the above. Hope that helps.

Ankush Kumar February 22, 2022

@Santwana Sarangi {Appfire} getting error "unable to resolve class RoleActor"

for(RoleActor actor : projectRoleActors.getRoleActorsByType(ProjectRoleActor.GROUP_ROLE_ACTOR_TYPE)) {

            projGroupNames.add(actor.getParameter());

        }

Suggest an answer

Log in or Sign up to answer