API TFS 2010, определяющий, на каком сервере сборки выполняется сборка.

Приношу извинения, это почти наверняка дубликат этого вопроса , но поскольку на этот вопрос нет ответа Я попробую еще раз.

Я пытаюсь создать инструмент, который позволит мне видеть все сборки, поставленные в очередь или выполняющиеся в TFS.

Одно из требований - иметь возможность видеть, на каком сервере сборки выполняется сборка. Все свойства и методы «BuildAgent» в IQueuedBuildsView устарели и вызывают нереализованные исключения. Есть много способов запросить агента, но вам нужен uri или имя агента, прежде чем вы сможете это сделать, и я чувствую, что попал в ситуацию с курицей и яйцом.

Кто-нибудь знает, как найти имя сервера сборки для работающей сборки? Приведенный ниже фрагмент кода может помочь.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Text;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.Client;


namespace TeamFoundationServerTools
{
    public static class TeamBuildData
    {

        public static void Main()
        {

            Uri teamFoundationServerUri = new Uri("http://tfs:8080/tfs");
            Uri teamFoudationServerProjectCollectionUri = new Uri("http://tfs:8080/tfs/collection");
            string teamFoundationServerName = "tfs";
            string teamFoundationServerProjectCollectionName = string.Empty;
            string teamFoundationServerProjectName = string.Empty;

            try
            {

                Dictionary collections = new Dictionary();

                if (string.IsNullOrEmpty(teamFoundationServerProjectCollectionName))
                {
                    DetermineCollections(teamFoundationServerUri, collections);
                }
                else
                {
                    collections.Add(teamFoundationServerName, teamFoudationServerProjectCollectionUri);
                }

                QueryCollections(teamFoundationServerName, teamFoundationServerProjectName, collections);

            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
        }

        /// 
        /// Queries the Team project collection for team builds
        /// 
        /// the name of the TFS server
        /// the name of the Team Project
        /// the Team Project Collections to be queried
        private static void QueryCollections(string teamFoundationServerName, string teamFoundationServerProjectName, Dictionary collections)
        {
            foreach (KeyValuePair collection in collections)
            {
                // connect to the collection
                using (TfsTeamProjectCollection teamProjectCollection = new TfsTeamProjectCollection(collection.Value, CredentialCache.DefaultCredentials))
                {
                    Console.WriteLine(teamProjectCollection.Name);

                    IBuildServer buildServer = (IBuildServer)teamProjectCollection.GetService(typeof(IBuildServer));

                    // get ICommonStructureService (later to be used to list all team projects)
                    ICommonStructureService commonStructureService = (ICommonStructureService)teamProjectCollection.GetService(typeof(ICommonStructureService));

                    // I need to list all the TFS Team Projects that exist on a server
                    ProjectInfo[] allTeamProjects;

                    if (!String.IsNullOrEmpty(teamFoundationServerProjectName))
                    {
                        allTeamProjects = new ProjectInfo[1];
                        allTeamProjects[0] = new ProjectInfo();
                        allTeamProjects[0] = commonStructureService.GetProjectFromName(teamFoundationServerProjectName);
                    }
                    else
                    {
                        allTeamProjects = commonStructureService.ListProjects();
                    }

                    // iterate thru the team project list
                    foreach (ProjectInfo teamProjectInfo in allTeamProjects)
                    {
                        Console.WriteLine(teamProjectInfo.Name);

                        // skip this team project if it is not WellFormed.
                        if (teamProjectInfo.Status != ProjectState.WellFormed)
                        {
                            continue;
                        }

                        IQueuedBuildsView queuedBuildsView = buildServer.CreateQueuedBuildsView(teamProjectInfo.Name);
                        queuedBuildsView.StatusFilter = QueueStatus.Queued | QueueStatus.InProgress | QueueStatus.Postponed;

                        queuedBuildsView.QueryOptions = QueryOptions.All;

                        queuedBuildsView.Refresh(false);
                        foreach (IQueuedBuild queuedBuild in queuedBuildsView.QueuedBuilds)
                        {
                            Console.WriteLine(queuedBuild.BuildDefinition.Name);
                            Console.WriteLine(queuedBuild.BuildController.Name);
                            Console.WriteLine(queuedBuild);
                            Console.WriteLine(queuedBuild.Status);
                            Console.WriteLine(queuedBuild.RequestedBy);
                            Console.WriteLine(queuedBuild.QueuePosition);
                            Console.WriteLine(queuedBuild.QueueTime);
                            Console.WriteLine(queuedBuild.Priority);
                            Console.WriteLine();

                            if (queuedBuild.Status == QueueStatus.InProgress)
                            {


                            }

                            Console.WriteLine("***********************");

                        }
                    }
                }
            }

            Console.ReadLine();
        }

        /// 
        /// Determins the team project collections for a given TFS instance
        /// 
        /// the uri of the Team Foundation Server
        /// a dictionary of collections to be added to
        private static void DetermineCollections(Uri teamFoundationServerUri, Dictionary collections)
        {
            // get a list of Team Project Collections and their URI's
            using (TfsConfigurationServer tfsConfigurationServer = new TfsConfigurationServer(teamFoundationServerUri))
            {
                CatalogNode configurationServerNode = tfsConfigurationServer.CatalogNode;

                // Query the children of the configuration server node for all of the team project collection nodes
                ReadOnlyCollection tpcNodes = configurationServerNode.QueryChildren(
                        new Guid[] { CatalogResourceTypes.ProjectCollection },
                        false,
                        CatalogQueryOptions.None);

                foreach (CatalogNode tpcNode in tpcNodes)
                {
                    ServiceDefinition tpcServiceDefinition = tpcNode.Resource.ServiceReferences["Location"];

                    ILocationService configLocationService = tfsConfigurationServer.GetService();
                    Uri tpcUri = new Uri(configLocationService.LocationForCurrentConnection(tpcServiceDefinition));

                    collections.Add(tpcNode.Resource.DisplayName, tpcUri);
                }
            }
        }
    }
}

7
задан Community 23 May 2017 в 12:17
поделиться