How to copy all users from Project Role to User Group

Recently, I faced to interesting and challenging task.

We are moving one huge Jira instance to unified Access Management, which will be driven by user request in dedicated project and approved / declined request will automatically add users to users groups, which are shared in Permission Scheme through more projects. 

Well, this part will be covered in another article, but as a predisposition, I had to migrate all of the users from Projects Roles to User Groups. 

Doing this manually would take hours, maybe days with possibility of making a lot of mistakes.

So I took inspiration @JamieA answer over there https://community.atlassian.com/t5/Answers-Developer-Questions/How-to-list-all-members-of-a-certain-project-role/qaq-p/528122 and created a script for that.

As sharing is caring, I would like to share this with everyone, because I find this quite useful :-) 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.bc.project.component.ProjectComponentManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.security.roles.ProjectRole
import com.atlassian.jira.security.roles.ProjectRoleActors
import com.atlassian.jira.security.roles.ProjectRoleManager

ProjectRoleManager projectRoleManager = ComponentAccessor.getOSGiComponentInstanceOfType(ProjectRoleManager.class) as ProjectRoleManager

def projectComponentManager = ComponentAccessor.getComponent(ProjectComponentManager)
def projectManager = ComponentAccessor.getComponent(ProjectManager)
def groupManager = ComponentAccessor.getGroupManager()
def usersKeyString
def usersObjList = []

ProjectRole devsRole = projectRoleManager.getProjectRole("Developers") //SOURCE: Change to appropriate Project Role
def group = groupManager.getGroup("group_developers") //TARGET: Change to appropriate Jira Group
def Projects = projectManager.getProjectObjByKey("FOO") //PROJECT: Change to Project you are working with

for (item in Projects)

{
ProjectRoleActors actors = projectRoleManager.getProjectRoleActors(devsRole, item)
usersKeyString = ("${actors.getUsers()*.key}").toString()
}

usersKeyString = usersKeyString.replace("[","")
usersKeyString = usersKeyString.replace("]","")
usersKeyString = usersKeyString.replace(" ","")

def usersKeyList = usersKeyString.split(',').collect {it as String}

for (item in usersKeyList)

{
def userObject = ComponentAccessor.getUserUtil().getUserByKey(item)
if (userObject != null)
{
usersObjList.add(userObject)
}
}

for (item in usersObjList)

{
groupManager.addUserToGroup(item,group)
}

 I also refactored original @JamieA answer, as It contain deprecated Component Manager and few not anymore available method. (Unfortunately original thread is locked, so I cannot post answer over there).

So here is the script compatible with Jira 8.x version which will generate list of specified Project role in all projects: 

Key | Name | Lead | Member

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.bc.project.component.ProjectComponentManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.security.roles.ProjectRole
import com.atlassian.jira.security.roles.ProjectRoleActors
import com.atlassian.jira.security.roles.ProjectRoleManager

ProjectRoleManager projectRoleManager = ComponentAccessor.getOSGiComponentInstanceOfType(ProjectRoleManager.class) as ProjectRoleManager

def projectComponentManager = ComponentAccessor.getComponent(ProjectComponentManager)
def projectManager = ComponentAccessor.getComponent(ProjectManager)
def actorsList = []
def instanceList = []

ProjectRole devsRole = projectRoleManager.getProjectRole("Developers") //Specify the project role you would like to list across the Jira

def Projects = projectManager.getProjectObjects()

for (item in Projects)

{
ProjectRoleActors actors = projectRoleManager.getProjectRoleActors(devsRole, item)
instanceList.add("$item.key, $item.name, $item.lead, ${actors.getUsers()*.name}\n")
}
return instanceList

 

4 comments

Milan Chheda [INFOSYSTA]
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.
November 12, 2020

Thanks for sharing. 

Like Tomáš Vrabec likes this
Monika Rani
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.
January 1, 2021

@Tomáš Vrabec  Interesting thanks for sharing!

Like Tomáš Vrabec likes this
Omar Rashid August 2, 2022

@Tomáš Vrabec Is there a way to refactor this script to copy all users and their roles from one project to an existing project? 

Tomáš Vrabec October 11, 2022

@Omar Rashid 

You have to go thru 

projectRoleManager

component, there is method to also create users with roles.

I can do that, but now have no spare time to do that :-(  

Comment

Log in or Sign up to comment
TAGS
AUG Leaders

Atlassian Community Events