首页 > 解决方案 > Azure Dev/Ops - 使用 Python 引入分析视图数据

问题描述

我想从 Azure DevOps 访问分析视图数据以访问已注册的项目活动。

有人有任何示例说明他们将如何使用 azure-devops python 库来做到这一点吗?

我没有发现涉及从分析视图中提取数据的示例。基本上,我需要一个 Python 脚本来显示我的项目的所有分析视图字段。

标签: pythonpython-3.xazureazure-devopsazure-devops-rest-api

解决方案


经过一番研究,我设法部分解决了我的问题,因为这个解决方案并没有从分析视图中带来所有内容,此外查询结果中有 20k 条记录的限制:

from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
from azure.devops.v5_1.work_item_tracking.models import Wiql


token = 'xxx'
team_instance = 'https://dev.azure.com/xxx'


credentials = BasicAuthentication("", token)
connection = Connection(base_url=team_instance, creds=credentials)


def print_work_items(work_items):
    for work_item in work_items:
        print(
            "{0} {1}: {2}".format(
                work_item.fields["System.WorkItemType"],
                work_item.id,
                work_item.fields["System.Title"],
            )
        )


wit_client = connection.clients.get_work_item_tracking_client()


def get_TC_from_query(query):
    query_wiql = Wiql(query=query)
    results = wit_client.query_by_wiql(query_wiql).work_items
    # WIQL query gives a WorkItemReference => we get the corresponding WorkItem from id
    work_items = (wit_client.get_work_item(int(result.id)) for result in results)
    print_work_items(work_items)

get_TC_from_query(
    """\
SELECT
        [System.Id],
        [System.WorkItemType],
        [System.Title],
        [System.State],
        [System.AreaPath],
        [System.IterationPath]
FROM workitems
WHERE
        [System.TeamProject] = 'Project'
        and [System.WorkItemType] = 'Product Backlog Item'
        and [System.State] = 'Done'
ORDER BY [System.ChangedDate] DESC
"""
)    

推荐阅读