TFS 服务器 API 仅列出 XAML 构建定义 [英] TFS server API only list the XAML build definitions

查看:25
本文介绍了TFS 服务器 API 仅列出 XAML 构建定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 TFS2015 构建定义中获取信息.我们有大约 100 个 XAML 格式的构建定义和大约 50 个新的 2015 格式.该服务器是内部团队基础服务器.(Microsoft Visual Studio Team Foundation Server版本 15.105.25910.0)

I am trying to obtain information from TFS2015 build definitions. We have about 100 build definitions in XAML format and about 50 in the new 2015 format. The server is an inhouse Team foundation Server. (Microsoft Visual Studio Team Foundation Server Version 15.105.25910.0)

我没有使用其余的 api,而是使用 Microsoft.TeamFoundationServer.ExtendedClient 作为这里推荐的:https://blogs.msdn.microsoft.com/buckh/2015/08/10/nuget-package-for-tfs-and-visual-studio-online-net-client-object-model/ .

I am not using the rest api but the Microsoft.TeamFoundationServer.ExtendedClient as recommended here : 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();
        }
    }
}

推荐答案

正如 Eddie - MSFT 和 Tore Østergaard 所回答的那样,我不得不使用其余的 api.但看起来我还必须使用旧的 api 从构建定义中获取所有信息.从 restapi 我得到了构建的名称,但没有得到 xaml 构建的深层信息.

As answered by Eddie - MSFT and Tore Østergaard I had to use the rest api. But It looks like I have to use the old api also to get all the information from the builddefinitions. From the restapi I got the name of build but not the deep information for the xaml builds.

我想从指出 Octopus Deploy 项目名称的变量中收集信息.我在这里发布代码以防有人遇到类似的问题.

I wanted to collect information from the variable that pointed out the Octopus Deploy project name. I Post the code here in case someone has similiar issue.

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

这篇关于TFS 服务器 API 仅列出 XAML 构建定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆