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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,467,455
Community Members
 
Community Events
177
Community Groups

Script to add user as admin to a list of projects

Edited

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

0 votes
Answer accepted

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!

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!

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)

}

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 ?

Hi,

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

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

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]

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events