private static void ExportDefinition(IBuildDefinition b, string filePath) { ExportedBuildDefinition buildToExport = new ExportedBuildDefinition { Name = b.Name, Description = b.Description }; if (b.BuildController != null) { buildToExport.BuildController = b.BuildController.Name; } buildToExport.ContinuousIntegrationType = b.ContinuousIntegrationType; buildToExport.DefaultDropLocation = b.DefaultDropLocation; buildToExport.Schedules = new List<ExportedISchedule>(); foreach (var schedule in b.Schedules) { buildToExport.Schedules.Add(new ExportedISchedule { StartTime = schedule.StartTime, DaysToBuild = schedule.DaysToBuild, TimeZone = schedule.TimeZone }); } buildToExport.SourceProviders = new List<ExportedIBuildDefinitionSourceProvider>(); foreach (var provider in b.SourceProviders) { buildToExport.SourceProviders.Add(new ExportedIBuildDefinitionSourceProvider { Fields = provider.Fields, Name = provider.Name, SupportedTriggerTypes = provider.SupportedTriggerTypes }); } buildToExport.QueueStatus = b.QueueStatus; buildToExport.ContinuousIntegrationQuietPeriod = b.ContinuousIntegrationQuietPeriod; if (b.SourceProviders.All(s => s.Name != "TFGIT")) { buildToExport.Mappings = new List<ExportedIWorkspaceMapping>(); foreach (var map in b.Workspace.Mappings) { buildToExport.Mappings.Add(new ExportedIWorkspaceMapping { Depth = map.Depth, LocalItem = map.LocalItem, MappingType = map.MappingType, ServerItem = map.ServerItem }); } } buildToExport.RetentionPolicyList = new List<ExportedIRetentionPolicy>(); foreach (var rp in b.RetentionPolicyList) { buildToExport.RetentionPolicyList.Add(new ExportedIRetentionPolicy { BuildDefinition = rp.BuildDefinition, BuildReason = rp.BuildReason, BuildStatus = rp.BuildStatus, NumberToKeep = rp.NumberToKeep, DeleteOptions = rp.DeleteOptions }); } if (b.Process != null) { buildToExport.ProcessTemplate = b.Process.ServerPath; } var processParameters = WorkflowHelpers.DeserializeProcessParameters(b.ProcessParameters); if (processParameters.ContainsKey("AgentSettings")) { if (processParameters["AgentSettings"].GetType() == typeof(AgentSettings)) { AgentSettings ags = (AgentSettings)processParameters["AgentSettings"]; AgentSettingsBuildParameter agentSet = new AgentSettingsBuildParameter(); agentSet.MaxExecutionTime = ags.MaxExecutionTime; agentSet.MaxWaitTime = ags.MaxWaitTime; agentSet.Name = ags.Name; agentSet.Comparison = ags.TagComparison; agentSet.Tags = ags.Tags; buildToExport.TfvcAgentSettings = agentSet; } else if (processParameters["AgentSettings"].GetType() == typeof(BuildParameter)) { BuildParameter ags = (BuildParameter)processParameters["AgentSettings"]; { buildToExport.GitAgentSettings = ags; } } } buildToExport.ProcessParameters = WorkflowHelpers.DeserializeProcessParameters(b.ProcessParameters); File.WriteAllText(Path.Combine(filePath, b.Name + ".json"), JsonConvert.SerializeObject(buildToExport, Formatting.Indented)); }
private void ButtonImport_OnClick(object sender, RoutedEventArgs e) { foreach (var item in this.DataGridBuildsToImport.Items) { BuildImport bi = item as BuildImport; try { if (bi == null) { return; } if (!File.Exists(bi.JsonFile)) { bi.Status = "Failed"; bi.StatusImage = "Graphics/Failed.png"; bi.Message = "File not found"; } else { ExportedBuildDefinition exdef = JsonConvert.DeserializeObject <ExportedBuildDefinition>(File.ReadAllText(bi.JsonFile)); var newBuildDefinition = this.buildServer.CreateBuildDefinition(this.lableTeamProject.Content.ToString()); newBuildDefinition.Name = exdef.Name; newBuildDefinition.Description = exdef.Description; newBuildDefinition.ContinuousIntegrationType = exdef.ContinuousIntegrationType; newBuildDefinition.ContinuousIntegrationQuietPeriod = exdef.ContinuousIntegrationQuietPeriod; newBuildDefinition.QueueStatus = exdef.QueueStatus; if (exdef.SourceProviders.All(s => s.Name != "TFGIT")) { foreach (var mapping in exdef.Mappings) { newBuildDefinition.Workspace.AddMapping(mapping.ServerItem, mapping.LocalItem, mapping.MappingType); } } newBuildDefinition.RetentionPolicyList.Clear(); foreach (var ret in exdef.RetentionPolicyList) { newBuildDefinition.AddRetentionPolicy(ret.BuildReason, ret.BuildStatus, ret.NumberToKeep, ret.DeleteOptions); } foreach (var sp in exdef.SourceProviders) { var provider = newBuildDefinition.CreateInitialSourceProvider(sp.Name); if (exdef.SourceProviders.All(s => s.Name == "TFGIT")) { provider.Fields["RepositoryName"] = sp.Fields["RepositoryName"]; provider.Fields["DefaultBranch"] = sp.Fields["DefaultBranch"]; provider.Fields["CIBranches"] = sp.Fields["CIBranches"]; provider.Fields["RepositoryUrl"] = sp.Fields["RepositoryUrl"]; } newBuildDefinition.SetSourceProvider(provider); } newBuildDefinition.BuildController = this.buildServer.GetBuildController(exdef.BuildController); var x = this.buildServer.QueryProcessTemplates(this.lableTeamProject.Content.ToString()); if (x.All(p => p.ServerPath != exdef.ProcessTemplate)) { bi.Status = "Failed"; bi.StatusImage = "Graphics/Failed.png"; bi.Message = "Process Template not found - " + exdef.ProcessTemplate; continue; } newBuildDefinition.Process = this.buildServer.QueryProcessTemplates(this.lableTeamProject.Content.ToString()).First(p => p.ServerPath == exdef.ProcessTemplate); newBuildDefinition.DefaultDropLocation = exdef.DefaultDropLocation; foreach (var sched in exdef.Schedules) { var newSched = newBuildDefinition.AddSchedule(); newSched.DaysToBuild = sched.DaysToBuild; newSched.StartTime = sched.StartTime; newSched.TimeZone = sched.TimeZone; } var process = WorkflowHelpers.DeserializeProcessParameters(newBuildDefinition.ProcessParameters); foreach (var param in exdef.ProcessParameters) { switch (param.Key) { case "BuildSettings": if (exdef.ProjectsToBuild != null) { process.Add(param.Key, new BuildSettings { ProjectsToBuild = exdef.ProjectsToBuild, PlatformConfigurations = exdef.ConfigurationsToBuild }); } break; case "AgentSettings": if (exdef.TfvcAgentSettings != null) { process.Add(param.Key, (AgentSettings)exdef.TfvcAgentSettings); } else if (exdef.GitAgentSettings != null) { process.Add(param.Key, exdef.GitAgentSettings); } break; case "TestSpecs": if (exdef.AgileTestSpecs != null) { process.Add(param.Key, exdef.AgileTestSpecs.ToSpecList()); } break; default: Newtonsoft.Json.Linq.JArray arrayItem = param.Value as Newtonsoft.Json.Linq.JArray; if (arrayItem == null) { Newtonsoft.Json.Linq.JObject objectItem = param.Value as Newtonsoft.Json.Linq.JObject; if (objectItem == null) { if (param.Key == "CleanWorkspace") { process.Add(param.Key, (CleanWorkspaceOption)Enum.Parse(typeof(CleanWorkspaceOption), param.Value.ToString())); } else if (param.Key == "RunCodeAnalysis") { process.Add(param.Key, (CodeAnalysisOption)Enum.Parse(typeof(CodeAnalysisOption), param.Value.ToString())); } else { process.Add(param.Key, param.Value); } } else { Microsoft.TeamFoundation.Build.Common.BuildParameter paramItem = new Microsoft.TeamFoundation.Build.Common.BuildParameter(param.Value.ToString()); process.Add(param.Key, paramItem); } } else { string[] arrayItemList = new string[arrayItem.Count]; for (int i = 0; i < arrayItem.Count; i++) { arrayItemList[i] = arrayItem[i].ToString(); } process.Add(param.Key, arrayItemList); } break; } } if (exdef.BuildReasons != null) { foreach (var key in exdef.BuildReasons.Keys) { if (process.ContainsKey(key)) { process[key] = exdef.BuildReasons[key]; } } } if (exdef.IntegerParameters != null) { foreach (var key in exdef.IntegerParameters.Keys) { if (process.ContainsKey(key)) { process[key] = exdef.IntegerParameters[key]; } } } if (exdef.BuildVerbosities != null) { foreach (var key in exdef.BuildVerbosities.Keys) { if (process.ContainsKey(key)) { process[key] = exdef.BuildVerbosities[key]; } } } newBuildDefinition.ProcessParameters = WorkflowHelpers.SerializeProcessParameters(process); newBuildDefinition.Save(); bi.Status = "Succeeded"; bi.StatusImage = "Graphics/Succeeded.png"; bi.Message = string.Empty; } } catch (Exception ex) { bi.Status = "Failed"; bi.StatusImage = "Graphics/Failed.png"; bi.Message = ex.Message; } finally { this.DataGridBuildsToImport.ItemsSource = null; this.DataGridBuildsToImport.ItemsSource = this.buildFiles; } } }