从 azure devops 获取所有任务

Getting all tasks from azure devops

我正在尝试从 Azure DevOps 获取所有任务。我尝试遵循此文档: https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/?view=azure-devops-rest-5.0#a-flat-query

我正在使用此代码段来执行请求:

using (HttpClient client = new HttpClient())
{
    client.DefaultRequestHeaders.Accept.Add(
        new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
        Convert.ToBase64String(
            System.Text.ASCIIEncoding.ASCII.GetBytes(
                string.Format("{0}:{1}", "", personalaccesstoken))));

    using (HttpResponseMessage response = await client.GetAsync(url))
    {
        response.EnsureSuccessStatusCode();
        var json = await response.Content.ReadAsStringAsync();

        JToken parsedJson = JToken.Parse(json);
        return parsedJson.ToString(Formatting.Indented);
    }
}

据我了解,您不能只完成所有任务。第一步是获取所有任务的 ID,这需要使用 WIQL 查询来完成。如果我错了请纠正我。

所以我尝试调用以下以获取一些数据作为开始: https://dev.azure.com/xxxprod/XXX/XXX Team/_apis/wit/wiql?$top=100&api-version=5.0

但是我收到 405 not allowed 我可以拨打其他电话,例如: https://dev.azure.com/xxxprod/_apis/projects

谁能提供有关如何通过 REST 获取所有任务的示例或说明 api?

As I understand it you cannot just get all tasks. First step is to get the ID's for all tasks and that needs to be done with a WIQL query.

对于这个问题,你是对的。我们可以写一个 WIQL query 来获取系统 ID,然后我们可以根据 system.Ids 来查询工作项。

首先,您可以使用工作项 wiql api 来查询这些项目:

POST https://dev.azure.com/{org}/{pro}/_apis/wit/wiql?api-version=5.1

请求正文:

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

我在邮递员中的结果:Result of my query

然后您可以使用work item list api列出上述步骤查询到的所有任务工作项。

powershell 脚本中的示例:

#查询那些项目

$qurl =  "https://dev.azure.com/{org}/{proj}/_apis/wit/wiql?api-version=5.1"

$WIQL_query = "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = 'Task'"
$body = @{ query = $WIQL_query }
$bodyJson=@($body) | ConvertTo-Json

$pat = {PAT}

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

$result = Invoke-RestMethod -Uri $qurl -Headers @{authorization = "Basic $base64AuthInfo"} -Method post -ContentType "application/json" -Body $bodyJson

# 获取工作项 ID

$ids = $result.workItems | select id | foreach{ $_.id }
$id= '{0}' -f ($ids -join ",")

# 使用工作项列表 api 列出那些工作项

$url = "https://dev.azure.com/{ORG}/{PROJ}/_apis/wit/workitems?ids=$($id)&api-version=5.1"

$result1 = Invoke-RestMethod -Uri $url -Headers @{authorization = "Basic $base64AuthInfo"} -Method get