我正在尝试从TFS2015构建定义中获取信息。我们有大约100个XAML格式的构建定义,大约50个新的2015格式。 该服务器是一个内部Team foundation Server。 (Microsoft Visual Studio Team Foundation Server 版本15.105.25910.0)
我没有使用其余的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 :(得分:2)
正如Eddie - MSFT和ToreØstergaard所回答的,我不得不使用其余的API。但看起来我必须使用旧的api来获取builddefinitions中的所有信息。从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;
}
}
}
答案 1 :(得分:1)
您使用的遗留肥皂API只能获得XAML版本。
假设您已经安装了最新的.NET Client Libraries,您可以使用库中的Rest API来获取这样的XAML和vNext构建:
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();
}
}
}
答案 2 :(得分:0)
Microsoft.TeamFoundationServer.ExtendedClient中的API主要用于向后兼容XAML构建。据我所知,他们不支持Build vNext,因为它们是在他们的时间之前编写的。
正如您提供的链接所示,REST API是未来的关注方式。