AzureDevOps Build Rest API:如何获取构建任务中的所有警告计数,超过 10 的限制

AzureDevOps Build Rest API: How to get all Warning count in build Task, exceed the limit of 10

我正在尝试在 AzureDevOps 项目上获取构建任务的警告数。但我仍然只收到前 10 个警告。你是怎么做到的?我特别需要警告的数量,不一定是警告的细节

var credential = new VssBasicCredential(string.Empty, myPat);
var connection = new VssConnection(new Uri(myCollection), credential);
var buildClient = connection.GetClient<BuildHttpClient>();
var timeline = buildClient.GetBuildTimelineAsync(myProject, myBuildId).Result;
var vsTask= timeline.Records.FirstOrDefault(p => p?.Task?.Name == "VSBuild");
// always 10 utmost  : warning and issues !!
var warning = vsTask.WarningCount;
var issues =  vsTask.Issues;

这真的很奇怪,但看起来你不能超越这里显示的内容

有解决方法(但有点难看)。您可以加载日志页面并统计分析页面的警告。

static void Main(string[] args)
{
    var credential = new VssBasicCredential(string.Empty, "PAT");
    var connection = new VssConnection(new Uri("https://dev.azure.com/your-organization/"), credential);
    var buildClient = connection.GetClient<BuildHttpClient>();
    var timeline = buildClient.GetBuildTimelineAsync("yoyr project", 377).Result;

    var vsTask = timeline.Records.FirstOrDefault(p => p?.Task?.Name == "VSBuild");
    // always 10 utmost  : warning and issues !!
    var warning = vsTask.WarningCount;
    var issues = vsTask.Issues;
    Console.WriteLine(warning);


    HttpClient client = new HttpClient();
    var response =  client.GetAsync(vsTask.Log.Url);
    var pageContents = response.Result.Content.ReadAsStringAsync().Result;
    var realNumberOfWarnings = AllIndexesOf(pageContents, "##[warning]");
    Console.WriteLine(realNumberOfWarnings.Count);
    Console.ReadLine();
}

public static List<int> AllIndexesOf(string str, string value)
{
    if (String.IsNullOrEmpty(value))
        throw new ArgumentException("the string to find may not be empty", "value");
    List<int> indexes = new List<int>();
    for (int index = 0; ; index += value.Length)
    {
        index = str.IndexOf(value, index);
        if (index == -1)
            return indexes;
        indexes.Add(index);
    }
}

希望对您有所帮助。

请查看黄廷洛 [MSFT] 写的内容 here:

We should only count and store 10 errors/warning (i guess we having some counting bug, so we count to 11), those issues get stored in our backend DB for driving the build summary page render. We don't want to store too many errors/warning since most of the time the first few errors/warnings are key to the problem, and the rest are just noise.