Welcome back to another blog where I will share some code to help optimize your time as a Jira Admin. My aim is to save you the hassle of having to manually search the Jira UI in order to find the data you need by using automation to quickly find this information, instead. For whatever reason, you may find yourself needing a list of project leads or admins across a subset or the entirety of your Jira projects. From my previous blogs, you will have learned that these repetitive and manual tasks can often benefit from the initial time investment of automation. Today I will share some snippets of code that you will be able to tailor to your specific situation, but at a base level, will provide you with a list of project leads and admins for your Jira projects. This automation can be used time and time again as you need to pull an updated list for your Jira admin needs, saving you tons of time in the long run!

Automating the search for Project Leads

First up, let’s take a look at some code that will provide you a list of the project leads for all Jira projects in your instance:

import com.atlassian.jira.component.ComponentAccessor
 
def projectManager = ComponentAccessor.getProjectManager()
def sb = new StringBuffer()
 
// Iterate over each Project name and get the info you need
projectManager.getProjectObjects().each {
    sb.append("Project Name: " + it.getName() + ", Project Lead: " + it.getLeadUserName() + "\n")
}
 
return sb.toString()

As you can see above, using the ProjectManager class, we can easily iterate over all the projects from our Jira instance and access some information about each. Here, we list each project’s name and associated project lead. You can check out more methods and data accessible to the Project class here.

Now, if you instead want a list of project leads only for a specific set of Projects, you can alter this code slightly to grab only the project leads you are interested in, as shown below:

import com.atlassian.jira.component.ComponentAccessor
 
def projectManager = ComponentAccessor.getProjectManager()
def sb = new StringBuffer()
 
//your array of project names here
def names = ["Alterian", "Operations"]
def project
 
// Iterate over each Project name and get the info you need
names.each{
    project = projectManager.getProjectObjByName(it)
    sb.append("Project Name: " + project.getName() + ", Project Lead: " + project.getLeadUserName() + "\n")
}
return sb.toString()

Automating the search for Project Admins

If instead of identifying project leads, we want to get a list of all project administrators for the projects in our Jira instance, we would go a slightly different route. Let’s take a look at the code, below:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.projectroles.ProjectRoleService;
import com.atlassian.jira.security.roles.ProjectRoleManager;
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.util.ErrorCollection;
import com.atlassian.jira.util.SimpleErrorCollection;
import com.atlassian.jira.user.util.Users
 
def sb = new StringBuffer()
ProjectRoleService projectRoleService = (ProjectRoleService) ComponentAccessor.getComponentOfType(ProjectRoleService.class);
ProjectRoleManager projectRoleManager = (ProjectRoleManager) ComponentAccessor.getComponentOfType(ProjectRoleManager.class);
ProjectManager projectManager = ComponentAccessor.getProjectManager();
ProjectRole administrator = projectRoleManager.getProjectRole("Administrators");
ErrorCollection ec = new SimpleErrorCollection();
 
//get all projects
projectManager.getProjectObjects().each{
  sb.append("Project Admins for " + it.getName() + ": ")
  //get all project admins for that project
  ProjectRoleActors existingActors = projectRoleService.getProjectRoleActors(administrator,it,ec);
  if (existingActors != null){
     //get list of user names
     Set<Users> users = existingActors.getUsers();
     users.each { user->
        sb.append(user.getName() + ', ')
     } 
  }
  sb.append("\n")
}
return sb.toString()

We begin this script, like before, by using the ProjectManager to iterate over all the projects in our instance. To pull certain permissions for a project, we use the ProjectRoleManager to define the project role we are looking for — in our case, the Administrators project role. We then use the ProjectRoleService which grabs the “actors” in the project role for the particular project we are currently iterating over. From there we are able to parse through the User objects to get their usernames and print everything out nicely. Now, as you can imagine, we could do many things with this list. The possible use cases for admin tasks utilizing these data sets are numerous, so feel free to use this script wherever you may need to quickly pull the list of project admins for your instance! You could similarly only pull the project administrators for a specific project or set of projects, using a slightly modified version of this code snippet. I will leave that to you to experiment with, as it is best to learn through doing!

I hope this session on automation has proven useful for you. Here at Isos, automation is a huge part what we do and one of the biggest assets we can deliver and suggest to our clients. This helps both free up their time and makes their operations run more efficiently. If you or your organization are interested in how automation could help in your situation, please reach out to a member of our team and we would be happy to assist you!

As always, happy automation!