public ProjectCloudCredentialModel ProjectCredentialResolver(OrganizationCMS organizationCMS, Project project)
        {
            var accountId         = organizationCMS.Type == ConfigurationManagementService.VSTS ? _dataProtectorService.Unprotect(organizationCMS.AccountId) : this._fakeAccountOptions.Value.AccountId;
            var accountName       = organizationCMS.Type == ConfigurationManagementService.VSTS ? _dataProtectorService.Unprotect(organizationCMS.AccountName) : _fakeAccountOptions.Value.AccountId;
            var accessSecret      = organizationCMS.Type == ConfigurationManagementService.VSTS ? _dataProtectorService.Unprotect(organizationCMS.AccessSecret) : _fakeAccountOptions.Value.AccessSecret;
            var accountProjectId  = organizationCMS.Type == ConfigurationManagementService.VSTS ? project.Name : project.ProjectVSTSFakeName;
            var projectName       = organizationCMS.Type == ConfigurationManagementService.VSTS ? project.Name : project.ProjectVSTSFakeName;
            var projectExternalId = organizationCMS.Type == ConfigurationManagementService.VSTS ? project.ProjectExternalId : project.ProjectVSTSFakeId;

            var accessToken = organizationCMS.Type == ConfigurationManagementService.VSTS ? _dataProtectorService.Unprotect(organizationCMS.AccessToken) : this._fakeAccountOptions.Value.AccessSecret;
            var accessId    = organizationCMS.Type == ConfigurationManagementService.VSTS ? _dataProtectorService.Unprotect(organizationCMS.AccessId) : this._fakeAccountOptions.Value.AccessId;
            var cmsType     = organizationCMS.Type == ConfigurationManagementService.VSTS ? organizationCMS.Type : ConfigurationManagementService.VSTS;
            CMSAuthCredentialModel cmsAuthCredential = this._cmsCredentialService(cmsType).GetToken(accountId,
                                                                                                    accountName,
                                                                                                    accessSecret,
                                                                                                    accessToken);

            return(new ProjectCloudCredentialModel {
                AccessId = accessId,
                AccountId = accountId,
                AccessToken = accessToken,
                AccountName = accountName,
                AccessSecret = accessSecret,
                AccountProjectId = accountProjectId,
                ProjectExternalId = projectExternalId,
                ProjectName = projectName,
                CMSType = cmsType,
                CMSAuthCredential = cmsAuthCredential
            });
        }
コード例 #2
0
        public async Task <ProjectExternalGetRp> GetProjectExternalById(Guid organizationId, Guid projectId)
        {
            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(null);
            }

            Project project = user.FindProjectById(projectId);

            if (project == null)
            {
                return(null);
            }

            OrganizationCMS organizationCMS = organization.GetConfigurationManagementServiceById(project.OrganizationCMSId);

            var projectCloudCredential = this._cloudCredentialService.ProjectCredentialResolver(organizationCMS, project);

            var accountId         = projectCloudCredential.AccountName;
            var projectExternalId = projectCloudCredential.ProjectExternalId;

            try
            {
                var projectExternal = await this._cmsQueryService(projectCloudCredential.CMSType)
                                      .GetProject(accountId, projectExternalId, projectCloudCredential.CMSAuthCredential);

                return(new ProjectExternalGetRp
                {
                    Name = projectExternal.Name,
                    Description = projectExternal.Description,
                    Url = projectExternal.Link.Web.Href
                });
            }
            catch (Exception)
            {
                return(new ProjectExternalGetRp {
                    Name = project.ProjectExternalName,
                    Description = project.ProjectExternalName,
                    Url = string.Empty
                });
            }
        }
コード例 #3
0
        public async Task DeleteConfigurationManagementService(Guid organizationId, Guid organizationCMSId)
        {
            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;
            }

            PipelineRole role = user.GetRoleInOrganization(organizationId);

            if (role != PipelineRole.OrganizationAdmin)
            {
                await _domainManagerService.AddForbidden($"You are not authorized to delete settings in this organization.");

                return;
            }

            OrganizationCMS organizationCMS = organization.GetConfigurationManagementServiceById(organizationCMSId);

            if (organizationCMS == null)
            {
                await _domainManagerService.AddConflict($"The configuration management service with id {organizationCMSId} does not exists.");

                return;
            }

            List <Project> relatedProjects = organization.GetProjectsByCMSId(organizationCMS.OrganizationCMSId);

            if (relatedProjects.Any())
            {
                await _domainManagerService.AddConflict($"There are projects already configured with the configuration management service {organizationCMS.Name}.");

                return;
            }

            user.DeleteConfigurationManagementService(organizationId, organizationCMSId);

            _userRepository.Update(user);

            await _userRepository.SaveChanges();
        }
コード例 #4
0
        public async Task <OrganizationCMSBranchListRp> GetOrganizationConfigurationManagementServiceRepositoriesBranches(Guid organizationId, Guid organizationCMSId,
                                                                                                                          string projectId,
                                                                                                                          string repositoryId)
        {
            var 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(null);
            }

            OrganizationCMS configurationManagement = organization.GetConfigurationManagementServiceById(organizationCMSId);

            if (configurationManagement == null)
            {
                await _domainManagerService.AddNotFound($"The organzation configuration management service with id {organizationCMSId} does not exists.");

                return(null);
            }

            var cmsAuthCredential = this._cmsCredentialService(configurationManagement.Type).GetToken(
                accountId: _dataProtectorService.Unprotect(configurationManagement.AccountId),
                accountName: _dataProtectorService.Unprotect(configurationManagement.AccountName),
                accessSecret: _dataProtectorService.Unprotect(configurationManagement.AccessSecret),
                accessToken: _dataProtectorService.Unprotect(configurationManagement.AccessToken));

            var repositories = await _cmsQueryService(configurationManagement.Type).GetBranches(projectId, repositoryId, cmsAuthCredential);

            var list = new OrganizationCMSBranchListRp();

            list.Items = repositories.Items.Select(c => new OrganizationCMSBranchListItemRp
            {
                Id   = c.Id,
                Name = c.Name
            }).ToList();

            return(list);
        }
コード例 #5
0
        public async Task UpdateConfigurationManagementService(Guid organizationId, Guid organizationCMSId, OrganizationCMSPutRp 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;
            }

            PipelineRole role = user.GetRoleInOrganization(organizationId);

            if (role != PipelineRole.OrganizationAdmin)
            {
                await _domainManagerService.AddForbidden($"You are not authorized to update settings in this organization.");

                return;
            }

            OrganizationCMS organizationCMS = organization.GetConfigurationManagementServiceById(organizationCMSId);

            if (organizationCMS == null)
            {
                await _domainManagerService.AddConflict($"The cloud provider service with id {organizationCMSId} does not exists.");

                return;
            }

            user.UpdateConfigurationManagementService(organizationId,
                                                      organizationCMSId,
                                                      _dataProtectorService.Protect(resource.AccessId),
                                                      _dataProtectorService.Protect(resource.AccessSecret));

            _userRepository.Update(user);

            await _userRepository.SaveChanges();
        }
コード例 #6
0
        public async Task <OrganizationCMSGetRp> GetOrganizationConfigurationManagementServiceById(Guid organizationId, Guid organizationCMSId)
        {
            var 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(null);
            }

            OrganizationCMS configurationManagement = organization.GetConfigurationManagementServiceById(organizationCMSId);

            if (configurationManagement == null)
            {
                return(null);
            }

            PipelineRole role = user.GetRoleInOrganization(organizationId);

            OrganizationCMSGetRp organizationCMSRp = new OrganizationCMSGetRp()
            {
                OrganizationCMSId = configurationManagement.OrganizationCMSId,
                Name         = configurationManagement.Name,
                Type         = configurationManagement.Type,
                AccountId    = role == PipelineRole.OrganizationAdmin ? _dataProtectorService.Unprotect(configurationManagement.AccountId) : DomainConstants.Obfuscator.Default,
                AccountName  = role == PipelineRole.OrganizationAdmin ? _dataProtectorService.Unprotect(configurationManagement.AccountName) : DomainConstants.Obfuscator.Default,
                AccessId     = role == PipelineRole.OrganizationAdmin ? _dataProtectorService.Unprotect(configurationManagement.AccessId) : DomainConstants.Obfuscator.Default,
                AccessSecret = role == PipelineRole.OrganizationAdmin ? _dataProtectorService.Unprotect(configurationManagement.AccessSecret) : DomainConstants.Obfuscator.Default,
                AccessToken  = role == PipelineRole.OrganizationAdmin ? _dataProtectorService.Unprotect(configurationManagement.AccessToken) : DomainConstants.Obfuscator.Default
            };

            return(organizationCMSRp);
        }
コード例 #7
0
        public async Task CreateConfigurationManagementService(Guid organizationId, OrganizationCMSPostRp 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;
            }

            PipelineRole role = user.GetRoleInOrganization(organizationId);

            if (role != PipelineRole.OrganizationAdmin)
            {
                await _domainManagerService.AddForbidden($"You are not authorized to create settings in this organization.");

                return;
            }

            //OrganizationCMS organizationCMS = organization.GetConfigurationManagementServiceByType(resource.Type);
            //if (organizationCMS != null && organizationCMS.ConnectionType == resource.ConnectionType)
            //{
            //    await _domainManagerService.AddConflict($"The configuration management service with type {resource.Type} already exists.");
            //    return;
            //}

            OrganizationCMS existingCMP = organization.GetConfigurationManagementServiceByName(resource.Name);

            if (existingCMP != null)
            {
                await _domainManagerService.AddConflict($"The configuration management service {resource.Name} has already been taken.");

                return;
            }

            //existing same connection in other account
            OrganizationCMS existingInOtherOrganization = await _organizationCMSRepository.FindOrganizationCMSByTypeAndAccountName(resource.Type, resource.AccountName);

            if (existingInOtherOrganization != null)
            {
                await _domainManagerService.AddConflict($"The configuration management service {resource.Type}/{resource.AccountName} has already been taken in other organization.");

                return;
            }

            user.AddConfigurationManagementService(organizationId,
                                                   resource.Name,
                                                   resource.Type,
                                                   resource.ConnectionType,
                                                   _dataProtectorService.Protect(resource.AccountId),
                                                   _dataProtectorService.Protect(resource.AccountName),
                                                   _dataProtectorService.Protect(resource.AccessId),
                                                   _dataProtectorService.Protect(resource.AccessSecret),
                                                   _dataProtectorService.Protect(resource.AccessToken));

            _userRepository.Update(user);

            await _userRepository.SaveChanges();
        }
コード例 #8
0
        public async Task CreateOrganizationProjectServiceTemplate(Guid organizationId, OrganizationProjectServiceTemplatePostRp 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;
            }

            PipelineRole role = user.GetRoleInOrganization(organizationId);

            if (role != PipelineRole.OrganizationAdmin)
            {
                await _domainManagerService.AddForbidden($"You are not authorized to create project service templates in this organization.");

                return;
            }

            OrganizationProjectServiceTemplate existingTemplate = organization.GetProjectServiceTemplateByName(resource.Name);

            if (existingTemplate != null)
            {
                await _domainManagerService.AddConflict($"The project service templates {resource.Name} has already been taken.");

                return;
            }

            ProjectServiceTemplate sourceProjectServiceTemplate = await _projectServiceTemplateRepository.GetProjectServiceTemplateInternalById(resource.SourceProjectServiceTemplateId);

            if (existingTemplate != null)
            {
                await _domainManagerService.AddConflict($"The source project service templates {resource.Name} does not exists.");

                return;
            }

            ProgrammingLanguage programmingLanguage = await _programmingLanguageRepository.GetProgrammingLanguageById(resource.ProgrammingLanguageId);

            if (programmingLanguage == null)
            {
                await _domainManagerService.AddNotFound($"The programming language with id {resource.ProgrammingLanguageId} does not exists.");

                return;
            }

            OrganizationCMS organizationCMS = organization.GetConfigurationManagementServiceById(resource.ConnectionId);

            if (organizationCMS == null)
            {
                await _domainManagerService.AddNotFound($"The connection with id {resource.ConnectionId} does not exists.");

                return;
            }

            if (organizationCMS.ConnectionType != Domain.Models.Enums.CMSConnectionType.TemplateLevel)
            {
                await _domainManagerService.AddConflict($"The connection with id {resource.ConnectionId} is not for templates.");

                return;
            }

            /*Create repository : BEGIN*/

            CMSAuthCredentialModel cmsAuthCredential = this._cmsCredentialService(organizationCMS.Type).GetToken(
                _dataProtectorService.Unprotect(organizationCMS.AccountId),
                _dataProtectorService.Unprotect(organizationCMS.AccountName),
                _dataProtectorService.Unprotect(organizationCMS.AccessSecret),
                _dataProtectorService.Unprotect(organizationCMS.AccessToken));

            var teamId    = string.Empty;
            var projectId = string.Empty;

            if (resource.RepositoryCMSType == ConfigurationManagementService.Bitbucket)
            {
                teamId    = resource.TeamId;
                projectId = resource.ProjectExternalId;
            }
            else
            {
                teamId    = resource.ProjectExternalId;
                projectId = resource.ProjectExternalId;
            }

            CMSServiceAvailabilityResultModel cmsServiceAvailability =
                await _cmsService(organizationCMS.Type).ValidateServiceAvailability(cmsAuthCredential, teamId, projectId, resource.ProjectExternalName, resource.Name);

            if (!cmsServiceAvailability.Success)
            {
                await _domainManagerService.AddConflict($"The CMS data is not valid. {cmsServiceAvailability.GetReasonForNoSuccess()}");

                return;
            }

            //SaveChanges in CMS
            CMSServiceCreateModel       serviceCreateModel = CMSServiceCreateModel.Factory.Create(teamId, projectId, resource.ProjectExternalName, resource.Name, true);
            CMSServiceCreateResultModel cmsServiceCreate   = await _cmsService(organizationCMS.Type).CreateService(cmsAuthCredential, serviceCreateModel);

            if (!cmsServiceCreate.Success)
            {
                await _domainManagerService.AddConflict($"The CMS data is not valid. {cmsServiceCreate.GetReasonForNoSuccess()}");

                return;
            }

            /*Create repository : END*/
            var template = user.AddProjectTemplateService(organizationId, resource.Name,
                                                          sourceProjectServiceTemplate.ServiceCMSType,
                                                          sourceProjectServiceTemplate.ServiceCPSType,
                                                          resource.Description,
                                                          cmsServiceCreate.ServiceExternalUrl,
                                                          resource.Logo,
                                                          resource.PipeType,
                                                          Domain.Models.Enums.TemplateType.Standard,
                                                          Domain.Models.Enums.TemplateAccess.Organization,
                                                          true, resource.ProgrammingLanguageId, resource.Framework, resource.RepositoryCMSType,
                                                          organizationCMS.AccessId,
                                                          organizationCMS.AccessSecret,
                                                          organizationCMS.AccessToken,
                                                          sourceProjectServiceTemplate.Parameters);

            _projectServiceTemplateRepository.Add(template);
            await _projectServiceTemplateRepository.SaveChanges();

            _userRepository.Update(user);
            await _userRepository.SaveChanges();

            var @event = new ProjectServiceTemplateCreatedEvent(_correlationId)
            {
                OrganizationId           = organization.OrganizationId,
                ProjectServiceTemplateId = template.ProjectServiceTemplateId,
                SourceTemplateUrl        = sourceProjectServiceTemplate.Url,
                TemplateAccess           = template.TemplateAccess,
                NeedCredentials          = template.NeedCredentials,
                RepositoryCMSType        = template.Credential.CMSType,
                RepositoryAccessId       = template.NeedCredentials ? _dataProtectorService.Unprotect(template.Credential.AccessId) : string.Empty,
                RepositoryAccessSecret   = template.NeedCredentials ? _dataProtectorService.Unprotect(template.Credential.AccessSecret) : string.Empty,
                RepositoryAccessToken    = template.NeedCredentials ? _dataProtectorService.Unprotect(template.Credential.AccessToken) : string.Empty,
                RepositoryUrl            = template.Url
            };

            await _eventBusService.Publish(queueName : "ProjectServiceTemplateCreatedEvent", @event : @event);
        }
コード例 #9
0
        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);
        }
コード例 #10
0
        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);
        }