Azure DevOps Wiql API 将字段添加到响应工作项

标签 azure azure-devops azure-devops-rest-api workitem wiql

我正在寻找一种方法(或查询)来在此查询的响应上添加 workItem 字段:

{
  "query": "Select [System.Id], [System.Title] From WorkItems Where [System.WorkItemType] = 'Task' OR [System.WorkItemType] = 'Bug'"
}

当前响应:

{
    "queryType": "flat",
    "queryResultType": "workItem",
    "asOf": "2020-08-17T15:13:32.75Z",
    "columns": [
        {
            "referenceName": "System.Id",
            "name": "ID",
            "url": "https://dev.azure.com/.../_apis/wit/fields/System.Id"
        },
        {
            "referenceName": "System.Title",
            "name": "Title",
            "url": "https://dev.azure.com/..._apis/wit/fields/System.Title"
        }
    ],
    "workItems": [
        {
            "id": 27,
            "url": "https://dev.azure.com/.../_apis/wit/workItems/27"
        },
        {
            "id": 44,
            "url": "https://dev.azure.com/.../_apis/wit/workItems/44"
        }
]
}

我需要扩展找到的每个工作项上的字段,就像我要做 GET workitems API request 一样。 ,即使有可能 ?$expand=relations

这将是我对 WorkItems 的预期输出 预期响应:

{
    "queryType": "flat",
    "queryResultType": "workItem",
    "asOf": "2020-08-17T15:13:32.75Z",
    "columns": [
        {
            "referenceName": "System.Id",
            "name": "ID",
            "url": "https://dev.azure.com/.../_apis/wit/fields/System.Id"
        },
        {
            "referenceName": "System.Title",
            "name": "Title",
            "url": "https://dev.azure.com/.../_apis/wit/fields/System.Title"
        }
    ],
    "workItems": [
{
    "id": 27,
    "rev": 22,
    "fields": {
        "System.AreaPath": "Cloud",
        "System.TeamProject": "Cloud",
        "System.IterationPath": "Cloud\\Sprint 6",
        "System.WorkItemType": "Task",
        "System.State": "Closed",
        "System.Reason": "Completed",
        "System.AssignedTo": {...},
        "System.CreatedDate": "2020-02-24T15:52:08.867Z",
        "System.CreatedBy": {...},
        "System.ChangedDate": "2020-06-24T14:48:26.593Z",
        "System.ChangedBy": {...},
        "System.CommentCount": 6,
        "System.Title": "Add XCAL import support to AAT3 framework and GUI",
        "Microsoft.VSTS.Common.StateChangeDate": "2020-06-24T14:48:26.593Z",
        "Microsoft.VSTS.Common.ActivatedDate": "2020-06-03T00:47:20.397Z",
        "Microsoft.VSTS.Common.ActivatedBy": {...},
        "Microsoft.VSTS.Common.ClosedDate": "2020-06-24T14:48:26.593Z",
        "Microsoft.VSTS.Common.ClosedBy": {...},
        "Microsoft.VSTS.Common.Priority": 2,
        "Microsoft.VSTS.Common.ValueArea": "Business",
        "WEF_DA224280DD46429CA75C96DC1082D816_Kanban.Column": "New",
        "WEF_DA224280DD46429CA75C96DC1082D816_Kanban.Column.Done": false,
        "System.Description": "...",
        "System.Parent": 45
    },
    "relations": [...],
    "_links": {...},
    "url": "...workItems/27"
},
        {
            "id": 44,
              ...
        }
]
}

最佳答案

恐怕 WIQL Rest API 无法返回详细的工作项信息。

但是您可以使用 WIQL 获取工作项 ID 并将 ID 发送到 Work item -List 。在这种情况下,您可以获得工作项信息。

这是一个 PowerShell 脚本示例:

$token = "PAT"

$url="https://dev.azure.com/Organizationname/_apis/wit/wiql?api-version=5.1"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$JSON = @'
{
   "query": "SELECT [System.Id], [System.WorkItemType],  [System.State],[System.AreaPath],[System.Tags],[System.CommentCount],[System.ChangedDate] FROM workitems WHERE[System.Id] IN(@follows) AND [System.TeamProject] = 'Azure' AND [System.State] <> '' ORDER BY [System.ChangedDate] DESC"
}
'@

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json

$listOfTasks = New-Object Collections.Generic.List[String]
ForEach( $workitem in $response.workItems ) {
  $listOfTasks.Add($workitem.id)

}
$listOfTasks = $listOfTasks -join ','
$listOfTasks

$url1="https://dev.azure.com/Organizationname/ProjectName/_apis/wit/workitems?ids=$listOfTasks" +"&" + "`$expand" + "=all&api-version=5.1"

$response1 = Invoke-RestMethod -Uri $url1 -Headers @{Authorization = "Basic $token"} -Method Get 

Write-Host "result = $($response1 | ConvertTo-Json -Depth 100)"

说明:

第一部分是获取 wiql 输出的工作项 id。

第二部分是在工作项列表api中添加工作项id列表,以获取所有工作项的具体信息。

结果如下:

enter image description here

关于Azure DevOps Wiql API 将字段添加到响应工作项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63453982/

相关文章:

docker - 尝试使用 .Net Core 3.0 SDK (preview5) 在 DevOps 管道中构建时抛出异常 "error MSB3024: Could not copy the file..."

azure - Azure DevOps 管道可以按特定顺序运行吗?

rest - 通过调用 rest-api 更新 tfs 的 wiki 页面

azure-devops - Azure DevOps YAML 管道参数不适用于 REST API 触发器

sql-server - 是否可以通过 Azure 云托管上的 SQL Server Management Studio 创建 .bak 文件?

azure - 将 Azure Active Directory 用户添加到 Azure SQL 数据库

azure - 按虚拟机名称查找 Azure 资源组

azure - 自定义域到 Azure CDN 端点(带或不带 www)

azure-devops - VSTS - 设置 $(Rev :. r ) 起始值

powershell - 在 VSTS 中发布失败时向所有开发人员发送通知(上一个 VSO)