Jira scriptrunner Get user role

Qi Jiang August 23, 2017

I am try to list all projects that a user is a Project Administrator, got code below with error on line 26. Any advise?

import com.atlassian.jira.bc.projectroles.ProjectRoleService;
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;

StringBuilder output = new StringBuilder();
ProjectManager projectManager = ComponentAccessor.getProjectManager();
ProjectRoleService projectRoleService = (ProjectRoleService) ComponentAccessor.getComponentOfType(ProjectRoleService.class);
ProjectRoleManager projectRoleManager = (ProjectRoleManager) ComponentAccessor.getComponentOfType(ProjectRoleManager.class);

final Collection<ProjectRole> projectRoles = projectRoleManager.getProjectRoles();
for(Project project : projectManager.getProjectObjects())
{
for(ProjectRole projectRole: projectRoles)
{
//for (RoleActor actor : projectRoleActors.getRoleActors()) {
// if(projectRole.getDescriptor().equals("Developers")) {
final ProjectRoleActors projectRoleActors = projectRoleManager.getProjectRoleActors(projectRole, project);
// name of role here
//final ProjectRole devsRole = projectRoleManager.getProjectRole("Project Administrators")
for (RoleActor actor : projectRoleActors.getRoleActors()) {
if(projectRoleManager.getProjectRole().equals("Project Administrators")){
output.append(project.getKey()).append(" : ").append(projectRole.getName()).append(" : ").append(actor.getDescriptor()).append("\n");
}
}
// }
// }
}
}
return output.toString();

I am  

2 answers

1 accepted

4 votes
Answer accepted
Jenna Davis
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.
August 28, 2017

I'm not sure what you're wanting to do throughout all of your code, but this should be a simple example to get you started. I have not tested it directly, but I think it should work. If you have any trouble,  please let me know. :)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager

def projectManager = ComponentAccessor.projectManager
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()

def allProjects = projectManager.getProjects()
def adminProjects = []

allProjects.each{
def projectRoles = projectRoleManager.getProjectRoles(user, it)
if(projectRoles.find(){it.getName() == "Project Administrators"}){
adminProjects.push(it.key)
}
}

return (adminProjects)

 Also, if you want to go more in detail with what you're trying to accomplish I can try to code something up for you. 

Shahriar July 24, 2019

Hi Jenna, 

I am trying to somehow display a list of selected JIRA projects and the user-roles of those project on a JIRA dashboard. I have looked into this and I couldn't find any dashboard gadget that could give me that. I think the only way I can get this information is by writing groovy script like above which I am not good at :( .  I was wondering if you could help me write a groovy script so that I could display the list I am asking for. I don't mind having a scripted field to show that information in a JIRA issue. I could create a story or task for each of those project and list those stories on the dashboard which would show that scripted field. 

I honestly don't care about how it should look but here is an example what I am trying to accomplish. 

Project

Developers

Scrum Master

Product Owner

Project A

Tom, Adam, Julia

Robert

Lew

Project B

Cynthia, Melissa, Jared

John

Terri

Project C

 

 

 

 

 

 

 

Thank you so much! 

- Shahriar

1 vote
Deleted user January 11, 2019

It is a great snippet and it works. Thanks for your work!

Jenna Davis
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.
January 11, 2019

Happy to help! 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events