It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to list all members of a certain project role?

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

8 votes
Answer accepted

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
}

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

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

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]

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.

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)

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

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 Shahriar Kabir likes this

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!

TAGS
Community showcase
Published in Jira Service Desk

ANNOUNCING: Early access ITSM features in Jira Service Desk

Meet the new and improved IT service management template for Jira Service Desk! Here are a few of the powerful features ready and waiting for you: Better focus on ITSM - clear ticket categories...

231 views 3 9
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you