如何获取Azure Devops项目的所有工作项(史诗、特性、问题、任务、测试用例、用户故事等)?

2024-04-29 20:26:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图获取所有的工作项(Epics、Features、Issue、Task、testcase、userstory等),然后使用Microsoft的azure devops python api(又名vsts)库为给定的项目对它们进行分类。在

在工作项跟踪中,我找不到任何函数来获取全部工作项或根据工作项的类型获取全部工作项。在

是否已经有一个函数来获取我找不到的所有工作项,或者我应该编写一个WIQL查询来获取所需的数据?在


Tags: 项目函数apitask分类issueazuretestcase
3条回答

首先,我不使用python库,但是我可以告诉您必须使用哪些api。在

有一个API可以检索所有work items。这只是一个具有所有工作项类型和属性的JSON对象。请注意,每个请求仅限于200个工作项。如果你想要更多的工作项,你必须写一个WIQL查询。在

GET https://dev.azure.com/{organization}/{project}/_apis/wit/workitems?ids={ids}&api-version=5.0-preview.3

我个人建议你使用WIQL查询从azuredevops检索数据。它非常灵活,可以在任何情况下使用。在

在这里您可以找到有关WIQL queries的更多信息

在这里您可以找到有关Azure DevOps Rest API for WIQL Queries的详细信息

我用这个documentation来做这个。在

有了Wiql,我们可以查询azuredevops或TFS,我使用Postman来处理它。 第一步是使用以下网址: https://dev.azure.com/{organization}/{projectd}/\u api/wit/wiql?api版本=5.0

好的,下面的步骤是通过wiql创建一个查询,为此我们需要使用json来发送查询:

{
  "query": "Select [System.Id], [System.Title], [System.State], [System.WorkItemType] From WorkItems"
}

如果请求是200行,那么您将获得一个包含所有工作项的json。在

我的结果: Result of my query

Is there already a function for getting all work items that I am unable to find or should I write a WIQL query to fetch the required data?

你说得对。我们可以使用编写一个WIQL查询来获取系统id,然后我们可以根据系统ID查询工作项。下面是用python代码获取所有工作项的演示代码。在

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)

关于更多的演示代码,您可以参考这个link。在

相关问题 更多 >