Azure DevOps API仅返回当前迭代WorkItems,历史迭代无结果求解答
我使用Azure DevOps REST API获取各迭代关联的工作项,但多数历史迭代返回空结果,仅当前迭代能正常返回。操作步骤如下:
1. 获取迭代列表
调用以下接口获取所有迭代:https://dev.azure.com/{organisation}/{project}/_apis/wit/classificationnodes?$depth=3
该接口成功返回所有迭代,其中最近两个迭代信息如下:
{ "id": 459, "identifier": "b72c09b4-4e9f-4b4b-a3bc-0daff628bf4d", "name": "Release 206", "structureType": "iteration", "hasChildren": false, "attributes": { "startDate": "2024-11-08T00:00:00Z", "finishDate": "2024-12-05T00:00:00Z" }, "path": "\\Digital-Web-and-Apps\\Iteration\\Release 206", "url": "https://dev.azure.com/BG/c6c8a298-a2e5-43de-9bc0-0d0a15f2eadc/_apis/wit/classificationNodes/Iterations/Release%20206" }, { "id": 485, "identifier": "9ecb6083-4d82-4584-93aa-97bf7877bb2f", "name": "Release 208", "structureType": "iteration", "hasChildren": false, "attributes": { "startDate": "2025-01-02T00:00:00Z", "finishDate": "2025-01-23T00:00:00Z" }, "path": "\\Digital-Web-and-Apps\\Iteration\\Release 208", "url": "https://dev.azure.com/BG/c6c8a298-a2e5-43de-9bc0-0d0a15f2eadc/_apis/wit/classificationNodes/Iterations/Release%20208" }
2. 获取迭代关联工作项
使用迭代的identifier调用以下接口:https://dev.azure.com/{organisation}/{project}/_apis/work/teamsettings/iterations/{iterationId}/workitems?api-version=7.1
- 当前迭代(identifier:
9ecb6083-4d82-4584-93aa-97bf7877bb2f)调用成功,返回工作项前两条结果:
{ "workItemRelations": [ { "rel": null, "source": null, "target": { "id": 26963, "url": "https://dev.azure.com/BG/_apis/wit/workItems/26963" } }, { "rel": null, "source": null, "target": { "id": 26944, "url": "https://dev.azure.com/BG/_apis/wit/workItems/26944" } },
- 历史迭代(identifier:
b72c09b4-4e9f-4b4b-a3bc-0daff628bf4d)调用后,workItemRelations返回空数组:
{ "workItemRelations": [], "url": "https://dev.azure.com/BerkeleyGroupDevOps/c6c8a298-a2e5-43de-9bc0-0d0a15f2eadc/9829b5e6-8fd2-43df-b674-1e1a8a500e3e/_apis/work/teamsettings/iterations/b72c09b4-4e9f-4b4b-a3bc-0daff628bf4d/workitems",
我确认该历史迭代下存在工作项,但接口返回空结果,请问为何仅当前迭代能返回工作项?
问题原因
你调用的/_apis/work/teamsettings/iterations/{iterationId}/workitems是团队设置迭代专用接口,它仅返回当前团队的激活/当前/未来迭代中的工作项。历史迭代(已结束的迭代)会被团队设置归档,该接口无法读取归档迭代的工作项关联数据。
解决办法
要获取历史迭代的关联工作项,需改用WiQL查询接口,通过指定迭代路径来查询所有匹配的工作项:
1. 构造WiQL查询语句
SELECT [System.Id], [System.Title], [System.IterationPath] FROM WorkItems WHERE [System.IterationPath] = '\Digital-Web-and-Apps\Iteration\Release 206'
2. 调用WiQL接口执行查询
请求地址:POST https://dev.azure.com/{organisation}/{project}/_apis/wit/wiql?api-version=7.1
请求体(JSON格式):
{ "query": "SELECT [System.Id], [System.Title], [System.IterationPath] FROM WorkItems WHERE [System.IterationPath] = '\\Digital-Web-and-Apps\\Iteration\\Release 206'" }
该接口会返回所有指定迭代路径下的工作项,无论迭代是否已结束。如果需要通过迭代identifier查询,可先通过分类节点接口获取对应迭代的path,再带入WiQL语句即可。
内容的提问来源于stack exchange,提问作者Warren Levy

