public async Task <CMSProjectAvailabilityResultModel> ValidateProjectAvailability(CMSAuthCredentialModel authCredential, string organization, string name) { CMSProjectAvailabilityResultModel result = new CMSProjectAvailabilityResultModel(); var dic = new Dictionary <string, string>(); dic.Add("Accept", "application/vnd.github.inertia-preview+json"); dic.Add("User-Agent", API_UserAgent); var accountList = await GetAccounts(authCredential); var defaultTeam = accountList.Items.FirstOrDefault(c => c.AccountId.Equals(authCredential.AccountId)); if (defaultTeam.IsOrganization) { var response = await _httpProxyService.GetAsync($"/orgs/{defaultTeam.Name}/projects", authCredential, dic); var projectResult = await response.MapTo <List <CMSGitHubProjectModel> >(); var existsProject = projectResult.Any(x => x.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)); if (existsProject) { result.Fail($"The project {name} has already been taken in the CMS service"); } } return(result); }
public async Task <CMSProjectAvailabilityResultModel> ValidateProjectAvailability(CMSAuthCredentialModel authCredential, string organization, string name) { CMSProjectAvailabilityResultModel result = new CMSProjectAvailabilityResultModel(); var response = await _httpProxyService.GetAsync($"/_apis/projects?api-version={_vstsOptions.Value.ApiVersion}", authCredential); if (!response.IsSuccessStatusCode || response.StatusCode == System.Net.HttpStatusCode.NonAuthoritativeInformation) { if (response.StatusCode == System.Net.HttpStatusCode.NonAuthoritativeInformation) { result.Fail($"Code: {response.StatusCode}, Reason: The credentials are not correct"); return(result); } result.Fail($"Code: {response.StatusCode}, Reason: {await response.Content.ReadAsStringAsync()}"); return(result); } var projectResult = await response.MapTo <CMSVSTSProjectListModel>(); var existsProject = projectResult.Items.Any(x => x.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)); if (existsProject) { result.Fail($"The project {name} has already been taken in the CMS service"); } return(result); }
public async Task <CMSVSTSObjectRepositoryModel> GetTemplateBuildDefinition(string repository, string templateName, string definitionName, CMSAuthCredentialModel authCredential) { string accountUrl = $"https://{authCredential.AccountId}.visualstudio.com"; string repositoryUrl = $"{accountUrl}/{this._vstsOptions.Value.ProjectName}/_apis/git/repositories/{repository}/items?path={templateName}/scripts/{definitionName}&$format=json&includeContent=true&versionDescriptor.version=master&versionDescriptor.versionType=branch&api-version=4.1&api-version={_vstsOptions.Value.ApiVersion}"; var response = await _httpProxyService.GetAsync(repositoryUrl, authCredential); var result = new CMSVSTSObjectRepositoryModel(); if (!response.IsSuccessStatusCode || response.StatusCode == System.Net.HttpStatusCode.NonAuthoritativeInformation) { if (response.StatusCode == System.Net.HttpStatusCode.NonAuthoritativeInformation) { result.Fail($"Code: {response.StatusCode}, Reason: The credentials are not correct"); return(result); } result.Fail($"Code: {response.StatusCode}, Reason: {await response.Content.ReadAsStringAsync()}"); return(result); } result = await response.MapTo <CMSVSTSObjectRepositoryModel>(); return(result); }
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 }); }
public async Task <CMSAgentPoolListModel> GetAgentPools(CMSAuthCredentialModel authCredential) { authCredential.Url = $"https://{_fakeAccountServiceOptions.Value.AccountId}.visualstudio.com"; authCredential.Type = "Basic"; authCredential.AccessToken = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", _fakeAccountServiceOptions.Value.AccessSecret))); CMSAgentPoolListModel result = new CMSAgentPoolListModel(); var response = await _httpProxyService.GetAsync($"/_apis/distributedtask/pools", authCredential); if (!response.IsSuccessStatusCode || response.StatusCode == System.Net.HttpStatusCode.NonAuthoritativeInformation) { if (response.StatusCode == System.Net.HttpStatusCode.NonAuthoritativeInformation) { result.Fail($"Code: {response.StatusCode}, Reason: The credentials are not correct"); return(result); } result.Fail($"Code: {response.StatusCode}, Reason: {await response.Content.ReadAsStringAsync()}"); return(result); } var agentoPoolResult = await response.MapTo <CMSAgentPoolListModel>(); result.Items = agentoPoolResult.Items.Where(x => x.Size > 0).Select(x => new CMSAgentPoolListItemModel() { Id = x.Id, Name = x.Name }).ToList(); return(result); }
public async Task <CMSAccountListModel> GetAccounts(CMSAuthCredentialModel authCredential) { CMSAccountListModel result = new CMSAccountListModel(); var response = await _httpProxyService.GetAsync($"/_apis/accounts", authCredential); if (!response.IsSuccessStatusCode || response.StatusCode == System.Net.HttpStatusCode.NonAuthoritativeInformation) { if (response.StatusCode == System.Net.HttpStatusCode.NonAuthoritativeInformation) { result.Fail($"Code: {response.StatusCode}, Reason: The credentials are not correct"); return(result); } result.Fail($"Code: {response.StatusCode}, Reason: {await response.Content.ReadAsStringAsync()}"); return(result); } var accountResult = await response.MapTo <List <CMSVSTSAccountListItemModel> >(); result.Items = accountResult.Select(x => new CMSAccountListItemModel() { AccountId = x.AccountId, Name = x.AccountName }).ToList(); return(result); }
public async Task <CMSAccountListModel> GetAccounts(CMSAuthCredentialModel authCredential) { var response = await _httpProxyService.GetAsync($"/user", authCredential, Headers); var user = await response.MapTo <CMSGitHubUserModel>(); CMSAccountListModel list = new CMSAccountListModel(); list.Items = new List <CMSAccountListItemModel>(); list.Items.Add(new CMSAccountListItemModel { AccountId = user.AccountId, Name = user.UserName, Description = user.UserName, IsOrganization = false, }); response = await _httpProxyService.GetAsync($"/user/orgs", authCredential, Headers); var teamResult = await response.MapTo <List <CMSGitHubTeamModel> >(); list.Items.AddRange(teamResult.Select(c => new CMSAccountListItemModel { AccountId = c.TeamId, Description = c.DisplayName, Name = c.DisplayName, IsOrganization = true })); return(list); }