TFS API - 如何独立于构建定义查询构建 [英] TFS API - How to query builds independent of which build definition they belong to

查看:29
本文介绍了TFS API - 如何独立于构建定义查询构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我的代码:

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.

推荐答案

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

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

这篇关于TFS API - 如何独立于构建定义查询构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆