public async Task <IActionResult> ImportProject(Guid organizationId, [FromBody] ProjectImportPostRp projectRp) { if (!ModelState.IsValid) { return(this.BadRequest(ModelState)); } await _projectService.ImportProject(organizationId, projectRp); if (_domainManagerService.HasNotFounds()) { return(this.NotFound(_domainManagerService.GetNotFounds())); } if (_domainManagerService.HasForbidden()) { return(this.Forbidden(_domainManagerService.GetForbidden())); } if (_domainManagerService.HasConflicts()) { return(this.Conflict(_domainManagerService.GetConflicts())); } return(this.Ok(new { ProjectId = await _domainManagerService.GetResult <Guid>("ProjectId") })); }
public async Task ImportProject(Guid organizationId, ProjectImportPostRp 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 }; } 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)); Project newProject = user.ImportProject(organizationId, string.Empty, resource.Name, resource.Description, resource.ProjectType, resource.OrganizationCMSId, resource.OrganizationCPSId, null, resource.AgentPoolId, resource.projectVisibility, organizationCPS.Type, organizationCMS.Type); //SaveChanges in CSM CMSProjectCreateModel projectCreateModel = CMSProjectCreateModel.Factory.Create(organization.Name, resource.Name, resource.Description, resource.projectVisibility); newProject.UpdateExternalInformation(resource.ProjectExternalId, resource.ProjectExternalName); _userRepository.Update(user); await _userRepository.SaveChanges(); await _domainManagerService.AddResult("ProjectId", newProject.ProjectId); //send event var @event = new ProjectImportedEvent(_correlationId) { OrganizationId = organization.OrganizationId, OrganizationCMSId = resource.OrganizationCMSId, ProjectId = newProject.ProjectId, ProjectName = resource.Name, InternalProjectName = newProject.InternalName, ProjectExternalId = resource.ProjectExternalId, ProjectExternalName = resource.ProjectExternalName, BuildDefinitionYML = resource.BuildDefinitionYML, ProjectServiceTemplateId = resource.ProjectServiceTemplateId, 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 = _dataProtectorService.Unprotect(organizationCPS.AccessId), CPSAccessName = _dataProtectorService.Unprotect(organizationCPS.AccessName), CPSAccessSecret = _dataProtectorService.Unprotect(organizationCPS.AccessSecret), CPSAccessAppId = _dataProtectorService.Unprotect(organizationCPS.AccessAppId), CPSAccessAppSecret = _dataProtectorService.Unprotect(organizationCPS.AccessAppSecret), CPSAccessDirectory = _dataProtectorService.Unprotect(organizationCPS.AccessDirectory), UserId = loggedUserId, ProjectRepository = new ProjectRepositoryCreatedEvent { Repositories = resource.Repositories.Select(c => new ProjectRepositoryServiceCreatedEvent { Id = c.Id, Name = c.Name, Link = c.Link, BranchName = c.BranchName, ExternalName = c.ExternalName }).ToList(), } }; await _eventBusService.Publish(queueName : "ProjectImportedEvent", @event : @event); }