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,368,487
Community Members
 
Community Events
168
Community Groups

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

Atlassian Community Events