You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Python获取Azure DevOps工作项的父/子关联链接?

获取Azure DevOps工作项的父/子关联链接

问题出在调用get_work_item时未指定扩展关系字段。默认情况下该接口不会返回工作项的关联关系,需要通过expand参数显式指定获取关联数据。

修改代码获取关联关系

修改get_work_item调用,添加expand='Relations'参数:

from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
import pprint

# 填入你的PAT和组织URL
personal_access_token = '<pat>'
organization_url = 'https://dev.azure.com/<org>'

# 创建连接
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)

# 获取工作项跟踪客户端
wi_client = connection.clients.get_work_item_tracking_client()

ads_id = 2129

# 添加expand参数获取关联关系
parent_work_item = wi_client.get_work_item(ads_id, expand='Relations')

# 查看relations字段
pprint.pprint(parent_work_item.relations)

解析父/子关联链接

关联关系中的rel字段标识链接类型:

  • System.LinkTypes.Hierarchy-Reverse:当前工作项的父项
  • System.LinkTypes.Hierarchy-Forward:当前工作项的子项

遍历relations列表提取父/子工作项ID:

if parent_work_item.relations:
    for relation in parent_work_item.relations:
        if relation.rel == 'System.LinkTypes.Hierarchy-Reverse':
            # 提取父工作项ID
            parent_id = int(relation.url.split('/')[-1])
            print(f"父工作项ID: {parent_id}")
        elif relation.rel == 'System.LinkTypes.Hierarchy-Forward':
            # 提取子工作项ID
            child_id = int(relation.url.split('/')[-1])
            print(f"子工作项ID: {child_id}")

若需获取父/子工作项的详细信息,可再次调用get_work_item传入对应ID即可。

内容的提问来源于stack exchange,提问作者POVR2

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 03:55:30