This post does not have a lot of text, only an introduction. I use the VSTS Rest API quite a lot and the documentation is great, but not everything is documented or hard to find. In this post, I list some snippets that you can use to get even more out of the VSTS Rest API.
To run the snippets you can use the great VS Code extension REST Client that can run the .http files and uses placeholder that you can fill with your own values
- BuildReleaseTasks.http
- Get All Build and Release Tasks in the account
- ServiceEndPoints.http
- Get all Service EndPoints
- Get all Service EndPoints of specific type
- Get 1 Service EndPoint
- Get Security of Service EndPoint
- Set User/Group in Security of Service EndPoint
- Create Service EndPoint
- Update Service EndPoint
- VSTSGroupsUsers.http
- Get Users / Groups in project
- VSTSProjects.http
- Get all VSTS Team projects in Account
- Get details of 1 VSTS Team Project
#Using the great extension in VS Code https://marketplace.visualstudio.com/items?itemName=humao.rest-client | |
#use the gist GetBasicAuthToken.ps1 to get a valid token from a PAT | |
@collectionurl=https://account.visualstudio.com | |
@token=Base64EncryptedPAT e.g. Basic ONOTAVALIDTOKEN=" | |
### | |
# Get all Available Branch Policies in the project | |
# –> value.id contains branch policy type id | |
GET {{collectionurl}}/_apis/projects/?api-version=1.0 | |
Authorization: {{token}} | |
### | |
# Get branch policies of all repos and all branches in VSTS project | |
GET {{collectionurl}}/{{project}}/_apis/policy/configurations?api-version=2.0-preview | |
Authorization: {{token}} | |
### | |
# Create a NEW branch policy | |
@repositoryid=<guid of git repo> | |
@policyid=<guid of policy type> | |
POST {{collectionurl}}/{{project}}/_apis/policy/configurations?api-version=2.0-preview | |
Authorization: {{token}} | |
Content-Type: application/json | |
{ | |
"isEnabled": "true", | |
"isBlocking": "true", | |
"settings": { | |
"minimumApproverCount": 1, | |
"creatorVoteCounts": "false", | |
"allowDownvotes": "false", | |
"resetOnSourcePush": "true", | |
"scope": [ | |
{ | |
"refName" : "refs/heads/master", | |
"matchKind" : "Exact", | |
"repositoryId" : "{{repositoryid}}" | |
} | |
], | |
}, | |
"type":{ | |
"id" : "{{policyid}}" | |
} | |
} | |
### | |
#Update branch policy. Same as New but PUT and adding polciyconfigurationid that can be retrieved by getting branch policies | |
# Create Branch Policy on branch | |
@policyConfigurationId=243 | |
PUT {{collectionurl}}/{{project}}/_apis/policy/configurations/{{policyConfigurationId}}?api-version=2.0-preview | |
Authorization: {{token}} | |
Content-Type: application/json | |
{ | |
"isEnabled": "true", | |
"isBlocking": "true", | |
"settings": { | |
"minimumApproverCount": 1, | |
"creatorVoteCounts": "false", | |
"allowDownvotes": "false", | |
"resetOnSourcePush": "true", | |
"scope": [ | |
{ | |
"refName" : "refs/heads/master", | |
"matchKind" : "Exact", | |
"repositoryId" : "{{repositoryid}}" | |
} | |
], | |
}, | |
"type":{ | |
"id" : "{{policyid}}" | |
} | |
} |
#Using the great extension in VS Code https://marketplace.visualstudio.com/items?itemName=humao.rest-client | |
#use the gist GetBasicAuthToken.ps1 to get a valid token from a PAT | |
@collectionurl=https://account.visualstudio.com | |
@token=Base64EncryptedPAT e.g. Basic ONOTAVALIDTOKEN=" | |
### Get All Build and Release Tasks in account | |
GET {{collectionurl}}/_apis/distributedtask/tasks | |
Authorization: {{token}} |
function _GetToken { | |
param | |
( | |
[string] $pat | |
) | |
$userpass = ":$($pat)" | |
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($userpass)) | |
$accesstoken = "Basic $($encodedCreds)" | |
return $accesstoken; | |
} |
#Using the great extension in VS Code https://marketplace.visualstudio.com/items?itemName=humao.rest-client | |
#use the gist GetBasicAuthToken.ps1 to get a valid token from a PAT | |
@collectionurl=https://account.visualstudio.com | |
@token=Base64EncryptedPAT e.g. Basic ONOTAVALIDTOKEN=" | |
@projectname=projectname | |
### | |
# Get all Service EndPoints | |
GET {{collectionurl}}/{{projectname}}/_apis/serviceendpoint/endpoints?api-version=4.1-preview.1 | |
Authorization: {{token}} | |
### | |
# Get Service EndPoints by type | |
GET {{collectionurl}}/{{projectname}}/_apis/serviceendpoint/endpoints?type=azurerm&api-version=4.1-preview.1 | |
Authorization: {{token}} | |
### | |
# Get Service Endpoint details by Id | |
@serviceendPointId=guid | |
GET {{collectionurl}}/{{projectname}}/_apis/serviceendpoint/endpoints/{{serviceendPointId}}?api-version=4.1-preview.1 | |
Authorization: {{token}} | |
# get value.id | |
### Get Service EndPoint Security Roles | |
@serviceendPointId=guid of serviceendpoint | |
@projectid=guid of vsts project –> See VSTSProjects.http | |
GET {{collectionurl}}/_apis/securityroles/scopes/distributedtask.serviceendpointrole/roleassignments/resources/{{projectid}}_{{serviceendPointId}} | |
Authorization: {{token}} | |
### Set Service EndPoint Security. Add user/Group in Role | |
@serviceendPointId=guid of serviceendpoint | |
@projectid=guid of vsts project –> See VSTSProjects.http | |
@userorgroupid=guid of VSTS user or group | |
PUT {{collectionurl}}/_apis/securityroles/scopes/distributedtask.serviceendpointrole/roleassignments/resources/{{projectid}}_{{servep}}?api-version=5.0-preview.1 | |
Authorization: {{token}} | |
Content-Type: application/json | |
[ | |
{ | |
"roleName":"User", | |
"userId":"{{userorgroupid}}" | |
} | |
] | |
### | |
# Create Service EndPoint | |
POST {{collectionurl}}/{{projectname}}/_apis/distributedtask/serviceendpoints?api-version=3.0-preview.1 | |
Authorization: {{token}} | |
Content-Type: application/json | |
{ | |
"name": "TestUpdateEP", | |
"type": "azurerm", | |
"url": "https://management.azure.com/", | |
"authorization": { | |
"parameters": { | |
"serviceprincipalid": "XXX", | |
"serviceprincipalkey": "XXX", | |
"tenantid": "XXX" | |
}, | |
"scheme": "ServicePrincipal" | |
}, | |
"data": { | |
"subscriptionId": "XXX", | |
"subscriptionName": "Sub Name" | |
} | |
} | |
### | |
# Update Service EndPoint | |
PUT {{collectionurl}}/{{projectname}}/_apis/distributedtask/serviceendpoints/{{serviceendpointid}}?api-version=3.0-preview.1 | |
Authorization: {{token}} | |
Content-Type: application/json | |
{ | |
"name": "new name", | |
"type": "azurerm", | |
"url": "https://management.azure.com/", | |
"authorization": { | |
"parameters": { | |
"serviceprincipalid": "XXX", | |
"serviceprincipalkey": "XXX", | |
"tenantid": "XXX" | |
}, | |
"scheme": "ServicePrincipal" | |
}, | |
"data": { | |
"subscriptionId": "XXX", | |
"subscriptionName": "Sub Name" | |
} | |
} |
#Using the great extension in VS Code https://marketplace.visualstudio.com/items?itemName=humao.rest-client | |
#use the gist GetBasicAuthToken.ps1 to get a valid token from a PAT | |
@collectionvsspsurl=https://account.vssps.visualstudio.com | |
@token=Base64EncryptedPAT e.g. Basic ONOTAVALIDTOKEN=" | |
@projectid=guid | |
### Get users / groups in project | |
#–> get value.id | |
GET {{collectionvsspsurl}}/_apis/identities?scopeId={{projectid}}&api-version=1.0 | |
Authorization: {{token}} | |
### Get Groups | |
GET {{collectionvsspsurl}}/_apis/graph/groups?api-version=4.1-preview.1 | |
Authorization: {{token}} | |
### Get members in group, based on descriptor | |
GET {{collectionvsspsurl}}/_apis/graph/memberships/vssgp.Uy0xLTktMTU1MTM3NDI0NS0yMjk0OTI4MDIzLTI5MjA5OTMwODktMjg2MzE4MzU3MS0xNTk1Nzg1MzI1LTEtMjYzMjMwMjUwNy0zMTQxNzc2MjAyLTIyNzcwNjk4MTUtMzQ5MzY4NDg2MA?api-version=4.1-preview.1&direction=down | |
Authorization: {{token}} |
#Using the great extension in VS Code https://marketplace.visualstudio.com/items?itemName=humao.rest-client | |
#use the gist GetBasicAuthToken.ps1 to get a valid token from a PAT | |
@collectionurl=https://account.visualstudio.com | |
@token=Base64EncryptedPAT e.g. Basic ONOTAVALIDTOKEN=" | |
### | |
# Get all VSTS Projects in account | |
# –> value.id contains projectid | |
# –> value.name contains projectname | |
GET {{collectionurl}}/_apis/projects/?api-version=1.0 | |
Authorization: {{token}} | |
### | |
# Get single VSTS Project by id | |
@projectid=guid | |
GET {{collectionurl}}/_apis/projects/{{projectid}}?api-version=1.0 | |
Authorization: {{token}} | |
### | |
# Get single VSTS Project by name | |
@projectname=projectname | |
GET {{collectionurl}}/_apis/projects/{{projectname}}?api-version=1.0 | |
Authorization: {{token}} |
#Using the great extension in VS Code https://marketplace.visualstudio.com/items?itemName=humao.rest-client | |
#use the gist GetBasicAuthToken.ps1 to get a valid token from a PAT | |
@collectionurl=https://account.visualstudio.com | |
@token=Base64EncryptedPAT e.g. Basic ONOTAVALIDTOKEN=" | |
@projectid=<guid of VSTS project> | |
@repositoryid=<guid of Git Repo> | |
POST {{collectionurl}}/_apis/hooks/subscriptions?api-version=4.1" | |
Authorization: {{token}} | |
Content-Type: application/json | |
{ | |
"publisherId":"tfs", | |
"eventType":"git.pullrequest.updated", | |
"resourceVersion":"1.0-preview.1", | |
"consumerId":"webHooks", | |
"consumerActionId":"httpRequest", | |
"publisherInputs":{ | |
"branch":"master", | |
"projectId":"{{projectid}}", | |
"notificationType":"StatusUpdateNotification", | |
"repository":"{{repositoryid}}", | |
}, | |
"consumerInputs":{ | |
"url":"http://xyz.requestcatcher.com/" | |
} | |
} |
Comments are closed.