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.
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Markus Fredén,
with above script if i would like to do only for few projects based on list?
Thanks
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Worked like a breeze and exactly what I was looking for.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.