public async Task <CMSProjectAvailabilityResultModel> ValidateProjectAvailability(CMSAuthCredentialModel authCredential, string organization, string name) { CMSProjectAvailabilityResultModel result = new CMSProjectAvailabilityResultModel(); var response = await _httpProxyService.GetAsync($"/_apis/projects?api-version={_vstsOptions.Value.ApiVersion}", authCredential); if (!response.IsSuccessStatusCode || response.StatusCode == System.Net.HttpStatusCode.NonAuthoritativeInformation) { if (response.StatusCode == System.Net.HttpStatusCode.NonAuthoritativeInformation) { result.Fail($"Code: {response.StatusCode}, Reason: The credentials are not correct"); return(result); } result.Fail($"Code: {response.StatusCode}, Reason: {await response.Content.ReadAsStringAsync()}"); return(result); } var projectResult = await response.MapTo <CMSVSTSProjectListModel>(); var existsProject = projectResult.Items.Any(x => x.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)); if (existsProject) { result.Fail($"The project {name} has already been taken in the CMS service"); } return(result); }
public async Task <CMSProjectAvailabilityResultModel> ValidateProjectAvailability(CMSAuthCredentialModel authCredential, string organization, string name) { CMSProjectAvailabilityResultModel result = new CMSProjectAvailabilityResultModel(); var dic = new Dictionary <string, string>(); dic.Add("Accept", "application/vnd.github.inertia-preview+json"); dic.Add("User-Agent", API_UserAgent); var accountList = await GetAccounts(authCredential); var defaultTeam = accountList.Items.FirstOrDefault(c => c.AccountId.Equals(authCredential.AccountId)); if (defaultTeam.IsOrganization) { var response = await _httpProxyService.GetAsync($"/orgs/{defaultTeam.Name}/projects", authCredential, dic); var projectResult = await response.MapTo <List <CMSGitHubProjectModel> >(); var existsProject = projectResult.Any(x => x.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)); if (existsProject) { result.Fail($"The project {name} has already been taken in the CMS service"); } } return(result); }
public async Task CreateVSTSFakeProject(Guid organizationId, Guid projectId) { var project = await _projectRepository.GetProjectWithOrgAndAccountOwnerByProjectId(organizationId, projectId); if (project == null) { await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists."); return; } var cmsType = ConfigurationManagementService.VSTS; var projectName = this._slugService.GetSlug($"{project.Organization.Owner.Email} {project.Organization.Name} {project.Name}"); var resource = new { Name = projectName, Description = project.Description }; CMSAuthCredentialModel cmsAuthCredential = this._cmsCredentialService(cmsType).GetToken(_vstsFakeOptions.Value.AccountId, _vstsFakeOptions.Value.AccountId, _vstsFakeOptions.Value.AccessSecret); CMSProjectAvailabilityResultModel cmsProjectAvailability = await _cmsService(cmsType).ValidateProjectAvailability(cmsAuthCredential, string.Empty, resource.Name); if (!cmsProjectAvailability.Success) { await _domainManagerService.AddConflict($"The CMS data is not valid. {cmsProjectAvailability.GetReasonForNoSuccess()}"); return; } CMSProjectCreateModel projectCreateModel = CMSProjectCreateModel.Factory.Create(project.Organization.Name, resource.Name, resource.Description, ProjectVisibility.Private); CMSProjectCreateResultModel cmsProjectCreate = await _cmsService(cmsType).CreateProject(cmsAuthCredential, projectCreateModel); if (!cmsProjectCreate.Success) { await _domainManagerService.AddConflict($"The CMS data is not valid. {cmsProjectCreate.GetReasonForNoSuccess()}"); return; } project.SetFakeVSTSProject(projectName); this._projectRepository.Update(project); await this._projectRepository.SaveChanges(); }
public async Task <CMSProjectAvailabilityResultModel> ValidateProjectAvailability(CMSAuthCredentialModel authCredential, string organization, string name) { CMSProjectAvailabilityResultModel result = new CMSProjectAvailabilityResultModel(); var client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authCredential.AccessToken); client.BaseAddress = new Uri(authCredential.Url); var teamResult = await GetAccountTeams(client, authCredential); var defaultTeam = teamResult.Teams.FirstOrDefault(c => c.TeamId.Equals(organization)); if (defaultTeam.IsTeam) { var response = await client.GetAsync($"/{API_VERSION}/teams/{defaultTeam.UserName}/projects/"); var projectResult = await response.MapTo <CMSBitBucketProjectListModel>(); var existsProject = projectResult.Projects.Any(x => x.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)); if (existsProject) { result.Fail($"The project {name} has already been taken in the CMS service"); } return(result); } var responseUser = await client.GetAsync($"/{API_VERSION}/users/{defaultTeam.UserName}/projects/"); //var projectResult = await response.MapTo<CMSBitBucketProjectListModel>(); //var existsProject = projectResult.Projects.Any(x => x.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)); //if (existsProject) //{ // result.Fail($"The project {name} has already been taken in the CMS service"); //} //return result; return(new CMSProjectAvailabilityResultModel { }); }
public async Task <CMSProjectAvailabilityResultModel> ValidateProjectAvailability(CMSAuthCredentialModel authCredential, string organization, string name) { CMSProjectAvailabilityResultModel result = new CMSProjectAvailabilityResultModel(); var httpClient = new HttpClient(); var request = new HttpRequestMessage(HttpMethod.Get, $"{authCredential.Url}/groups?owned=true"); request.Headers.Add("Private-Token", authCredential.AccessToken); var response = await httpClient.SendAsync(request); var data = response.Content.ReadAsStringAsync().Result; var projectResult = await response.MapTo <List <CMSGitLabTeamModel> >(); var existsProject = projectResult.Any(x => x.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)); if (existsProject) { result.Fail($"The project {name} has already been taken in the {authCredential.Provider} service"); } return(result); }
public async Task CreateProject(Guid organizationId, ProjectPostRp resource) { string loggedUserId = _identityService.GetUserId(); User user = await _userRepository.GetUser(loggedUserId); Organization organization = user.FindOrganizationById(organizationId); if (organization == null) { await _domainManagerService.AddNotFound($"The organzation with id {organizationId} does not exists."); return; } OrganizationCMS organizationCMS = organization.GetConfigurationManagementServiceById(resource.OrganizationCMSId); if (organizationCMS == null) { await _domainManagerService.AddNotFound($"The configuration management service with id {resource.OrganizationCMSId} does not exists."); return; } if (organizationCMS.Type == ConfigurationManagementService.VSTS && resource.projectVisibility == ProjectVisibility.None) { await _domainManagerService.AddConflict($"The project visibility should be Private or Public."); return; } OrganizationCPS organizationCPS = null; if (resource.OrganizationCPSId.HasValue) { organizationCPS = organization.GetCloudProviderServiceById(resource.OrganizationCPSId.Value); if (organizationCPS == null) { await _domainManagerService.AddNotFound($"The cloud provider service with id {resource.OrganizationCPSId} does not exists."); return; } } else { organizationCPS = new OrganizationCPS { Type = CloudProviderService.None }; } ProjectTemplate projectTemplate = null; if (resource.ProjectTemplateId.HasValue) { projectTemplate = await _projectTemplateRepository.GetProjectTemplateById(resource.ProjectTemplateId.Value); if (projectTemplate == null) { await _domainManagerService.AddNotFound($"The project template with id {resource.ProjectTemplateId.Value} does not exists."); return; } } Project existingProject = organization.GetProjectByName(resource.Name); if (existingProject != null) { await _domainManagerService.AddConflict($"The project name {resource.Name} has already been taken."); return; } //Auth CMSAuthCredentialModel cmsAuthCredential = this._cmsCredentialService(organizationCMS.Type).GetToken( _dataProtectorService.Unprotect(organizationCMS.AccountId), _dataProtectorService.Unprotect(organizationCMS.AccountName), _dataProtectorService.Unprotect(organizationCMS.AccessSecret), _dataProtectorService.Unprotect(organizationCMS.AccessToken)); CMSProjectAvailabilityResultModel cmsProjectAvailability = await _cmsService(organizationCMS.Type).ValidateProjectAvailability(cmsAuthCredential, resource.TeamId, resource.Name); if (!cmsProjectAvailability.Success) { await _domainManagerService.AddConflict($"The CMS data is not valid. {cmsProjectAvailability.GetReasonForNoSuccess()}"); return; } Project newProject = user.CreateProject(organizationId, resource.TeamId, resource.Name, resource.Description, resource.ProjectType, resource.OrganizationCMSId, resource.OrganizationCPSId, resource.ProjectTemplateId, resource.AgentPoolId, resource.projectVisibility, organizationCPS.Type, organizationCMS.Type); //SaveChanges in CSM CMSProjectCreateModel projectCreateModel = CMSProjectCreateModel.Factory.Create(organization.Name, resource.Name, resource.Description, resource.projectVisibility); projectCreateModel.TeamId = resource.TeamId; CMSProjectCreateResultModel cmsProjectCreate = await _cmsService(organizationCMS.Type).CreateProject(cmsAuthCredential, projectCreateModel); if (!cmsProjectCreate.Success) { await _domainManagerService.AddConflict($"The CMS data is not valid. {cmsProjectCreate.GetReasonForNoSuccess()}"); return; } newProject.UpdateExternalInformation(cmsProjectCreate.ProjectExternalId, resource.Name); _userRepository.Update(user); await _userRepository.SaveChanges(); await _domainManagerService.AddResult("ProjectId", newProject.ProjectId); //send event var @event = new ProjectCreatedEvent(_correlationId) { OrganizationId = organization.OrganizationId, ProjectId = newProject.ProjectId, ProjectName = newProject.Name, InternalProjectName = newProject.InternalName, ProjectVSTSFake = this._slugService.GetSlug($"{organization.Owner.Email} {organization.Name} {newProject.Name}"), AgentPoolId = newProject.AgentPoolId, CMSType = organizationCMS.Type, CMSAccountId = _dataProtectorService.Unprotect(organizationCMS.AccountId), CMSAccountName = _dataProtectorService.Unprotect(organizationCMS.AccountName), CMSAccessId = _dataProtectorService.Unprotect(organizationCMS.AccessId), CMSAccessSecret = _dataProtectorService.Unprotect(organizationCMS.AccessSecret), CMSAccessToken = _dataProtectorService.Unprotect(organizationCMS.AccessToken), CPSType = organizationCPS.Type, CPSAccessId = organizationCPS.Type != CloudProviderService.None ? _dataProtectorService.Unprotect(organizationCPS.AccessId) : string.Empty, CPSAccessName = organizationCPS.Type != CloudProviderService.None ? _dataProtectorService.Unprotect(organizationCPS.AccessName) : string.Empty, CPSAccessSecret = organizationCPS.Type != CloudProviderService.None ? _dataProtectorService.Unprotect(organizationCPS.AccessSecret) : string.Empty, CPSAccessAppId = organizationCPS.Type != CloudProviderService.None ? _dataProtectorService.Unprotect(organizationCPS.AccessAppId) : string.Empty, CPSAccessAppSecret = organizationCPS.Type != CloudProviderService.None ? _dataProtectorService.Unprotect(organizationCPS.AccessAppSecret) : string.Empty, CPSAccessDirectory = organizationCPS.Type != CloudProviderService.None ? _dataProtectorService.Unprotect(organizationCPS.AccessDirectory) : string.Empty, UserId = loggedUserId }; if (resource.ProjectTemplateId.HasValue) { @event.ProjectTemplate = new ProjectTemplateCreatedEvent(); @event.ProjectTemplate.Services = projectTemplate.Services.Select(x => new ProjectTemplateServiceCreatedEvent() { Name = x.Name, ProjectServiceTemplateId = x.ProjectServiceTemplateId }).ToList(); } await _eventBusService.Publish(queueName : "ProjectCreatedEvent", @event : @event); }