How to retrieve a list of Changesets (or work items) that were checked-in between builds?

Lukas picture Lukas · Jan 14, 2010 · Viewed 26.1k times · Source

I need a list of changesets (or Work Items) that were made betweend builds (I can label builds if its necessary). I need that list for our test team (and to publish 'changelist').

Is MSBuild task able to retrieve that list and save as file (then I can process that list further.
Or maybe I need to connect to TFS from C# code and retrieve that list myself (I'm familiar with retrieving WorkItems in C#).

Answer

Ted Reid picture Ted Reid · Jan 27, 2012

I know this thread is a couple of years old, but I found it when trying to accomplish the same thing. I've been working on this for a couple of days now, and came up with a solution that accomplishes this specific task. (TFS 2010)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.Build.Client;


namespace BranchMergeHistoryTest
{
  class Program
  {
    private static Uri tfsUri = new Uri("http://sctf:8080/tfs");
    private static TfsTeamProjectCollection tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(tfsUri);

    static void Main(string[] args)
    {

      IBuildServer buildServer = tfs.GetService<IBuildServer>();
      IBuildDefinition buildDef = buildServer.GetBuildDefinition("Project", "Specific Build");
      IOrderedEnumerable<IBuildDetail> builds = buildServer.QueryBuilds(buildDef).OrderByDescending(build => build.LastChangedOn);
      /* I had to use some logic to find the last two builds that had actual changesets attached - we have some builds that don't have attached changesets. You may want to do the same. */ 
      IBuildDetail newestBuild = builds.ElementAt(0); 
      IBuildDetail priorBuild = builds.ElementAt(1);

      string newestBuildChangesetId = newestBuild.Information.GetNodesByType("AssociatedChangeset")[0].Fields["ChangesetId"];
      string priorBuildChangesetId = priorBuild.Information.GetNodesByType("AssociatedChangeset")[0].Fields["ChangesetId"];

      VersionControlServer vcs = tfs.GetService<VersionControlServer>();
      const string sourceBranch = @"$SourceBranch-ProbablyHEAD";
      const string targetBranch = @"$TargetBranch-ProbablyRelease";
      VersionSpec versionFrom = VersionSpec.ParseSingleSpec(newestBuildChangesetId, null);
      VersionSpec versionTo = VersionSpec.ParseSingleSpec(priorBuildChangesetId, null);
      ChangesetMergeDetails results = vcs.QueryMergesWithDetails(sourceBranch, VersionSpec.Latest, 0, targetBranch,VersionSpec.Latest, 0, versionFrom, versionTo, RecursionType.Full);
      foreach(Changeset change in results.Changesets)
      {
        Changeset details = vcs.GetChangeset(change.ChangesetId);
        // extract info about the changeset
      }
    }
  }
}

Hope this helps the next person trying to accomplish the task!