Add user to a project role

Nasser Aloraini April 6, 2023

Greetings,

 

I have this script which supposed to add a user to a project role when it runs.

I'm new to scriptrunner and I get the error that user is not active or does not exist is user directory. I was trying on my user ID 

 

Here is script below, Thanks in advance !!

 

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")

 

// Fetch group (case insensitive) and add to list (addActorsToProjectRole requires a list)

def actors = ['User_ID']

 

// Fetch ALL Jira projects and loop add group to project role on each project

def projects = projectManager.getProjectObjByKey("Project_Key")

for(item in projects) {

projectRoleService.addActorsToProjectRole(projectRole,item,ProjectRoleActor.GROUP_ROLE_ACTOR_TYPE,errorCollection) 

}

// Print errors in log

log.warn(errorCollection)

2 answers

1 vote
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 6, 2023

Hi @Nasser Aloraini

For your requirement, I suggest trying something like:-

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
import com.adaptavist.hapi.jira.projects.Projects

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

def selectedUsers = ['admin', 'ram'] // List of the usernames
def selectedProjects = ['MOCK', 'ST'] // List of the Project Keys

def projectList = selectedProjects.collect {Projects.getByKey(it)}
def developerRole = projectRoleManager.getProjectRole('Developers')

projectList.each {
projectRoleService.addActorsToProjectRole(selectedUsers, developerRole, it, ProjectRoleActor.GROUP_ROLE_ACTOR_TYPE, new SimpleErrorCollection())
}

Please note the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a screenshot of the code on the ScriptRunner console:-

scriptrunner_console.png

The code above uses ScriptRunner's HAPI feature to simplify the invocation of objects like the Project and User whereby you do not need to use the ComponentAccessor to invoke it.

Kindly upgrade your ScriptRunner plugin to the latest release, i.e. 8.0.0 to use the HAPI code.

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram

1 vote
Kristján Geir Mathiesen
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 6, 2023

Hi @Nasser Aloraini and welcome to the Community.

Unfortunately, I am not a script person but in all honesty, have you tried asking chatGPT?

Make sure to state that this is for Server.

HTH,
KGM

Fun Man Andy
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 6, 2023

Hahah! Or ask a human who knows what they’re doing with Scriptrunner…

Adaptavist have an amazing support team and the best Head of Product Support, Katy!

Just raise a ticket with them and they’ll help!!

https://productsupport.adaptavist.com/servicedesk/customer/user/login?destination=portals

and if you luck out and get the living legend herself, tell Katy:

Fun Man Andy says it’s #HammerTime 🃏 “

(don’t forget the emoji for added impact!)

Like Nic Brough -Adaptavist- likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events