19

似乎没有IBuildServer.QueryBuilds(...)允许我这样做的重载。

这是我的代码:

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

我不想指定构建定义,因为我想要的构建可能是任何类型。

这个问题似乎很简单,但谷歌搜索它没有给我答案。

4

2 回答 2

46

此代码将获取所有构建。. . 曾经

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);
    }            
}

但是,您可能对这段代码更感兴趣,它枚举每个团队项目并获取每个定义的最新构建状态:

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();
}
于 2011-03-11T13:33:39.623 回答
8

您可以使用以下方法(受文章http://www.incyclesoftware.com/2012/09/fastest-way-to-get-list-of-builds-using-ibuildserver-querybuilds-2/启发):

好处 - 更快的响应(根据我自己的测试 - 大约快 50 倍!)

//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;
于 2013-08-02T13:55:41.963 回答