Esempio n. 1
0
        public static void BuildDefinition(string collection, string teamProject, string buildController, string buildName, string buildDescription, string user, string password)
        {
            string teamProjectPath = "$/" + teamProject;
            // Get a reference to our Team Foundation Server.
            TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(collection),
                                                                        new System.Net.NetworkCredential(user, password));

            tpc.EnsureAuthenticated();
            VersionControlServer versionControl = tpc.GetService <VersionControlServer>();
            IBuildServer         buildServer    = tpc.GetService <IBuildServer>();

            //Create build definition and give it a name and desription
            IBuildDefinition buildDef = buildServer.CreateBuildDefinition(teamProject);

            buildDef.Name        = buildName;
            buildDef.Description = buildDescription;
            buildDef.ContinuousIntegrationType = ContinuousIntegrationType.Individual; //CI

            //Controller and default build process template
            buildDef.BuildController = buildServer.GetBuildController(buildController);
            var defaultTemplate = buildServer.QueryProcessTemplates(teamProject).First(p => p.TemplateType == ProcessTemplateType.Default);

            buildDef.Process = defaultTemplate;

            buildDef.Workspace.AddMapping(teamProjectPath, "$(SourceDir)", WorkspaceMappingType.Map);
            buildDef.Workspace.AddMapping(teamProjectPath + "/Drops", "$(SourceDir)", WorkspaceMappingType.Cloak);
            //What to build
            string pattern       = "$/*.sln";
            var    lists         = versionControl.GetItems(pattern, VersionSpec.Latest, RecursionType.Full, DeletedState.NonDeleted, ItemType.File);
            var    processParams = new Dictionary <string, string[]>();


            if (lists.Items.Any())
            {
                var list = lists.Items
                           .Select(i => i.ServerItem)
                           .ToList();
                processParams.Add("ProjectsToBuild", list.ToArray());
            }
            processParams.Add("ConfigurationsToBuild", new[] { "Any CPU|Debug" });

            buildDef.ProcessParameters = SerializeParams(processParams);

            buildDef.RetentionPolicyList.Clear();
            buildDef.AddRetentionPolicy(BuildReason.Triggered, BuildStatus.Succeeded, 10, DeleteOptions.All);
            buildDef.AddRetentionPolicy(BuildReason.Triggered, BuildStatus.Failed, 10, DeleteOptions.All);
            buildDef.AddRetentionPolicy(BuildReason.Triggered, BuildStatus.Stopped, 1, DeleteOptions.All);
            buildDef.AddRetentionPolicy(BuildReason.Triggered, BuildStatus.PartiallySucceeded, 10, DeleteOptions.All);

            //Lets save it
            buildDef.Save();
        }
        private IBuildDefinition CreateBuildDefinition(IBuildServer buildServer, string buildController, string project, string dropLocation, string buildDefinition)
        {
            var controller = GetBuildController(buildServer, buildController);

            // Get the Upgrade template to use as the process template
            var processTemplate = buildServer.QueryProcessTemplates(project, new[] { ProcessTemplateType.Upgrade })[0];

            var definition = buildServer.CreateBuildDefinition(project);
            definition.Name = buildDefinition;
            definition.ContinuousIntegrationType = ContinuousIntegrationType.None;
            definition.BuildController = controller;
            definition.DefaultDropLocation = dropLocation;
            definition.Description = "Fake build definition used to create fake builds.";
            definition.QueueStatus = DefinitionQueueStatus.Enabled;
            definition.Workspace.AddMapping("$/", "c:\\fake", WorkspaceMappingType.Map);
            definition.Process = processTemplate;
            definition.Save();

            return definition;
        }
Esempio n. 3
0
        private IBuildDefinition CreateBuildDefinition(IBuildServer buildServer, string buildController, string project, string dropLocation, string buildDefinition)
        {
            var controller = GetBuildController(buildServer, buildController);

            // Get the Upgrade template to use as the process template
            var processTemplate = buildServer.QueryProcessTemplates(project, new[] { ProcessTemplateType.Upgrade })[0];

            var definition = buildServer.CreateBuildDefinition(project);

            definition.Name = buildDefinition;
            definition.ContinuousIntegrationType = ContinuousIntegrationType.None;
            definition.BuildController           = controller;
            definition.DefaultDropLocation       = dropLocation;
            definition.Description = "Fake build definition used to create fake builds.";
            definition.QueueStatus = DefinitionQueueStatus.Enabled;
            definition.Workspace.AddMapping("$/", "c:\\fake", WorkspaceMappingType.Map);
            definition.Process = processTemplate;
            definition.Save();

            return(definition);
        }
Esempio n. 4
0
        internal static void Add_BuildProcessTemplates(XlHlp.XlLocation insertAt,
                                                       Options_AZDO_TFS options,
                                                       IBuildServer buildServer,
                                                       TeamProject teamProject)
        {
            Int64 startTicks = Log.APPLICATION("Enter", Common.LOG_CATEGORY);

            var processTemplates = buildServer.QueryProcessTemplates(teamProject.Name);

            foreach (IProcessTemplate processTemplate in processTemplates)
            {
                insertAt.ClearOffsets();

                XlHlp.AddOffsetContentToCell(insertAt.AddOffsetColumn(), string.Format("{0}", processTemplate.Id));
                XlHlp.AddOffsetContentToCell(insertAt.AddOffsetColumn(), string.Format("{0}", processTemplate.Description));
                XlHlp.AddOffsetContentToCell(insertAt.AddOffsetColumn(), string.Format("{0}", processTemplate.TemplateType));
                XlHlp.AddOffsetContentToCell(insertAt.AddOffsetColumn(), string.Format("{0}", processTemplate.Version));

                insertAt.IncrementRows();
            }

            Log.APPLICATION("Exit", Common.LOG_CATEGORY, startTicks);
        }
Esempio n. 5
0
        public static IList <BuildProcessTemplateInfo> GetBuildProcessTemplates(ApplicationTask task, IBuildServer buildServer, IEnumerable <string> teamProjectNames)
        {
            var processTemplates = new List <BuildProcessTemplateInfo>();
            var step             = 0;

            foreach (var teamProjectName in teamProjectNames)
            {
                task.SetProgress(step++, string.Format(CultureInfo.CurrentCulture, "Processing Team Project \"{0}\"", teamProjectName));
                try
                {
                    var teamProjectBuildDefinitions = buildServer.QueryBuildDefinitions(teamProjectName, QueryOptions.Process);

                    foreach (var processTemplate in buildServer.QueryProcessTemplates(teamProjectName))
                    {
                        var processTemplateBuildDefinitions = new List <IBuildDefinition>();
                        foreach (var teamProjectBuildDefinition in teamProjectBuildDefinitions)
                        {
                            if (teamProjectBuildDefinition.Process != null && BuildProcessTemplateInfo.AreEquivalent(teamProjectBuildDefinition.Process, processTemplate))
                            {
                                processTemplateBuildDefinitions.Add(teamProjectBuildDefinition);
                            }
                        }
                        processTemplates.Add(new BuildProcessTemplateInfo(processTemplate, processTemplateBuildDefinitions));
                    }
                }
                catch (Exception exc)
                {
                    task.SetWarning(string.Format(CultureInfo.CurrentCulture, "An error occurred while processing Team Project \"{0}\"", teamProjectName), exc);
                }
                if (task.IsCanceled)
                {
                    task.Status = "Canceled";
                    break;
                }
            }
            return(processTemplates);
        }
Esempio n. 6
0
        public static void RegisterBuildProcessTemplate(ApplicationTask task, IBuildServer buildServer, IEnumerable <string> teamProjectNames, string templateServerPath, ProcessTemplateType templateType, bool registerIfTemplateDoesNotExist, bool unregisterAllOtherTemplates, bool unregisterAllOtherTemplatesIncludesUpgradeTemplate, bool simulate)
        {
            var step = 0;

            foreach (var teamProjectName in teamProjectNames)
            {
                try
                {
                    task.SetProgress(step++, string.Format(CultureInfo.CurrentCulture, "Processing Team Project \"{0}\"", teamProjectName));

                    var allTemplates      = buildServer.QueryProcessTemplates(teamProjectName);
                    var matchingTemplates = allTemplates.Where(t => t.ServerPath.Equals(templateServerPath, StringComparison.OrdinalIgnoreCase)).ToList();

                    if (unregisterAllOtherTemplates)
                    {
                        var templatesToUnregister = allTemplates.Except(matchingTemplates);
                        if (!unregisterAllOtherTemplatesIncludesUpgradeTemplate)
                        {
                            templatesToUnregister = templatesToUnregister.Where(t => t.TemplateType != ProcessTemplateType.Upgrade);
                        }
                        foreach (var templateToUnregister in templatesToUnregister)
                        {
                            task.Status = string.Format(CultureInfo.CurrentCulture, "Unregistering existing build process template \"{0}\"", templateToUnregister.ServerPath);
                            var buildDefinitions = buildServer.QueryBuildDefinitions(teamProjectName, QueryOptions.Process);
                            foreach (var buildDefinition in buildDefinitions)
                            {
                                if (buildDefinition.Process != null && BuildProcessTemplateInfo.AreEquivalent(buildDefinition.Process, templateToUnregister))
                                {
                                    task.SetWarning(string.Format(CultureInfo.CurrentCulture, "WARNING - The build \"{0}\" uses the build process template \"{1}\" that is being unregistered", buildDefinition.Name, templateToUnregister.ServerPath));
                                }
                            }
                            if (!simulate)
                            {
                                templateToUnregister.Delete();
                            }
                        }
                    }
                    if (!(unregisterAllOtherTemplates && unregisterAllOtherTemplatesIncludesUpgradeTemplate))
                    {
                        if (templateType == ProcessTemplateType.Default || templateType == ProcessTemplateType.Upgrade)
                        {
                            // There can be only one upgrade or default template for a team project.
                            // Make sure there isn't already a template with that type.
                            foreach (var template in allTemplates.Except(matchingTemplates).Where(t => t.TemplateType == templateType))
                            {
                                task.Status = string.Format(CultureInfo.CurrentCulture, "Changing type of existing build process template \"{0}\" from \"{1}\" to \"{2}\"", template.ServerPath, template.TemplateType.ToString(), ProcessTemplateType.Custom.ToString());
                                if (!simulate)
                                {
                                    template.TemplateType = ProcessTemplateType.Custom;
                                    template.Save();
                                }
                            }
                        }
                    }

                    if (registerIfTemplateDoesNotExist && !matchingTemplates.Any())
                    {
                        task.Status = string.Format(CultureInfo.CurrentCulture, "Registering new build process template \"{0}\" as type \"{1}\"", templateServerPath, templateType.ToString());
                        if (!simulate)
                        {
                            var template = buildServer.CreateProcessTemplate(teamProjectName, templateServerPath);
                            template.TemplateType = templateType;
                            template.Save();
                        }
                    }
                    else
                    {
                        foreach (var template in matchingTemplates.Where(t => t.TemplateType != templateType))
                        {
                            task.Status = string.Format(CultureInfo.CurrentCulture, "Changing type of existing build process template \"{0}\" from \"{1}\" to \"{2}\"", template.ServerPath, template.TemplateType.ToString(), templateType.ToString());
                            if (!simulate)
                            {
                                template.TemplateType = templateType;
                                template.Save();
                            }
                        }
                    }
                }
                catch (Exception exc)
                {
                    task.SetError(string.Format(CultureInfo.CurrentCulture, "An error occurred while registering the build process template \"{0}\" for Team Project \"{1}\"", templateServerPath, teamProjectName), exc);
                }
                if (task.IsCanceled)
                {
                    task.Status = "Canceled";
                    break;
                }
            }
        }