TFS获取所有构建定义

时间:2017-05-16 10:57:00

标签: c# tfs

下午好,我在这里滚动,阅读问题并尝试使用不同的代码来获取构建及其中的所有定义,但是,每当我尝试执行代码并获得构建定义时,它都不返回任何内容,即使我确定知道那里有成功和不成功的构建。但是我一无所获。

private static void Main(string[] args)
    {
        NetworkCredential credential = new NetworkCredential("MyUsername", "MyPassword");
        VssBasicCredential basicCred = new VssBasicCredential(credential);

        TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri("https://tomheza.visualstudio.com/DefaultCollection"), basicCred);

        tpc.Authenticate();

        CatalogNode catalogNode = tpc.CatalogNode;
        ReadOnlyCollection<CatalogNode> collectionNodes = tpc.CatalogNode.QueryChildren(new[] { CatalogResourceTypes.TeamProject }, false, CatalogQueryOptions.None);

        foreach (CatalogNode collectionNode in collectionNodes)
        {
            Console.WriteLine(collectionNode.Resource.DisplayName);
        }

        var buildServer = (IBuildServer)tpc.GetService(typeof(IBuildServer));
        var vcs = tpc.GetService<VersionControlServer>();
        var teamProjects = vcs.GetAllTeamProjects(true);

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

            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];

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

            Console.WriteLine();
        }
    }

我正在使用VS2017社区版

1 个答案:

答案 0 :(得分:5)

与上述评论一样,使用旧版API无法访问VNext构建定义信息。使用以下方法为项目安装此TFS ExtendedClient Nuget包以获取所有构建定义。

using Microsoft.VisualStudio.Services.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.TeamFoundation.Build.WebApi;
using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.VisualStudio.Services.Operations;

        public static void GetBuild()
        {
            var u = new Uri("http://servername:8080/tfs/MyCollection/");
            VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.WindowsCredential(new NetworkCredential("v-tinmo", "123456.w", "fareast")));
            VssConnection connection = new VssConnection(u, c);

            BuildHttpClient buildServer = connection.GetClient<BuildHttpClient>();

            //get all build definitions in your team projects
            List<BuildDefinitionReference> builddefs = buildServer.GetDefinitionsAsync(project:"team project name").Result;   
            foreach (BuildDefinitionReference builddef in builddefs)
            {
                Console.WriteLine(builddef.Name);
                ...
            }

            //get all builds information in your team projects
            var builds = buildServer.GetBuildsAsync(project: "team project name").Result;
            foreach (var build in builds)
            {
                Console.WriteLine(build.Definition.Name + "--" + build.BuildNumber + "--" +build.Result);
            }

        }

您还可以使用t his kind of REST API来获取构建定义:

Http method: GET

http://v-tinmo-12r2:8080/tfs/DefaultCollection/teamprojectname/_apis/build/definitions?api-version=2.0