public async Task <CMSPipelineAgentQueueResultModel> GetQueue(CMSPipelineAgentQueueParamModel @options)
        {
            string accountUrl = $"https://{@options.VSTSAccountName}.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 queueUrl      = $"/{@options.VSTSAccountProjectId}/_apis/distributedtask/queues";
            var    queueResponse = await _httpProxyService.GetAsync(queueUrl, authCredentials);

            queueResponse.EnsureSuccessStatusCode();

            var queues = await queueResponse.MapTo <CMSPipelineQueueListModel>();

            var defaultQueue = queues.Items.FirstOrDefault(x => x.Pool.Id == int.Parse(@options.AgentPoolId));

            if (defaultQueue == null)
            {
                throw new Exception($"Agent Pool with id {@options.AgentPoolId} was not found");
            }

            return(new CMSPipelineAgentQueueResultModel()
            {
                QueueId = defaultQueue.Id,
                QueueName = defaultQueue.Name,
                PoolId = defaultQueue.Pool.Id,
                PoolName = defaultQueue.Pool.Name
            });
        }
        //actions
        public async Task CreateBuildProjectService(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 Build.");

                return;
            }

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

            CMSPipelineAgentQueueParamModel getQueueOptions = new CMSPipelineAgentQueueParamModel();

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

            getQueueOptions.ProjectName = serviceCredential.ProjectName;
            getQueueOptions.AgentPoolId = project.AgentPoolId;

            var queue = await _cmsPipelineService.GetQueue(getQueueOptions);

            if (queue == null)
            {
                await _domainManagerService.AddConflict($"The agent pool id {project.AgentPoolId} is not available.");

                return;
            }

            CMSPipelineBuildParamModel queueBuildOptions = new CMSPipelineBuildParamModel();

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

            queueBuildOptions.ProjectName       = serviceCredential.ProjectName;
            queueBuildOptions.ProjectExternalId = serviceCredential.ProjectExternalId;
            queueBuildOptions.QueueId           = queue.QueueId;
            queueBuildOptions.BuildDefinitionId = service.CommitStageId.Value;
            queueBuildOptions.SourceBranch      = service.BranchName;

            await _cmsPipelineService.CreateBuild(queueBuildOptions);

            var @event = new ProjectServiceBuildQueuedEvent(_correlationId)
            {
                OrganizationId = organization.OrganizationId,
                ProjectId      = project.ProjectId,
                ServiceId      = service.ProjectServiceId
            };

            await _eventBusService.Publish(queueName : "ProjectServiceBuildQueuedEvent", @event : @event);
        }
        public async Task CreateBuildProjectFeatureService(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;
            }

            var serviceCredential = this._cloudCredentialService.ProjectFeatureServiceCredentialResolver(project, featureService);

            CMSPipelineAgentQueueParamModel getQueueOptions = new CMSPipelineAgentQueueParamModel();

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

            getQueueOptions.ProjectName = serviceCredential.ProjectName;
            getQueueOptions.AgentPoolId = project.AgentPoolId;

            var queue = await _cmsPipelineService.GetQueue(getQueueOptions);

            if (queue == null)
            {
                await _domainManagerService.AddConflict($"The agent pool id {project.AgentPoolId} is not available.");

                return;
            }


            CMSPipelineBuildParamModel queueBuildOptions = new CMSPipelineBuildParamModel();

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

            queueBuildOptions.ProjectName       = serviceCredential.ProjectName;
            queueBuildOptions.ProjectExternalId = serviceCredential.ProjectExternalId;
            queueBuildOptions.QueueId           = queue.QueueId;
            queueBuildOptions.BuildDefinitionId = featureService.CommitStageId.Value;
            queueBuildOptions.SourceBranch      = project.OrganizationCMS.Type == ConfigurationManagementService.VSTS ? $"refs/heads/{feature.Name.ToLower()}" : feature.Name.ToLower();

            await _cmsPipelineService.CreateBuild(queueBuildOptions);

            var @event = new ProjectFeatureServiceBuildQueuedEvent(_correlationId)
            {
                OrganizationId = organization.OrganizationId,
                ProjectId      = project.ProjectId,
                FeatureId      = feature.ProjectFeatureId,
                ServiceId      = featureService.ProjectServiceId
            };

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