python - azure-devops-python-api 查询工作项,其中字段 == 字符串

标签 python azure azure-devops

我正在使用 azure python api ( https://github.com/microsoft/azure-devops-python-api ),我需要能够根据自定义字段值查询和查找特定工作项。

我能找到的最接近的是函数create_query,但我希望能够运行诸如

queryRsp = wit_5_1_client.run_query(
    posted_query='',
    project=project.id, 
    query='Custom.RTCID=282739'
)

我只需要找到我的 azure devops 工作项,其中自定义字段 RTCID 具有特定的唯一值。

我是否需要使用 API 创建查询、运行它、获取结果,然后删除查询?或者有什么方法可以运行这个简单的查询并使用 azure devops api 获取结果?

最佳答案

您的要求可以实现。

例如,在我这边,有两个工作项具有自定义字段“RTCID”:

enter image description here

enter image description here

下面是如何使用python来设计这个功能(在我这边,组织名称和项目名称都名为“BowmanCP”):

#query workitems from azure devops

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

# Fill in with your personal access token and org URL
personal_access_token = '<Your Personal Access Token>'
organization_url = 'https://dev.azure.com/BowmanCP'

# Create a connection to the org
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)

# Get a client (the "core" client provides access to projects, teams, etc)
core_client = connection.clients.get_core_client()

#query workitems, custom field 'RTCID' has a certain specific unique value
work_item_tracking_client = connection.clients.get_work_item_tracking_client()
query  = "SELECT [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State], [System.Tags] FROM workitems WHERE [System.TeamProject] = 'BowmanCP' AND [Custom.RTCID] = 'xxx'"
#convert query str to wiql
wiql = Wiql(query=query)
query_results = work_item_tracking_client.query_by_wiql(wiql).work_items
#get the results via title
for item in query_results:
    work_item = work_item_tracking_client.get_work_item(item.id)
    pprint.pprint(work_item.fields['System.Title'])

成功地让他们站在我这边:

enter image description here

SDK源代码在这里:

https://github.com/microsoft/azure-devops-python-api/blob/451cade4c475482792cbe9e522c1fee32393139e/azure-devops/azure/devops/released/work_item_tracking/work_item_tracking_client.py#L704

您可以引用上面的源代码。

关于python - azure-devops-python-api 查询工作项,其中字段 == 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73819267/

相关文章:

linux - 使用 xRDP 访问 Azure 虚拟机 (Ubuntu 14.04LTS)

azure - 无法对嵌套资源执行请求的操作。找不到父资源 '****.io'

azure-devops - ADF Pipeline 触发 DevOps 上的部署

python - Pandas 数据帧 col 转换为 timedelta 以使用重新采样

python - MySQL:使用 "SELECT INTO OUTFILE"时对字符串进行 Wiered 切割

python - 如何以及何时在 Python 中适本地使用弱引用

c# - 如何使用 C# 连接 storageuri 和 SAS token 以及如何使用连接的 storageuri 和 SAS token 上传 blob?

python - 获取父shell的路径

java - TrustManagerFactory - 忽略证书检查

azure - 通过 Azure DevOps 迁移工具运行迁移的管道时出现 "No spool was specified"错误