How to add user to project role using jira rest api and json format to be used

Madhuri June 5, 2017

HI ,

 

Im trying to add user to a specific project role in a project by using JIRA rest api and invoking it throw a power shell ,but when i'm passing json data as below , its giving me "Remote server returned error:400 bad request" error.

 

 Invoke Rest-Method -Uri  "https://jira url/jira/rest/api/2/project/project id/role/ role id" -Method post -Headers $Headers -Body $data1 -ContentType  "application/json"

 

Json data

$data= @"

{

"user" : ["userid/name"]

}

"@

$data1= $data | ConvertTo-Json

 

and please let me know whether i have to use PUT/POST functionality to add user to project role and its helpful if any code need to be added to Json .

3 answers

3 votes
Chander Inguva
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.
June 6, 2017

Hey Madhuri,

This should work for you

curl -D- -u <username>:<password> -H "Content-Type:application/json" -X POST -d '{"user":["username"]}' -k https://jira-stg.example.com/rest/api/2/project/ABC/role/10002

curl command.PNG

Using Node.js

//Use .defaults({strictSSL: false}) to by pass SSL certificate or https connection
var request = require('request').defaults({strictSSL: false})

var headers = {
    'Content-Type': 'application/json'
};

var dataString = '{"user":["cinguva","apawl"]}';

var options = {
    url: 'https://jira-stg.gogoair.com/rest/api/2/project/PSOBAT/role/10002',
    method: 'POST',
    headers: headers,
    body: dataString,
    auth: {
        'user': 'admin',
        'pass': 'password'
    }

};

function callback(error, response, body) {
        console.log(error);

}

request(options, callback);

Hope this helps

Reference threads and this one

Cheers

Chander

Donald Skanes November 8, 2017

The curl command worked for me for setting the project role. How would you unselect a role?

Vijay Koleti December 12, 2018

How can I get a list of project role ids in this case?

Amir Katz (Outseer)
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.
January 13, 2019

There is a set of ROLE APIs. One of them (GET) is used to get all the roles and the ID of each:

https://developer.atlassian.com/cloud/jira/platform/rest/v3/?_ga=2.160847869.1154825308.1547392536-665427180.1519729030#api-api-3-role-get

Ishan Hassija June 8, 2022

Thanks it helps me to add users to a project using role id. As each user is having different role id so just replace while assigning role to respective member.

1 vote
David Hrdý October 13, 2021

Hello,

 

just wanted to share a simple Power shell script that will add users to project roles directly from a CSV file. You will need 3 columns in the CSV - User (username of the user in JIRA), Project (project key), and RoleID (ID of the role).

and

With just a little bit of adjustment, it can be used for many more in JIRA.

 

$csvpath="C:\YOURPATHTOCSV\YourCSVFile.csv"

$auth = "Yourusername:Yourpassword"
$Encoded = [System.Text.Encoding]::UTF8.GetBytes($auth)
$authorizationInfo = [System.Convert]::ToBase64String($Encoded)
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("Authorization","Basic $($authorizationInfo)")


$csvfile = Import-Csv -Path $csvpath -Delimiter ";" #change to , if you use comma as delimiter
Foreach($el in $csvfile){
$body = "{`"user`": [`"$($el.User)`"]}"
$response = Invoke-RestMethod "https://YOURURL/rest/api/2/project/$($el.Project)/role/$($el.RoleID)" -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json
}

You need to edit bold parts of the code accordingly.

Have a nice day!

Tim Perrault
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.
January 28, 2022

Hi @David Hrdý

 

I liked the look of your script and want to give it a try. I made a tiny adjustment to hopefully use a personal access token. I still need to upgrade my instance of Jira so I haven't been able to test it, but I was wondering if you could take a quick look to see if my adjustments look alright?

 

$csvpath="C:\YOURPATHTOCSV\YourCSVFile.csv"

$auth = "YourToken"
#$Encoded = [System.Text.Encoding]::UTF8.GetBytes($auth)
#$authorizationInfo = [System.Convert]::ToBase64String($Encoded)
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("Authorization","Bearer $($auth)")


$csvfile = Import-Csv -Path $csvpath -Delimiter ";" #change to , if you use comma as delimiter
Foreach($el in $csvfile){
$body = "{`"user`": [`"$($el.User)`"]}"
$response = Invoke-RestMethod "https://YOURURL/rest/api/2/project/$($el.Project)/role/$($el.RoleID)" -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json
}

Thanks,

Tim

0 votes
Karthik Arun June 17, 2021

Hello, I have a scenario from my user . Situation is he wants around 100 users to be added to around 20 projects and assigned to multiple project roles. Can you suggest through curl example. From my end. we are not creating new groups due to our maintenance issues in jira. Without groups. is it possible through rest api from curl to achive this. Summarizinfg again. Adding m,ultople users to multiple project roles for around 20 projects. Please suggest/.

Suggest an answer

Log in or Sign up to answer