示例#1
0
        public async Task <CMSServiceCreateResultModel> CreateService(CMSAuthCredentialModel authCredential, CMSServiceCreateModel model)
        {
            var vstsmodel = new CMSVSTSServiceCreateModel();

            vstsmodel.Name       = model.Name;
            vstsmodel.Project    = new CMSVSTSServiceProjectCreateModel();
            vstsmodel.Project.Id = model.ProjectExternalId;

            CMSServiceCreateResultModel result = new CMSServiceCreateResultModel();

            var response = await _httpProxyService.PostAsync($"{model.ProjectExternalId}/_apis/git/repositories?api-version={_vstsOptions.Value.ApiVersion}", vstsmodel, 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 responseModel = await response.MapTo <CMSVSTSServiceCreateResultModel>();

            result.ServiceExternalId  = responseModel.Id;
            result.ServiceExternalUrl = responseModel.RemoteUrl;

            return(result);
        }
        public async Task <CMSServiceCreateResultModel> CreateService(CMSAuthCredentialModel authCredential, CMSServiceCreateModel model)
        {
            CMSServiceCreateResultModel result = new CMSServiceCreateResultModel();

            var client = new HttpClient();

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authCredential.AccessToken);
            client.BaseAddress = new Uri(authCredential.Url);

            var teamResult = await GetAccountTeams(client, authCredential);

            var defaultTeam = teamResult.Teams.FirstOrDefault(c => c.TeamId.Equals(model.TeamId));

            var response = await client.GetAsync($"/{API_VERSION}/teams/{defaultTeam.UserName}/projects/");

            var projectResult = await response.MapTo <CMSBitBucketProjectListModel>();

            var defaultProject = projectResult.Projects.FirstOrDefault(c => c.Key.Equals(model.ProjectExternalId));

            var bitbucketModel = new
            {
                scm         = "git",
                is_private  = true,
                name        = model.Name,
                description = model.Name,
                project     = new
                {
                    key = defaultProject.Key
                }
            };

            var repositoryKey = model.Name.ToLower().Replace(" ", "");

            response = await client.PostAsync($"/{API_VERSION}/repositories/{defaultTeam.UserName}/{repositoryKey}", new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(bitbucketModel), Encoding.UTF8, "application/json"));

            if (!response.IsSuccessStatusCode)
            {
                result.Fail($"Code: {response.StatusCode}, Reason: {await response.Content.ReadAsStringAsync()}");
                return(result);
            }

            var serviceCreated = await response.MapTo <CMSBitBucketRepositoryModel>();

            result.ServiceExternalId  = repositoryKey;
            result.ServiceExternalUrl = serviceCreated.Links.Clone.FirstOrDefault(c => c.Name.Equals("https", StringComparison.OrdinalIgnoreCase)).Href;

            return(result);
        }
示例#3
0
        public async Task <CMSServiceCreateResultModel> CreateService(CMSAuthCredentialModel authCredential, CMSServiceCreateModel model)
        {
            CMSServiceCreateResultModel result = new CMSServiceCreateResultModel();

            var accountList = await GetAccounts(authCredential);

            var defaultTeam = accountList.Items.FirstOrDefault(c => c.AccountId.Equals(authCredential.AccountId));

            var gitHubModel = new
            {
                name         = GetServiceName(model.ProjectName, model.Name),
                description  = model.ProjectName,
                Private      = model.IsPublic ? false : true,
                has_issues   = true,
                has_projects = true,
                has_wiki     = true
            };

            var urlRepo = "";

            if (defaultTeam != null && defaultTeam.IsOrganization)
            {
                urlRepo = $"/orgs/{defaultTeam.Name}/repos";
            }
            else
            {
                urlRepo = $"/user/repos";
            }

            var response = await _httpProxyService.PostAsync(urlRepo, gitHubModel, authCredential, Headers);

            if (!response.IsSuccessStatusCode)
            {
                result.Fail($"Code: {response.StatusCode}, Reason: {await response.Content.ReadAsStringAsync()}");
                return(result);
            }

            var serviceCreated = await response.MapTo <CMSGitHubRepositoryModel>();

            result.ServiceExternalId  = serviceCreated.Id;
            result.ServiceExternalUrl = serviceCreated.CloneUrl;

            return(result);
        }
        public async Task <CMSServiceCreateResultModel> CreateService(CMSAuthCredentialModel authCredential, CMSServiceCreateModel model)
        {
            CMSServiceCreateResultModel result = new CMSServiceCreateResultModel();

            var httpClient = new HttpClient();

            var gitLabModel = new
            {
                name                   = model.Name,
                description            = model.ProjectName,
                namespace_id           = model.ProjectExternalId,
                issues_enabled         = true,
                merge_requests_enabled = true,
                ci_config_path         = "scripts/build.definition.yml"
            };

            var request = new HttpRequestMessage(HttpMethod.Post, $"{authCredential.Url}/projects");

            request.Content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(gitLabModel), Encoding.UTF8, "application/json");

            request.Headers.Add("Private-Token", authCredential.AccessToken);

            var response = await httpClient.SendAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                result.Fail($"Code: {response.StatusCode}, Reason: {await response.Content.ReadAsStringAsync()}");
                return(result);
            }

            var serviceCreated = await response.MapTo <CMSGitLabProjectModel>();

            result.ServiceExternalId  = serviceCreated.Id.ToString();
            result.ServiceExternalUrl = serviceCreated.CloneUrl;

            return(result);
        }