如何在c#中通过1个查询获取Wiql的任务ID(storyid,Featureid,功能名称)?

时间:2018-02-28 06:08:22

标签: c# tfs-workitem workitem wiql

我有一个像这样的分层结构,其中一个功能可以有许多用户故事,每个用户故事都有一个到多个任务。
但是用户故事可能没有父功能,而某些任务可能没有父用户故事

F1
  -->U1,U2,U3 
     --->t1,t2,t3

我需要在c#中使用Wiql查询,对于任何Taskid输入,我获取其父级故事(或者如果没有父级,则为0)以及后一个父级功能ID(如果没有父级用户故事,则为0)和名称(如果功能ID为0,则为“其他”

1 个答案:

答案 0 :(得分:2)

您可以使用此查询找到工作项的任何父分支: enter image description here

您可以使用此代码获取父母列表:

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System;
using System.Collections.Generic;
using System.Linq;

namespace QueryLinkedWIQL
{
    class Program
    {
        static void Main(string[] args)
        {
            int _id = 742;

            try
            {
                TfsTeamProjectCollection _tpc = new TfsTeamProjectCollection(new Uri("http://myserver/DefaultCollection"));

                WorkItemStore _wistore = _tpc.GetService<WorkItemStore>();

                var _parents = GetParentsWithWIQL(_wistore, _id);

                if (_parents.Count == 0)
                {
                    Console.WriteLine("There is no parent for ID: " + _id);
                    return;
                }

                for (int i = 0; i < _parents.Count; i++)
                    Console.WriteLine("{0} parent: Team project '{1}', Type '{2}', Id '{3}', Title '{4}'",
                        (i == 0) ? "Main" : "Next", _parents[i].Project.Name, _parents[i].Type.Name, _parents[i].Id, _parents[i].Title);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }

        private static List<WorkItem> GetParentsWithWIQL(WorkItemStore pWiStore, int pId)
        {
            List<WorkItem> _parents = new List<WorkItem>();

            string _wiql = String.Format("SELECT [System.Id] FROM WorkItemLinks WHERE ([Source].[System.WorkItemType] <> '') And ([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward') And ([Target].[System.Id] = {0}) ORDER BY [System.Id] mode(Recursive,ReturnMatchingChildren)", pId);

            Query _query = new Query(pWiStore, _wiql);

            WorkItemLinkInfo[] _links = _query.RunLinkQuery();

            for (int i = 0; i < _links.Count(); i++)
                if (_links[i].TargetId != pId) _parents.Add(pWiStore.GetWorkItem(_links[i].TargetId));

            return _parents;
        }
    }
}

您也可以修改最后一个周期并获取父信息(用户故事或功能)

            if (_parents.Count == 0)
            {
                Console.WriteLine("There is no parent for ID: " + _id);
                return;
            }

            int _featureId = 0, _userstoryId = 0;
            string _featureTitle = "", _userstoryTitle = "";

            for (int i = 0; i < _parents.Count; i++)
                switch (_parents[i].Type.Name)
                {
                    case "Feature":
                        if (_featureId == 0) { _featureId = _parents[i].Id; _featureTitle = _parents[i].Title; }
                        break;
                    case "Product Backlog Item": //for scrum process template. for agile use User Story
                        if (_userstoryId == 0) { _userstoryId = _parents[i].Id; _userstoryTitle = _parents[i].Title; }
                        break;
                }

            if (_featureId != 0) Console.WriteLine("The main parent is Feature: {0}: '{1}'", _featureId, _featureTitle);
            else if (_userstoryId != 0) Console.WriteLine("The main parent is User Story: {0}: '{1}'", _userstoryId, _userstoryTitle);