[英]C# - How do I deserialize json from Azure DevOps Work Item List?
[英]How do I Get the Children of a DevOps Work Item?
我正在嘗試拼湊一個 C# 控制台應用程序,它通過它的 API 訪問 TFS/DevOps 中的工作項,並將原始估計字段父工作項與其所有子項的工作項進行比較,然后吐出任何工作項的名稱和 ID這不加起來。
到目前為止,我已經能夠獲取包含原始估計的所有工作項的列表,但我仍然需要獲取每個工作項的子項,以便我可以遍歷它們並比較它們原始估計的總和與他們的父母。 鑒於我對 C# 和查詢知之甚少,我現在很困惑。
由於鏈接的項目不是可報告的字段,我必須使用 $expand 來執行查詢以獲取我需要的信息(至少這是下面鏈接的文檔所說的)。 這就是我被困的地方。 有小費嗎?
這是我到目前為止所擁有的。
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
,如果Rel
是System.LinkTypes.Hierarchy-Forward
,如果是-這是一個孩子:
現在,您在Url
有了 child id,提取它並制作一個 API 來獲取他的詳細信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.