TFS REST API 获取构建失败的项目

TFS REST API Get items that failed to build

有人知道如何获取构建失败的项目, 通过 TFS REST API?

您可以使用 Build - Get Build Log Rest API. You need to know which log id is the build log (if you have few tasks so each task has a log), so you can check all the available logs with Build - Get Build Logs Rest API 下载构建日志,然后迭代它们并检查构建的内容。获得日志后,您可以搜索和过滤错误。

PowerShell 的一个小例子:

$collection = "http://tfs-server:8080/tfs/collection"
$project = "team-project"
$buildId = 10

#"api-version=4.1" is for TFS 2018, for other versions you need to change the number
$logsUrl = "$collection/$project/_apis/build/builds/$buildId/lgos?api-version=4.1"
$logs = Invoke-RestMethod -Uri $logsUrl -Method Get -UseDeafultCredntials -ContentType application/json

$buildLog = ""
ForEach($log in $logs.value)
{
    $urlLog = "$collection/$project/_apis/build/builds/$buildId/logs/$($log.id)?api-version=4.1"
    $logDetails = Invoke-RestMethod -Uri $urlLog -Method Get -UseDefaultCredntials -ContentType application/json
    if($logDetails.Contains("msbuild"))
    {
         $buildLog = $logDetails
         break
    }
}
# Now you have the logs in your hand and you can find in which projects have errors

如果你想用 C# 来做,你可以用 HttpClient:

调用 Rest API
using (HttpClient client = new HttpClient(new HttpClientHandler() { UseDeafultCredntials = true } )
{
     string url = "the-url-from-above-powershell-script";
     var method = new HttpMethod("GET");
     var reqeust = new HttpRequestMessage(mehtod,url);
     var result = await client.SendAsync(rqeuest);
}
// In this way you get the Rest API response, so you need to it once for the logs, iterate them to get each log and then filter the errors.