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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

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)

}

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Jira Cloud Performance Improvements

Hello everyone, I am a product manager in the Jira Cloud team focused on making sure our customers have a delightful experience using our products. Towards that goal, one of the areas which is extr...

158 views 1 8
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