You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
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)
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
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!)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:-
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.