Hi
Is there a way to query who broke the last build in TFS 2010.
I know that it is possible to subscribe to the build failed event but I would like to query the TFS
The following code will get you the most recent build. This is TFS2008, but the call should work fine under TFS2010 as well.
public static IBuildDetail GetMostRecentBuild(TeamFoundationServer tfs, string teamProject, string buildName)
{
IBuildServer buildServer = (IBuildServer)tfs.GetService(typeof(IBuildServer));
IBuildDetailSpec buildDetailSpec = buildServer.CreateBuildDetailSpec(teamProject, buildName);
buildDetailSpec.MaxBuildsPerDefinition = 1;
buildDetailSpec.QueryOrder = BuildQueryOrder.FinishTimeDescending;
buildDetailSpec.Status = BuildStatus.Failed | BuildStatus.PartiallySucceeded | BuildStatus.Stopped | BuildStatus.Succeeded;
IBuildQueryResult results = buildServer.QueryBuilds(buildDetailSpec);
if (results.Failures.Length != 0)
{
throw new ApplicationException("this needs to go away and be handled more nicely");
}
if (results.Builds.Length == 1)
{
return results.Builds[0];
}
else
{
return null;
}
}
Trying to see who broke the build isn't going to be that straightforward, though. What you're going to need to do is down through the results.Builds[]
array and find the last build that worked. Once you have that, you can query the team project for all changesets that have occurred since the last successful build. The following code will allow you to do that:
public static List<Changeset> GetChangesetsSinceDate(TeamFoundationServer tfs, DateTime date, string path)
{
VersionControlServer vcs = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
VersionSpec versionFrom = GetDateVSpec(date);
VersionSpec versionTo = GetDateVSpec(DateTime.Now);
IEnumerable results = vcs.QueryHistory(path, VersionSpec.Latest, 0, RecursionType.Full, "", versionFrom, versionTo, int.MaxValue, false, true);
List<Changeset> changes = new List<Changeset>();
foreach (Changeset changeset in results)
{
changes.Add(changeset);
}
return changes;
}
private static VersionSpec GetDateVSpec(DateTime date)
{
//Format is Dyyy-MM-ddTHH:mm example: D2009-11-16T14:32
string dateSpec = string.Format("D{0:yyy}-{0:MM}-{0:dd}T{0:HH}:{0:mm}", date);
return VersionSpec.ParseSingleSpec(dateSpec, "");
}
This will give you the list of candidate changesets that may have broken the build. These would be the people you would want to talk to.
That's probably as far as you would want to go with this. You could try to do some magic by matching up the failed project in the buildlog to the files in the changesets, but that's going to mean parsing out a potentially large build log file.