How to add Groups to a project role in groovy, script runner console

Carlos David November 10, 2017

I'm trying to add (AD) groups to project roles using the Script Runner (5.04) Console, using Jira 7.3.1. I'm trying to use the method:

void addActorsToProjectRole(Collection<String> actors,
                            ProjectRole projectRole,
                            Project project,
                            String actorType,
                            ErrorCollection errorCollection)

source:

https://docs.atlassian.com/jira/7.3.1/com/atlassian/jira/bc/projectroles/ProjectRoleService.html#addActorsToProjectRole-java.util.Collection-com.atlassian.jira.security.roles.ProjectRole-com.atlassian.jira.project.Project-java.lang.String-com.atlassian.jira.util.ErrorCollection-

import com.atlassian.jira.bc.projectroles.ProjectRoleService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRole
import com.atlassian.jira.util.SimpleErrorCollection
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.project.Project

def projectRoleService =ComponentAccessor.getComponent(ProjectRoleService);
def projectRoleManager =ComponentAccessor.getComponent(ProjectRoleManager);
def projectManager =ComponentAccessor.getComponent(ProjectManager);

ProjectRole projectRoleObject = projectRoleManager.getProjectRole("Role Name")
Project project = projectManager.getProjectObjByKey("XYZ")
def errorCollection = new SimpleErrorCollection();

projectRoleService.AddActorsToProjectRole(['Group A'], projectRoleObject, project, "GroupRoleActor.TYPE", errorCollection)

 

Static type checking errors are the issue. The problem lies with the errorCollection - I am also unable to use:

projectRoleManager.getProjectRoleActors(projectRoleObject, project, errorCollection)

Can anyone help?

 

2 answers

2 accepted

1 vote
Answer accepted
Alexey Matveev
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.
November 10, 2017

Hello,

I changed your code a bit and it was compiled successfuly

import com.atlassian.jira.bc.projectroles.ProjectRoleService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRole
import com.atlassian.jira.util.SimpleErrorCollection
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.security.roles.ProjectRoleActor

def projectRoleService =ComponentAccessor.getComponent(ProjectRoleService);
def projectRoleManager =ComponentAccessor.getComponent(ProjectRoleManager);
def projectManager =ComponentAccessor.getComponent(ProjectManager);

ProjectRole projectRoleObject = projectRoleManager.getProjectRole("Role Name")
Project project = projectManager.getProjectObjByKey("XYZ")
def errorCollection = new SimpleErrorCollection();
Collection<String> actorCollection = new ArrayList<>();
actorCollection.add("Group A");

projectRoleService.addActorsToProjectRole(actorCollection, projectRoleObject, project, ProjectRoleActor.USER_ROLE_ACTOR_TYPE, errorCollection)
Carlos David November 10, 2017

Thanks also Alexey

Like Vasanthakumar Velusamy likes this
Slava Gefen October 27, 2022

Hi, 

USER_ROLE_ACTOR_TYPE

Didn't work for me. I used 

GROUP_ROLE_ACTOR_TYPE

instead, and it works! 👍

Slava Gefen October 27, 2022

BTW @Alexey Matveev Appfire_ can you help with how to add groups into four different roles in the project at the same time?

If I use in the console

projectRoleService.addActorsToProjectRole(actorCollection, projectRoleObject, project, ProjectRoleActor.USER_ROLE_ACTOR_TYPE, errorCollection)

four times then only first one executes.

Thanks in advance
With kind regards
Slava

Likhitha Kosanam July 19, 2023

Hi Slava Gefen,

 

I tried the code its working for me when I tried with 

GROUP_ROLE_ACTOR_TYPE

But I need to change the role of  users.  

Please suggest

Slava Gefen July 25, 2023

Hey @Likhitha Kosanam great that it works for you. To change a role I suppose you need two actions: Remove user from a role and add them to another as described in the doc:
ProjectRoleService (Atlassian JIRA 7.1.2 API)

With kind regards
Slava

0 votes
Answer accepted
Anton Chemlev - Toolstrek -
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.
November 10, 2017

Check this out - https://community.atlassian.com/t5/Product-Apps-questions/Jira-Script-runner-add-user-to-project-role/qaq-p/230748

Below script have no static check errors. Compare your script line by line. And just adapt it to use groups instead of users.

Carlos David November 10, 2017

Thanks Anton - I had capitalized the 'a' in addActors by mistake

Suggest an answer

Log in or Sign up to answer