Wednesday, July 4, 2012

TFS Queries: Generating a changelog from the branch history

For an intro on LinqPad and the TFS API please read this post
For the list of all the posts in this series please read this one

Context


If you ever need to produce a changelog for a release you could do it by hand, looking at all the work items associated with the changesets or you could use the TFS API to generate it automatically.

Required references


Microsoft.TeamFoundation.Client
Microsoft.TeamFoundation.Common
Microsoft.TeamFoundation.VersionControl.Client
Microsoft.TeamFoundation.WorkItemTracking.Client

Query (C# Statements)

var branch = "$/Project1/Main";
var fromVersion = new ChangesetVersionSpec(1);
var toVersion = VersionSpec.Latest;

var tfs = TfsTeamProjectCollectionFactory
    .GetTeamProjectCollection(new Uri("http://localhost:8088/tfs"));

tfs.EnsureAuthenticated();

var versionControl = tfs.GetService<VersionControlServer>();

var history = versionControl.QueryHistory(branch, VersionSpec.Latest, 0, 
    RecursionType.Full, null, fromVersion, toVersion, int.MaxValue, false, 
    false, false);

history.OfType<Changeset>()
    .Select(x => new
    {
        x.ChangesetId,
        x.CreationDate,
        x.Committer,
        x.Comment,
        WorkItems = x.WorkItems.Select(wi => new
        {
            wi.Id,
            wi.Title,
            wi.Description,
            wi.State,
            wi.Reason
        })
    })
    .Dump("Branch history of: " + branch + 
        " from " + fromVersion.DisplayString + " to " + toVersion.DisplayString);
 
history.OfType<Changeset>()
    .OrderBy(x => x.ChangesetId)
    .SelectMany(x => x.WorkItems)
    .Select(wi => string.Format("[{0}] {1} #{2} - {3}", 
        wi.Reason, wi.Type.Name, wi.Id, wi.Title))
    .Dump("Changelog of branch: " + branch + 
        " from " + fromVersion.DisplayString + " to " + toVersion.DisplayString);

Result


With this we will get the list of all the changesets and associated work items from the history of the given branch.

Branch history of: $/Project1/Main from C1 to T


IEnumerable<> (4 items)
ChangesetId CreationDate Committer Comment WorkItems
11 28/06/2012 10:42:14 PM Domain\Pascal Change the phone number

IEnumerable<> (1 item)
Id Title Description State Reason
2 Fix the phone number on the Contact page   Done Work finished
10 28/06/2012 10:39:58 PM Domain\Pascal Changed the version to 1.1.0.0

IEnumerable<> (1 item)
Id Title Description State Reason
1 Change the version number to 1.1.0.0   Done Work finished
9 28/06/2012 10:31:55 PM Domain\Pascal Created a MVC4 Mobile application
IEnumerable<> (0 items)
8 28/06/2012 10:23:52 PM Domain\Pascal Added Main folder for the branch
IEnumerable<> (0 items)
Changelog of branch: $/Project1/Main from C1 to T


IEnumerable<String> (2 items)
[Work finished] Task #1 - Change the version number to 1.1.0.0
[Work finished] Bug #2 - Fix the phone number on the Contact page

2 comments:

Mladen said...

Thanks man - this really helped me out. I added merged items and posted it here: https://gist.github.com/mika76/d2b6defbf20d7cfc7800

Hong said...

thanks this is really neat. i'm surprised TFS doesn't make info like this readily available anywhere