如何获取 DevOps 工作项的 Children?

How do I Get the Children of a DevOps Work Item?

我正在尝试拼凑一个 C# 控制台应用程序,该应用程序通过 API 访问 TFS/DevOps 中的工作项,并将原始估计字段 parent 工作项与它的所有 children,然后吐出任何不相加的工作项的名称和 ID。

到目前为止,我已经能够取回包含原始估计在内的所有工作项的列表,但我仍然需要获取每个工作项的 children 以便循环遍历他们并将他们的原始估计总和与 parent 的总和进行比较。考虑到我对 C# 和查询知之甚少,我现在很困惑。

由于链接项不是可报告字段,我必须使用 $expand 来执行查询以获取我需要的信息(至少下面链接的文档是这么说的)。这就是我被困的地方。有什么建议吗?

https://docs.microsoft.com/en-us/azure/devops/report/extend-analytics/work-item-links?view=azure-devops

这是我目前的情况。

using System;
using System.Collections.Generic;
using System.Linq;

using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
using Microsoft.VisualStudio.Services.Common;

namespace QueryWorkitems0619
{
    class Program
    {
        static void Main(string[] args)
        {
            string orgName = "{Organization's name}";
            string PAT = "{Personal Access Token}";

            Uri uri = new Uri($"https://dev.azure.com/{orgName}");
            string project = "Wingnit_2";
            VssBasicCredential credentials = new VssBasicCredential("", PAT);
            //create a wiql object and build our query
            Wiql wiql = new Wiql()
            {
                Query = "Select * " +
                "From WorkItems " +
                "Where [System.TeamProject] = '" + project + "' " +
                "And [System.State] <> 'Closed' " +
                "And [System.RelatedLinkCount] > '0'" +
                "Order By [State] Asc, [Changed Date] Desc"
            };
            //create instance of work item tracking http client
            using (WorkItemTrackingHttpClient workItemTrackingHttpClient = new WorkItemTrackingHttpClient(uri, credentials))
            {
                //execute the query to get the list of work items in the results
                WorkItemQueryResult workItemQueryResult = workItemTrackingHttpClient.QueryByWiqlAsync(wiql).Result;
                //some error handling
                if (workItemQueryResult.WorkItems.Count() != 0)
                {
                    //need to get the list of our work item ids and put them into an array
                    List<int> list = new List<int>();
                   
                    foreach (var item in workItemQueryResult.WorkItems)
                    {
                        list.Add(item.Id);
                    }
                    int[] arr = list.ToArray();
                    //build a list of the fields we want to see
                    string[] fields = new string[5];
                    fields[0] = "System.Id";
                    fields[1] = "System.Title";
                    fields[2] = "System.RelatedLinkCount";
                    fields[3] = "System.Description";
                    fields[4] = "Microsoft.VSTS.Scheduling.OriginalEstimate";
                    //get work items for the ids found in query
                    var workItems = workItemTrackingHttpClient.GetWorkItemsAsync(arr, fields, workItemQueryResult.AsOf).Result;
                    //loop though work items and write to console
                    foreach (var workItem in workItems)
                    {
                        foreach (var field in workItem.Fields)
                        {
                            Console.WriteLine("- {0}: {1}", field.Key, field.Value);
                        }
                    }

                    Console.ReadLine();
                }
            }
        }
    }
}

你的方向是正确的,你需要在执行GetWorkItemsAsync方法的时候加上expand

var workItems = workItemTrackingHttpClient.GetWorkItemsAsync(arr, expand: WorkItemExpand.Relations workItemQueryResult.AsOf).Result;

注意:fields不能和expand一起使用,需要去掉(会得到所有字段)

循环结果,在工作项结果中您将看到 Relations,检查 Relations 中是否 RelSystem.LinkTypes.Hierarchy-Forward,如果是 - 这是一个child:

现在,您在 Url 中有了 child id,提取它并制作一个 API 以获取他的详细信息。