public void Run(TFSConfiguration tfsConfiguration = null)
        {
            Logger.Log.Info("Run TFS integration");

            if (tfsConfiguration == null)
            {
                Logger.Log.Info($"Application runs from this location: {System.Reflection.Assembly.GetExecutingAssembly().Location}");
                Logger.Log.Info("Read configuration");
                tfsConfiguration = ReadTFSConfiguration(Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), CONFIG_FILE));
            }

            Logger.Log.Info($"Authenticate to server {tfsConfiguration.TfsUrl}");
            NetworkCredential credentials = CredentialUtil.GetCredential(
                tfsConfiguration.TfsUrl.AbsoluteUri.Substring(0, tfsConfiguration.TfsUrl.AbsoluteUri.Length - tfsConfiguration.TfsUrl.AbsolutePath.Length + 1));
            TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(tfsConfiguration.TfsUrl, credentials);

            tfs.Authenticate();


            Logger.Log.Info("Get build server!");
            IBuildServer buildServer = tfs.GetService <IBuildServer>();

            Logger.Log.Info($"Build server version: {buildServer.BuildServerVersion}!");

            Parallel.ForEach(tfsConfiguration.TFSSettings, (tfsIntegrationSettings) =>
            {
                Logger.Log.Info($"Get all build definitions from team project {tfsIntegrationSettings.TeamProject}.");
                IBuildDefinition[] buildDefinitions = buildServer.QueryBuildDefinitions(tfsIntegrationSettings.TeamProject, QueryOptions.Definitions);

                if (buildDefinitions == null || !buildDefinitions.Any())
                {
                    Logger.Log.Info("No build definitions found! Exiting!");
                    return;
                }

                Logger.Log.Info($"Found {buildDefinitions.Length} build definitions.");
                IBuildDefinition buildDefinition =
                    buildDefinitions.Where(build => build.Name == tfsIntegrationSettings.BuildDefinitionName)
                    .Select(b => b)
                    .FirstOrDefault();

                Logger.Log.Info($"Get build details from {buildDefinition.Name}.");
                IBuildDetail buildDetail = GetLatestBuildDetails(buildServer, buildDefinition,
                                                                 tfsIntegrationSettings.TeamProject);

                if (buildDetail != null)
                {
                    DownloadVantageInstaller(
                        Path.Combine(buildDetail.DropLocation, tfsIntegrationSettings.SourcePathFragment,
                                     tfsIntegrationSettings.SourceFile),
                        Path.Combine(tfsIntegrationSettings.CopyTo, tfsIntegrationSettings.BuildDefinitionName,
                                     buildDetail.BuildNumber, tfsIntegrationSettings.SourceFile));
                }

                Logger.Log.Info("Cleanup old builds");
                CleanUp(Path.Combine(tfsIntegrationSettings.CopyTo, tfsIntegrationSettings.BuildDefinitionName),
                        tfsIntegrationSettings.MaxBuildsToKeep);
                Logger.Log.Info("FINISHED");
            });
        }
Пример #2
0
        protected override void OnStart(string[] args)
        {
            Logger.Log.Info("Running in service mode!");
            Logger.Log.Info("Start");

            aTimer = new System.Timers.Timer(10000);

            // Hook up the Elapsed event for the timer.
            aTimer.Elapsed += this.OnTimedEvent;



            Logger.Log.Info("Thread start");

            integrationImplementation = new TFSIntegrationImplementation();
            Logger.Log.Info($"Application runs from this location: {System.Reflection.Assembly.GetExecutingAssembly().Location}");
            Logger.Log.Info("Read configuration");
            tfsConfiguration = integrationImplementation.ReadTFSConfiguration(Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), TFSIntegrationImplementation.CONFIG_FILE));

            // Set the Interval
            if (tfsConfiguration != null && tfsConfiguration.RepetTaskEveryXSeconds != 0)
            {
                aTimer.Interval = TimeSpan.FromSeconds(tfsConfiguration.RepetTaskEveryXSeconds).TotalMilliseconds;
            }
            else
            {
                aTimer.Interval = 300000;//5 minutes
            }

            Logger.Log.Info($"Downloading will start in {aTimer.Interval} seconds!");
            aTimer.Enabled = true;
            Logger.Log.Info("End onstart");
        }
        public TFSConfiguration ReadTFSConfiguration(string configPath)
        {
            TFSConfiguration config = null;

            using (StreamReader file = File.OpenText(configPath))
            {
                JsonSerializer serializer = new JsonSerializer();
                config = (TFSConfiguration)serializer.Deserialize(file, typeof(TFSConfiguration));
            }

            return(config);
        }