public async Task CompleteProjectFeature(ProjectFeatureCompletedEvent @event) { var client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", @event.CMSAccountId, @event.CMSAccessSecret)))); client.BaseAddress = new Uri(API_URL); var response = await client.GetAsync($"/{API_VERSION}/teams?role=admin"); var teamResult = await response.MapTo <CMSBitBucketTeamListModel>(); var defaultTeam = teamResult.Teams.FirstOrDefault(c => c.TeamId.Equals(@event.OrganizationExternalId)); foreach (var item in @event.Services) { /*Delete Infrastructure*/ if (@event.DeleteInfrastructure) { CPSAuthModel authModel = new CPSAuthModel(); authModel.AccessId = @event.CPSAccessId; authModel.AccessName = @event.CPSAccessName; authModel.AccessSecret = @event.CPSAccessSecret; authModel.AccessRegion = @event.CPSAccessRegion; authModel.AccessAppId = @event.CPSAccessAppId; authModel.AccessAppSecret = @event.CPSAccessAppSecret; authModel.AccessDirectory = @event.CPSAccessDirectory; string cloudServiceName = $"{@event.OrganizationName}{@event.ProjectName}{item.ServiceName}development{@event.FeatureName}".ToLower(); await _cpsService(@event.CPSType).DeleteService(cloudServiceName, authModel); } var pullRequestModel = new { title = $"feature {@event.FeatureName}", //Description = $"The feature {@event.FeatureName} requests merge operation", source = new { branch = new { name = @event.FeatureName.ToLower() } }, destination = new { branch = new { name = "master" } } }; response = await client.PostAsync($"/{API_VERSION}/repositories/{defaultTeam.UserName}/{item.ServiceExternalId}/pullrequests", new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(pullRequestModel), Encoding.UTF8, "application/json")); response.EnsureSuccessStatusCode(); } }
public async Task CompleteProjectFeature(ProjectFeatureCompletedEvent @event) { string accountUrl = $"https://{@event.CMSAccountName}.visualstudio.com"; foreach (var item in @event.Services) { /*Delete Infrastructure*/ if (@event.DeleteInfrastructure) { CPSAuthModel authModel = new CPSAuthModel(); authModel.AccessId = @event.CPSAccessId; authModel.AccessName = @event.CPSAccessName; authModel.AccessSecret = @event.CPSAccessSecret; authModel.AccessRegion = @event.CPSAccessRegion; authModel.AccessAppId = @event.CPSAccessAppId; authModel.AccessAppSecret = @event.CPSAccessAppSecret; authModel.AccessDirectory = @event.CPSAccessDirectory; string cloudServiceName = $"{@event.OrganizationName}{@event.ProjectName}{item.ServiceName}development{@event.FeatureName}".ToLower(); await _cpsService(@event.CPSType).DeleteService(cloudServiceName, authModel); } HttpClientWrapperAuthorizationModel authCredentials = new HttpClientWrapperAuthorizationModel(); authCredentials.Schema = "Basic"; authCredentials.Value = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", @event.CMSAccessSecret))); CMSVSTSPullRequestCreateModel pullRequestModel = new CMSVSTSPullRequestCreateModel(); pullRequestModel.SourceRefName = $"refs/heads/{@event.FeatureName.ToLower()}"; pullRequestModel.TargetRefName = "refs/heads/master"; pullRequestModel.Title = $"feature {@event.FeatureName}"; pullRequestModel.Description = $"The feature {@event.FeatureName} requests merge operation"; string pullRequestUrl = $"{accountUrl}/{@event.ProjectExternalId}/_apis/git/repositories/{item.ServiceExternalId}/pullrequests?api-version={_vstsOptions.Value.ApiVersion}"; var pullRequestResponse = await _httpClientWrapperService.PostAsync(pullRequestUrl, pullRequestModel, authCredentials); pullRequestResponse.EnsureSuccessStatusCode(); } }
public async Task CompleteProjectFeature(ProjectFeatureCompletedEvent @event) { foreach (var item in @event.Services) { /*Delete Infrastructure*/ if (@event.DeleteInfrastructure) { CPSAuthModel authModel = new CPSAuthModel(); authModel.AccessId = @event.CPSAccessId; authModel.AccessName = @event.CPSAccessName; authModel.AccessSecret = @event.CPSAccessSecret; authModel.AccessRegion = @event.CPSAccessRegion; authModel.AccessAppId = @event.CPSAccessAppId; authModel.AccessAppSecret = @event.CPSAccessAppSecret; authModel.AccessDirectory = @event.CPSAccessDirectory; string cloudServiceName = $"{@event.OrganizationName}{@event.ProjectName}{item.ServiceName}development{@event.FeatureName}".ToLower(); await _cpsService(@event.CPSType).DeleteService(cloudServiceName, authModel); } CMSGitHubPullRequestCreateModel pullRequestModel = new CMSGitHubPullRequestCreateModel(); pullRequestModel.SourceRefName = $"{@event.FeatureName.ToLower()}"; pullRequestModel.TargetRefName = "master"; pullRequestModel.Title = $"feature {@event.FeatureName}"; pullRequestModel.Description = $"The feature {@event.FeatureName} requests merge operation"; HttpClientWrapperAuthorizationModel authCredentials = new HttpClientWrapperAuthorizationModel(); authCredentials.Schema = "Bearer"; authCredentials.Value = @event.CMSAccessToken; string pullRequestUrl = $"https://api.github.com/repos/{@event.CMSAccountName}/{item.ServiceName}/pulls"; var pullRequestResponse = await _httpClientWrapperService.PostAsync(pullRequestUrl, pullRequestModel, authCredentials, headers : Headers); pullRequestResponse.EnsureSuccessStatusCode(); } }
public async Task CompleteProjectFeature(Guid organizationId, Guid projectId, Guid featureId, bool deleteInfrastructure) { string loggedUserId = _identityService.GetUserId(); DomainModels.User user = await _userRepository.GetUser(loggedUserId); DomainModels.Organization organization = user.FindOrganizationById(organizationId); if (organization == null) { await _domainManagerService.AddNotFound($"The organzation with id {organizationId} does not exists."); return; } DomainModels.Project project = user.FindProjectById(projectId); if (project == null) { await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists."); return; } DomainModels.PipelineRole role = user.GetRoleInProject(projectId); if (role != DomainModels.PipelineRole.ProjectAdmin) { await _domainManagerService.AddForbidden($"You are not authorized to complete features in this project."); return; } if (project.Status != DomainModels.EntityStatus.Active) { await _domainManagerService.AddConflict($"The project with id {projectId} must be in status Active to ask for pull request."); return; } DomainModels.ProjectFeature feature = project.GetFeatureById(featureId); if (feature == null) { await _domainManagerService.AddNotFound($"The feature with id {featureId} does not exists."); return; } if (feature.Status != DomainModels.EntityStatus.Active) { await _domainManagerService.AddConflict($"The feature with id {featureId} must be in status Active to to ask for pull request."); return; } //if (feature.IsCompleted()) //{ // await _domainManagerService.AddConflict($"The feature with id {featureId} has already been completed."); // return; //} //services asociated (TODO: services on demand) List <ProjectFeatureServiceCompletedEvent> @events = new List <ProjectFeatureServiceCompletedEvent>(); foreach (var item in feature.Services) { DomainModels.ProjectService projectService = project.GetServiceById(item.ProjectServiceId); if (projectService == null) { await _domainManagerService.AddConflict($"The pipe id {item} does not exists."); return; } @events.Add(new ProjectFeatureServiceCompletedEvent(_correlationId) { ServiceId = item.ProjectServiceId, ServiceExternalId = projectService.ProjectServiceExternalId, ServiceExternalUrl = projectService.ProjectServiceExternalUrl, ServiceName = projectService.Name, ServiceTemplateUrl = projectService.ProjectServiceTemplate.Url, CommitStageId = item.CommitStageId, ReleaseStageId = item.ReleaseStageId }); } user.CompleteProjectFeature(organizationId, projectId, featureId); _userRepository.Update(user); await _userRepository.SaveChanges(); var @event = new ProjectFeatureCompletedEvent(_correlationId) { OrganizationExternalId = project.OrganizationExternalId, OrganizationId = organization.OrganizationId, OrganizationName = organization.Name, ProjectId = project.ProjectId, Services = @events, ProjectExternalId = project.ProjectExternalId, ProjectExternalEndpointId = project.ProjectExternalEndpointId, ProjectVSTSFakeName = project.ProjectVSTSFakeName, ProjectName = project.Name, FeatureId = feature.ProjectFeatureId, FeatureName = feature.Name, CMSType = project.OrganizationCMS.Type, CMSAccountId = _dataProtectorService.Unprotect(project.OrganizationCMS.AccountId), CMSAccountName = _dataProtectorService.Unprotect(project.OrganizationCMS.AccountName), CMSAccessId = _dataProtectorService.Unprotect(project.OrganizationCMS.AccessId), CMSAccessSecret = _dataProtectorService.Unprotect(project.OrganizationCMS.AccessSecret), CMSAccessToken = _dataProtectorService.Unprotect(project.OrganizationCMS.AccessToken), DeleteInfrastructure = deleteInfrastructure }; //Cloud Provider Data @event.CPSType = project.OrganizationCPS.Type; @event.CPSAccessId = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessId); @event.CPSAccessName = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessName); @event.CPSAccessSecret = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessSecret); @event.CPSAccessRegion = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessRegion); @event.CPSAccessAppId = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppId); @event.CPSAccessAppSecret = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppSecret); @event.CPSAccessDirectory = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessDirectory); await _eventBusService.Publish(queueName : "ProjectFeatureCompletedEvent", @event : @event); }