示例#1
0
        public async Task CreateProjectEnvironment(Guid organizationId, Guid projectId, ProjectEnvironmentPostRp resource)
        {
            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 create environments in this project.");

                return;
            }


            if (project.Status != DomainModels.EntityStatus.Active)
            {
                await _domainManagerService.AddConflict($"The project with id {projectId} must be in status Active to add a new feature.");

                return;
            }

            var activeServices = project.GetServicesWithReleaseStages();

            if (!activeServices.Any())
            {
                await _domainManagerService.AddConflict($"At least one pipe must be configured in the project.");

                return;
            }

            DomainModels.ProjectEnvironment existingEnvironment = project.GetEnvironmentByName(resource.Name);
            if (existingEnvironment != null)
            {
                await _domainManagerService.AddConflict($"The environment name {resource.Name} has already been taken.");

                return;
            }

            DomainModels.ProjectEnvironment newEnvironment = user.CreateProjectEnvironment(organizationId, projectId, resource.Name, resource.Description, resource.RequiresApproval, resource.AutoProvision);

            _userRepository.Update(user);

            await _userRepository.SaveChanges();

            await _domainManagerService.AddResult("EnvironmentId", newEnvironment.ProjectEnvironmentId);
        }