August 12th, 2006

Getting Changeset objects from associated work items

Buck Hodges
Director of Engineering

On internal mailing list, someone asked how to do this, and I thought it’s worth sharing.  You can get a Changeset object using its artifact URI (aka link) via VersionControlServer.ArtifactProvider.  Here’s how that would look like, based on modifying code from James Manning’s blog posthttp://blogs.msdn.com/jmanning/archive/2005/09/21/472524.aspx.  Added/changed lines are hightlighted.

using System;
using System.Collections.Generic;

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation;
using Microsoft.TeamFoundation.VersionControl.Client;

class ChangesetsFromWorkItems
{
    static void Main(string[] args)
    {
        if (args.Length < 2)
        {
            Console.Error.Write(“Usage: ChangesetsFromWorkItems <server> <workitemid> [workitemid…]”);
            Environment.Exit(1);
        }

        TeamFoundationServer server = TeamFoundationServerFactory.GetServer(args[0]);
        WorkItemStore wiStore = (WorkItemStore)server.GetService(typeof(WorkItemStore));
        VersionControlServer vcs = (VersionControlServer) server.GetService(typeof(VersionControlServer));

        int workItemId;
        for (int i = 1; i < args.Length; i++)
        {
            if (!int.TryParse(args[i], out workItemId))
            {
                Console.Error.WriteLine(“ignoring unparseable argument {0}”, args[i]);
                continue;
            }

            WorkItem workItem = wiStore.GetWorkItem(workItemId);
            List<Changeset> associatedChangesets = new List<Changeset>();
            foreach (Link link in workItem.Links)
            {
                ExternalLink extLink = link as ExternalLink;
                if (extLink != null)
                {
                    ArtifactId artifact = LinkingUtilities.DecodeUri(extLink.LinkedArtifactUri);
                    if (String.Equals(artifact.ArtifactType, “Changeset”, StringComparison.Ordinal))
                    {
                        // Convert the artifact URI to Changeset object.
                        associatedChangesets.Add(vcs.ArtifactProvider.GetChangeset(new Uri(extLink.LinkedArtifactUri);
                    }
                }
            }

            // Do something with the changesets.  Changes property is an array, each Change
            // has an Item object, each Item object has a path, download method, etc.
        }
    }
}

Author

Buck Hodges
Director of Engineering

Director of Engineering, Azure DevOps

0 comments