TFS API - How to query builds independent of which build definition they belong to

青春壹個敷衍的年華 提交于 2019-11-27 01:20:40

问题


It seems no overloads of IBuildServer.QueryBuilds(...) allows me to do that.

Here's my code:

TfsTeamProjectCollection tfs = context.GetValue(TeamProject);
IBuildServer buildServer = (IBuildServer)tfs.GetService(typeof(IBuildServer));
buildServer.QueryBuilds( // **what should i put here?**

I don't want to specify the build definition, because the build I want may be of any type.

This question seems easy, but googling it gave me no answers.


回答1:


This code will get all builds . . . ever

TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("http://tfs:8080"));

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

var teamProjects = vcs.GetAllTeamProjects(true);

IBuildServer buildServer = (IBuildServer)tfs.GetService(typeof(IBuildServer));

foreach (TeamProject proj in teamProjects)
{
    var builds = buildServer.QueryBuilds(proj.Name);

    foreach (IBuildDetail build in builds)
    {
        var result = string.Format("Build {0}/{3} {4} - current status {1} - as of {2}",
        build.BuildDefinition.Name,
        build.Status.ToString(),
        build.FinishTime,
        build.LabelName,
        Environment.NewLine);

        System.Console.WriteLine(result);
    }            
}

However, you're probably more interested in this code, which enumerates each team project and gets the latest build status for each of the definitions:

TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("http://tfs:8080"));

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

var teamProjects = vcs.GetAllTeamProjects(true);

IBuildServer buildServer = (IBuildServer)tfs.GetService(typeof(IBuildServer));

foreach (TeamProject proj in teamProjects)
{
    var defs = buildServer.QueryBuildDefinitions(proj.Name);

    System.Console.WriteLine(string.Format("Team Project: {0}", proj.Name));

    foreach(IBuildDefinition def in defs)
    {
        IBuildDetailSpec spec = buildServer.CreateBuildDetailSpec(proj.Name, def.Name);
        spec.MaxBuildsPerDefinition = 1;
        spec.QueryOrder = BuildQueryOrder.FinishTimeDescending;

        var builds = buildServer.QueryBuilds(spec);

        if (builds.Builds.Length > 0)
        {
            var buildDetail = builds.Builds[0];

            System.Console.WriteLine(string.Format("   {0} - {1} - {2}", def.Name, buildDetail.Status.ToString(), buildDetail.FinishTime));
        }                
    }

    System.Console.WriteLine();
}



回答2:


You can use the following approach (inspired by the article http://www.incyclesoftware.com/2012/09/fastest-way-to-get-list-of-builds-using-ibuildserver-querybuilds-2/):

Benefit - much faster response (from my own tests - about 50 times faster!)

//Connect to TFS build server
string serverName = "http://myserver:8080/tfs/my_collection";
Uri tfsUri = new Uri(serverName);
TeamFoundationServer tfsServer = TeamFoundationServerFactory.GetServer(serverName);
IBuildServer buildServer = (IBuildServer)tfsServer.GetService(typeof(IBuildServer));
ILinking iLinkingService = tfsServer.GetService<ILinking>(); //can be used later to get build URL link


//Specify query
IBuildDetailSpec spec = buildServer.CreateBuildDetailSpec("*");
spec.InformationTypes = null; // for speed improvement
spec.MinFinishTime = DateTime.Now.AddDays(-21); //to get only builds of last 3 weeks
spec.MaxBuildsPerDefinition = 1; //get only one build per build definintion
spec.QueryOrder = BuildQueryOrder.FinishTimeDescending; //get the latest build only
spec.QueryOptions = QueryOptions.All;

var builds = buildServer.QueryBuilds(spec).Builds;


来源:https://stackoverflow.com/questions/5263005/tfs-api-how-to-query-builds-independent-of-which-build-definition-they-belong

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!