Exemple #1
0
        public void IntegrationTestUpdateRetentionPolicies()
        {
            TfsConfigurationServer tfsConfigurationServer = TfsConfigurationServerFactory.GetConfigurationServer(new Uri("myTfsUrl"));

            tfsConfigurationServer.Authenticate();
            TeamProjectDtoCollection allTeamProjectCollections = tfsConfigurationServer.GetAllTeamProjectsInAllTeamProjectCollections();

            foreach (TeamProjectDto teamProjectDto in allTeamProjectCollections)
            {
                TfsTeamProjectCollection teamProjectCollection = tfsConfigurationServer.GetTeamProjectCollection(teamProjectDto.CollectionId);
                IBuildServer             buildServer           = teamProjectCollection.GetService <IBuildServer>();
                BuildServerFacade        buildServerFacade     = new BuildServerFacade(buildServer);
                buildServerFacade.UpdateRetentionPolicies(teamProjectDto.DisplayName, 1, 5, 5, 10, "All");
            }
        }
        protected override void Execute(CodeActivityContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            if (!this.BuildNumberFormat.Get(context).Contains(VersionReplaceToken))
            {
                string message = string.Format(CultureInfo.CurrentCulture, "The 'BuildNumberFormat' value did not contain '{0}' as required (this will be replaced by the version number).", VersionReplaceToken);
                throw new ArgumentException(message);
            }

            IBuildDetail      buildDetail       = context.GetExtension <IBuildDetail>();
            BuildServerFacade buildServerFacade = new BuildServerFacade(buildDetail.BuildServer);
            DateTime          dateOfBuild       = DateTime.Now;

            // e.g. latestBuildNumberFromAllBuildDefinitions = "Acme.PetShop-Trunk-Full-0.0.11114.3"
            string[] latestBuildNumbersFromAllBuildDefinitions = buildServerFacade.GetLatestBuildNumbersFromAllBuildDefinitions(buildDetail.TeamProject, 10);
            Version  nextVersionNumber;

            if (latestBuildNumbersFromAllBuildDefinitions.Length < 1)
            {
                // No previous builds at all so just create a new version from scratch.
                nextVersionNumber = GetFreshVersionNumber(this.MajorVersion.Get(context), this.MinorVersion.Get(context), dateOfBuild);
            }
            else
            {
                Version latestVersionNumber = GetLatestVersionNumberFromPreviousBuildNumbers(latestBuildNumbersFromAllBuildDefinitions);
                if (latestVersionNumber == null)
                {
                    // The previous build number was not found to have a version number contained in it.
                    nextVersionNumber = GetFreshVersionNumber(this.MajorVersion.Get(context), this.MinorVersion.Get(context), dateOfBuild);
                }
                else
                {
                    // We found a version number in the previous build number so increment it.
                    nextVersionNumber = GetNextVersionNumber(latestVersionNumber, this.MajorVersion.Get(context), this.MinorVersion.Get(context), dateOfBuild);
                }
            }

            string nextBuildNumber = string.Format(CultureInfo.CurrentCulture, this.BuildNumberFormat.Get(context), nextVersionNumber);

            this.BuildNumber.Set(context, nextBuildNumber);
            this.VersionNumber.Set(context, nextVersionNumber.ToString());
            buildDetail.BuildNumber = nextBuildNumber;
            buildDetail.Save();
        }
Exemple #3
0
        private static void ListAllTeamBuilds(Uri tfsUrl)
        {
            System.Console.WriteLine("Listing all Team Builds for all Team Projects.");
            BuildDefinitionDtoCollection buildDefinitionDtoCollection = BuildServerFacade.ListTeamBuilds(tfsUrl);
            string currentTeamProject = null;

            foreach (BuildDefinitionDto buildDefinitionDto in buildDefinitionDtoCollection)
            {
                if (buildDefinitionDto.TeamProjectName != currentTeamProject)
                {
                    System.Console.WriteLine();
                    System.Console.WriteLine("Team Project: '{0}'.", buildDefinitionDto.TeamProjectName);
                }

                System.Console.WriteLine("Team Build: '{0}'.", buildDefinitionDto.Name);
                currentTeamProject = buildDefinitionDto.TeamProjectName;
            }

            System.Console.WriteLine();
            System.Console.WriteLine("Done.");
        }
Exemple #4
0
        private static void UpdateRetentionPolicies(Uri tfsUrl, string[] args)
        {
            string teamProjectName                        = string.Empty;
            int    numberOfStoppedBuildsToKeep            = 10;
            int    numberOfFailedBuildsToKeep             = 10;
            int    numberOfPartiallySucceededBuildsToKeep = 10;
            int    numberOfSucceededBuildsToKeep          = 10;
            string deleteOptions = string.Empty;
            bool   force         = false;

            foreach (string arg in args)
            {
                if (arg.StartsWith("/teamProjectName", StringComparison.OrdinalIgnoreCase))
                {
                    teamProjectName = arg.Replace("/teamProjectName:", string.Empty);
                }
                else if (arg.StartsWith("/numberOfStoppedBuildsToKeep", StringComparison.OrdinalIgnoreCase))
                {
                    numberOfStoppedBuildsToKeep = int.Parse(arg.Replace("/numberOfStoppedBuildsToKeep:", string.Empty), CultureInfo.CurrentCulture);
                }
                else if (arg.StartsWith("/numberOfFailedBuildsToKeep", StringComparison.OrdinalIgnoreCase))
                {
                    numberOfFailedBuildsToKeep = int.Parse(arg.Replace("/numberOfFailedBuildsToKeep:", string.Empty), CultureInfo.CurrentCulture);
                }
                else if (arg.StartsWith("/numberOfPartiallySucceededBuildsToKeep", StringComparison.OrdinalIgnoreCase))
                {
                    numberOfPartiallySucceededBuildsToKeep = int.Parse(arg.Replace("/numberOfPartiallySucceededBuildsToKeep:", string.Empty), CultureInfo.CurrentCulture);
                }
                else if (arg.StartsWith("/numberOfSucceededBuildsToKeep", StringComparison.OrdinalIgnoreCase))
                {
                    numberOfSucceededBuildsToKeep = int.Parse(arg.Replace("/numberOfSucceededBuildsToKeep:", string.Empty), CultureInfo.CurrentCulture);
                }
                else if (arg.StartsWith("/deleteOptions", StringComparison.OrdinalIgnoreCase))
                {
                    deleteOptions = arg.Replace("/deleteOptions:", string.Empty);
                }
                else if (arg.StartsWith("/force", StringComparison.OrdinalIgnoreCase))
                {
                    force = bool.Parse(arg.Replace("/force:", string.Empty));
                }
            }

            System.Console.WriteLine("Updating retention policies across all Build Definitions for Team Project '{0}'.", teamProjectName);
            System.Console.WriteLine("Number of stopped builds to keep is '{0}'.", numberOfStoppedBuildsToKeep);
            System.Console.WriteLine("Number of failed builds to keep is '{0}'.", numberOfFailedBuildsToKeep);
            System.Console.WriteLine("Number of partially succeeded builds to keep is '{0}'.", numberOfPartiallySucceededBuildsToKeep);
            System.Console.WriteLine("Number of succeeded builds to keep is '{0}'.", numberOfSucceededBuildsToKeep);
            System.Console.WriteLine("Delete options are '{0}'.", deleteOptions);
            System.Console.WriteLine();
            bool execute = true;

            if (!force)
            {
                System.Console.WriteLine("Are these options correct? (y/n)");
                ConsoleKeyInfo consoleKeyInfo = System.Console.ReadKey();
                if (consoleKeyInfo.KeyChar != 'y')
                {
                    execute = false;
                }

                System.Console.WriteLine();
                System.Console.WriteLine();
            }

            if (execute)
            {
                BuildServerFacade.UpdateRetentionPolicies(tfsUrl, teamProjectName, numberOfStoppedBuildsToKeep, numberOfFailedBuildsToKeep, numberOfPartiallySucceededBuildsToKeep, numberOfSucceededBuildsToKeep, deleteOptions);
                System.Console.WriteLine("Done.");
            }
        }