public async Task CreateRelease(CMSPipelineReleaseParamModel @options)
        {
            string accountUrl = $"https://{@options.VSTSAccountName}.vsrm.visualstudio.com";

            CMSAuthCredentialModel authCredentials = new CMSAuthCredentialModel();

            authCredentials.Type        = "Basic";
            authCredentials.AccessToken = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", @options.VSTSAccessSecret)));
            authCredentials.Url         = accountUrl;

            string queueReleaseUrl = $"/{@options.VSTSAccountProjectId}/_apis/release/releases?api-version={@options.VSTSAPIVersion}";

            var queueRelease         = PipelineReleaseModel.Create(@options.ReleaseDefinitionId, @options.Alias, @options.VersionId, @options.VersionName, options.Description);
            var queueReleaseResponse = await _httpProxyService.PostAsync(queueReleaseUrl, queueRelease, authCredentials);

            queueReleaseResponse.EnsureSuccessStatusCode();
        }
Exemple #2
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 CreateReleaseProjectService(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 request a Request.");

                return;
            }

            if (string.IsNullOrEmpty(service.LastBuildSuccessVersionId))
            {
                await _domainManagerService.AddConflict($"The project service with id {serviceId} does not have any success build yet.");

                return;
            }

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

            CMSPipelineReleaseParamModel releaseBuildOptions = new CMSPipelineReleaseParamModel();

            releaseBuildOptions.VSTSAPIVersion       = _vstsOptions.Value.ApiVersion;
            releaseBuildOptions.VSTSAccountName      = serviceCredential.AccountName;
            releaseBuildOptions.VSTSAccessSecret     = serviceCredential.AccessSecret;
            releaseBuildOptions.VSTSAccountProjectId = serviceCredential.AccountProjectId;

            releaseBuildOptions.ProjectName         = serviceCredential.ProjectName;
            releaseBuildOptions.ProjectExternalId   = serviceCredential.ProjectExternalId;
            releaseBuildOptions.ReleaseDefinitionId = service.ReleaseStageId.Value;
            releaseBuildOptions.Alias = service.Name;

            releaseBuildOptions.VersionId   = int.Parse(service.LastBuildSuccessVersionId);
            releaseBuildOptions.VersionName = service.LastBuildSuccessVersionName;
            releaseBuildOptions.Description = "Release created from PipelineSpace";

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

                return;
            }

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

                return;
            }

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

                return;
            }

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

                return;
            }

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

                return;
            }

            if (string.IsNullOrEmpty(featureService.LastBuildSuccessVersionId))
            {
                await _domainManagerService.AddConflict($"The feature service with id {serviceId} does not have any success build yet.");

                return;
            }

            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 = featureService.ReleaseStageId.Value;
            releaseBuildOptions.Alias = $"{featureService.ProjectService.Name}-ft-{feature.Name.ToLower()}";

            releaseBuildOptions.VersionId   = int.Parse(featureService.LastBuildSuccessVersionId);
            releaseBuildOptions.VersionName = featureService.LastBuildSuccessVersionName;
            releaseBuildOptions.Description = "Release created from PipelineSpace";

            await _cmsPipelineService.CreateRelease(releaseBuildOptions);
        }