How to add group to all projects

kyco4ektopta September 21, 2017

Hi! Can anybody help me pls? How can i add a group to the role in all projects via REST API? Since there are a lot of projects and it is very difficult to do this by hands.

I have a jira version 6.3.8

 

Thanks.

3 answers

1 accepted

1 vote
Answer accepted
kyco4ektopta September 22, 2017

Ok, i found one way.
Take the ID of all projects and rewrite them in a text file
Next, create a script with the following code:

for (( i = 1; i < 8; i++ ))
do
key=$(sed -n $i"p" $1)
echo $key
key=${key%$'\r'}
curl -x http://YourProxyServer:3128/ -i -H 'Content-Type: application/json' -X POST -d '{"group": ["YourGroup"]}' -u user:password https://jira_url/rest/api/latest/project/${key}/role/{roleid}
done
1 vote
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 @kyco4ektopta

If you don't want to do this via REST API there's a way to do it using the ScriptRunner "Script Console";

//// 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)

 

Then you don't have to do those manual steps in excel.

Obviously, this requires you to have ScriptRunner installed. Just run the code in the SR "Script Console" but make sure to test it in a test environment first!

Best regards,

Markus Fredén

Solnet Solutions April 22, 2020

This worked perfectly for me. Thank you Markus!

Like Markus Fredén likes this
Janardhan May 13, 2020

Hi @Markus Fredén,

 

with above script if i would like to do only for few projects based on list?

Thanks

Swathi November 2, 2021

@Markus Fredén - 

 

def projects = projectManager.getProjects()

 

The above code is giving me all projects in jira.

If we have only 10 projects to be updated, how can we define the list? I have the names of all 10 projects.

 

--Swathi.

Vasanthakumar Velusamy December 27, 2022

Hi @Markus Fredén  Could you please explain this script, I am not sure which place I need to update my details. Thanks in advance!

Ralph Kringhs January 4, 2023

Worked like a breeze and exactly what I was looking for.

Like Vasanthakumar Velusamy likes this
Vasanthakumar Velusamy January 16, 2023

Hi @Ralph Kringhs ,

 

I am having the same requirement, could you please tell which code you have used and how.

 

Here is my requirement, I am having list of projects and its groups. I have to add those groups into projects. Thanks in advance.

Ralph Kringhs January 24, 2023

I used @Markus Fredén 's ScriptRunner bit of code as shown in this thread

0 votes
josh
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.
September 21, 2017

When I need to do this, I open in excel the list of project keys (copy and paste from admin page), then build a lot of curl commands that put a group in a role for a single project.

Repeat this command for every project:

curl -i -H 'Content-Type: application/json' -X POST -d '{"group": ["GroupName"]}' -u username:password https://<jiraurl>/rest/api/latest/project/<projectKey>/role/<roleId>

Suggest an answer

Log in or Sign up to answer