The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
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
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:
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello everyone, Hope everyone is safe! A few months ago we posted an article sharing all the new articles and documentation that we, the AMER Jira Service Management team created. As mentioned ...
Connect with like-minded Atlassian users at free events near you!
Find an eventConnect with like-minded Atlassian users at free events near you!
Unfortunately there are no Community Events near you at the moment.
Host an eventYou're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.