Groovy - Add group to project role in ALL Jira projects

Markus Fredén
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 27, 2019

Hi,

I just wanted to share my solution to this use case. Please feel free to come up with suggestions on how to simplify it or make more sense of it, I'm open to any feedback;

//// Script to add group to project role in ALL Jira projects
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 group = groupManager.getGroup("testgroup").name
def list = new ArrayList<String>()
list.add(group)

// Fetch ALL Jira projects and loop add group to project role on each project
def projects = projectManager.getProjects()
for(item in projects) {
projectRoleService.addActorsToProjectRole(list,projectRole,item,ProjectRoleActor.GROUP_ROLE_ACTOR_TYPE,errorCollection)
}
// Print errors in log
log.warn(errorCollection)

 

Best regards,

Markus Fredén

5 answers

0 votes
Akash Panchal August 21, 2023

Is there a way to modify the script to add a group with project role to specific, selected projects?

For example, add a group with Read/Write project role to projects ABC, DEF, GHI only. 

Thanks!

0 votes
Mihkel Kagovere
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 19, 2022

Thank you. This works. You saved me a lot of pain.

0 votes
Nitish Sajwan July 13, 2020

Hi Markus,

 

I have also tried your way to pass the group name directly to the "addActorsToProjectRole" method, just like he way you did. But it is also not working for me. 

The logs says

Error Messages: [We can't find 'TAAG Global Reporting' in any accessible user directory. Check they're still active and available, and try again.]

I am using Jira server 8.5 and I have also checked the group name exists or not. Also, I have tried manually to do it. And manually adding a project group to a role is happening.

0 votes
Nitish Sajwan July 13, 2020

Hi , 

I am using the same method, it was not working for me. And very surprisingly it was working fine for me few days back.

 

 

 

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 static com.atlassian.jira.issue.IssueFieldConstants.ISSUE_TYPE
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.Issue
import com.atlassian.crowd.embedded.impl.ImmutableGroup
import com.atlassian.jira.user.ApplicationUser

def projectManager = ComponentAccessor.getProjectManager()
def projectRoleService = ComponentAccessor.getComponent(ProjectRoleService)
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def errorCollection = new SimpleErrorCollection()
// here we have to figure going to get the list of selected projects
def project = projectManager.getProjectObjByKey("TPSNC")
//log.warn(project)
def projectRole = projectRoleManager.getProjectRole("Read Only")
//log.warn(projectRole)
def searchService = ComponentAccessor.getComponent(SearchService)
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def queryParser = ComponentAccessor.getComponent(JqlQueryParser)
def issueManager = ComponentAccessor.getIssueManager()
def query = queryParser.parseQuery("issuetype = \"Project Configuration\" AND project= TPSNC")
def search = searchService.search(user, query, new PagerFilter())
def groupManager = ComponentAccessor.getGroupManager()

// Get the custom field that you want to update
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issue
def customField
def groups
List<String> actors=new ArrayList<>()
// Loop through the search results. If the key of one of those issues matches, grab the value from its custom field
search.results.each { documentIssue ->
issue = issueManager.getIssueObject(documentIssue.id)
// Make sure to pass the correct custom field id below. It might change instance to instance
customField= customFieldManager.getCustomFieldObject("customfield_13604")
groups=issue.getCustomFieldValue(customField)
}
Collection<ApplicationUser> users=new ArrayList<>();
log.warn(issue)
if(issue==null)
return
for(Object group: groups){
ImmutableGroup ig=(ImmutableGroup)group
log.warn(ig.getName())
users = groupManager.getUsersInGroup(ig.getName())
log.warn( users)
for(ApplicationUser u : users){
log.warn(u.getName())
actors.add(u.getName())
log.warn(actors)
}
}

projectRoleService.addActorsToProjectRole(actors,
projectRole,
project,
ProjectRoleActor.USER_ROLE_ACTOR_TYPE,
errorCollection)


log.warn(errorCollection)

0 votes
Pavel Tyumenev May 8, 2020

Works great, Thank you!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events