Skip to content

Instantly share code, notes, and snippets.

@khpraful
Created April 19, 2022 16:59
Show Gist options
  • Save khpraful/be06fa44372c335ebefc1f6394ca9eed to your computer and use it in GitHub Desktop.
Save khpraful/be06fa44372c335ebefc1f6394ca9eed to your computer and use it in GitHub Desktop.
Approve deployment requests programatically using Azure DevOps REST APIs

Azure-DevOps-RESTful-Sample

In the config.py file, add the assignee (person or group) in the user field and personal access token (generated through Azure DevOps) in the token field
In the approve_deployments.py file, replace organization with Azure DevOps organization name and project with the name of project under that organizaiton in the urls
Execute approve_deployments.py
It will display list of all pending approvals for the configured user along with useful details such as release pipeline name, environment, approval type, etc in order to make informed decision
It will also display the list of approval ids
Enter the approval ids you would like to approve in comma-separated fashion. The list of approval ids is available as output in above step in case you want to copy-paste and approve the entire list in one go. Please make sure to provide only comma-separated list without any column brackets ([ ]) at beginning and end
It will ask for confirmation. To confirm, enter “y” or “Y”, else enter “n” or “N”
Enter approval comments
If valid approval ids are provided, the approvals will take place and confirmation messages will be displayed
import json
import requests
import base64
import config
pending_approvals_base_url = 'https://vsrm.dev.azure.com/<organization>/<project>/_apis/release/approvals'
approve_base_url = 'https://vsrm.dev.azure.com/<organization>/<project/_apis/release/approvals/'
authorization = str(base64.b64encode(bytes(':'+config.pat, 'ascii')), 'ascii')
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Basic '+authorization
}
params = {
'assignedToFilter': config.user
}
response = requests.get(url=pending_approvals_base_url, headers=headers, params=params)
approvalDetails=[]
approvalIds=[]
for i in response.json()['value']:
approvalDetails.append([i['id'],i['releaseDefinition']['name'],i['releaseEnvironment']['name'], i['approvalType'],i['approver']['uniqueName']])
approvalIds.append(i['id'])
print('====================================================================================================')
print('Approval List:')
print('Approval Id, Release, Environment, Approval Type, Approver')
for j in approvalDetails:
print(j)
print('====================================================================================================')
print ('Approval Ids:')
print(approvalIds)
print('====================================================================================================')
approvals = input("Enter approval ids for the release requests you want to approve (as comma-separated values):")
print('====================================================================================================')
approval_list = approvals.split(",") if approvals else []
if approval_list==[]:
print("No approval ids provided. Operation cancelled!!!")
else:
confirm = input("Are you sure you want to approve specified release requests(y/n)?")
print('====================================================================================================')
if (confirm.lower()=='y' or confirm.lower()=='yes'):
comments=input("Enter approval comments:")
print('====================================================================================================')
for i in approval_list:
data = {
'status': 'approved',
'comments': comments
}
response = requests.patch(url=approve_base_url + str(i) +'?api-version=6.0', headers=headers, data=json.dumps(data))
if response.status_code==200:
print('Deployment request with id ' + i + ' approved successfully.')
elif response.status_code==401:
print('You are not authorized to perform this operation. Please check your permissions.')
elif response.status_code == 400:
print('Bad Request, unable to proceed. Please check approval settings for this stage.')
else:
print('====================================================================================================')
print("Operation cancelled!!!")
user = "[email protected]"
pat = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment