Change the Role of an user in a Jira Project automatically

Karthikeyan Devi November 4, 2024

Hi Everyone,

Hope everyone is doing good. In our Organization we have around 6000 users per Jira Project and they have been assigned to 2 major roles irrespective of their actual role.

I am leading an initiative to create more roles in Jira and assign the existing 6000 users to their respective roles.

To my knowledge there is no way to perform this Automatically in Jira. So, Please help with the ways to change or an Script Runner script which can pick the user from the project and change to the new role.

 

 

1 answer

0 votes
Salih Tuç
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 4, 2024

Hi @Karthikeyan Devi ,

I created following script in order to solve your problem. Following script removes users from roleNameToRemove and add these users to roleNameToAdd:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.bc.projectroles.ProjectRoleService

def userManager = ComponentAccessor.userManager
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def projectRoleService = ComponentAccessor.getComponent(ProjectRoleService)

//Specify project and role
final def projectKey = '<PROJECT_KEY>'
final def roleNameToRemove = '<ROLE_NAME_TO_REMOVE_USERS>'
final def roleNameToAdd = '<ROLE_NAME_TO_ADD_USERS>'

final def actorType = 'atlassian-user-role-actor'

def project = ComponentAccessor.projectManager.getProjectByCurrentKey(projectKey)
def projectRoleToRemove = projectRoleManager.getProjectRole(roleNameToRemove)
def projectRoleToAdd = projectRoleManager.getProjectRole(roleNameToAdd)

def usernames = []

projectRoleService.getProjectRoleActors(projectRoleToRemove, project, null).roleActors.each {

    usernames.add(it.parameter)

}

log.warn(usernames)
projectRoleService.removeActorsFromProjectRole(usernames as List<String>, projectRoleToRemove, project, actorType, null)
projectRoleService.addActorsToProjectRole(usernames as List<String>, projectRoleToAdd, project, actorType, null)

Please, just in case, try the code at a test project first.

And also, note that this script removes all users in the roleNameToRemove. You should modify the code if you need to remove specific users.

And finally, note that you can directly give a list of usernames to the last two functions if there are some specific set of users, for example:

def usernames = ["user1", "user2"]

Have a nice day!

Karthikeyan Devi November 4, 2024

Hi @Salih Tuç thanks for your effort, Let me try this script in my SIT Environment and Provide you the feedback. If any trouble will trouble you further more.

Karthikeyan Devi November 4, 2024

below is my script,

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.bc.projectroles.ProjectRoleService

def userManager = ComponentAccessor.userManager
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def projectRoleService = ComponentAccessor.getComponent(ProjectRoleService)

//Specify project and role
final def projectKey = 'TP'
final def roleNameToRemove = 'Administrators'
final def roleNameToAdd = 'Users'

final def actorType = 'atlassian-user-role-actor'

def project = ComponentAccessor.projectManager.getProjectByCurrentKey(projectKey)
def projectRoleToRemove = projectRoleManager.getProjectRole(roleNameToRemove)
def projectRoleToAdd = projectRoleManager.getProjectRole(roleNameToAdd)

def usernames = ["venlp8"]

projectRoleService.getProjectRoleActors(projectRoleToRemove, project, null).roleActors.each {

    usernames.add(it.parameter)

}

log.warn(usernames)
projectRoleService.removeActorsFromProjectRole(usernames as List<String>, projectRoleToRemove, project, actorType, null)
projectRoleService.addActorsToProjectRole(usernames as List<String>, projectRoleToAdd, project, actorType, null)
Below is the Log,
2024-11-05 13:40:25,171 WARN [runner.ScriptBindingsManager]: [venlp8, JIRAUSER51133, JIRAUSER47446, JIRAUSER24354, JIRAUSER46764] 2024-11-05 13:40:25,207 INFO [CachingProjectRoleAndActorStore.projectRoleActors]: Cache com.atlassian.jira.security.roles.CachingProjectRoleAndActorStore.projectRoleActors was flushed 2024-11-05 13:40:25,224 INFO [IssueSecurityLevelManagerImpl.projectAndUserToSecurityLevelCache]: Cache com.atlassian.jira.issue.security.IssueSecurityLevelManagerImpl.projectAndUserToSecurityLevelCache was flushed 2024-11-05 13:40:25,288 INFO [structure.process-state]: Cache com.almworks.structure.process-state was flushed 2024-11-05 13:40:25,384 INFO [structure.process-state]: Cache com.almworks.structure.process-state was flushed 2024-11-05 13:40:25,413 INFO [structure.process-state]: Cache com.almworks.structure.process-state was flushed 2024-11-05 13:40:25,431 INFO [structure.process-state]: Cache com.almworks.structure.process-state was flushed 2024-11-05 13:40:25,446 ERROR [common.UserScriptEndpoint]: ************************************************************************************* 2024-11-05 13:40:25,446 ERROR [common.UserScriptEndpoint]: Script console script failed: java.lang.NullPointerException at com.atlassian.jira.bc.projectroles.DefaultProjectRoleService.createRoleActors(DefaultProjectRoleService.java:891) at com.atlassian.jira.bc.projectroles.DefaultProjectRoleService.updateActorsToProjectRole(DefaultProjectRoleService.java:715) at com.atlassian.jira.bc.projectroles.DefaultProjectRoleService.addActorsToProjectRole(DefaultProjectRoleService.java:1068) at Script9.run(Script9.groovy:30)
Result:
It wiped all users assigned to the project.
Salih Tuç
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 4, 2024

Hmm, it seems like the first function is removing the content of usernames. so the second one fails.

Can you please try following for the last two lines:

projectRoleService.removeActorsFromProjectRole(usernames.clone() as List<String>, projectRoleToRemove, project, actorType, null)
log.warn(usernames)
projectRoleService.addActorsToProjectRole(usernames.clone() as List<String>, projectRoleToAdd, project, actorType, null)

 

Karthikeyan Devi November 4, 2024
Modified Script as instructed
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.bc.projectroles.ProjectRoleService

 

def userManager = ComponentAccessor.userManager
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def projectRoleService = ComponentAccessor.getComponent(ProjectRoleService)

 

//Specify project and role
final def projectKey = 'IP'
final def roleNameToRemove = 'Administrators'
final def roleNameToAdd = 'Users'

 

final def actorType = 'atlassian-user-role-actor'

 

def project = ComponentAccessor.projectManager.getProjectByCurrentKey(projectKey)
def projectRoleToRemove = projectRoleManager.getProjectRole(roleNameToRemove)
def projectRoleToAdd = projectRoleManager.getProjectRole(roleNameToAdd)

 

def usernames = ["venlp8"]

 

projectRoleService.getProjectRoleActors(projectRoleToRemove, project, null).roleActors.each {

 

    usernames.add(it.parameter)

 

}

 

projectRoleService.removeActorsFromProjectRole(usernames.clone() as List<String>, projectRoleToRemove, project, actorType, null)
log.warn(usernames)
projectRoleService.addActorsToProjectRole(usernames.clone() as List<String>, projectRoleToAdd, project, actorType, null)

Error Message:
java.lang.NullPointerException at com.atlassian.jira.bc.projectroles.DefaultProjectRoleService.createRoleActors(DefaultProjectRoleService.java:891) at com.atlassian.jira.bc.projectroles.DefaultProjectRoleService.updateActorsToProjectRole(DefaultProjectRoleService.java:715) at com.atlassian.jira.bc.projectroles.DefaultProjectRoleService.addActorsToProjectRole(DefaultProjectRoleService.java:1068) at Script10.run(Script10.groovy:30)
null
Log :
2024-11-05 15:22:59,705 INFO [CachingProjectRoleAndActorStore.projectRoleActors]: Cache com.atlassian.jira.security.roles.CachingProjectRoleAndActorStore.projectRoleActors was flushed 2024-11-05 15:22:59,711 INFO [IssueSecurityLevelManagerImpl.projectAndUserToSecurityLevelCache]: Cache com.atlassian.jira.issue.security.IssueSecurityLevelManagerImpl.projectAndUserToSecurityLevelCache was flushed 2024-11-05 15:22:59,712 WARN [runner.ScriptBindingsManager]: [venlp8] 2024-11-05 15:22:59,713 ERROR [common.UserScriptEndpoint]: ************************************************************************************* 2024-11-05 15:22:59,713 ERROR [common.UserScriptEndpoint]: Script console script failed: java.lang.NullPointerException at com.atlassian.jira.bc.projectroles.DefaultProjectRoleService.createRoleActors(DefaultProjectRoleService.java:891) at com.atlassian.jira.bc.projectroles.DefaultProjectRoleService.updateActorsToProjectRole(DefaultProjectRoleService.java:715) at com.atlassian.jira.bc.projectroles.DefaultProjectRoleService.addActorsToProjectRole(DefaultProjectRoleService.java:1068) at Script10.run(Script10.groovy:30)
Result: 
Wiped all users
Salih Tuç
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 5, 2024

What is your Jira DC version?

Karthikeyan Devi November 5, 2024

Jira version v9.2.0

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events