Wednesday, May 16, 2012

TFS Queries: Recent builds of all team projects

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


We can see all the build results of a team project easily inside Team Explorer or with the Web Access but there is no way to see the build results of all team projects at the same time. Using a simple query and a few Linq operators we can get a useful little report in LinqPad.

Required references


Query (C# Statements)

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


var buildServer = tfs.GetService<IBuildServer>();

var spec = buildServer.CreateBuildDetailSpec("*");
spec.MinFinishTime = DateTime.Now.Subtract(TimeSpan.FromDays(7));
spec.MaxFinishTime = DateTime.Now;
spec.QueryDeletedOption = QueryDeletedOption.IncludeDeleted;

var builds = buildServer.QueryBuilds(spec).Builds;
var total = builds.Sum(b => b.FinishTime.Subtract(b.StartTime).TotalMinutes);

builds.Select(x => new
 Project = x.TeamProject,
 Definition = x.BuildDefinition.Name,
 Version = x.SourceGetVersion,
 Developer = x.RequestedFor,
 Type = x.Reason,
 Start = x.StartTime,
 Duration = x.FinishTime.Subtract(x.StartTime),
 Build = x.CompilationStatus,
 Tests = x.TestStatus,
 Result = x.Status,
 StyleCopViolations = InformationNodeConverters.GetBuildWarnings(x)
  .Count(inc => inc.Message.StartsWith("SA")),
 FxCopViolations = InformationNodeConverters.GetBuildWarnings(x)
  .Count(inc => inc.Message.StartsWith("CA")),
 Warnings = InformationNodeConverters.GetBuildWarnings(x)
  .Select(inc => inc.Message)
  .Where(m => !m.StartsWith("SA") && !m.StartsWith("CA")),
 Errors = InformationNodeConverters.GetBuildErrors(x)
  .Select(inc => inc.Message)
.OrderByDescending(b => b.Start)
.Dump("All builds of the week.  Total build duration: " + total);


You will get a list of the build results of all the team projects for the last week. The total build time is at the top of list. Each build result include the build errors, warnings and the number of StyleCop and FxCop violations found (if you have included those static analysis tools in your project template). Also, on line 9 you can change the query to get build results over a longer period of time.

All builds of the week.  Total build duration: 20.666666666666667

IOrderedEnumerable<> (1 item)
Project Definition Version Developer Type Start Duration Build Tests Result StyleCopViolations FxCopViolations Warnings Errors
Project 1 Main 25 Pascal Manual 09/05/2012 9:34:07 PM 00:12:06 Success Success Success 4 1
IEnumerable<String> (0 items)

IEnumerable<String> (0 items)
Project 2 Main 28 Pascal Manual 09/05/2012 11:25:26 PM 00:08:34 Success Success Success 0 0
IEnumerable<String> (0 items)

IEnumerable<String> (0 items)


MB said...

Can we get list of Build Definitions used per project?

Any idea?

Pascal Laurin said...

You could call buildServer.QueryBuildDefinitions("*") and then get the Name and TeamProject properties