代码之家  ›  专栏  ›  技术社区  ›  Soham Dasgupta

即使我看到来自vsts web app的相关链接,与vsts工作项相关的计数字段也显示为零

  •  1
  • Soham Dasgupta  · 技术社区  · 6 年前

    我试图从我们的VSTS托管环境中提取一些信息,我需要将其作为报告呈现。我观察到api库报告没有与之相关的项 WorkItem 尽管我从vsts的web应用程序中看到了相关链接。

    这是我的密码-

    void Main()
    {
        string url = "https://[redacted].visualstudio.com";
        string token = "[redacted]";
        string project = "[redacted]";
        string version = "[redacted]";
    
        VssConnection conn = GetConnection(url, token);
        WorkItemTrackingHttpClient witClient = conn.GetClient<WorkItemTrackingHttpClient>();
    
        Wiql q = new Wiql();
        q.Query = $"SELECT * FROM WorkItems WHERE [System.TeamProject] = '{project}' AND [System.Tags] CONTAINS '{version}' AND [System.WorkItemType] IN ('Product Backlog Item', 'Defect') ORDER BY [System.CreatedDate] desc";
    
        var qi = witClient.QueryByWiqlAsync(q).Result;
        var ids = qi.WorkItems.Select(x => x.Id);
        var workitems = witClient.GetWorkItemsAsync(ids).Result.Select(r =>
        {
            return new
            {
                ItemId = r.Id,
                ItemAssignedTo = r.Fields["System.AssignedTo"],
                ItemCreatedBy = r.Fields["System.CreatedBy"],
                ItemTitle = r.Fields["System.Title"],
                ItemType = r.Fields["System.WorkItemType"],
                State = r.Fields["System.State"],
                ItemHasDescription = r.Fields.ContainsKey("System.Description") ? "Yes" : "No",
                ItemHasAcceptanceCriteria = r.Fields.ContainsKey("Microsoft.VSTS.Common.AcceptanceCriteria") ? "Yes" : "No",
                RelatedItems = r.Fields.ContainsKey("System.RelatedLinkCount") ? r.Fields["System.RelatedLinkCount"] : null //This line reports no related links,
                Links = r.Links != null ? r.Links.Links : null //So does this line report null
            };
        });
        workitems.Dump();
        conn.Disconnect();
    }
    
    private static VssConnection GetConnection(string accountUri, string personalAccessToken)
    {
        var cred = new VssBasicCredential(string.Empty, personalAccessToken);
        VssHttpMessageHandler vssHandler = new VssHttpMessageHandler(cred, VssClientHttpRequestSettings.Default.Clone());
        return new VssConnection(
                            new Uri(accountUri),
                            vssHandler,
                            new DelegatingHandler[] { new SuppressHandler() });
    }
    
    public class SuppressHandler : DelegatingHandler
    {
    
    }
    

    同时我也得到了这些控制台日志,我想避免。

    Web method running: [https://[redacted].visualstudio.com/_apis/wit/wiql] (POST)wiql[wit]
    

    有没有办法删除这些控制台日志记录?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Andy Li-MSFT    6 年前

    无法根据代码找出问题。

    但是,您可以使用下面的代码示例从vsts中检索工作项信息,它在我这边起作用:

    为了避免这些控制台日志记录,您可以禁用 程序输出 以下内容:

    右键单击 输出 窗口-->取消选择选项 程序输出 ,然后再试一次。

    using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
    using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
    using Microsoft.VisualStudio.Services.Common;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace QueryWorkitems0619
    {
        class Program
        {
            static void Main(string[] args)
            {
                Uri uri = new Uri("https://{account}.visualstudio.com");
                string PAT = "TokenHere";
                string project = "ProjectName";
    
                VssBasicCredential credentials = new VssBasicCredential("", PAT);
    
                //create a wiql object and build our query
                Wiql wiql = new Wiql()
                {
                    Query = "Select * " +
                            "From WorkItems " +
                            "Where [Work Item Type] IN ('Product Backlog Item', 'Task') " +
                            "And [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[3];
                        fields[0] = "System.Id";
                        fields[1] = "System.Title";
                        fields[2] = "System.RelatedLinkCount";
    
                        //get work items for the ids found in query
                        var workItems = workItemTrackingHttpClient.GetWorkItemsAsync(arr, fields, workItemQueryResult.AsOf).Result;
    
                        Console.WriteLine("Query Results: {0} items found", workItems.Count);
    
                        //loop though work items and write to console
                        foreach (var workItem in workItems)
                        {
                            Console.WriteLine("ID:{0} Title:{1}  RelatedLinkCount:{2}", workItem.Id, workItem.Fields["System.Title"], workItem.Fields["System.RelatedLinkCount"]);
                        }
    
                        Console.ReadLine();
                    }
                }
            }
        }
    }
    

    enter image description here