Search code examples
buildtfs-sdk

TFS: Query for builds containing a specific changeset


I have a number of build definitions that get executed based upon a single branch in TFS (eg Main).

I'd like to (somehow) query TFS to find all builds containing a specific changeset number that I supply, and return a list of string of the names of the builds that TFS contains. Any kind of app (VS extension, CLI app, winforms, whatever) will do.

Note: this isn't a 'plz give me the code' request; I'm willing to hoof it and do serious work on this. Any pointers to documentation on how to query the database or SDK, or an example of how to query builds; just some place to start looking would be extremely helpful. Thanks.


Solution

  • The following snippet will crawl all Build Definitions of all Team Project of a Collection, and will check each and every build for an Association to the input changeset number:

    using System;
    using System.Linq;
    using Microsoft.TeamFoundation.Build.Client;
    using Microsoft.TeamFoundation.Client;
    using Microsoft.TeamFoundation.VersionControl.Client;
    
    namespace FindChangesetInBuild
    {
        class Program
        {
            static void Main(string[] args)
            {
                TfsTeamProjectCollection teamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://tfs:8080/tfs/collectionName"));
    
                var versionControl = teamProjectCollection.GetService<VersionControlServer>();
                var buildService = (IBuildServer)teamProjectCollection.GetService(typeof(IBuildServer));
    
                var teamProjects = versionControl.GetAllTeamProjects(true);
                foreach (var teamProject in teamProjects)
                {
                    var buildDefinitions = buildService.QueryBuildDefinitions(teamProject.Name);
                    foreach (var buildDefinition in buildDefinitions)
                    {
                        var builds = buildService.QueryBuilds(buildDefinition);
                        foreach (var buildDetail in builds)
                        {
                            var changesets = InformationNodeConverters.GetAssociatedChangesets(buildDetail);
                            if (changesets.Any(changesetSummary => changesetSummary.ChangesetId == Convert.ToInt32(args[0])))
                            {
                                Console.WriteLine("Changeset was build in "+buildDetail.BuildNumber);
                            }
                        }
                    }
                }
            }
        }
    }
    

    Needless to say, this is a brute force attack.
    You can further refine the code if you narrow down the list of buildDefinition, make focus on specific teamProjects etc. In any case I can hardly imagine the above to be useful as-is!

    Apart from (obviously) MSDN, a great resource for TFS-SDK is Shai Raiten's blog.
    For Build-Speficic examples, check also here & here for some possibly interesting SO posts.