Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Is that possible to assign an user to a project using Jira REST API?

paulo_silva October 6, 2020

Hi. My name is Paulo and i'm working on implementing some automations using Jira REST API as part of my work and studying. I'm Trying to assign an user to a project via API but I'm strugling to find a way to do that. I searched and found someting called Project Role that I can create and assign a user via API, but I couldn't find a way to associate a Project Role to a Project. Can you help me with that? Sorry for my bad english, its not my native language. 

I need to find a way to manage an user to a project via API. The possibility of add and remove the user of a project. using REST API v3. Is that possible?

 

Thanks!

5 answers

2 accepted

0 votes
Answer accepted
Vikrant Yadav
Community Champion
October 7, 2020

Hi @paulo_silva 

https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-project-role-actors/#api-rest-api-3-project-projectidorkey-role-id-put

 

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

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

}
paulo_silva October 8, 2020

Hi @Vikrant Yadav. Thanks for your answer! I'm not used to that programming language yet. I'm using python to run my aplication. I'm strugling with where to place the user information to specify the user. 

Should I put something like that next to the group section? 

payload = json.dumps( {"user": ["user-information"]} )

# This code sample uses the 'requests' library:
# http://docs.python-requests.org
import requests
from requests.auth import HTTPBasicAuth
import json

url = "https://your-domain.atlassian.com/rest/api/2/project/{projectIdOrKey}/role/{id}"

auth = HTTPBasicAuth("email@example.com", "")
 
headers = {
   "Accept": "application/json",
   "Content-Type": "application/json"
}

payload = json.dumps( {
  "group": [
    "jira-developers"
  ]
} )

response = requests.request(
   "POST",
   url,
   data=payload,
   headers=headers,
   auth=auth
)

print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
0 votes
Answer accepted
Niranjan
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.
October 6, 2020

Hi @paulo_silva ,

It should be a POST method. Kindly check this section - https://developer.atlassian.com/cloud/jira/platform/rest/v2/api-group-project-role-actors/#api-group-project-role-actors

POST /rest/api/2/project/{projectIdOrKey}/role/{id}

 

To get the list of all project role ids - https://your-domain.atlassian.com/rest/api/2/role

 

Hope that helps.

paulo_silva October 8, 2020

Hi @Niranjan. Thanks for your answer! Sorry for the delay on replying to you. I'm still learning how to use api and i'm trying to run this aplication via python. My problem is to find where to add the information to specify the user.

I'm editing that python code sample that should Add actors to project role. Should I write something like that?

payload = json.dumps( {"user": ["user-information"]} )

# This code sample uses the 'requests' library:
# http://docs.python-requests.org
import requests
from requests.auth import HTTPBasicAuth
import json

url = "https://your-domain.atlassian.com/rest/api/2/project/{projectIdOrKey}/role/{id}"

auth = HTTPBasicAuth("email@example.com", "")

headers = {
   "Accept": "application/json",
   "Content-Type": "application/json"
}

payload = json.dumps( {
  "group": [
    "jira-developers"
  ]
} )

response = requests.request(
   "POST",
   url,
   data=payload,
   headers=headers,
   auth=auth
)

print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
1 vote
paulo_silva October 8, 2020

I've got it! It is so simple that I'm kinda shamed..

I just add the 'user' inside payload just like the 'group' is and filled the others parameters just like I saw on Jira API website.

When the aplication runned it add the user to the project.

This is how I add user on payload:

payload = json.dumps( {
"group": ["jira-software-users"],
'user': ['{user-id-example}']
} )
1 vote
paulo_silva October 8, 2020

My doubt is how to put the information on the code to assign the user to the project. I trying to use that python sample that are avaliable on Jira API in the section that "Add actors to project role", but I'm not finding where to place the user information.

Should I create another variable called payload = json.dumps( {"user": ["user-information"]} )?

# This code sample uses the 'requests' library:
# http://docs.python-requests.org
import requests
from requests.auth import HTTPBasicAuth
import json

url = "https://your-domain.atlassian.com/rest/api/2/project/{projectIdOrKey}/role/{id}"

auth = HTTPBasicAuth("email@example.com", "")

headers = {
   "Accept": "application/json",
   "Content-Type": "application/json"
}

payload = json.dumps( {
  "group": [
    "jira-developers"
  ]
} )

response = requests.request(
   "POST",
   url,
   data=payload,
   headers=headers,
   auth=auth
)

print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
0 votes
Karthik Mohan May 15, 2024

Hello @paulo_silva , 

I hope you have solved this problem. But here is my suggestion to this

If you want to add a particular user to the project role, you can use the below example for the payload:

{
  "user": [
    "accountID"
  ]
}
If you want to add a group to the project role, you can use this payload:
{
  "groupId": [
    "952d12c3-5b5b-4d04-bb32-44d383afc4b2"
  ]
}
always use the ID, since the names are subject to change.
With regards,
Karthik.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events