2017-05-16 136 views
-1

下午好,我在這裏滾動,閱讀問題,並嘗試使用不同的代碼來獲取構建以及它們的所有定義,但是,每當我嘗試執行代碼並獲取構建定義時,它都不會返回任何內容,即使我確定知道那裏有成功和失敗的構建。但是我什麼也沒得到。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社區版

+0

原來getAllTeamProjects返回它們的0。 –

+0

此API只適用於XAML構建定義,如果您使用的是構建vNext(TFS 2015+中的缺省構建),則必須使用REST API。 – DaveShaw

回答

0

像評論上面所說的,VNext構建定義信息無法使用舊版本的API來達到。爲您的項目安裝此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 
+0

但是我正在努力獲得構建狀態。可以用這種方式嗎? –

+0

@TomasŽemgulys我已更新我的代碼,請嘗試。 –

+0

它呢,謝謝:) –