Overview

It’s surprisingly difficult to simply return a list of administrators for a Jira project.    I had to hunt for a relatively simple way to accomplish this.  In the end I found a snippet to build on, written by Mark Markov and located here on the Atlassian forums.

I initially got frustrated because I was looking to return all of the administrators using the libraries that deal with permissions.  Permissions and roles are two very different things in Jira; permissions are set up using a permission scheme, and applied to more than one Jira Project.  Roles and Users are specific to each Project.

Returning the users in a Projec role requires the use of several libraries.   Outlined below are two pieces of Groovy code for accomplishing this.  The first returns all users in a given role, across all of the projects in Jira.  The second returns the users in a role for an explicitly defined project.

The Code (Return all jira users in a role)

As always, we start by declaring our imports.   The foundation of any Jira or Confluence Groovy script is the Component Accessor. We’ll also need the Project Manager and the Project Role Manager libraries.

We’ll next need an array to hold our eventual results; ScriptRunner console provides very few ways of returning output.

Using the ComponentAccessor, we define our Project Role Manager, Project Manager, and Project List.  

Lastly, we define the role we’re working with as a Role object.

For each item in projectList (that is, all of the projects in Jira), we then return the list of people in the Administrators role.

We do this by defining a Project object, which takes the name of the current project from the projectList. 

We next define usersInRole, which uses the getProjectRoleActors() method to return a list of users in a role for a given project. It takes two arguments that we’ve already defined: projectRole and project.

The results of the users in the role are added to a list.  Each item in the list is added to stringArray, which is returned at the end as the output.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.project.ProjectManager
//Define the needed imports

def stringArray = []
//Declare an array to hold the results

def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager.class)
//Declare a Project Role Manager
def projectManager = ComponentAccessor.getComponent(ProjectManager)
//Declare a Project Manager

def projectList = projectManager.getProjectObjects().name
//Declare a list of projects

def projectRole = projectRoleManager.getProjectRole("Administrators")
//Define a role object. In this example the role is Administrators
projectList.each{ eachProject ->
           
    def project = projectManager.getProjectObjByName(eachProject)
    //Define a project object.  We're looping through all of the projects
    
    def usersInRole = projectRoleManager.getProjectRoleActors(projectRole, project).getApplicationUsers().toList()
   //We feed the projectRoleManager two already-defined arguments, projectRole and project
  //The results are added to a list
    
usersInRole.each{
    users -> stringArray.add("<br> Project name: " + eachProject + " Administrator: " + users.name)
    //Add each user in the role to the 
  }	
}
return stringArray

 

The Code (RETURN JIRA USERS IN A ROLE FOR A SINGLE PROJECT)

The code for returning the users in a role on a single Jira Project is similar. The only difference is that instead of defining a list of projects, we define a single project object.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.project.ProjectManager
//Define the needed imports

def stringArray = []
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager.class)

//Declare an array to hold the results

def projectManager = ComponentAccessor.getComponent(ProjectManager)
//Declare a Project Manager

def projectList = projectManager.getProjectObjects().name
//Declare a list of projects

def projectName = projectManager.getProjectObjByName("<PROJECT NAME>")
//Define a single project object
       
def projectRole = projectRoleManager.getProjectRole("Administrators")
//Define a role object. In this example the role is Administrators
    
def project = projectManager.getProjectObjByName(projectName.name)
//Define a project object, using the name of the projectName object
    
def usersInRole = projectRoleManager.getProjectRoleActors(projectRole, projectName).getApplicationUsers().toList()
   //We feed the projectRoleManager two already-defined arguments, projectRole and projectName
  //The results are added to a list
    
usersInRole.each{
    users -> stringArray.add("<br> Project name: " + projectName.name + " Administrator: " + users.name)
    //Add each user in the role to the 
  }	

return stringArray

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>