通过Visual Studio Team Service REST API查询工作项更新的有效方法

时间:2017-11-28 10:30:02

标签: azure-devops azure-devops-rest-api

我想查询错误的状态是否已更改为“活动”以进行报告,并通过阅读visual studio文档成功检索到所需的结果。

  1. 通过Work item query language获取我感兴趣的所有工作项ID。我查询一年的数据有很多。
  2. 使用for循环为每个ID调用updates API
  3. 解析更新API结果以查找 System.State 字段。
  4. 问题是第2步太,因为更新API每个请求只接受一个I​​D。 有没有办法批量获取工作项的更新?如果我们可以限制更新API中的字段以简化步骤3,那也更好。

1 个答案:

答案 0 :(得分:1)

主要由step1引起不仅在当前项目中返回工作项,还在其他项目中返回

您可以在REST API下面使用第1步:

{
  "query": 
  "Select [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State] 
  From WorkItems 
  Where [System.WorkItemType] = 'Bug' AND [State] = 'Active'"
}

Content-Type:application / json

POST https://account.visualstudio.com/DefaultCollection/_apis/wit/wiql?api-version=1.0

然后它会从所有团队项目中返回状态为Active的Bugs。

您只需要为在where子句中指定当前团队项目添加条件:

  • 如果团队项目WIT只有一个Area (没有子区域)且名称与团队项目名称相同,则可以将REST API用作:

    {
      "query": 
      "Select [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State] 
      From WorkItems 
      Where [System.WorkItemType] = 'Bug' AND [State] = 'Active' AND [System.AreaPath] = 'projectname'"
    

    Content-Type:application / json

    projectname

    }

  • 如果团队项目WIT有多个区域(例如区域为:projectname\child1projectname\child2POST https://account.visualstudio.com/DefaultCollection/_apis/wit/wiql?api-version=1.0 ),可以在REST API下面使用:

    {
      "query": 
      "Select [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State] 
      From WorkItems 
      Where [System.WorkItemType] = 'Bug' AND [State] = 'Active' AND [System.AreaPath] IN ('projectname', 'projectname\\child1', 'projectname\\child2)"
    }
    

    Content-Type:application / json

    extension Array {
        func anyFlatten() -> [Any] {
            var res = [Any]()
            for val in self {
                if let arr = val as? [Any] {
                    res.append(contentsOf: arr.anyFlatten())
                } else {
                    res.append(val)
                }
            }
    
            return res
        }
    }
    
    let numbers = [1,[2, [4, 5] ,3], "Hi"] as [Any]
    print(numbers.anyFlatten())
    

然后step2将更快,更有效。