Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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 - Find group in Project Role and add another group into same role

I would like to pull a list of all project roles where the "staff" group is located and add the "Employees" group into the role as well.

Where do I even start? I'm hoping for a groovy solution to run in Script Console.

1 answer

1 accepted

2 votes
Answer accepted

Hi Michael,

First thing to note is that role membership can vary from project to project. So you can't just say "Give me the names of all project roles, where "Staff" group is located" simply because this group can be in, say, "Developers" role in Project A but not in this very role in Project B. 

With that in mind, assuming you want to give your "Employees" group exactly the same role membership as your "Staff" group the correct way to do this would be to check every role membership of every project and if your "Staff" group is found in any role/s  in a given project add your "Employees" group to the same role/s in those projects.

And yes, this can be done using scriptrunner script console. Let me know if you need further help with the code.

Hi Ivan, 

Yes. I need help with the code to implement this.

"First thing to note is that role membership can vary from project to project. So you can't just say "Give me the names of all project roles, where "Staff" group is located" simply because this group can be in, say, "Developers" role in Project A but not in this very role in Project B. "

--- I am entirely aware that this is the case, that roles changes per project which is why I'm not looking for a list of projects, more so, I would like a list of roles in which "staff" is located within those projects that are meaningful to me.

"With that in mind, assuming you want to give your "Employees" group exactly the same role membership as your "Staff" group the correct way to do this would be to check every role membership of every project and if your "Staff" group is found in any role/s  in a given project add your "Employees" group to the same role/s in those projects."

--- This is exactly what I am looking to script.

Ok then, let's get this show on the road, shall we? I'm going to assume your Jira version is 7.x. Use this code to give your "Employees" group the exact same role membership as your "Staff" group:

import com.atlassian.jira.bc.projectroles.ProjectRoleService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRole
import com.atlassian.jira.util.SimpleErrorCollection
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.security.roles.ProjectRoleActor

ProjectRoleService projectRoleService = ComponentAccessor.getComponent(ProjectRoleService);
ProjectRoleManager projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager);
ProjectManager projectManager = ComponentAccessor.getComponent(ProjectManager);

List<Project> projects = projectManager.getProjects();
Collection<ProjectRole> projectRoles = projectRoleManager.getProjectRoles();
SimpleErrorCollection errorCollection = new SimpleErrorCollection();
Collection<String> actorCollection = new ArrayList<String>();
actorCollection.add("Employees");

projects.each{project ->
projectRoles.each{role ->
Set roleActors = projectRoleManager.getProjectRoleActors(role, project).getRoleActorsByType("atlassian-group-role-actor");
if (roleActors.size() > 0){
roleActors.each{group ->
if (group.getDescriptor() == "Staff"){
projectRoleService.addActorsToProjectRole(actorCollection, role, project, ProjectRoleActor.GROUP_ROLE_ACTOR_TYPE, errorCollection)
}
}
}
}
}
Like Raul MrAddon likes this

Hi ivan,

This script is working perfectly. My requirement is that to replace the group with existing one.
For Example: replace the Staff group with employee. 

or could it be to remove the staff group and add one or  more group where staff group is existing 
can you please help me out how i can do it.  

your effort will be much appreciated.

Thanks

BR,

Adnan Haider 

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

Staying organized with Jira: best practices for a better project management

Project managers know this problem: A “mountain of work” lays in front of you, and you don’t know how and where to tackle them. Different to-dos lie ahead, but just one task after the other can be ha...

245 views 2 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