public async Task <ProjectServiceEventListRp> GetProjectServiceEvents(Guid organizationId, Guid projectId, Guid serviceId, BaseEventType baseEventType)
        {
            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(null);
            }

            DomainModels.Project project = user.FindProjectById(projectId);
            if (project == null)
            {
                await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists.");

                return(null);
            }

            DomainModels.ProjectService service = project.GetServiceById(serviceId);
            if (service == null)
            {
                await _domainManagerService.AddNotFound($"The project pipe with id {serviceId} does not exists.");

                return(null);
            }

            ProjectServiceEventListRp list = new ProjectServiceEventListRp();

            if (service.Events != null)
            {
                if (baseEventType == BaseEventType.None)
                {
                    list.Items = service.Events.Select(x => new ProjectServiceEventListItemRp
                    {
                        EventType        = x.EventType,
                        EventDescription = x.EventDescription,
                        EventStatus      = x.EventStatus,
                        CreationDate     = x.CreationDate,
                        EventDate        = x.EventDate
                    }).OrderByDescending(x => x.EventDate).Take(10).ToList();
                }
                else
                {
                    list.Items = service.Events.Where(x => x.BaseEventType == baseEventType).Select(x => new ProjectServiceEventListItemRp
                    {
                        EventType        = x.EventType,
                        EventDescription = x.EventDescription,
                        EventStatus      = x.EventStatus,
                        CreationDate     = x.CreationDate,
                        EventDate        = x.EventDate
                    }).OrderByDescending(x => x.EventDate).Take(10).ToList();
                }
            }

            return(list);
        }
        public async Task <CPSCloudResourceSummaryModel> GetProjectFeatureServiceCloudSummary(Guid organizationId, Guid projectId, Guid featureId, Guid serviceId)
        {
            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(null);
            }

            DomainModels.Project project = user.FindProjectById(projectId);
            if (project == null)
            {
                await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists.");

                return(null);
            }

            DomainModels.ProjectFeature feature = project.GetFeatureById(featureId);
            if (feature == null)
            {
                await _domainManagerService.AddNotFound($"The project feature with id {featureId} does not exists.");

                return(null);
            }

            DomainModels.ProjectFeatureService featureService = feature.GetFeatureServiceById(serviceId);
            if (featureService == null)
            {
                await _domainManagerService.AddNotFound($"The project feature pipe with id {serviceId} does not exists.");

                return(null);
            }

            var environments = new List <string>()
            {
                DomainConstants.Environments.Development
            };

            CPSAuthCredentialModel authCredentials = new CPSAuthCredentialModel();

            authCredentials.AccessId        = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessId);
            authCredentials.AccessName      = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessName);
            authCredentials.AccessSecret    = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessSecret);
            authCredentials.AccessRegion    = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessRegion);
            authCredentials.AccessAppId     = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppId);
            authCredentials.AccessAppSecret = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppSecret);
            authCredentials.AccessDirectory = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessDirectory);

            var summary = await _cpsQueryService(project.OrganizationCPS.Type).GetSummary(organization.Name, project.Name, featureService.ProjectService.Name, environments, feature.Name, authCredentials);

            return(summary);
        }
예제 #3
0
        public async Task <ProjectFeatureServiceDeliveryListRp> GetProjectFeatureServiceDeliveries(Guid organizationId, Guid projectId, Guid featureId, Guid serviceId)
        {
            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(null);
            }

            DomainModels.Project project = user.FindProjectById(projectId);
            if (project == null)
            {
                await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists.");

                return(null);
            }

            DomainModels.ProjectFeature feature = project.GetFeatureById(featureId);
            if (feature == null)
            {
                await _domainManagerService.AddNotFound($"The feature with id {featureId} does not exists.");

                return(null);
            }

            DomainModels.ProjectFeatureService featureService = feature.GetFeatureServiceById(serviceId);
            if (featureService == null)
            {
                await _domainManagerService.AddNotFound($"The feature pipe with id {featureId} does not exists.");

                return(null);
            }

            ProjectFeatureServiceDeliveryListRp list = new ProjectFeatureServiceDeliveryListRp();

            if (featureService.Deliveries != null)
            {
                list.Items = featureService.Deliveries
                             .OrderByDescending(x => x.DeliveryDate)
                             .Take(5)
                             .Select(x => new ProjectFeatureServiceDeliveryListItemRp
                {
                    VersionId    = x.VersionId,
                    VersionName  = x.VersionName,
                    DeliveryDate = x.DeliveryDate,
                    Data         = JsonConvert.DeserializeObject <ProjectServiceDeliveryDataRp>(x.Data)
                }).ToList();
            }

            return(list);
        }
        public async Task <ProjectFeatureAllServiceListRp> GetProjectFeatureAllServices(Guid organizationId, Guid projectId, Guid featureId)
        {
            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(null);
            }

            DomainModels.Project project = user.FindProjectById(projectId);
            if (project == null)
            {
                await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists.");

                return(null);
            }

            DomainModels.ProjectFeature feature = project.GetFeatureById(featureId);
            if (feature == null)
            {
                await _domainManagerService.AddNotFound($"The project feature with id {featureId} does not exists.");

                return(null);
            }

            ProjectFeatureAllServiceListRp list = new ProjectFeatureAllServiceListRp();

            var projectServices = project.GetServices();

            foreach (var projectService in projectServices)
            {
                var featureService = feature.GetFeatureServiceById(projectService.ProjectServiceId);

                var item = new ProjectFeatureAllServiceListItemRp()
                {
                    ProjectServiceId = projectService.ProjectServiceId,
                    Name             = projectService.Name
                };

                if (featureService != null)
                {
                    item.IsFeatureService = true;
                }

                list.Items.Add(item);
            }

            return(list);
        }
        public async Task <ProjectFeatureServiceActivityListRp> GetProjectFeatureServiceActivities(Guid organizationId, Guid projectId, Guid featureId, Guid serviceId)
        {
            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(null);
            }

            DomainModels.Project project = user.FindProjectById(projectId);
            if (project == null)
            {
                await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists.");

                return(null);
            }

            DomainModels.ProjectFeature feature = project.GetFeatureById(featureId);
            if (feature == null)
            {
                await _domainManagerService.AddNotFound($"The feature with id {featureId} does not exists.");

                return(null);
            }

            DomainModels.ProjectFeatureService featureService = feature.GetFeatureServiceById(serviceId);
            if (featureService == null)
            {
                await _domainManagerService.AddNotFound($"The feature pipe with id {featureId} does not exists.");

                return(null);
            }

            ProjectFeatureServiceActivityListRp list = new ProjectFeatureServiceActivityListRp();

            if (featureService.Activities != null)
            {
                list.Items = featureService.Activities.Select(x => new ProjectFeatureServiceActivityListItemRp
                {
                    Name           = x.Name,
                    Log            = x.Log,
                    ActivityStatus = x.ActivityStatus,
                    CreationDate   = x.CreationDate
                }).OrderBy(x => x.CreationDate).ToList();
            }

            return(list);
        }
        public async Task <ProjectFeatureServiceListRp> GetProjectFeatureServices(Guid organizationId, Guid projectId, Guid featureId)
        {
            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(null);
            }

            DomainModels.Project project = user.FindProjectById(projectId);
            if (project == null)
            {
                await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists.");

                return(null);
            }

            DomainModels.ProjectFeature feature = project.GetFeatureById(featureId);
            if (feature == null)
            {
                await _domainManagerService.AddNotFound($"The project feature with id {featureId} does not exists.");

                return(null);
            }

            ProjectFeatureServiceListRp list = new ProjectFeatureServiceListRp();

            if (feature.Services != null)
            {
                list.Items = feature.Services.Select(x => new ProjectFeatureServiceListItemRp()
                {
                    ProjectFeatureId = x.ProjectFeatureId,
                    ProjectServiceId = x.ProjectServiceId,
                    Name             = x.ProjectService.Name,
                    Description      = x.ProjectService.Description,
                    Template         = x.ProjectService.ProjectServiceTemplate.Name,
                    Status           = x.Status,
                    PipelineStatus   = x.PipelineStatus
                }).ToList();
            }

            return(list);
        }
예제 #7
0
        public async Task <ProjectEnvironmentVariableListRp> GetProjectEnvironmentVariables(Guid organizationId, Guid projectId, Guid environmentId)
        {
            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(null);
            }

            DomainModels.Project project = user.FindProjectById(projectId);
            if (project == null)
            {
                await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists.");

                return(null);
            }

            DomainModels.ProjectEnvironment environment = project.GetEnvironmentById(environmentId);
            if (environment == null)
            {
                return(null);
            }

            DomainModels.ProjectEnvironment  defaultEnvironment = project.GetRootEnvironment();
            ProjectEnvironmentVariableListRp list = new ProjectEnvironmentVariableListRp();

            if (environment.Variables != null)
            {
                var variables = from dv in defaultEnvironment.Variables
                                join ev in environment.Variables on dv.Name equals ev.Name into ev
                                from p in ev.DefaultIfEmpty()
                                select new ProjectEnvironmentVariableListItemRp
                {
                    Name  = dv.Name,
                    Value = p == null ? "" : p.Value
                };

                list.Items = variables.ToList();
            }

            return(list);
        }
        public async Task <ProjectServiceGetRp> GetProjectServiceById(Guid organizationId, Guid projectId, Guid serviceId)
        {
            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(null);
            }

            DomainModels.Project project = organization.GetProjectById(projectId);
            if (project == null)
            {
                await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists.");

                return(null);
            }

            DomainModels.ProjectService service = project.GetServiceById(serviceId);
            if (service == null)
            {
                return(null);
            }

            ProjectServiceGetRp serviceRp = new ProjectServiceGetRp
            {
                ProjectServiceId   = service.ProjectServiceId,
                Name               = service.Name,
                Description        = service.Description,
                Template           = service.ProjectServiceTemplate.Name,
                Status             = service.Status,
                PipelineStatus     = service.PipelineStatus,
                ServiceExternalUrl = service.ProjectServiceExternalUrl,
                GitProviderType    = service.OrganizationCMS.Type,
            };

            return(serviceRp);
        }
예제 #9
0
        public async Task <ProjectFeatureGetRp> GetProjectFeatureById(Guid organizationId, Guid projectId, Guid featureId)
        {
            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(null);
            }

            DomainModels.Project project = user.FindProjectById(projectId);
            if (project == null)
            {
                await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists.");

                return(null);
            }

            DomainModels.ProjectFeature feature = project.GetFeatureById(featureId);
            if (feature == null)
            {
                return(null);
            }

            ProjectFeatureGetRp featureRp = new ProjectFeatureGetRp
            {
                ProjectFeatureId = feature.ProjectFeatureId,
                Name             = feature.Name,
                Description      = feature.Description,
                StartDate        = feature.CreationDate,
                CompletionDate   = feature.CompletionDate,
                Status           = feature.GetStatusName()
            };

            return(featureRp);
        }
예제 #10
0
        public async Task <ProjectEnvironmentGetRp> GetProjectEnvironmentById(Guid organizationId, Guid projectId, Guid environmentId)
        {
            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(null);
            }

            DomainModels.Project project = user.FindProjectById(projectId);
            if (project == null)
            {
                await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists.");

                return(null);
            }

            DomainModels.ProjectEnvironment environment = project.GetEnvironmentById(environmentId);
            if (environment == null)
            {
                return(null);
            }

            ProjectEnvironmentGetRp environmentRp = new ProjectEnvironmentGetRp
            {
                ProjectEnvironmentId = environment.ProjectEnvironmentId,
                Name        = environment.Name,
                Description = environment.Description,
                Status      = environment.Status,
                Type        = environment.Type
            };

            return(environmentRp);
        }
        public async Task <ProjectServiceFeatureListRp> GetProjectServiceFeaturesById(Guid organizationId, Guid projectId, Guid serviceId)
        {
            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(null);
            }

            DomainModels.Project project = user.FindProjectById(projectId);
            if (project == null)
            {
                await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists.");

                return(null);
            }

            DomainModels.ProjectService service = project.GetServiceById(serviceId);
            if (service == null)
            {
                return(null);
            }

            ProjectServiceFeatureListRp featureListRp = new ProjectServiceFeatureListRp
            {
                Items = service.Features.Select(x => new ProjectServiceFeatureListItemRp()
                {
                    FeatureId   = x.ProjectFeatureId,
                    FeatureName = x.ProjectFeature.Name
                }).ToList()
            };

            return(featureListRp);
        }
예제 #12
0
        public async Task <ProjectFeatureListRp> GetProjectFeatures(Guid organizationId, Guid projectId)
        {
            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(null);
            }

            DomainModels.Project project = user.FindProjectById(projectId);
            if (project == null)
            {
                await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists.");

                return(null);
            }

            ProjectFeatureListRp list = new ProjectFeatureListRp();

            if (project.Features != null)
            {
                list.Items = project.Features.Select(x => new ProjectFeatureListItemRp()
                {
                    ProjectFeatureId = x.ProjectFeatureId,
                    Name             = x.Name,
                    Description      = x.Description,
                    StartDate        = x.CreationDate,
                    CompletionDate   = x.CompletionDate,
                    Status           = x.GetStatusName()
                }).ToList();
            }

            return(list);
        }
예제 #13
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);
        }
예제 #14
0
        public async Task CreateProjectEnvironmentVariables(Guid organizationId, Guid projectId, Guid environmentId, ProjectEnvironmentVariablePostRp 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 variables 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;
            }

            DomainModels.ProjectEnvironment environment = project.GetEnvironmentById(environmentId);
            if (environment == null)
            {
                await _domainManagerService.AddNotFound($"The environment with id {environmentId} does not exists.");

                return;
            }

            bool autoProvision = false;

            if (environment.Type == DomainModels.EnvironmentType.Root)
            {
                foreach (var resourceVariable in resource.Items)
                {
                    if (string.IsNullOrEmpty(resourceVariable.Name) || string.IsNullOrEmpty(resourceVariable.Value))
                    {
                        await _domainManagerService.AddConflict($"The environment variable name/value is mandatory.");

                        return;
                    }

                    var variable = environment.GetVariableByName(resourceVariable.Name);
                    if (variable != null)
                    {
                        environment.SetVariable(resourceVariable.Name, resourceVariable.Value);
                    }
                    else
                    {
                        environment.AddVariable(resourceVariable.Name, resourceVariable.Value);
                    }
                }
            }
            else
            {
                DomainModels.ProjectEnvironment rootEnvironment = project.GetRootEnvironment();

                foreach (var variable in rootEnvironment.Variables)
                {
                    var resourceVariable = resource.Items.FirstOrDefault(x => x.Name.Equals(variable.Name, StringComparison.InvariantCultureIgnoreCase));
                    if (resourceVariable == null)
                    {
                        await _domainManagerService.AddConflict($"The environment variable {variable.Name} is mandatory.");

                        return;
                    }

                    if (string.IsNullOrEmpty(resourceVariable.Value))
                    {
                        await _domainManagerService.AddConflict($"The environment variable value {variable.Name} is mandatory.");

                        return;
                    }

                    var existingVariable = environment.GetVariableByName(resourceVariable.Name);
                    if (existingVariable != null)
                    {
                        environment.SetVariable(resourceVariable.Name, resourceVariable.Value);
                    }
                    else
                    {
                        environment.AddVariable(resourceVariable.Name, resourceVariable.Value);
                    }
                }

                if (environment.Status == DomainModels.EntityStatus.Preparing)
                {
                    autoProvision = environment.AutoProvision;
                }

                environment.Activate();
            }

            var projectServices = project.GetServicesWithReleaseStages();

            //replicate service environments
            foreach (var projectService in projectServices)
            {
                var rootVariables = projectService.GetRootEnvironmentVariables();
                projectService.AddEnvironment(environment.ProjectEnvironmentId, rootVariables);
            }

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

            //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),
                    EnvironmentId                      = environment.ProjectEnvironmentId,
                    EnvironmentName                    = environment.Name,
                    EnvironmentRank                    = environment.Rank,
                    EnvironmentAutoProvision           = autoProvision,
                    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);
            }
        }
예제 #15
0
        public async Task <ProjectEnvironmentListRp> GetProjectEnvironments(Guid organizationId, Guid projectId)
        {
            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(null);
            }

            DomainModels.Project project = user.FindProjectById(projectId);
            if (project == null)
            {
                await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists.");

                return(null);
            }

            ProjectEnvironmentListRp list = new ProjectEnvironmentListRp();

            if (project.Environments != null)
            {
                list.Items = project.Environments.Select(x => new ProjectEnvironmentListItemRp()
                {
                    ProjectEnvironmentId = x.ProjectEnvironmentId,
                    Name        = x.Name,
                    Description = x.Description,
                    Type        = x.Type,
                    Status      = x.Status,
                    Rank        = x.Rank
                }).OrderBy(x => x.Rank).ToList();


                var services = project.Services.Select(x => new
                {
                    ProjectServiceId = x.ProjectServiceId,
                    Name             = x.Name
                }).ToList();

                CPSAuthCredentialModel authCredentials = new CPSAuthCredentialModel();
                authCredentials.AccessId        = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessId);
                authCredentials.AccessName      = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessName);
                authCredentials.AccessSecret    = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessSecret);
                authCredentials.AccessRegion    = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessRegion);
                authCredentials.AccessAppId     = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppId);
                authCredentials.AccessAppSecret = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppSecret);
                authCredentials.AccessDirectory = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessDirectory);

                if (services != null && services.Any())
                {
                    foreach (var env in list.Items)
                    {
                        var projectServiceEnvironments    = new ProjectServiceEnvironmentListRp();
                        var projectServiceEnvironmentList = new ConcurrentBag <ProjectServiceEnvironmentListItemRp>();

                        await services.ForEachAsync(5, async service =>
                        {
                            var projectServiceEnvironmentListItem     = new ProjectServiceEnvironmentListItemRp();
                            projectServiceEnvironmentListItem.Name    = service.Name;
                            projectServiceEnvironmentListItem.Summary = await _cpsQueryService(project.OrganizationCPS.Type).GetEnvironmentSummary(organization.Name, project.Name, service.Name, env.Name, "Root", authCredentials);
                            projectServiceEnvironmentList.Add(projectServiceEnvironmentListItem);
                        });

                        projectServiceEnvironments.Items = projectServiceEnvironmentList.ToList();

                        env.Services = projectServiceEnvironments;
                    }
                }
            }

            return(list);
        }
        public async Task <ProjectFeatureServiceEnvironmentListRp> GetFeatureProjectServiceEnvironments(Guid organizationId, Guid projectId, Guid featureId, Guid serviceId)
        {
            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(null);
            }

            DomainModels.Project project = user.FindProjectById(projectId);
            if (project == null)
            {
                await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists.");

                return(null);
            }

            DomainModels.ProjectFeature projectFeature = project.GetFeatureById(featureId);
            if (projectFeature == null)
            {
                await _domainManagerService.AddNotFound($"The project feature with id {featureId} does not exists.");

                return(null);
            }

            DomainModels.ProjectFeatureService projectFeatureService = projectFeature.GetFeatureServiceById(serviceId);
            if (projectFeatureService == null)
            {
                await _domainManagerService.AddNotFound($"The feature service with id {serviceId} does not exists.");

                return(null);
            }

            ProjectFeatureServiceEnvironmentListRp list = new ProjectFeatureServiceEnvironmentListRp();

            CPSAuthCredentialModel authCredentials = new CPSAuthCredentialModel();

            authCredentials.AccessId        = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessId);
            authCredentials.AccessName      = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessName);
            authCredentials.AccessSecret    = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessSecret);
            authCredentials.AccessRegion    = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessRegion);
            authCredentials.AccessAppId     = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppId);
            authCredentials.AccessAppSecret = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppSecret);
            authCredentials.AccessDirectory = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessDirectory);

            if (projectFeatureService.Environments != null)
            {
                List <ProjectFeatureServiceEnvironmentListItemRp> projectFeatureServiceEnvironmentList = new List <ProjectFeatureServiceEnvironmentListItemRp>();
                var environments = projectFeatureService.Environments.OrderBy(x => x.ProjectFeatureEnvironment.Rank);
                foreach (var item in environments)
                {
                    var projectServiceEnvironmentListItem = new ProjectFeatureServiceEnvironmentListItemRp();
                    projectServiceEnvironmentListItem.ProjectFeatureServiceEnvironmentId = item.ProjectFeatureServiceEnvironmentId;
                    projectServiceEnvironmentListItem.Name    = item.ProjectFeatureEnvironment.Name;
                    projectServiceEnvironmentListItem.Status  = item.Status;
                    projectServiceEnvironmentListItem.Summary = await _cpsQueryService(project.OrganizationCPS.Type).GetEnvironmentSummary(organization.Name, project.Name, projectFeatureService.ProjectService.Name, item.ProjectFeatureEnvironment.Name, projectFeature.Name, authCredentials);

                    projectServiceEnvironmentListItem.Variables = item.Variables.Select(p => new ProjectFeatureServiceEnvironmentVariableListItemRp()
                    {
                        Name  = p.Name,
                        Value = p.Value
                    }).ToList();
                    projectFeatureServiceEnvironmentList.Add(projectServiceEnvironmentListItem);
                }
                list.Items = projectFeatureServiceEnvironmentList;
            }

            return(list);
        }
예제 #17
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);
            }
        }
        public async Task <ProjectFeatureServiceExternalGetRp> GetProjectFeatureServiceExternalById(Guid organizationId, Guid projectId, Guid featureId, Guid serviceId)
        {
            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(null);
            }

            DomainModels.Project project = user.FindProjectById(projectId);
            if (project == null)
            {
                await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists.");

                return(null);
            }

            DomainModels.ProjectFeature feature = project.GetFeatureById(featureId);
            if (feature == null)
            {
                await _domainManagerService.AddNotFound($"The project feature with id {featureId} does not exists.");

                return(null);
            }

            CMSAuthCredentialModel cmsAuthCredential = null;
            var serviceExternalName = string.Empty;
            var projectExternalId   = project.ProjectExternalId;
            var service             = feature.Services.FirstOrDefault(c => c.ProjectServiceId.Equals(serviceId)).ProjectService;

            //Auth
            cmsAuthCredential = this._cmsCredentialService(service.OrganizationCMS.Type).GetToken(
                _dataProtectorService.Unprotect(service.OrganizationCMS.AccountId),
                _dataProtectorService.Unprotect(service.OrganizationCMS.AccountName),
                _dataProtectorService.Unprotect(service.OrganizationCMS.AccessSecret),
                _dataProtectorService.Unprotect(service.OrganizationCMS.AccessToken));

            switch (service.OrganizationCMS.Type)
            {
            case ConfigurationManagementService.VSTS:
                serviceExternalName = service.ProjectServiceExternalName;
                projectExternalId   = project.ProjectExternalId;
                break;

            case ConfigurationManagementService.Bitbucket:
                serviceExternalName = service.ProjectServiceExternalId;
                projectExternalId   = project.OrganizationExternalId;
                break;

            case ConfigurationManagementService.GitHub:
                serviceExternalName = service.ProjectServiceExternalName;
                projectExternalId   = project.ProjectExternalId;
                break;

            case ConfigurationManagementService.GitLab:
                serviceExternalName = service.ProjectServiceExternalId;
                projectExternalId   = project.ProjectExternalId;
                break;

            default:
                break;
            }

            if (service.IsImported)
            {
                projectExternalId = service.ProjectExternalId;
            }

            var serviceExternal = await this._cmsQueryService(service.OrganizationCMS.Type).GetRepository(projectExternalId, serviceExternalName, cmsAuthCredential);

            return(new ProjectFeatureServiceExternalGetRp
            {
                DefaultBranch = feature.Name,
                GitUrl = serviceExternal.Link,
                SSHUrl = serviceExternal.SSHUrl
            });
        }
        public async Task CreateProjectServiceEnvironmentVariables(Guid organizationId, Guid projectId, Guid serviceId, Guid environmentId, ProjectServiceEnvironmentVariablePostRp 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 variables 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;
            }

            DomainModels.ProjectService projectService = project.GetServiceById(serviceId);
            if (projectService == null)
            {
                await _domainManagerService.AddNotFound($"The project service with id {serviceId} does not exists.");

                return;
            }

            if (projectService.Status != DomainModels.EntityStatus.Active)
            {
                await _domainManagerService.AddConflict($"The pipe with id {serviceId} must be in status Active to add/modify variables.");

                return;
            }

            DomainModels.ProjectServiceEnvironment environment = projectService.GetServiceEnvironment(environmentId);
            if (environment == null)
            {
                await _domainManagerService.AddNotFound($"The environment with id {environmentId} does not exists.");

                return;
            }

            foreach (var resourceVariable in resource.Items)
            {
                if (string.IsNullOrEmpty(resourceVariable.Name) || string.IsNullOrEmpty(resourceVariable.Value))
                {
                    await _domainManagerService.AddConflict($"The environment variable name/value is mandatory.");

                    return;
                }

                var variable = environment.GetVariableByName(resourceVariable.Name);
                if (variable != null)
                {
                    environment.SetVariable(resourceVariable.Name, resourceVariable.Value);
                }
                else
                {
                    environment.AddVariable(resourceVariable.Name, resourceVariable.Value);
                }
            }

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

            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
            };

            @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.Name             = parentEnvironment.Name;
                serviceEnvironment.RequiredApproval = parentEnvironment.RequiresApproval;
                serviceEnvironment.Rank             = parentEnvironment.Rank;

                serviceEnvironment.Variables = new List <ProjectEnvironmentItemVariableCreatedEvent>();
                foreach (var variable in parentEnvironment.Variables)
                {
                    serviceEnvironment.Variables.Add(new ProjectEnvironmentItemVariableCreatedEvent()
                    {
                        Name  = variable.Name,
                        Value = variable.Value
                    });
                }
                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);
        }
        public async Task <ProjectFeatureServicePipelineGetRp> GetProjectFeatureServicePipelineById(Guid organizationId, Guid projectId, Guid featureId, Guid serviceId)
        {
            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(null);
            }

            DomainModels.Project project = user.FindProjectById(projectId);
            if (project == null)
            {
                await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists.");

                return(null);
            }

            DomainModels.ProjectFeature feature = project.GetFeatureById(featureId);
            if (feature == null)
            {
                await _domainManagerService.AddNotFound($"The project feature with id {featureId} does not exists.");

                return(null);
            }

            DomainModels.ProjectFeatureService featureService = feature.GetFeatureServiceById(serviceId);
            if (featureService == null)
            {
                return(null);
            }

            ProjectFeatureServicePipelineGetRp featureServicePipelineRp = new ProjectFeatureServicePipelineGetRp
            {
                Name        = featureService.ProjectService.Name,
                Description = featureService.ProjectService.Description,
                Status      = featureService.Status,
                PipeType    = featureService.ProjectService.PipeType,
                Phases      = new List <ProjectFeatureServicePipelinePhaseGetRp>()
            };

            featureServicePipelineRp.Phases.Add(new ProjectFeatureServicePipelinePhaseGetRp()
            {
                Type                   = "Build",
                Name                   = "Build",
                Rank                   = 1,
                LastStatusCode         = featureService.LastPipelineBuildStatus.ToString(),
                LastStatusDescription  = featureService.LastPipelineBuildStatus.GetDescription(),
                LastVersionId          = featureService.LastBuildVersionId,
                LastVersionName        = featureService.LastBuildVersionName,
                LastSuccessVersionId   = featureService.LastBuildSuccessVersionId,
                LastSuccessVersionName = featureService.LastBuildSuccessVersionName,
                LastApprovalId         = string.Empty
            });

            foreach (var environment in featureService.Environments)
            {
                featureServicePipelineRp.Phases.Add(new ProjectFeatureServicePipelinePhaseGetRp()
                {
                    Type                   = "Release",
                    Name                   = environment.ProjectFeatureEnvironment.Name,
                    Rank                   = environment.ProjectFeatureEnvironment.Rank + 1,
                    LastStatusCode         = string.IsNullOrEmpty(environment.LastStatusCode) ? PipelineReleaseStatus.Pending.ToString() : environment.LastStatusCode,
                    LastStatusDescription  = string.IsNullOrEmpty(environment.LastStatus) ? PipelineReleaseStatus.Pending.ToString() : environment.LastStatus,
                    LastVersionId          = environment.LastVersionId,
                    LastVersionName        = environment.LastVersionName,
                    LastSuccessVersionId   = environment.LastSuccessVersionId,
                    LastSuccessVersionName = environment.LastSuccessVersionName,
                    LastApprovalId         = environment.LastApprovalId
                });
            }

            //Order
            featureServicePipelineRp.Phases = featureServicePipelineRp.Phases.OrderBy(x => x.Rank).ToList();

            return(featureServicePipelineRp);
        }
예제 #21
0
        public async Task InactivateProjectEnvironment(Guid organizationId, Guid projectId, Guid environmentId)
        {
            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 delete 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 inactivate a project environment.");

                return;
            }

            DomainModels.ProjectEnvironment environment = project.GetEnvironmentById(environmentId);
            if (environment == null)
            {
                await _domainManagerService.AddNotFound($"The environment with id {environmentId} does not exists.");

                return;
            }
            ;

            if (environment.Type == DomainModels.EnvironmentType.Fact)
            {
                await _domainManagerService.AddConflict($"The environment fact cannot be inactivated.");

                return;
            }
            ;

            if (environment.Status != DomainModels.EntityStatus.Active)
            {
                await _domainManagerService.AddConflict($"The environment {environment.Name} must be in status Active to be inactivated.");

                return;
            }

            user.InactivateProjectEnvironment(organizationId, projectId, environmentId);

            _userRepository.Update(user);

            await _userRepository.SaveChanges();

            var projectServices = project.GetServicesWithReleaseStages();

            foreach (var projectService in projectServices)
            {
                var @event = new ProjectEnvironmentInactivatedEvent(_correlationId)
                {
                    OrganizationId            = organization.OrganizationId,
                    OrganizationName          = organization.Name,
                    ProjectId                 = project.ProjectId,
                    ProjectExternalId         = project.ProjectExternalId,
                    ProjectExternalEndpointId = project.ProjectExternalEndpointId,
                    ProjectVSTSFakeName       = project.ProjectVSTSFakeName,
                    ProjectName               = project.Name,
                    ServiceName               = projectService.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),
                    EnvironmentName = environment.Name,
                    ReleseStageId   = projectService.ReleaseStageId.Value
                };

                //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);
                @event.CPSAccessAppId     = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppId);
                @event.CPSAccessAppSecret = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppSecret);
                @event.CPSAccessDirectory = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessDirectory);

                await _eventBusService.Publish(queueName : "ProjectEnvironmentInactivatedEvent", @event : @event);
            }
        }
예제 #22
0
        public async Task CreateReleaseProjectEnvironment(Guid organizationId, Guid projectId, Guid environmentId)
        {
            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;
            }

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

                return;
            }

            DomainModels.ProjectEnvironment environment = project.GetEnvironmentById(environmentId);
            if (environment == null)
            {
                await _domainManagerService.AddNotFound($"The environment with id {environmentId} does not exists.");

                return;
            }
            ;

            var projectServices = project.GetServicesWithReleaseStages();

            var environmentsToBeSkippedList = project.Environments.Where(x => x.Rank < environment.Rank);
            var descriptionsToBeSkipped     = $"Release created from PipelineSpace.";

            if (environmentsToBeSkippedList.Any())
            {
                descriptionsToBeSkipped = $"{descriptionsToBeSkipped} Detail: {string.Join(", ", environmentsToBeSkippedList.Select(x => $"PS_SKIP_ENVIRONMENT_{x.Name}"))}";
            }

            Parallel.ForEach(projectServices, async(service) =>
            {
                var previousEnvironment = service.Environments.FirstOrDefault(x => x.ProjectEnvironment.Rank == ((environment.Type == EnvironmentType.Root) ? environment.Rank : environment.Rank - 1));
                if (!string.IsNullOrEmpty(previousEnvironment.LastSuccessVersionId))
                {
                    CMSPipelineReleaseParamModel releaseBuildOptions = new CMSPipelineReleaseParamModel();
                    releaseBuildOptions.VSTSAPIVersion       = _vstsOptions.Value.ApiVersion;
                    releaseBuildOptions.VSTSAccountName      = project.OrganizationCMS.Type == ConfigurationManagementService.VSTS ? _dataProtectorService.Unprotect(project.OrganizationCMS.AccountName) : _fakeAccountOptions.Value.AccountId;
                    releaseBuildOptions.VSTSAccessSecret     = project.OrganizationCMS.Type == ConfigurationManagementService.VSTS ? _dataProtectorService.Unprotect(project.OrganizationCMS.AccessSecret) : _fakeAccountOptions.Value.AccessSecret;
                    releaseBuildOptions.VSTSAccountProjectId = project.OrganizationCMS.Type == ConfigurationManagementService.VSTS ? project.Name : project.ProjectVSTSFakeName;

                    releaseBuildOptions.ProjectName         = project.Name;
                    releaseBuildOptions.ProjectExternalId   = project.OrganizationCMS.Type == ConfigurationManagementService.VSTS ? project.ProjectExternalId : project.ProjectVSTSFakeId;
                    releaseBuildOptions.ReleaseDefinitionId = service.ReleaseStageId.Value;
                    releaseBuildOptions.Alias = service.Name;

                    releaseBuildOptions.VersionId   = int.Parse(previousEnvironment.LastSuccessVersionId);
                    releaseBuildOptions.VersionName = previousEnvironment.LastSuccessVersionName;
                    releaseBuildOptions.Description = descriptionsToBeSkipped;

                    await _cmsPipelineService.CreateRelease(releaseBuildOptions);
                }
            });
        }
        public async Task CompleteProjectFeature(Guid organizationId, Guid projectId, Guid featureId, bool deleteInfrastructure)
        {
            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 complete features in this project.");

                return;
            }

            if (project.Status != DomainModels.EntityStatus.Active)
            {
                await _domainManagerService.AddConflict($"The project with id {projectId} must be in status Active to ask for pull request.");

                return;
            }

            DomainModels.ProjectFeature feature = project.GetFeatureById(featureId);
            if (feature == null)
            {
                await _domainManagerService.AddNotFound($"The feature with id {featureId} does not exists.");

                return;
            }

            if (feature.Status != DomainModels.EntityStatus.Active)
            {
                await _domainManagerService.AddConflict($"The feature with id {featureId} must be in status Active to to ask for pull request.");

                return;
            }

            //if (feature.IsCompleted())
            //{
            //    await _domainManagerService.AddConflict($"The feature with id {featureId} has already been completed.");
            //    return;
            //}

            //services asociated (TODO: services on demand)
            List <ProjectFeatureServiceCompletedEvent> @events = new List <ProjectFeatureServiceCompletedEvent>();

            foreach (var item in feature.Services)
            {
                DomainModels.ProjectService projectService = project.GetServiceById(item.ProjectServiceId);
                if (projectService == null)
                {
                    await _domainManagerService.AddConflict($"The pipe id {item} does not exists.");

                    return;
                }

                @events.Add(new ProjectFeatureServiceCompletedEvent(_correlationId)
                {
                    ServiceId          = item.ProjectServiceId,
                    ServiceExternalId  = projectService.ProjectServiceExternalId,
                    ServiceExternalUrl = projectService.ProjectServiceExternalUrl,
                    ServiceName        = projectService.Name,
                    ServiceTemplateUrl = projectService.ProjectServiceTemplate.Url,
                    CommitStageId      = item.CommitStageId,
                    ReleaseStageId     = item.ReleaseStageId
                });
            }

            user.CompleteProjectFeature(organizationId, projectId, featureId);

            _userRepository.Update(user);

            await _userRepository.SaveChanges();

            var @event = new ProjectFeatureCompletedEvent(_correlationId)
            {
                OrganizationExternalId = project.OrganizationExternalId,
                OrganizationId         = organization.OrganizationId,
                OrganizationName       = organization.Name,
                ProjectId                 = project.ProjectId,
                Services                  = @events,
                ProjectExternalId         = project.ProjectExternalId,
                ProjectExternalEndpointId = project.ProjectExternalEndpointId,
                ProjectVSTSFakeName       = project.ProjectVSTSFakeName,
                ProjectName               = project.Name,
                FeatureId                 = feature.ProjectFeatureId,
                FeatureName               = feature.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),
                DeleteInfrastructure = deleteInfrastructure
            };

            //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);
            @event.CPSAccessAppId     = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppId);
            @event.CPSAccessAppSecret = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppSecret);
            @event.CPSAccessDirectory = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessDirectory);

            await _eventBusService.Publish(queueName : "ProjectFeatureCompletedEvent", @event : @event);
        }
        public async Task <ProjectFeatureServiceSummaryGetRp> GetProjectFeatureServiceSummaryById(Guid organizationId, Guid projectId, Guid featureId, Guid serviceId)
        {
            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(null);
            }

            DomainModels.Project project = user.FindProjectById(projectId);
            if (project == null)
            {
                await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists.");

                return(null);
            }

            DomainModels.ProjectFeature feature = project.GetFeatureById(featureId);
            if (feature == null)
            {
                await _domainManagerService.AddNotFound($"The project feature with id {featureId} does not exists.");

                return(null);
            }

            DomainModels.ProjectFeatureService featureService = feature.GetFeatureServiceById(serviceId);
            if (featureService == null)
            {
                return(null);
            }

            ProjectFeatureServiceSummaryGetRp featureServiceSummaryRp = new ProjectFeatureServiceSummaryGetRp
            {
                Name                      = featureService.ProjectService.Name,
                Description               = featureService.ProjectService.Description,
                Status                    = featureService.Status,
                PipeType                  = featureService.ProjectService.PipeType,
                PipelineStatus            = featureService.PipelineStatus,
                LastPipelineBuildStatus   = featureService.LastPipelineBuildStatus,
                LastPipelineReleaseStatus = featureService.LastPipelineReleaseStatus,
                Activities                = new ProjectFeatureServiceActivityListRp()
                {
                    Items = featureService.Activities.OrderBy(x => x.CreationDate).Select(x => new ProjectFeatureServiceActivityListItemRp()
                    {
                        Name           = x.Name,
                        Log            = x.Log,
                        CreationDate   = x.CreationDate,
                        ActivityStatus = x.ActivityStatus,
                    }).ToList()
                },
                Events = new ProjectFeatureServiceEventListRp()
                {
                    Items = featureService.Events.OrderByDescending(x => x.EventDate).Take(6).Select(x => new ProjectFeatureServiceEventListItemRp()
                    {
                        EventDescription = x.EventDescription,
                        EventType        = x.EventType,
                        EventStatus      = x.EventStatus,
                        CreationDate     = x.CreationDate,
                        EventDate        = x.EventDate
                    }).ToList()
                }
            };

            return(featureServiceSummaryRp);
        }
        public async Task CreateProjectFeature(Guid organizationId, Guid projectId, ProjectFeaturePostRp 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 features 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;
            }

            if (resource.Services.Length == 0)
            {
                await _domainManagerService.AddConflict($"At least one pipe must be included in the feature.");

                return;
            }

            var preparingServices = project.GetPreparingServices();

            if (preparingServices.Any())
            {
                await _domainManagerService.AddConflict($"The project with id {projectId} has pipes in status Preparing. All services must be in status Active to create a new feature");

                return;
            }

            DomainModels.ProjectFeature existingFeature = project.GetFeatureByName(resource.Name);
            if (existingFeature != null)
            {
                await _domainManagerService.AddConflict($"The feature name {resource.Name} has already been taken.");

                return;
            }

            DomainModels.ProjectFeature newFeature = user.CreateProjectFeature(organizationId, projectId, resource.Name, resource.Description);

            //services asociated (TODO: services on demand)
            List <ProjectFeatureServiceCreatedEvent> @events = new List <ProjectFeatureServiceCreatedEvent>();

            foreach (var item in resource.Services)
            {
                DomainModels.ProjectService projectService = project.GetServiceById(item);
                if (projectService == null)
                {
                    await _domainManagerService.AddConflict($"The pipe id {item} does not exists.");

                    return;
                }

                var variables = projectService.Environments.First(x => x.ProjectEnvironment.Type == EnvironmentType.Root).Variables;

                newFeature.AddService(item, variables);

                var serviceCredential = this._cloudCredentialService.ProjectServiceCredentialResolver(project, projectService);

                @events.Add(new ProjectFeatureServiceCreatedEvent(_correlationId)
                {
                    ServiceId                  = item,
                    ServiceExternalId          = projectService.ProjectServiceExternalId,
                    ServiceExternalUrl         = projectService.ProjectServiceExternalUrl,
                    ServiceName                = projectService.Name,
                    InternalServiceName        = projectService.InternalName,
                    ServiceTemplateUrl         = serviceCredential.BranchUrl,
                    ReleaseStageId             = projectService.ReleaseStageId,
                    AgentPoolId                = project.AgentPoolId,
                    OrganizationId             = organization.OrganizationId,
                    ProjectId                  = project.ProjectId,
                    FeatureId                  = newFeature.ProjectFeatureId,
                    ProjectExternalId          = serviceCredential.ProjectExternalId,
                    ProjectExternalEndpointId  = project.ProjectExternalEndpointId,
                    ProjectExternalGitEndpoint = project.ProjectExternalGitEndpoint,
                    ProjectVSTSFakeName        = project.ProjectVSTSFakeName,
                    ProjectVSTSFakeId          = project.ProjectVSTSFakeId,
                    OrganizationName           = organization.Name,
                    ProjectName                = serviceCredential.ProjectName,
                    FeatureName                = newFeature.Name,
                    CMSType                = serviceCredential.CMSType,
                    CMSAccountId           = serviceCredential.AccountId,
                    CMSAccountName         = serviceCredential.AccountName,
                    CMSAccessId            = serviceCredential.AccessId,
                    CMSAccessSecret        = serviceCredential.AccessSecret,
                    CMSAccessToken         = serviceCredential.AccessToken,
                    CPSType                = project.OrganizationCPS.Type,
                    CPSAccessId            = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessId),
                    CPSAccessName          = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessName),
                    CPSAccessSecret        = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessSecret),
                    CPSAccessRegion        = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessRegion),
                    TemplateAccess         = projectService.ProjectServiceTemplate.TemplateAccess,
                    IsImported             = projectService.IsImported,
                    NeedCredentials        = projectService.ProjectServiceTemplate.NeedCredentials,
                    RepositoryCMSType      = serviceCredential.CMSType,
                    RepositoryAccessId     = serviceCredential.AccessId,
                    RepositoryAccessSecret = serviceCredential.AccessSecret,
                    RepositoryAccessToken  = serviceCredential.AccessToken,
                    UserId = loggedUserId
                });
            }

            _userRepository.Update(user);

            await _userRepository.SaveChanges();

            await _domainManagerService.AddResult("FeatureId", newFeature.ProjectFeatureId);

            //send events
            foreach (var @event in @events)
            {
                await _eventBusService.Publish(queueName : "ProjectFeatureServiceCreatedEvent", @event : @event);
            }
        }