Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to list all members of a certain project role?

Wim Deblauwe
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.
September 26, 2011

Hi,

Using the groovy script runner, I get the following working:

import com.atlassian.jira.ComponentManager
 
   def projectManager = ComponentManager.getInstance().getProjectManager()
 
def result="";
result += "Key, Name, Lead\n";
for(p in projectManager.projectObjects)
{    
result += "$p.key, $p.name, $p.lead\n";
}
 
return result;

I would like to add for each project, the users that are assigned a certain project role. I know that for this specific project role only 1 user is assigned, never a group if that would make it easier to do.

I looked at http://docs.atlassian.com/software/jira/docs/api/latest/com/atlassian/jira/project/Project.html to see what is possible, but I could not find anything related to project roles there.

regards,

Wim

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

10 votes
Answer accepted
JamieA
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.
September 26, 2011

Try this:

import com.atlassian.jira.ComponentManager
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

ComponentManager componentManager = ComponentManager.getInstance()
ProjectManager projectManager = componentManager.getProjectManager()
ProjectRoleManager projectRoleManager = ComponentManager.getComponentInstanceOfType(ProjectRoleManager.class) as ProjectRoleManager

// name of role here
ProjectRole devsRole = projectRoleManager.getProjectRole("Developers")

projectManager.getProjectObjects().each {Project project ->
    ProjectRoleActors actors = projectRoleManager.getProjectRoleActors(devsRole, project)
    log.debug "$project.key, $project.name, $project.lead, ${actors.getUsers()*.name}\n";

    // if there is only one member of the role or you only want the first you could do:
    log.debug actors.getUsers().toList()?.first()?.name
}

JamieA
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.
September 27, 2011

Odd that I got a mail saying my answer had been accepted but here there is no tick.... wondering how that happened.

Wim Deblauwe
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.
April 15, 2013

I just accepted again, tick seems to be there now.

Sammy September 7, 2016

Hi Jamie. I tried this script in Script Console and it return all the project keys. Below is the output. But I want something project name and all the users in the roles of that project. I want this for multiple projects based on project category. Is it possible? Also will I be able to display the output somewhere other than below the script console? Sorry lot of questions 

 

Output is something like this:

[Project: EEOS, Project: CPRM, Project: AAGS, Project: AAT, Project: CTO, Project: AGI, Project: AGILE, Project: SBGZZ, Project: AINN, Project: AGPG, Project: AGLT, Project: MAI, Project: TERMS, Project: RPPL, Project: AND, Project: ANPF, Project: SPO, Project: SPOA, Project: CSM, Project: ADC, Project: CDPMAHTAB, Project: LMA, Project: LM, Project: TRM, Project: TOMCCA]

JamieA
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.
September 8, 2016

it returns the project objects, but the role data is logged using log.debug. Change it to log.warn then check your logs. This is just example code.

Sammy September 9, 2016

I got this error too.. Don't know why !


java.util.NoSuchElementException: Cannot access first() element from an empty List at Script92$_run_closure1.doCall(Script92.groovy:20) at Script92.run(Script92.groovy:15)

Qi Jiang August 22, 2017

@JamieA got error message at line 9 can not find matching method. 

Alex Bedeker January 23, 2019

The code has some depricated items. Can't get it to work with Jira Server v7.12.

I'm not an expert on writing code but do need a list of Users with their projectrole.

Like # people like this
Shahriar Kabir July 23, 2019

I agree. I am using JIRA v8.1.2 and some of the classes used in the above script don't work anymore. I believe those classes have been removed. Please update. 

Thank you!

Like # people like this
TAGS
AUG Leaders

Atlassian Community Events