Instantly share code, notes, and snippets.

Embed
What would you like to do?
This collection of Gists contain all kinds of snippets to get information from the VSTS Rest API, that is not easily found in the API
#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/"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment