Remove a group from a project using REST API Jira Cloud
Asked by Aymeric THOMAS:

Hello everyone,

This is my first question on the community side ! :)

I want to remove a group from a specific project on Jira Cloud using REST API.

Does anyone have already done this type of task ?

I've a script to add a group on project using REST API but I can't solved the deletion part.

ERROR 400 : BAD REQUEST  
-----------------------------------------------------------------------------

def get_projects_by_category(jira_url, username, api_token, category):  
auth = HTTPBasicAuth(username, api_token)  
headers = {  
'Accept': 'application/json'  
}  
start_at = 0  
max_results = 50  
all_projects = \[\]

while True:  
url = f'{jira_url}/rest/api/3/project/search?startAt={start_at}\&maxResults={max_results}'  
response = requests.get(url, headers=headers, auth=auth)  
if response.status_code == 200:  
data = response.json()  
projects = data.get('values', \[\])  
all_projects.extend(projects)  
if data\['isLast'\]:  
break  
start_at += max_results  
else:  
print(f'Erreur {response.status_code}: {response.text}')  
return None

filtered_projects = \[project for project in all_projects if 'projectCategory' in project and project\['projectCategory'\]\['name'\] == category\]  
return filtered_projects

def get_role_id(jira_url, username, api_token, role_name):  
auth = HTTPBasicAuth(username, api_token)  
headers = {  
'Accept': 'application/json'  
}

url = f'{jira_url}/rest/api/3/role'  
response = requests.get(url, headers=headers, auth=auth)  
if response.status_code == 200:  
roles = response.json()  
for role in roles:  
if role\['name'\] == role_name:  
return role\['id'\]  
print(f'Erreur lors de la r?cup?ration des r?les: {response.status_code}: {response.text}')  
return None

def remove_group_from_projects(jira_url, username, api_token, projects, role_id, group_name):  
auth = HTTPBasicAuth(username, api_token)  
headers = {  
'Accept': 'application/json',  
'Content-Type': 'application/json'  
}  
for project in projects:  
project_id = project\['id'\]  
url = f'{jira_url}/rest/api/3/project/{project_id}/role/{role_id}'  
print(f'Trying to delete group {group_name} from project {project\["name"\]} with role ID {role_id}')  
response = requests.delete(url, headers=headers, auth=auth, json={'group': \[group_name\]})  
print(f'Response status code: {response.status_code}')  
print(f'Response text: {response.text}')  
if response.status_code == 204:  
print(f"Groupe {group_name} supprim? du projet {project\['name'\]}")  
else:  
print(f'Erreur lors de la suppression du groupe du projet {project\["name"\]}: {response.status_code}: {response.text}')

# Appeler les fonctions pour r?cup?rer les projets, obtenir l'ID du r?le, et supprimer le groupe des projets  
projects = get_projects_by_category(JIRA_URL, JIRA_USERNAME, JIRA_API_TOKEN, PROJECT_CATEGORY)

if projects is not None:  
role_id = get_role_id(JIRA_URL, JIRA_USERNAME, JIRA_API_TOKEN, ROLE_NAME)  
if role_id is not None:  
remove_group_from_projects(JIRA_URL, JIRA_USERNAME, JIRA_API_TOKEN, projects, role_id, GROUP_NAME)  
else:  
print("Aucun projet trouv? ou erreur lors de la r?cup?ration des projets.")  
--------------------------------------------------------------------------------------  
Thank you in advance for helping me.


Answers:
author: Pierre-Alexandre LOUGNON
body: Hi Aymeric,

I noticed a small mistake in your delete request:  

response = requests.delete(url, headers=headers, auth=auth, json={'group': \[group_name\]})

Your query should be in '**params** ' instead of '**json** '  

response = requests.delete(url, headers=headers, auth=auth, **params** ={'group': \[group_name\]})  

After making this change, everything should work perfectly! :)  

<br />


Tags: cloud

jira-cloud