【问题标题】:Interacting with AzureDevops using python使用 python 与 AzureDevops 交互
【发布时间】:2021-10-13 05:03:12
【问题描述】:

我正在尝试在使用 python 与 AzureDevops 交互时实现以下目标:

  1. Boards部分获取故事
  2. 获取Repos部分下的分支

我能够使用 requests API 建立与 AzureDevops URL 的连接,但不确定如何使用它读取数据。 有什么方法可以做到这一点,或者是否有其他 API 可以做到这一点?

提前致谢!!

【问题讨论】:

  • 嗨@Yogesh Sharma,这个问题怎么样?下面的答案是否解决了您的问题?如果没有,请告诉我有关此问题的最新信息吗?

标签: python-3.x azure-devops azure-devops-rest-api


【解决方案1】:

要获取故事,请查看此answer,但请记住设置适当的过滤器 [System.WorkItemType] = 'Task'"

要获取分支,您需要使用Refs - List 端点

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

示例响应

{
  "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": "dev@mailserver.com",
        "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": "dev@mailserver.com",
        "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"
    },

【讨论】:

  • 感谢您的回复!!我对此很陌生,但不知何故,我设法建立了与 ADO 的连接,如下所示: response = requests.get("dev.azure.com{company_name}/_git/{project_name}/", auth=HTTPBasicAuth(username, p) ) 我可以以某种方式修改它以使这段代码与 ADO 交互吗?
  • 嗨@YogeshSharma,你介意通过power shell获得结果吗?如果是的话,我可以在这里分享脚本,你可以试试,然后在这里分享结果。
  • 谢谢@Krzysztof Madej,请分享;也将尝试使用 Powershell。
【解决方案2】:

同意 Krzysztof Madej,

我们可以使用编写 WIQL 查询来列出所有用户故事,您可以参考此link 了解更多详细信息。

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. 列出所有分支,我们可以使用Refs - List并添加变量filter来列出所有分支,如果我们不添加它,它也会列出拉取请求。

示例: 列出所有 repo 并获取 repo ID。

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

通过 repo ID 列出 repo 分支

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

结果:

更新1

Power shell 脚本

#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

结果:

【讨论】:

  • 嗨@Yogesh Sharma,我已经更新了答案并在这里分享了power shell脚本,你可以检查一下,然后在这里分享结果。谢谢。
猜你喜欢
  • 1970-01-01
  • 2016-07-14
  • 1970-01-01
  • 2016-10-11
  • 2020-10-08
  • 2012-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多