示例#1
0
        public async Task SortProjectEnvironments(Guid organizationId, Guid projectId, ProjectEnvironmentSortPostRp 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 sort 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 sort de environments.");

                return;
            }

            var developmentEnvironment = project.GetDevelopmentEnvironment();
            var productionEnvironment  = project.GetProductionEnvironment();

            foreach (var item in resource.Items)
            {
                var environment = project.GetEnvironmentById(item.EnvironmentId);

                if (environment == null)
                {
                    await _domainManagerService.AddNotFound($"The environment with id {item.EnvironmentId}) does not exists.");

                    return;
                }

                if (environment.Type == EnvironmentType.Root || environment.Type == EnvironmentType.Fact)
                {
                    await _domainManagerService.AddConflict($"The environment {environment.Name} ({environment.ProjectEnvironmentId}) is not sortable.");

                    return;
                }

                if (!(developmentEnvironment.Rank < item.Rank && item.Rank < productionEnvironment.Rank))
                {
                    await _domainManagerService.AddConflict($"The rank of the environment {environment.Name} ({environment.ProjectEnvironmentId}) must be between {developmentEnvironment.Rank + 1} and {productionEnvironment.Rank - 1}.");

                    return;
                }

                environment.Rank = item.Rank;
            }

            var groupped = project.Environments.GroupBy(x => x.Rank);

            if (groupped.Count() != project.Environments.Count)
            {
                await _domainManagerService.AddConflict($"The rank of the environments must be sorted sequentially between {developmentEnvironment.Rank + 1} and {productionEnvironment.Rank - 1}.");

                return;
            }

            _userRepository.Update(user);

            await _userRepository.SaveChanges();

            var projectServices = project.GetServicesWithReleaseStages();

            //send events
            foreach (var projectService in projectServices)
            {
                var @event = new ProjectEnvironmentCreatedEvent(_correlationId)
                {
                    OrganizationId            = organization.OrganizationId,
                    OrganizationName          = organization.Name,
                    ProjectId                 = project.ProjectId,
                    ProjectExternalId         = project.ProjectExternalId,
                    ProjectExternalEndpointId = project.ProjectExternalEndpointId,
                    ProjectVSTSFakeName       = project.ProjectVSTSFakeName,
                    ProjectName               = project.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),
                    ReleseStageId   = projectService.ReleaseStageId.Value,
                    ServiceName     = projectService.Name,
                    ServiceLastBuildSuccessVersionId   = projectService.LastBuildSuccessVersionId,
                    ServiceLastBuildSuccessVersionName = projectService.LastBuildSuccessVersionName
                };

                @event.Environments = new List <ProjectEnvironmentItemCreatedEvent>();

                foreach (var item in projectService.Environments)
                {
                    var parentEnvironment = project.GetEnvironments().First(x => x.ProjectEnvironmentId == item.ProjectEnvironmentId);

                    var serviceEnvironment = new ProjectEnvironmentItemCreatedEvent();
                    serviceEnvironment.Id                     = item.ProjectEnvironmentId;
                    serviceEnvironment.Name                   = parentEnvironment.Name;
                    serviceEnvironment.RequiredApproval       = parentEnvironment.RequiresApproval;
                    serviceEnvironment.Variables              = new List <ProjectEnvironmentItemVariableCreatedEvent>();
                    serviceEnvironment.Rank                   = parentEnvironment.Rank;
                    serviceEnvironment.LastSuccessVersionId   = item.LastSuccessVersionId;
                    serviceEnvironment.LastSuccessVersionName = item.LastSuccessVersionName;

                    if (parentEnvironment.Variables != null)
                    {
                        foreach (var variable in parentEnvironment.Variables)
                        {
                            serviceEnvironment.Variables.Add(new ProjectEnvironmentItemVariableCreatedEvent()
                            {
                                Name  = variable.Name,
                                Value = variable.Value
                            });
                        }
                    }

                    if (item.Variables != null)
                    {
                        foreach (var variable in item.Variables)
                        {
                            serviceEnvironment.Variables.Add(new ProjectEnvironmentItemVariableCreatedEvent()
                            {
                                Name  = variable.Name,
                                Value = variable.Value
                            });
                        }
                    }

                    @event.Environments.Add(serviceEnvironment);
                }

                //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);

                await _eventBusService.Publish(queueName : "ProjectEnvironmentCreatedEvent", @event : @event);
            }
        }