public async Task AddTags(BuildDefinition definition, CustomBuildDefinitionPayload payload) { var client = await _client.GetBuildClientAsync(); await client.AddDefinitionTagsAsync(payload.Tags, payload.Project, definition.Id); }
public async Task UpdateTags(BuildDefinition definition, CustomBuildDefinitionPayload payload) { var client = await _client.GetBuildClientAsync(); var tags = await client.GetDefinitionTagsAsync(payload.Project, definition.Id); foreach (var tag in tags) { await client.DeleteDefinitionTagAsync(payload.Project, definition.Id, tag); } await client.AddDefinitionTagsAsync(payload.Tags, payload.Project, definition.Id); }
public async Task AddVariableGroups(BuildDefinition definition, CustomBuildDefinitionPayload payload) { if (payload.VariableGroups == null || payload.VariableGroups.Length == 0) { return; } var client = await _client.GetTaskAgentAsync(); foreach (var variableGroup in payload.VariableGroups) { var groups = await client.GetVariableGroupsAsync(project : payload.Project, groupName : variableGroup); if (groups == null || !groups.Any()) { throw new Exception($"Could not find any variablegroup with name: {variableGroup}"); } if (groups.Count() > 1) { throw new Exception($"There are more thant one variable group in the same project ({payload.Project}) with the same : {variableGroup}"); } var group = groups.FirstOrDefault(); if (group != null) { var g = new VariableGroup { Name = group.Name, Description = group.Description, Id = group.Id, Type = group.Type }; foreach (var(key, value) in group.Variables) { g.Variables[key] = new BuildDefinitionVariable { Value = value.Value, IsSecret = value.IsSecret }; } definition.VariableGroups.Add(g); } } }
public async Task <string> PostAsync( CustomBuildDefinitionPayload payload) { try { var client = await _azureClient.GetBuildClientAsync(); var definition = new BuildDefinition { Name = $"{payload.ApplicationName}-{payload.BuildTemplate}-{payload.Branch}", Path = payload.Path, Repository = new BuildRepository { DefaultBranch = payload.Branch, Name = payload.Repository, Type = "TfsGit", }, Queue = new AgentPoolQueue { Name = payload.QueuePool, Pool = new TaskAgentPoolReference { Name = payload.QueuePool } } }; await _variableGroupService.AddVariableGroups(definition, payload); await _taskGroupService.AddTaskGroupSteps(definition, payload); _variableService.AddVariables(definition, payload); _triggersService.AddTriggers(definition, payload); var result = await client .CreateDefinitionAsync(definition, payload.Project); await _tagsService.AddTags(result, payload); return($"{payload.Project}@@@{result.Id.ToString()}"); } catch (Exception e) { throw new Exception( "Error: Something went wrong when calling the AzureDevOps API", e); } }
public void AddVariables(BuildDefinition definition, CustomBuildDefinitionPayload payload) { if (payload.Variables == null) { return; } foreach (var(key, value) in payload.Variables) { definition.Variables[key] = new BuildDefinitionVariable { Value = value, IsSecret = false, }; } }
public async Task <ActionResult> Put( string id, [FromBody] CustomBuildDefinitionPayload payload) { try { await _buildDefinitionService. PutAsync(id, payload); return(NoContent()); } catch (Exception e) { return(StatusCode( StatusCodes.Status500InternalServerError, e)); } }
public async Task AddTaskGroupSteps( BuildDefinition definition, CustomBuildDefinitionPayload payload) { var client = await _azureClient.GetTaskAgentAsync(); var taskGroups = await client.GetTaskGroupsAsync(ProvisioningProject); var amount = taskGroups.Count(x => x.Name.Equals(payload.BuildTemplate, StringComparison.CurrentCultureIgnoreCase)); if (amount > 1) { throw new Exception($"There are more than one TaskGroup with the name {payload.BuildTemplate} in the provisioning repository"); } if (amount == 0) { throw new Exception($"There is no TaskGroup with the name {payload.BuildTemplate} in the provisioning repository"); } var tg = taskGroups.FirstOrDefault(x => x.Name.Equals(payload.BuildTemplate, StringComparison.InvariantCultureIgnoreCase)); if (tg != null) { definition.Process = new DesignerProcess { Phases = { new Phase { Name = "Agent Job 1", RefName = "Job_1", Condition = "succeeded()", Steps = AddTasks(tg) } } }; definition.Properties.Add("tg_name", payload.BuildTemplate); } }
public void AddTriggers( BuildDefinition definition, CustomBuildDefinitionPayload payload) { if (payload.CITriggers != null) { foreach (var trg in payload.CITriggers) { var trigger = new ContinuousIntegrationTrigger(); trigger.BranchFilters.AddRange(trg.BranchFilter); trigger.PathFilters.AddRange(trg.PathFilter); trigger.SettingsSourceType = 1; trigger.BatchChanges = false; trigger.MaxConcurrentBuildsPerBranch = 1; trigger.PollingInterval = 0; definition.Triggers.Add(trigger); } } if (payload.ScheduleTriggers != null) { var schTriggers = new ScheduleTrigger { Schedules = new List <Schedule>() }; foreach (var trg in payload.ScheduleTriggers) { var sch = new Schedule(); sch.BranchFilters.AddRange(trg.BranchFilter); sch.StartHours = trg.Hours; sch.StartMinutes = trg.Minutes; sch.TimeZoneId = trg.TimeZone; sch.DaysToBuild = GetScheduleDay(trg.DayOfTheWeek); schTriggers.Schedules.Add(sch); } definition.Triggers.Add(schTriggers); } }
public async Task GetTaskGroup(BuildDefinition definition, CustomBuildDefinitionPayload payload) { var buildClient = await _azureClient.GetBuildClientAsync(); var tgClient = await _azureClient.GetTaskAgentAsync(); var taskGroups = await tgClient.GetTaskGroupsAsync(ProvisioningProject); var props = await buildClient.GetDefinitionPropertiesAsync(project : payload.Project, definition.Id); if (props == null || !props.ContainsKey("tg_name")) { throw new Exception("Build definition does not have the metadata necessary"); } var tgName = props["tg_name"] as string; var amount = taskGroups.Count(x => x.Name.Equals(tgName, StringComparison.CurrentCultureIgnoreCase)); if (amount > 1) { throw new Exception($"There are more than one TaskGroup with the name {tgName} in the provisioning repository"); } if (amount == 0) { throw new Exception($"There is no TaskGroup with the name {tgName} in the provisioning repository"); } var tg = taskGroups.FirstOrDefault(x => x.Name.Equals(tgName, StringComparison.InvariantCultureIgnoreCase)); if (tg != null) { payload.BuildTemplate = tg.Name; payload.TaskGroupRevision = tg.Revision.ToString(); } }
public async Task <ActionResult <CustomBuildDefinitionId> > Post( string project, [FromBody] CustomBuildDefinitionPayload payload) { try { var id = await _buildDefinitionService. PostAsync(payload); return(new CustomBuildDefinitionId { Id = id }); } catch (Exception e) { return(StatusCode( StatusCodes.Status500InternalServerError, e)); } }
public async Task <CustomBuildDefinitionPayload> GetAsync( string id) { try { var project = getProject(id); var buildId = getAdosId(id); var client = await _azureClient.GetBuildClientAsync(); var definitions = await client .GetFullDefinitionsAsync(project : project, definitionIds : new List <int> { Convert.ToInt32(buildId) }); if (definitions.Count > 1) { throw new Exception("Error: Found more thant one build with the same id"); } if (definitions.Count == 0) { throw new NotFoundException($"Error: Build wit ID: {buildId} not found"); } var result = definitions.FirstOrDefault(); if (result != null) { var appName = result.Name.Split('-'); var name = appName.Length == 0 ? $"Modified+{id}" : appName[0]; var definition = new CustomBuildDefinitionPayload { ApplicationName = name, Branch = result.Repository.DefaultBranch, Repository = result.Repository.Name, QueuePool = result.Queue.Name, BuildRevision = result.Revision.ToString(), Path = result.Path, Project = result.Project.Name, Tags = result.Tags.ToArray(), VariableGroups = result.VariableGroups.Select(x => x.Name).ToArray(), Variables = _variableService.GetVariables(result), CITriggers = new List <CITriggers> { _triggersService.GetCITriggers(result) }, ScheduleTriggers = _triggersService.GetScheduleTriggers(result) }; await _taskGroupService.GetTaskGroup(result, definition); return(definition); } return(null); } catch (NotFoundException) { throw; } catch (Exception e) { throw new Exception("Error: Something went wrong when calling the AzureDevOps API", e); } }
public async Task PutAsync( string id, CustomBuildDefinitionPayload payload) { try { var project = getProject(id); var buildId = getAdosId(id); var client = await _azureClient.GetBuildClientAsync(); var definitions = await client .GetFullDefinitionsAsync(project : project, definitionIds : new List <int> { Convert.ToInt32(buildId) }); if (definitions is null || definitions.Count > 1 || definitions.Count == 0) { throw new Exception("Error: Cannot have more than 1 build with the same name on the same project"); } var df = definitions.FirstOrDefault(); var definition = new BuildDefinition { Id = Convert.ToInt32(buildId), Revision = Convert.ToInt32(payload.BuildRevision), Name = $"{ payload.ApplicationName }-{ payload.BuildTemplate }-{ payload.Branch }", Path = payload.Path, Repository = new BuildRepository { DefaultBranch = payload.Branch, Name = payload.Repository, Type = "TfsGit", }, Queue = new AgentPoolQueue { Name = payload.QueuePool, Pool = new TaskAgentPoolReference { Name = payload.QueuePool } }, }; await _tagsService.UpdateTags(df, payload); await _variableGroupService.AddVariableGroups(definition, payload); _variableService.AddVariables(definition, payload); await _taskGroupService.AddTaskGroupSteps(definition, payload); _triggersService.AddTriggers(definition, payload); await client.UpdateDefinitionAsync( definition : definition, project : project, definitionId : definition.Id); } catch (Exception e) { throw new Exception( "Error: Something went wrong when calling the AzureDevOps API", e); } }