Yogesh Sharma
Yogesh Sharma

Reputation: 135

Interacting with AzureDevops using python

I am trying to achieve following things while interacting with AzureDevops using python:

  1. To get the stories under Boards section
  2. To get the branches under Repos section

I am able to establish connection to AzureDevops URL using requests API, but not sure how to read the data using it. Is there any way by which I can do the same or if there is some other API using which I can do this?

Thanks in advance!!

Upvotes: 0

Views: 1491

Answers (2)

Vito Liu
Vito Liu

Reputation: 8278

Agree with Krzysztof Madej,

We could use write a WIQL query to list all User Story, you could refer this link for more details.

from vsts.vss_connection import VssConnection
from msrest.authentication import BasicAuthentication
import json
from vsts.work_item_tracking.v4_1.models.wiql import Wiql

def emit(msg, *args):
print(msg % args)

def print_work_item(work_item):
    emit(
        "{0} {1}: {2}".format(
            work_item.fields["System.WorkItemType"],
            work_item.id,
            work_item.fields["System.Title"],
        )
    )

personal_access_token = 'YourPATToken'
organization_url = 'https://dev.azure.com/YourorgName'
# Create a connection to the org
credentials = BasicAuthentication('', personal_access_token)
connection = VssConnection(base_url=organization_url, creds=credentials)
wiql = Wiql(
        query="""select [System.Id] From WorkItems """
    )

wit_client = connection.get_client('vsts.work_item_tracking.v4_1.work_item_tracking_client.WorkItemTrackingClient')
wiql_results = wit_client.query_by_wiql(wiql).work_items
if wiql_results:
        # WIQL query gives a WorkItemReference with ID only
        # => we get the corresponding WorkItem from id
        work_items = (
            wit_client.get_work_item(int(res.id)) for res in wiql_results
        )
        for work_item in work_items:
            print_work_item(work_item)
  1. List all branches, we could use Refs - List and add variable filter to list all branches, If we do not add it, it will also list pull request.

Sample: List all repo and get repo ID.

GET https://dev.azure.com/{Org name}/{Project name}/_apis/git/repositories?api-version=4.1

List repo branches via repo ID

GET https://dev.azure.com/{Org name}/{Project name}/_apis/git/repositories/{Repo ID}/refs?filter=heads&api-version=4.1

Result:

enter image description here

Update1

Power shell script

#List all branches name    
$connectionToken="PAT"
    $base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
    $BranchDetailURL = "https://dev.azure.com/{Org name}/{Project name}/_apis/git/repositories/{Repo ID}/refs?filter=heads&?api-version=6.0" 
    $BranchInfo = Invoke-RestMethod -Uri $BranchDetailURL -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get 
    Write-Host $BranchInfo.value.name
    
#List all User Story ID

    $WorkItemWiqlQuery = "https://dev.azure.com/v-viliu/test_Agile/test/_apis/wit/wiql?api-version=5.1"
    $query = "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = 'User Story'"
    $body = @{query=$query} | ConvertTo-Json
    $WorkItemDetailInfo = Invoke-RestMethod -Uri $WorkItemWiqlQuery -Method Post -ContentType "application/json" -Headers @{Authorization=("Basic $base64AuthInfo")} -Body $body
    
    Write-Host $WorkItemDetailInfo.workItems.id

Result:

enter image description here

Upvotes: 1

Krzysztof Madej
Krzysztof Madej

Reputation: 40553

To get stories please check this answer but keep in mind to set proper filter [System.WorkItemType] = 'Task'".

To get branches you need to use Refs - List endpoint

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/refs?api-version=6.0

Sample response

{
  "value": [
    {
      "name": "refs/heads/feature/calcApp",
      "objectId": "ffe9cba521f00d7f60e322845072238635edb451",
      "creator": {
        "displayName": "Normal Paulk",
        "url": "https://vssps.dev.azure.com/fabrikam/_apis/Identities/ac5aaba6-a66a-4e1d-b508-b060ec624fa9",
        "_links": {
          "avatar": {
            "href": "https://dev.azure.com/fabrikam/_apis/GraphProfile/MemberAvatars/aad.YmFjMGYyZDctNDA3ZC03OGRhLTlhMjUtNmJhZjUwMWFjY2U5"
          }
        },
        "id": "ac5aaba6-a66a-4e1d-b508-b060ec624fa9",
        "uniqueName": "[email protected]",
        "imageUrl": "https://dev.azure.com/fabrikam/_api/_common/identityImage?id=ac5aaba6-a66a-4e1d-b508-b060ec624fa9",
        "descriptor": "aad.YmFjMGYyZDctNDA3ZC03OGRhLTlhMjUtNmJhZjUwMWFjY2U5"
      },
      "url": "https://dev.azure.com/fabrikam/7484f783-66a3-4f27-b7cd-6b08b0b077ed/_apis/git/repositories/d3d1760b-311c-4175-a726-20dfc6a7f885/refs?filter=heads%2Ffeature%2FcalcApp"
    },
    {
      "name": "refs/heads/feature/replacer",
      "objectId": "917131a709996c5cfe188c3b57e9a6ad90e8b85c",
      "creator": {
        "displayName": "Normal Paulk",
        "url": "https://vssps.dev.azure.com/fabrikam/_apis/Identities/ac5aaba6-a66a-4e1d-b508-b060ec624fa9",
        "_links": {
          "avatar": {
            "href": "https://dev.azure.com/fabrikam/_apis/GraphProfile/MemberAvatars/aad.YmFjMGYyZDctNDA3ZC03OGRhLTlhMjUtNmJhZjUwMWFjY2U5"
          }
        },
        "id": "ac5aaba6-a66a-4e1d-b508-b060ec624fa9",
        "uniqueName": "[email protected]",
        "imageUrl": "https://dev.azure.com/fabrikam/_api/_common/identityImage?id=ac5aaba6-a66a-4e1d-b508-b060ec624fa9",
        "descriptor": "aad.YmFjMGYyZDctNDA3ZC03OGRhLTlhMjUtNmJhZjUwMWFjY2U5"
      },
      "url": "https://dev.azure.com/fabrikam/7484f783-66a3-4f27-b7cd-6b08b0b077ed/_apis/git/repositories/d3d1760b-311c-4175-a726-20dfc6a7f885/refs?filter=heads%2Ffeature%2Freplacer"
    },

Upvotes: 0

Related Questions