Team Build 2010: Associate Changesets and Work Items with a Dummy Build

Grant Holliday’s blog

Senior Service Engineer, Microsoft Visual Studio Team Foundation Service

Team Build 2010: Associate Changesets and Work Items with a Dummy Build

  • Comments 1

Jason Prickett has a blog post called Creating Fake Builds in TFS Build 2010. It includes code for creating a dummy build service host, build controller, build definition and then a build result (IBuildDetail).

One thing that isn’t shown is how to associate Changesets and Work Items to the build result. Fortunately its not that hard and you can use the following methods in the Team Build Object Model:

Once you’ve added the associations, you may need to save the InformationNode using IBuildInformationNode.Save(). However, some of them will save as part of the association. In my sample code below, I choose to save after making all the associations to minimize the number of round-trips to the server.

The following sample code will go and find the specified Build Number then add an association for the specified changeset ID and all the work items that are associated to the changeset.

/// <summary>
/// Finds the specified build number and adds changeset + work item associations
/// so that they appear in the build report.
/// </summary>
private static void AssociateBuild(String collectionUri, String teamProject, String buildNumber, int changesetId)
{
  TfsTeamProjectCollection tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(collectionUri), new UICredentialsProvider());
  IBuildServer buildServer = (IBuildServer)tpc.GetService(typeof(IBuildServer));

  // Create a IBuildDefinitionSpec so that we can specify our build detail exactly and
  // avoid a potentially very expensive call to the server with QueryBuilds()
  IBuildDetailSpec spec = buildServer.CreateBuildDetailSpec(teamProject);
  spec.BuildNumber = buildNumber;
  spec.MaxBuildsPerDefinition = 1; // Redundant, but just for safety - we’re specifying the build number there should only be one
  IBuildDetail buildDetail = buildServer.QueryBuilds(spec).Builds[0];

  // Get the changeset that we want to associate
  Changeset changeset = tpc.GetService<VersionControlServer>().GetChangeset(changesetId);
  buildDetail.Information.AddAssociatedChangesets(new Changeset[] { changeset });
  buildDetail.Information.AddAssociatedWorkItems(changeset.WorkItems);
  buildDetail.Information.Save();
  buildDetail.Save();
}

By creating fake builds and associating changesets and work items, you’re closing the traceability loop within TFS. Build results act as a synchronization point and are an important metric in the data warehouse and reporting capabilities of TFS.

  • Hello,

    Thank you for your post.

    I wonder whether this code can be used for integrating Team City builds with TFS?

Page 1 of 1 (1 items)