TFS .NET客户端库查找与合并相关的工作项

时间:2018-02-01 08:49:17

标签: c# tfs tfs2015 tfs-workitem workitem

我想构建一个查询应用程序,它将在TFS中查询分支,并检索所有工作项并将其显示为主详细信息报告:

User Story:30669    [Closed]    Ongoing Support and Maintenance
    Task:31566      [Closed]    Upgrade latest version of client libraries
    Bug:30905       [Closed]    Executing Error "The given Key was not present in the dictionary"
User Story:30351    [Closed]    Add new Function
    Task:28188      [Closed]    Implement new Function

这将为我们提供一个好主意,我们在给定分支上所做的工作,以及如果我们选择构建将在构建中做什么。

所以目前我可以检索日期之间的分支的所有工作项,不包括合并。我想包含合并,但我很难理解TFS客户端界面。

这就是我目前所做的:BranchModelChangesetModelUserStoriesTaskBugs是帮助我安排WorkItem个对象的类输出报告:

        const String c_collectionUri = "http://tfsServer:81/tfs/ProjectCollection";
        const String c_projectName = "Project";

        using (TfsTeamProjectCollection tpc = 
               new TfsTeamProjectCollection(new Uri(c_collectionUri)))
        {
            VersionControlServer vcServer =
                 tpc.GetService<VersionControlServer>();
            WorkItemStore workItemStore = tpc.GetService<WorkItemStore>();

            // Fetch the branch history
            var history = vcServer.QueryHistory(@"$/ProjectCollection/Project/Branch1/Source", RecursionType.Full)
                .Where(h => h.CreationDate >= new DateTime(2018, 1, 1) && h.CreationDate <= new DateTime(2018, 1, 5));

            BranchModel branch = new BranchModel("Branch1");

            // Get the changesets, for from the fetched history
            List<Changeset> changesets = new List<Changeset>();
            foreach (Changeset changeset in history)
            {
                changesets.Add(changeset);

                // TODO: Add code to get Changesets related to merge
            }

            // Get the WorkItems from Changesets Fetched 
            foreach (Changeset changeset in changesets)
            {
                ChangesetModel changesetModel = branch.AddChangeset(changeset);

                foreach (WorkItem workItem in changeset.WorkItems)
                {
                    //  Fetch the UserStory for a Bug/Task
                    foreach (var linkedItem in workItem.Links)
                    {
                        RelatedLink relatedItem = linkedItem as RelatedLink;
                        WorkItem userStory = null;

                        if (relatedItem != null)
                            userStory = GetWorkItem(workItemStore, c_projectName, relatedItem.RelatedWorkItemId);

                        if (userStory != null)
                        {
                            var userStoryModel = branch.AddUserStory(userStory);
                            var taskBugModel = userStoryModel.Add(workItem);
                        }
                    }
                }

            }

            // Finally print the report
            Console.WriteLine();
            Console.WriteLine("Related Work:");
            Console.WriteLine("=".PadLeft(80, '='));

            foreach (var userStory in branch.UserStories)
            {
                Console.WriteLine(userStory.Value.ToString());

                foreach (var taskBug in userStory.Value.TaskBugs)
                {
                    Console.WriteLine(taskBug.Value.ToString());
                }
            }

GetWorkItem的方法:

    void WorkItem GetWorkItem(WorkItemStore workItemStore, string c_projectName, int workitemID)
    {
        return workItemStore.Projects[c_projectName].Store.GetWorkItem(workitemID);
    }

我想知道,对于恰好是合并的Changeset,如何获取与该合并相关的相关工作项。

1 个答案:

答案 0 :(得分:1)

您无法直接从合并变更集获取相关工作项。您需要获取合并历史记录(version fromversion to),然后从version fromversion to更改集中检索相关工作项。

您可以使用VersionControlServer.GetChangesForChangeset方法获取合并变更集的合并历史记录。然后从历史记录Changesets Fetched中获取WorkItems。

以下示例供您参考以获取合并变更集历史记录:

static void Main(string[] args)
        {
            TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri("http://server:8080/tfs/DefaultCollection"));
            new System.Net.NetworkCredential("username", "password");
            tpc.EnsureAuthenticated();
            VersionControlServer vcs = tpc.GetService<VersionControlServer>();

            int cid = 376;

            //List<IChangesetSummary> changeSets = InformationNodeConverters.GetAssociatedChangesets(build);
            IEnumerable<Change> changes = vcs.GetChangesForChangeset(cid, false, Int32.MaxValue, null, null, true);

            foreach (Change change in changes)
            {
                if ((change.ChangeType & ChangeType.Merge) == 0) continue;
                foreach (var m in change.MergeSources)
                {
                    Console.WriteLine("{0} {1} {2} {3}", change.Item.ChangesetId, m.ServerItem, m.VersionFrom, m.VersionTo);
                }                   
            }
            Console.ReadLine();
        }