public async Task DeleteRepository(ProjectServiceDeletedEvent @event)
        {
            HttpClientWrapperAuthorizationModel authCredentials = new HttpClientWrapperAuthorizationModel();

            authCredentials.Schema = "Bearer";
            authCredentials.Value  = @event.CMSAccessToken;

            string deleteRepositoryUrl      = $"https://api.github.com/repos/{@event.CMSAccountName}/{@event.ServiceName}";
            var    deleteRepositoryResponse = await _httpClientWrapperService.DeleteAsync(deleteRepositoryUrl, authCredentials, Headers);

            deleteRepositoryResponse.EnsureSuccessStatusCode();
        }
Beispiel #2
0
        public async Task DeleteRepository(ProjectServiceDeletedEvent @event)
        {
            string accountUrl = $"https://{@event.CMSAccountName}.visualstudio.com";

            HttpClientWrapperAuthorizationModel authCredentials = new HttpClientWrapperAuthorizationModel();

            authCredentials.Schema = "Basic";
            authCredentials.Value  = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", @event.CMSAccessSecret)));

            string projectUrl      = $"{accountUrl}/{@event.ProjectExternalId}/_apis/git/repositories/{@event.ProjectServiceExternalId}?api-version={_vstsOptions.Value.ApiVersion}";
            var    projectResponse = await _httpClientWrapperService.DeleteAsync(projectUrl, authCredentials);

            projectResponse.EnsureSuccessStatusCode();
        }
        public async Task DeleteRepository(ProjectServiceDeletedEvent @event)
        {
            var client = new HttpClient();

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", @event.CMSAccountId, @event.CMSAccessSecret))));
            client.BaseAddress = new Uri(API_URL);

            var response = await client.GetAsync($"/{API_VERSION}/teams?role=admin");

            var teamResult = await response.MapTo <CMSBitBucketTeamListModel>();

            var defaultTeam = teamResult.Teams.FirstOrDefault(c => c.TeamId.Equals(@event.OrganizationExternalId));

            response = await client.DeleteAsync($"/{API_VERSION}/repositories/{defaultTeam.UserName}/{@event.ProjectServiceExternalId}");

            response.EnsureSuccessStatusCode();
        }
        public async Task DeleteProjectService(Guid organizationId, Guid projectId, Guid serviceId)
        {
            string loggedUserId = _identityService.GetUserId();

            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 = organization.GetProjectById(projectId);
            if (project == null)
            {
                await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists.");

                return;
            }

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

                return;
            }

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

                return;
            }

            if (service.Status != EntityStatus.Active)
            {
                await _domainManagerService.AddConflict($"The pipe with id {serviceId} must be in status Active to be modified/deleted.");

                return;
            }

            /*Check If any feature is associated with the service*/
            var features = project.GetFeatures();

            foreach (var feature in features)
            {
                var featureService = feature.GetFeatureServiceById(serviceId);
                if (featureService != null)
                {
                    await _domainManagerService.AddConflict($"The are active features ({feature.Name}) using the service, you cannot delete the service.");

                    return;
                }
            }

            user.DeleteProjectService(organizationId, projectId, serviceId);

            _userRepository.Update(user);

            await _userRepository.SaveChanges();

            if (project.OrganizationCPS == null)
            {
                project.OrganizationCPS = new OrganizationCPS {
                    Type = CloudProviderService.None
                }
            }
            ;

            //send event
            var projectServiceDeletedEvent = new ProjectServiceDeletedEvent(_correlationId)
            {
                OrganizationExternalId                = project.OrganizationExternalId,
                OrganizationName                      = organization.Name,
                ProjectName                           = project.Name,
                ServiceName                           = service.Name,
                ProjectVSTSFakeName                   = project.ProjectVSTSFakeName,
                ProjectExternalId                     = project.ProjectExternalId,
                ProjectServiceExternalId              = service.ProjectServiceExternalId,
                CommitStageId                         = service.CommitStageId,
                ReleaseStageId                        = service.ReleaseStageId,
                CommitServiceHookId                   = service.CommitServiceHookId,
                CodeServiceHookId                     = service.CodeServiceHookId,
                ReleaseStartedServiceHookId           = service.ReleaseStartedServiceHookId,
                ReleasePendingApprovalServiceHookId   = service.ReleasePendingApprovalServiceHookId,
                ReleaseCompletedApprovalServiceHookId = service.ReleaseCompletedApprovalServiceHookId,
                ReleaseServiceHookId                  = service.ReleaseServiceHookId,
                Environments                          = project.GetEnvironments().Select(x => x.Name).ToList(),
                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),
                CPSType            = project.OrganizationCPS.Type,
                CPSAccessId        = project.OrganizationCPS.Type == CloudProviderService.None ? string.Empty : _dataProtectorService.Unprotect(project.OrganizationCPS.AccessId),
                CPSAccessName      = project.OrganizationCPS.Type == CloudProviderService.None ? string.Empty : _dataProtectorService.Unprotect(project.OrganizationCPS.AccessName),
                CPSAccessSecret    = project.OrganizationCPS.Type == CloudProviderService.None ? string.Empty : _dataProtectorService.Unprotect(project.OrganizationCPS.AccessSecret),
                CPSAccessRegion    = project.OrganizationCPS.Type == CloudProviderService.None ? string.Empty : _dataProtectorService.Unprotect(project.OrganizationCPS.AccessRegion),
                CPSAccessAppId     = project.OrganizationCPS.Type == CloudProviderService.None ? string.Empty : _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppId),
                CPSAccessAppSecret = project.OrganizationCPS.Type == CloudProviderService.None ? string.Empty : _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppSecret),
                CPSAccessDirectory = project.OrganizationCPS.Type == CloudProviderService.None ? string.Empty : _dataProtectorService.Unprotect(project.OrganizationCPS.AccessDirectory),
                SourceEvent        = DomainModels.Enums.SourceEvent.Service
            };

            await _eventBusService.Publish(queueName : "ProjectServiceDeletedEvent", @event : projectServiceDeletedEvent);
        }
        public async Task DeleteProject(Guid organizationId, Guid projectId)
        {
            string loggedUserId = _identityService.GetUserId();

            User user = await _userRepository.GetUser(loggedUserId);

            Organization organization = user.FindOrganizationById(organizationId);

            if (organization == null)
            {
                await _domainManagerService.AddNotFound($"The organzation with id {organizationId} does not exists.");

                return;
            }

            Project project = user.FindProjectById(projectId, false);

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

                return;
            }

            PipelineRole role = user.GetRoleInProject(projectId);

            if (role != PipelineRole.ProjectAdmin)
            {
                await _domainManagerService.AddForbidden($"You are not authorized to delete this project.");

                return;
            }

            if (project.Status != EntityStatus.Active)
            {
                await _domainManagerService.AddConflict($"The project with id {projectId} must be in status Active to be modified/deleted.");

                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 delete the project");

                return;
            }

            var preparingFeatures = project.GetPreparingFeatures();

            if (preparingFeatures.Any())
            {
                await _domainManagerService.AddConflict($"The project with id {projectId} has features in status Preparing. All features must be in status Active to delete the project");

                return;
            }

            user.DeleteProject(organizationId, projectId);

            _userRepository.Update(user);

            await _userRepository.SaveChanges();

            //send event to delete project in CMS
            var projectDeletedEvent = new ProjectDeletedEvent(_correlationId, project.IsImported)
            {
                OrganizationExternalId = project.OrganizationExternalId,
                ProjectExternalId      = project.ProjectExternalId,
                ProjectVSTSFakeId      = project.ProjectVSTSFakeId,
                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)
            };

            await _eventBusService.Publish(queueName : "ProjectDeletedEvent", @event : projectDeletedEvent);

            //send event to delete cloud services in CPS - Services
            var services = await _projectRepository.GetProjectServices(organizationId, projectId);

            var environments = await _projectRepository.GetProjectEnvironments(organizationId, projectId);

            foreach (var service in services)
            {
                var projectServiceDeletedEvent = new ProjectServiceDeletedEvent(_correlationId)
                {
                    OrganizationExternalId                = project.OrganizationExternalId,
                    OrganizationName                      = organization.Name,
                    ProjectName                           = project.Name,
                    ServiceName                           = service.Name,
                    ProjectVSTSFakeName                   = project.ProjectVSTSFakeName,
                    ProjectExternalId                     = project.ProjectExternalId,
                    ProjectServiceExternalId              = service.ProjectServiceExternalId,
                    CommitStageId                         = service.CommitStageId,
                    ReleaseStageId                        = service.ReleaseStageId,
                    CommitServiceHookId                   = service.CommitServiceHookId,
                    ReleaseServiceHookId                  = service.ReleaseServiceHookId,
                    CodeServiceHookId                     = service.CodeServiceHookId,
                    ReleaseStartedServiceHookId           = service.ReleaseStartedServiceHookId,
                    ReleasePendingApprovalServiceHookId   = service.ReleasePendingApprovalServiceHookId,
                    ReleaseCompletedApprovalServiceHookId = service.ReleaseCompletedApprovalServiceHookId,
                    Environments                          = environments.Select(x => x.Name).ToList(),
                    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),
                    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),
                    CPSAccessAppId     = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppId),
                    CPSAccessAppSecret = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppSecret),
                    CPSAccessDirectory = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessDirectory),
                    SourceEvent        = Domain.Models.Enums.SourceEvent.Project
                };

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

            //send event to delete cloud services in CPS - Features
            var features = await _projectRepository.GetProjectFeatures(organizationId, projectId);

            foreach (var feature in features)
            {
                List <ProjectFeatureServiceDeletedEvent> projectFeatureServiceDeletedEventList = new List <ProjectFeatureServiceDeletedEvent>();

                var featureServices = await _projectFeatureRepository.GetProjectFeatureServices(organizationId, projectId, feature.ProjectFeatureId);

                foreach (var item in featureServices)
                {
                    projectFeatureServiceDeletedEventList.Add(new ProjectFeatureServiceDeletedEvent(_correlationId)
                    {
                        ServiceId                             = item.ProjectServiceId,
                        ServiceExternalId                     = item.ProjectService.ProjectServiceExternalId,
                        ServiceExternalUrl                    = item.ProjectService.ProjectServiceExternalUrl,
                        ServiceName                           = item.ProjectService.Name,
                        ServiceTemplateUrl                    = item.ProjectService.ProjectServiceTemplate.Url,
                        CommitStageId                         = item.CommitStageId,
                        ReleaseStageId                        = item.ReleaseStageId,
                        CommitServiceHookId                   = item.CommitServiceHookId,
                        ReleaseServiceHookId                  = item.ReleaseServiceHookId,
                        CodeServiceHookId                     = item.CodeServiceHookId,
                        ReleaseStartedServiceHookId           = item.ReleaseStartedServiceHookId,
                        ReleasePendingApprovalServiceHookId   = item.ReleasePendingApprovalServiceHookId,
                        ReleaseCompletedApprovalServiceHookId = item.ReleaseCompletedApprovalServiceHookId
                    });
                }

                var projectFeatureDeletedEvent = new ProjectFeatureDeletedEvent(_correlationId)
                {
                    OrganizationId            = organization.OrganizationId,
                    OrganizationName          = organization.Name,
                    ProjectId                 = project.ProjectId,
                    Services                  = projectFeatureServiceDeletedEventList,
                    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),
                    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),
                    CPSAccessAppId     = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppId),
                    CPSAccessAppSecret = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppSecret),
                    CPSAccessDirectory = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessDirectory),
                    SourceEvent        = Domain.Models.Enums.SourceEvent.Project
                };

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