TFS Get all build definitions

2019-08-20 04:35发布

Good afternoon, I was scrolling here, reading questions and trying different code on how to get builds and all the definitions from them, however, whenever I try to execute code and get builds definitions it returns nothing, even though I for sure know there are both successful and unsuccessful builds in there. However I get nothing.

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

I am using VS2017 community version

标签: c# tfs
1条回答
老娘就宠你
2楼-- · 2019-08-20 05:18

Like the comment said above, the VNext build definition information couldn't be reached using the old version API. Install this TFS ExtendedClient Nuget package for your project, using the method below to get all build definitions.

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

        }

You could also use this kind of REST API to get build definitions:

Http method: GET

http://v-tinmo-12r2:8080/tfs/DefaultCollection/teamprojectname/_apis/build/definitions?api-version=2.0
查看更多
登录 后发表回答