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");
     }
 }
 public static void UpdateRetentionPolicies(Uri tfsUrl, string teamProjectName, int numberOfStoppedBuildsToKeep, int numberOfFailedBuildsToKeep, int numberOfPartiallySucceededBuildsToKeep, int numberOfSucceededBuildsToKeep, string deleteOptions)
 {
     TfsConfigurationServer tfsConfigurationServer = TfsConfigurationServerFactory.GetConfigurationServer(tfsUrl);
     tfsConfigurationServer.Authenticate();
     TeamProjectDtoCollection allTeamProjectCollections = tfsConfigurationServer.GetAllTeamProjectsInAllTeamProjectCollections();
     foreach (TeamProjectDto teamProjectDto in allTeamProjectCollections)
     {
         if (teamProjectDto.DisplayName == teamProjectName)
         {
             TfsTeamProjectCollection teamProjectCollection = tfsConfigurationServer.GetTeamProjectCollection(teamProjectDto.CollectionId);
             IBuildServer buildServer = teamProjectCollection.GetService<IBuildServer>();
             BuildServerFacade buildServerFacade = new BuildServerFacade(buildServer);
             buildServerFacade.UpdateRetentionPolicies(teamProjectDto.DisplayName, numberOfStoppedBuildsToKeep, numberOfFailedBuildsToKeep, numberOfPartiallySucceededBuildsToKeep, numberOfSucceededBuildsToKeep, deleteOptions);
         }
     }
 }
Example #3
0
        public static void UpdateRetentionPolicies(Uri tfsUrl, string teamProjectName, int numberOfStoppedBuildsToKeep, int numberOfFailedBuildsToKeep, int numberOfPartiallySucceededBuildsToKeep, int numberOfSucceededBuildsToKeep, string deleteOptions)
        {
            TfsConfigurationServer tfsConfigurationServer = TfsConfigurationServerFactory.GetConfigurationServer(tfsUrl);

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

            foreach (TeamProjectDto teamProjectDto in allTeamProjectCollections)
            {
                if (teamProjectDto.DisplayName == teamProjectName)
                {
                    TfsTeamProjectCollection teamProjectCollection = tfsConfigurationServer.GetTeamProjectCollection(teamProjectDto.CollectionId);
                    IBuildServer             buildServer           = teamProjectCollection.GetService <IBuildServer>();
                    BuildServerFacade        buildServerFacade     = new BuildServerFacade(buildServer);
                    buildServerFacade.UpdateRetentionPolicies(teamProjectDto.DisplayName, numberOfStoppedBuildsToKeep, numberOfFailedBuildsToKeep, numberOfPartiallySucceededBuildsToKeep, numberOfSucceededBuildsToKeep, deleteOptions);
                }
            }
        }
        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();
        }