Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,646,353
Community Members
 
Community Events
196
Community Groups

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.
Nov 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

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!

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

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.

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)

Works great, Thank you!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events