如何通过Azure DevOps REST API获取特定版本包含的所有用户故事?
通过ADO REST API自动化获取特定版本的用户故事并导出CSV
我想把手动从Shared Query导出.csv的流程自动化,请问怎么用ADO REST Services获取特定版本包含的所有用户故事?我已经能用以下GET请求获取查询信息:
GET https://dev.azure.com/{organization}/{project}/_apis/wit/queries/{query}?api-version=7.1-preview.2但返回的是查询的元数据(如下),我需要的是该版本对应的所有用户故事数据:
{ "id": "fdb90321-9f60-45a4-9097-3dad03e8f451", "name": "Release 1.83", "path": "Shared Queries/AT_Eclipse/Releases/Release 1.83", "createdDate": "2023-02-21T07:56:08.34Z", "lastModifiedBy": { "id": "647073f7-a82a-63ec-90f8-2594fbec5795", "name": "Removed", "displayName": "Removed", "url": "Removed", "avatar": { "href": "https://dev.azure.com/{Organization}/_apis/GraphProfile/MemberAvatars/aad.NjQ3MDczZjctYTgyYS03M2VjLTkwZjgtMjU5NGZiZWM1Nzk1" }, "uniqueName": "Removed", "imageUrl": "https://dev.azure.com/{Organization}/_apis/GraphProfile/MemberAvatars/aad.NjQ3MDczZjctYTgyYS03M2VjLTkwZjgtMjU5NGZiZWM1Nzk1", "descriptor": "aad.NjQ3MDczZjctYTgyYS03M2VjLTkwZjgtMjU5NGZiZWM1Nzk1" }, "lastModifiedDate": "2023-03-02T06:12:13.687Z", "queryType": "flat", "isPublic": true, "lastExecutedBy": { "id": "Removed", "displayName": "Removed", "url": "Removed", "avatar": { "href": "https://dev.azure.com/{Organization}/_apis/GraphProfile/MemberAvatars/aad.OGM3MTdlMWYtOGM2OS03ZjY4LWEwNzgtYjE0YzZkYWM0YmI1" }, "uniqueName": "Removed", "imageUrl": "https://dev.azure.com/{Organization}/_apis/GraphProfile/MemberAvatars/aad.OGM3MTdlMWYtOGM2OS03ZjY4LWEwNzgtYjE0YzZkYWM0YmI1", "descriptor": "aad.OGM3MTdlMWYtOGM2OS03ZjY4LWEwNzgtYjE0YzZkYWM0YmI1" }, "lastExecutedDate": "2023-03-08T16:36:31.677Z", "_links": { "self": { "href": "https://dev.azure.com/{Organization}/7729aa8c-f280-4e74-8eec-03f3a94e0b31/_apis/wit/queries/fdb90321-9f60-45a4-9097-3dad03e8f451" }, "html": { "href": "https://dev.azure.com/{Organization}/web/qr.aspx?pguid=7729aa8c-f280-4e74-8eec-03f3a94e0b31&qid=fdb90321-9f60-45a4-9097-3dad03e8f451" }, "parent": { "href": "https://dev.azure.com/{Organization}/7729aa8c-f280-4e74-8eec-03f3a94e0b31/_apis/wit/queries/989c11d5-bff0-40dd-bff3-8e9a6a99e25b" }, "wiql": { "href": "https://dev.azure.com/{Organization}/7729aa8c-f280-4e74-8eec-03f3a94e0b31/_apis/wit/wiql/fdb90321-9f60-45a4-9097-3dad03e8f451" } }, "url": "https://dev.azure.com/{Organization}/7729aa8c-f280-4e74-8eec-03f3a94e0b31/_apis/wit/queries/fdb90321-9f60-45a4-9097-3dad03e8f451" }
步骤1:获取查询的WIQL语句
你现在调用的API仅返回查询的元数据,要拿到实际查询逻辑,需调用返回结果中_links.wiql对应的URL,或直接构造请求:
GET https://dev.azure.com/{organization}/{project}/_apis/wit/wiql/{queryId}?api-version=7.1-preview.2
替换{queryId}为你获取到的查询ID(示例中为fdb90321-9f60-45a4-9097-3dad03e8f451),该请求会返回查询对应的WIQL(工作项查询语言)语句。
步骤2:执行WIQL获取工作项ID列表
拿到WIQL后,发起POST请求执行查询,获取匹配的工作项ID:
POST https://dev.azure.com/{organization}/{project}/_apis/wit/wiql?api-version=7.1-preview.2 Content-Type: application/json
请求体示例(替换为你拿到的WIQL语句):
{ "query": "SELECT [System.Id], [System.Title], [System.State] FROM WorkItems WHERE [System.TeamProject] = @project AND [System.IterationPath] = 'Release 1.83' AND [System.WorkItemType] = 'User Story'" }
返回结果会包含所有符合条件的工作项ID数组。
步骤3:批量获取工作项详细数据
用步骤2拿到的ID数组,批量请求工作项的详细信息:
POST https://dev.azure.com/{organization}/{project}/_apis/wit/workitemsbatch?api-version=7.1-preview.2 Content-Type: application/json
请求体示例:
{ "ids": [123, 456, 789], // 替换为实际ID列表 "fields": ["System.Id", "System.Title", "System.State", "System.Description", "System.AssignedTo"] // 指定需要导出的字段 }
若ID数量较少,也可使用GET请求:
GET https://dev.azure.com/{organization}/{project}/_apis/wit/workitems?ids={ids}&fields={fields}&api-version=7.1-preview.2
替换{ids}为逗号分隔的ID,{fields}为逗号分隔的字段名。
步骤4:转换为CSV格式
拿到工作项数据后,将其转换为CSV格式。可通过编程语言(如Python、PowerShell)的工具库实现,例如Python中使用csv模块,遍历工作项列表,将每个工作项的字段值写入CSV文件。
内容的提问来源于stack exchange,提问作者Mike Ubezzi
相关产品推荐
相关产品推荐

