Script to add user as admin to a list of projects

Peter Newton August 17, 2020

Hi,

I need to add a user to multiple projects in Jira. I have a list with project keys but doing this manually will take a lot of time.

I think ScriptRuner Console will do this job.

Any idea on how can I loop the projects from a list and then add user to specific project role?

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

def groupManager = ComponentAccessor.getGroupManager()
def projectManager = ComponentAccessor.getProjectManager()
def projectRoleService = ComponentAccessor.getComponent(ProjectRoleService)
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def errorCollection = new SimpleErrorCollection()

// Fetch project role object by name
def projectRole = projectRoleManager.getProjectRole("Administrators")


def projects = ["key1", "key2", "key3"]
for (X in projects)
{
def project = projectManager.getProjectObjByKey(X) ???????!!!!!

}

Thank you

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Amanda Kirk
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 17, 2020

Here is some pseduocode that I had saved to accomplish this:

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

def projectManager = ComponentAccessor.getProjectManager()
def projectRoleService = ComponentAccessor.getComponent(ProjectRoleService)
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def errorCollection = new SimpleErrorCollection()

def project = projectManager.getProjectObjByKey("PROJKEY")
def projectRole = projectRoleManager.getProjectRole("Service Desk Customers")
def actors = ['actor1Key', 'actor2Key']
projectRoleService.addActorsToProjectRole(actors,
        projectRole,
        project,
        ProjectRoleActor.USER_ROLE_ACTOR_TYPE,
        errorCollection)

See more of how to use this method here:

https://docs.atlassian.com/software/jira/docs/api/7.1.6/com/atlassian/jira/bc/projectroles/ProjectRoleService.html

If you have a set of users and a set of projects, it should be relatively easy with the above methods to iterate over them and assign those users to the Administrators role.

Good luck and thanks for the great question!

Peter Newton August 17, 2020

Hi @Amanda Kirk 

Actually that was my main issue - not knowing how to iterate and use the methods accordingly since I am new to Jira scripting

I found the same script you sent on the net but it took me couple of h to understand that addActorsToProjectRole accept only array as actors argument and if not declared as such, console will trow an error related to ProjectRoleService definition

Here is the working script :)

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

def groupManager = ComponentAccessor.getGroupManager()
def projectManager = ComponentAccessor.getProjectManager()
def projectRoleService = ComponentAccessor.getComponent(ProjectRoleService)
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def errorCollection = new SimpleErrorCollection()

def projects = ["key1", "key2", "key3"]

for (pname in projects)
{
def actors = ['user_name_of_user_to_be_added']
def project = projectManager.getProjectObjByKey(pname)
def projectRole = projectRoleManager.getProjectRole("Administrators")
def projectService = ComponentAccessor.getComponentOfType(ProjectRoleService.class);

projectRoleService.addActorsToProjectRole(
actors,
projectRole,
project,
ProjectRoleActor.USER_ROLE_ACTOR_TYPE,
errorCollection)
}

 

Thank you!

Amanda Kirk
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 18, 2020

Very nice Peter! Since you are new to Groovy, an alternative for iterating over projects would be:

projects.each {

---

def project = projectManager.getProjectObjByKey(it)

}

or even

projects.each{ pname->

def project = projectManager.getProjectObjByKey(pname)

}

Sayan Kaiser June 22, 2022

Hi,

I have tried using the above script, it works for few users and doesn't work for others. Is there any constraint behind this ?

Sayan Kaiser August 9, 2022

Hi,

I have tried both the scripts and only it works for few users 

Priyanka Karguppikar August 29, 2022

Hello Team,

 

I am continuously getting the below error when I trying to run the above script. Can anybody help me with the solution.

 

Error[We can't find 'priyanka' in any accessible user directory. Check they're still active and available, and try again.]

 

Thanks

MC November 10, 2022

The actors Collection actually accepts user keys, not user names.  While these often match, in some cases the user key can be different from the username.

 

Try replacing:

def actors = ['prianka']

 

with

def targetUser =  ComponentAccessor.userManager.getUserByName('priyanka')
def actors = [targetUser.key]
Andrea Maria Loi April 27, 2023

Thanks a lot MC.

I am using a little different case but this last suggestion made it working for me.

I added

return errorCollection

and received this message as result

Error Messages: [We can't find '...' in any accessible user directory. Check they're still active and available, and try again.]

I hope it helps someone else

TAGS
AUG Leaders

Atlassian Community Events