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)
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.