2017-01-09 4 views
0

TFS2015ビルド定義から情報を取得しようとしています。 XAML形式のビルド定義は約100あり、新しい2015形式のビルド定義は約50です。 サーバーは社内チーム基盤サーバーです。 (Microsoft Visual Studio Team Foundation Server バージョン15.105.25910.0)TFSサーバーAPIは、XAMLビルド定義のみを一覧表示します。

私は残りのAPIを使用していませんが、Microsoft.TeamFoundationServer.ExtendedClientはここで推奨されています:https://blogs.msdn.microsoft.com/buckh/2015/08/10/nuget-packages-for-tfs-and-visual-studio-online-net-client-object-model/

using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.Linq; 
using Microsoft.TeamFoundation.Build.Client; 
using Microsoft.TeamFoundation.Client; 
using Microsoft.TeamFoundation.Framework.Client; 
using Microsoft.TeamFoundation.Framework.Common; 
using Serilog; 

namespace TFSExperiment 
{ 
    class Program 
    { 
     // see https://blogs.msdn.microsoft.com/buckh/2015/08/10/nuget-packages-for-tfs-and-visual-studio-online-net-client-object-model/ 
     //Needs nuget package Install-Package Microsoft.TeamFoundationServer.ExtendedClient -Version 14.102.0 
     // to use serilogg: Install-Package Serilog ; Install-Package Serilog.Sinks.RollingFile 
     static void Main(string[] args) 
     { 
      var myLog = new LoggerConfiguration() 
       .WriteTo.RollingFile("..\\..\\Applog\\mylog-{Date}.log").CreateLogger();   
      TfsConfigurationServer configurationServer = 
       TfsConfigurationServerFactory.GetConfigurationServer(new Uri("https://tfs.inhouseserver2015.org/tfs/")); 
      ReadOnlyCollection<CatalogNode> collectionNodes = 
       configurationServer.CatalogNode.QueryChildren(new[] {CatalogResourceTypes.ProjectCollection}, false, 
        CatalogQueryOptions.None); 
      CatalogNode defultTfsCol = collectionNodes.AsQueryable().Single(c=>c.Resource.DisplayName.Equals("DefaultCollection")); 
      Console.WriteLine(defultTfsCol.Resource.DisplayName); 
       TfsTeamProjectCollection tfsProjectCollection = 
       configurationServer.GetTeamProjectCollection(new Guid(defultTfsCol.Resource.Properties["InstanceId"])); 
       tfsProjectCollection.Authenticate(); 
       var buildServer = (IBuildServer)tfsProjectCollection.GetService(typeof(IBuildServer));     
       ReadOnlyCollection<CatalogNode> projectNodes = defultTfsCol.QueryChildren(
        new[] { CatalogResourceTypes.TeamProject }, 
        false, CatalogQueryOptions.None); 
       foreach (var proj in projectNodes) 
       { 
        var buildDefinitionList = new List<IBuildDefinition>(buildServer.QueryBuildDefinitions(proj.Resource.DisplayName)); 
        foreach (var buildDef in buildDefinitionList) 
        {      
         Console.WriteLine(buildDef.Name);     
         myLog.Information($"{buildDef.Id} --{buildDef.Name} --{buildDef.BuildServer.BuildServerVersion} "); 
        } 
       }    
      Console.WriteLine(" Hit any key to exit "); 
      Console.ReadKey(); 
     } 
    } 
} 

答えて

0

Microsoft.TeamFoundationServer.ExtendedClientにおけるAPIはXAMLとの下位互換性を提供するために主に存在し構築します

は、ここに私のコード例です。私の知る限り、彼らは時間の前に書かれていたBuild vNextをサポートしていません。

提供したリンクのとおり、REST APIは今後の対応方法です。

+0

[OK]を、多分私はMicrosoft.TeamFoundationServer.ExtendedClient読ん高速にそれを読む:」..Becauseは、すべてのAPIはRESTのAPIとして、現在、TFS 2015またはVSOで提供されていない、あなたのケースがあるように予定されていますこのパッケージを使わなければならない... "だから私はすべてがこれにあったと思った。 (したがって、名前が拡張された " –

+0

私は残りのAPIを試みたが、私はSystem.Net.Http.HttpRequestExceptionを動作させるためにWindows認証を取得していませんでした:レスポンスステータスコードは成功を示していません:401(無認可) –

+0

UseDefaultCredentials = true; WebClientオブジェクトに? –

1

XAMLビルドのみを取得できる従来のSOAP APIを使用しています。

あなたはXAMLとvNextの両方を取得するために図書館で休憩APIを使用することができ、あなたが最新の.NET Client Librariesインストールしていると仮定すると、このように構築します:エディで答えたよう

using System; 
using System.Collections.Generic; 
using Microsoft.TeamFoundation.Client; 
using Microsoft.TeamFoundation.Build.WebApi; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Uri tfsurl = new Uri("http://xxxx:8080/tfs/CollectionName"); 
      TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(tfsurl); 
      BuildHttpClient bhc = ttpc.GetClient<BuildHttpClient>(); 
      List<Build> builds = bhc.GetBuildsAsync("ProjectName").Result; 
      foreach (Build bu in builds) 
      { 
       Console.WriteLine(bu.BuildNumber); 
      } 
      Console.ReadLine(); 
     } 
    } 
} 
+0

こんにちはthx。私はxamlビルドでは得られなかった変数に関するビルドdefsについての情報が必要です。私はブースapi:sを使用しなければなりませんでした。 –

1

は - MSFTとØstergaardをとれ私が持っていました残りのAPIを使用してください。しかし、ビルド定義からすべての情報を取得するために古いAPIを使用する必要があるように見えます。 restapiからビルドの名前は得られましたが、xamlビルドの深い情報は得られませんでした。

Octopus Deployプロジェクト名を指摘した変数から情報を収集したかったのです。誰かが同じような問題を抱えている場合に備えて、ここにコードを投稿してください。

using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.Diagnostics; 
using System.Linq; 
using System.Net; 
using System.Text.RegularExpressions; 
using Microsoft.TeamFoundation.Build.Client; 
using Microsoft.TeamFoundation.Client; 
using Microsoft.TeamFoundation.Build.WebApi; 
using Microsoft.TeamFoundation.Common; 
using Microsoft.TeamFoundation.Framework.Client; 
using Microsoft.TeamFoundation.Framework.Common; 
using Newtonsoft.Json; 
using Serilog; 
using Serilog.Core; 


namespace ListOctopusProjectsAndTfsBuilds 
{ 
    class Program 
    { 
     // see https://blogs.msdn.microsoft.com/buckh/2015/08/10/nuget-packages-for-tfs-and-visual-studio-online-net-client-object-model/ 
     //Needs nuget package Install-Package Microsoft.TeamFoundationServer.ExtendedClient -Version 14.102.0 
     // to use serilogg: Install-Package Serilog ; Install-Package Serilog.Sinks.RollingFile 

     static Logger _myLog = new LoggerConfiguration() 
       .WriteTo.RollingFile($"..\\..\\Applog\\myBuildDeflog-{DateTime.Now:yyyyMMddHHHHmmss}.log").CreateLogger(); 
     static void Main(string[] args) 
     { 
      Console.WriteLine("Start looking for BuildDefs"); 
      Dictionary<string, string> octopusDeployDictionary = GetvNextBuildDefVariable(new Uri("https://inhouseserver2015/tfs/DefaultCollection"), "YourTfsProject", "OctoProj"); 
      Dictionary<string, string> octopusDeployXamlDictionary = GetXamlBuildDefVariable(new Uri("https://inhouseserver2015/tfs/DefaultCollection"), "YourTfsProject", "ArgOctopusProjectsToPublish"); 
      Console.WriteLine("Builds vnext defs -- Octopus project"); 
      foreach (var cd in octopusDeployDictionary) 
      { 
       Console.WriteLine($"{cd.Key}; {cd.Value}"); 
      } 
      Console.WriteLine("Builds xaml defs -- Octopus project"); 
      foreach (var cd in octopusDeployXamlDictionary) 
      { 
       Console.WriteLine($"{cd.Key}; {cd.Value}"); 
      } 
      Console.ReadLine(); 
     } 

     private static Dictionary<string, string> GetXamlBuildDefVariable(Uri tfsUri, string tfsProject, string buildDefVar) 
     { 
      var collection = tfsUri.LocalPath.Split('/')[2]; 
      //Define Reg expression ahead so it can faster parse xml and find the Octopus Deploy projectname. 
      Regex findArgOctopusProjectsToPublish = null; 
      try 
      { 
       findArgOctopusProjectsToPublish = new Regex($"x:Key=\"{buildDefVar}\".*<x:String>([^<]*)", 
        RegexOptions.IgnoreCase | RegexOptions.Singleline); 
      } 
      catch (ArgumentException ex) 
      { 
       _myLog.Error(ex, "Error with RegularExpression syntax"); 
      } 
      Dictionary<string, string> tfsVarBuildDefDict = new Dictionary<string, string>(); 
      TfsConfigurationServer configurationServer = 
       TfsConfigurationServerFactory.GetConfigurationServer(tfsUri)); 
      ReadOnlyCollection<CatalogNode> collectionNodes = 
       configurationServer.CatalogNode.QueryChildren(new[] {CatalogResourceTypes.ProjectCollection}, false, 
        CatalogQueryOptions.None); 
      CatalogNode defultTfsCol = 
       collectionNodes.AsQueryable().Single(c => c.Resource.DisplayName.Equals(collection)); 
      TfsTeamProjectCollection tfsProjectCollection = 
       configurationServer.GetTeamProjectCollection(new Guid(defultTfsCol.Resource.Properties["InstanceId"])); 
      tfsProjectCollection.Authenticate(); 
      var buildServer = (IBuildServer) tfsProjectCollection.GetService(typeof(IBuildServer)); 
      ReadOnlyCollection<CatalogNode> projectNodes = defultTfsCol.QueryChildren(
       new[] {CatalogResourceTypes.TeamProject}, 
       false, CatalogQueryOptions.None); 

       var buildDefinitionList = 
        new List<IBuildDefinition>(buildServer.QueryBuildDefinitions(tfsProject)); 
       foreach (var buildDef in buildDefinitionList) 
       { 
       Debug.Assert(findArgOctopusProjectsToPublish != null, "findArgOctopusProjectsToPublish != null"); 
       var octopusProjectsToPublish = 
         findArgOctopusProjectsToPublish?.Match(buildDef.ProcessParameters).Groups[1].Value; 
        if (octopusProjectsToPublish.IsNullOrEmpty()) 
        { 
         octopusProjectsToPublish = "NoOctopus Projekt"; 
        } 
       tfsVarBuildDefDict.Add(buildDef.Name, octopusProjectsToPublish); 
        _myLog.Information($"{buildDef.Id} --{buildDef.Name} --{buildDef.BuildServer.BuildServerVersion} "); 
       } 

      return tfsVarBuildDefDict; 
     } 

     private static Dictionary<string, string> GetvNextBuildDefVariable(Uri tfsUri,string tfsProject,string buildDefVar) 
     { 
      Dictionary<string, string> tfsVarBuildDefDict = new Dictionary<string, string>(); 
      TfsTeamProjectCollection ttpc = 
       new TfsTeamProjectCollection(tfsUri); 
      BuildHttpClient bhc = ttpc.GetClient<BuildHttpClient>(); 
      var definitions = bhc.GetDefinitionsAsync(project: tfsProject); 
      var client = new WebClient {UseDefaultCredentials = true}; 
      foreach (var buildDef in definitions.Result) 
      { 
       if (buildDef.Type == DefinitionType.Build) 
       { 
        var json = client.DownloadString(buildDef.Url); 
        BuildDefinition result = JsonConvert.DeserializeObject<BuildDefinition>(json); 
        if (result.Variables.ContainsKey(buildDefVar)) 
        { 
         tfsVarBuildDefDict.Add(buildDef.Name, result.Variables[buildDefVar].Value.ToString()); 
         _myLog.Information($"{buildDef.Name} Octoproject: {result.Variables[buildDefVar].Value.ToString()}"); 
        } 
       } 
       else 
       { 
        _myLog.Information($"{buildDef.Name} BuildType {buildDef.Type} "); 
       } 
      } 
      return tfsVarBuildDefDict; 
     } 
    } 
} 
関連する問題