我正在使用Python的Azure Devops模块将链接的工作项获取到树结构中的用户故事(父链接,子链接)
我试过这个代码
from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
from azure.devops.v7_1.work_item_tracking.models import Wiql
from variable import personal_access_token,org,project
# Replace with your personal access token and organization URL
personal_access_token = personal_access_token
organization_url = org
project_name = project
# Create a connection to the Azure DevOps organization
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)
# Get the work item tracking client
wit_client = connection.clients.get_work_item_tracking_client()
# Define a WIQL (Work Item Query Language) query to get work items
wiql_query = Wiql(
query=f"""
SELECT
[System.Id],
[System.WorkItemType],
[System.Title],
[System.AssignedTo],
[System.State],
[System.Tags]
FROM workitems
WHERE
(
[Source].[System.TeamProject] = 'DevOps RnD'
AND [Source].[System.Id] = 326797
)
AND (
[System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward'
)
ORDER BY [System.Id]
MODE (Recursive)
"""
)
wiql_result = wit_client.query_by_wiql(wiql=wiql_query)
但这在这一行给出了错误
wiql_result = wit_client.query_by_wiql(wiql=wiql_query)
说
发生异常:AzureDevOpsServiceError MODE 关键字不能在工作项查询中使用。该错误是由“递归”引起的。
问题:如何解决此错误?
任何帮助将不胜感激!
测试相同的 Python 示例,我可以重现相同的问题。
问题的原因可能是
wit_client.query_by_wiql()
不支持 wiql 定义中的 Mode 关键字。
要在 Python 示例中使用 wiql 格式,您可以使用 Rest API:Wiql - 在 Python 脚本中通过 Wiql 查询 来获取工作项。
这是一个例子:
import requests
from requests.auth import HTTPBasicAuth
personal_access_token = 'PAT'
organization_url = 'https://dev.azure.com/OrganizationName'
project = 'ProjectName'
parent_work_item_id = 326797 # Replace with your parent work item ID
wiql_url = f'{organization_url}/{project}/_apis/wit/wiql?api-version=6.0'
wiql_query = {
"query": f"""
SELECT
[System.Id],
[System.WorkItemType],
[System.Title]
FROM workitemLinks
WHERE
(
[Source].[System.TeamProject] = 'DevOps RnD'
And [Source].[System.Id] = {parent_work_item_id}
)
AND (
[System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward'
)
ORDER BY [System.Id]
MODE (Recursive)
"""
}
response = requests.post(wiql_url, json=wiql_query, auth=HTTPBasicAuth('', personal_access_token))
response.raise_for_status()
wiql_results = response.json()
print(f"{wiql_results }")
结果:
它将返回 System.LinkTypes.Hierarchy-Forward 类型链接的工作项。 源 -> ID 是父工作项 ID,目标 -> ID 是子工作项 ID。