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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Groovy - Add group to project role in ALL Jira projects

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

3 comments

Works great, Thank you!

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)

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.

Comment

Log in or Sign up to comment
TAGS
Community showcase
Published in Jira Service Management

Security Advisory for Jira Service Management

On October 20, 2021, Atlassian published a security advisory for Jira Service Management. The full advisory is available at this link.  We've seen a number of questions already asking for...

62 views 0 1
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you