Automating deletion of users from projects who are not in the company any more.
Hope this helps someone.
This code is not clean, but does the job.
getJiraProjects.py & deprovisionJirausers.py Scripts
#getJiraprojects.py
import requests
import credentials
import json
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
headers = {'Content-type':'application/json'}
def getProjectKeys():
url='<JIRA_BASE_URL>/rest/api/2/project'
r= requests.get(url=url,headers=headers,auth=(credentials.jira_user,credentials.jira_password),verify=False)
project_data= r.json()
project_counter = len(project_data)
proj_keys=[]
for index in range(project_counter):
proj_keys.append(project_data[index]['key'])
proj_keys = sorted(proj_keys)
return proj_keys
#deProvisionJirausers.py
import requests
import credentials
import getJiraProjects
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
#Get List of All Jira Projects
proj_keys = getJiraProjects.getProjectKeys()
num_projects = len(proj_keys)
for proj_index in range(num_projects):
role_id_list = ['10002','10001','10000']
num_roles = len(role_id_list)
jira_base_url= '<JIRA_BASE_URL>/rest/api/2/project/'
headers = {'Content-type':'application/json'}
for role_index in range(num_roles):
role_base_url = jira_base_url + proj_keys [proj_index] + '/role/' +role_id_list[role_index]
r= requests.get(url=role_base_url,headers=headers,verify=False,auth=(credentials.jira_user,credentials.jira_password))
role_data = r.json()['actors']
num_actors_per_role = len(role_data)
usernames_array_per_role = []
for username_index in range(num_actors_per_role):
if role_data[username_index]['type'] == "atlassian-user-role-actor" and "[X]" in role_data[username_index]['displayName']:
usernames_array_per_role.append(role_data[username_index]['name'].lower())
num_user_per_role_counter = len(usernames_array_per_role)
data={}
for counter in range(num_user_per_role_counter):
data['user'] = usernames_array_per_role[counter]
t= requests.delete(url=role_base_url,headers=headers,auth=(credentials.jira_user,credentials.jira_password),verify=False,params=data)
print("Deleted Inactive users for role " +role_id_list[role_index] +" from project : " +proj_keys[proj_index] )
Hope this helps
Thanks
Chander Inguva
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.