如何使用 Python REST API 从 VSTS (Azure DevOps) 中的查询中提取工作项? [英] How to extract WorkItems from query in VSTS (Azure DevOps) with Python REST API?
问题描述
我正在使用 Azure DevOps 的官方 Python REST API:https://github.com/Microsoft/azure-devops-python-api
I'm using the official Python REST API of Azure DevOps: https://github.com/Microsoft/azure-devops-python-api
多亏了这些示例,我才能从 id 中检索有关测试用例的信息.
Thanks to the samples I have been able to retrieve information on Test Cases from ids.
如何从查询 (WIQL) 中做到这一点?
How to do it from queries (WIQL)?
到目前为止,这是我的代码(带有修改的令牌和链接):
Here is my code so far (with modified tokens and links):
from vsts.vss_connection import VssConnection
from msrest.authentication import BasicAuthentication
token = "hcykwckuhe6vbnigsjs7r3ai2jefsdlkfjslkfj5mxizbtfu6k53j4ia"
team_instance = "https://tfstest.toto.com:8443/tfs/Development/"
credentials = BasicAuthentication("", token)
connection = VssConnection(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 = (
"vsts.work_item_tracking.v4_1.work_item_tracking_client.WorkItemTrackingClient"
)
wit_client = connection.get_client(WIT_CLIENT)
def get_TC_by_id(desired_ids):
work_items = wit_client.get_work_items(ids=desired_ids, error_policy="omit")
print_work_items(work_items)
def get_TC_from_query(query):
# THIS FUNCTION IS NOT WORKING...
work_items = wit_client.get_work_items(query=query, error_policy="omit")
print_work_items(work_items)
get_TC_by_id([1035375])
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] = 'Test Case'
ORDER BY [System.ChangedDate] DESC
"""
)
这是我得到的错误
File "test_TFS.py", line 35, in get_TC_from_query
work_items = wit_client.get_work_items(query=query, error_policy="omit")
TypeError: get_work_items() got an unexpected keyword argument 'query'
如何从查询中检索测试用例?
How shall I retrieve Test Cases from a query?
特别是,我不理解客户端"的值,例如vsts.work_item_tracking.v4_1.work_item_tracking_client.WorkItemTrackingClient"
In particular, I do not understand the values of the "client" such as "vsts.work_item_tracking.v4_1.work_item_tracking_client.WorkItemTrackingClient"
谢谢大家!
推荐答案
发布消息后(即问题导致 pull request) 在 Github 存储库中的 VSTS 示例,我得到了解决我的问题的提示.
After posting a message (i.e. issue leading to a pull request) on the Github repository for VSTS examples, I got a hint to solve my issue.
解决方法是使用:
- 一个带有
Wiql
类的 wiql 查询对象 query_by_wiql
函数- 使用
get_work_item
函数(或get_work_items
一次处理多个WorkItem)将查询结果(带有WorkItem id 的引用)转换为WorkItem
- a wiql query object with
Wiql
class query_by_wiql
function- transforming the results of the query (references with WorkItem id) to WorkItem with
get_work_item
function (orget_work_items
to process several WorkItems in a single pass)
这是我对这个问题的解决方案:
Here is my solution to this problem:
[更新版本 5.1 和 azure-devops 模块替换 VSTS]
from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
from azure.devops.v5_1.work_item_tracking.models import Wiql
token = "hcykwckuhe6vbnigsjs7r3ai2jefsdlkfjslkfj5mxizbtfu6k53j4ia"
team_instance = "https://tfstest.toto.com:8443/tfs/Development/"
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)
这篇关于如何使用 Python REST API 从 VSTS (Azure DevOps) 中的查询中提取工作项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!