Blog - Isos Technology

Jira Admin Tip #5: A Groovy Way to Find All Projects with a Specific Project Role

Written by Amanda Kirk | Aug 17, 2021

Here at Isos Technology, we've seen use cases for so many different scenarios! One that comes up now and then for both my clients (and myself, as an admin) is identifying all the projects where a specific project role is being used. Whether we're auditing permissions or looking to deprecate a role, we need to know who is using the role and if it can it be consolidated with another role.

Going through projects one by one to gather this information would take a heck of a long time, so in this blog post, I will show you how to utilize a simple Groovy script in order to quickly identify all projects using a project role. To run it, just pull up a Groovy console in Jira (note that a Groovy console comes with some typical apps like JMWE and may not be available out-of-the-box).

Let's jump right in: 

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.project.ProjectManager;
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.security.roles.RoleActor;
 
def roleName = "Developers"
  
StringBuilder output = new StringBuilder();
ProjectManager projectManager = ComponentAccessor.getProjectManager();
ProjectRoleManager projectRoleManager = (ProjectRoleManager) ComponentAccessor.getComponentOfType(ProjectRoleManager.class);
  
//gets all project roles
def projectRoles = projectRoleManager.getProjectRoles()
  
//for each project
for(Project project : projectManager.getProjectObjects())
{   //and each project role
    for(ProjectRole projectRole: projectRoles)
    
        if(projectRole.getName() == roleName){
            //see if that project uses the project role
            def ProjectRoleActors projectRoleActors = projectRoleManager.getProjectRoleActors(projectRole, project)
            for (RoleActor actor : projectRoleActors.getRoleActors()) {
                    output.append(project.getKey()).append(" : ").append(projectRole.getName()).append(" : ").append(actor.getDescriptor()).append("\n")
                }
              
        }
    }
}
return output.toString();

 

As you can see from above, we use the ProjectManager and ProjectRoleManagers from the Jira API to iterate over all project roles in each project to see if the name matches the role we are looking for. In our example above, this is the project role "Developers." You can edit the script to find the specific project role you need. After it's found, it outputs the information along with which users OR groups are in that project role. Obviously, the output format can be changed to be more valuable for whatever you are trying to measure.

For now, though, here's a sample output generated from the script above:

 

 

And voilĂ ! It's as simple as that! As you can see, the script outputs not only each project that is utilizing the project role, but which users or groups are utilizing that role within each project. I hope these script snippets can help speed your admin tasks or client needs.

And as always, happy automating!