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 @Kirkie
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.
Hi,
I have tried both the scripts and only it works for few users
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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]
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.