Ejemplo n.º 1
0
        public async Task <CMSProjectCreateResultModel> CreateProject(CMSAuthCredentialModel authCredential, CMSProjectCreateModel model)
        {
            //default data
            var vstsmodel = new CMSVSTSProjectCreateModel();

            vstsmodel.Name         = model.Name;
            vstsmodel.Description  = model.Description;
            vstsmodel.Visibility   = model.ProjectVisibility == Domain.Models.ProjectVisibility.Private ? "private" : "public";
            vstsmodel.Capabilities = new CMSVSTSProjectCapabilityModel();
            vstsmodel.Capabilities.VersionControl = new CMSVSTSProjectVersionControlModel();
            vstsmodel.Capabilities.VersionControl.SourceControlType = "Git";
            vstsmodel.Capabilities.ProcessTemplate = new CMSVSTSProjectProcessTemplateModel();
            vstsmodel.Capabilities.ProcessTemplate.TemplateTypeId = "adcc42ab-9882-485e-a3ed-7678f01f66bc";

            CMSProjectCreateResultModel result = new CMSProjectCreateResultModel();

            var response = await _httpProxyService.PostAsync($"/_apis/projects?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);
            }

            return(result);
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> Insert(Category obj)
        {
            var resource = new CategoryPostRep
            {
                Name        = obj.Name,
                Description = obj.Description
            };

            var response = await _proxy.PostAsync("categories", resource : resource);

            if (response.IsSuccessStatusCode)
            {
                CategoryViewModel model = new CategoryViewModel();
                var responseList        = await this._proxy.GetAsync("categories");

                dynamic responseContent = await responseList.Content.ReadAsAsync <Object>();

                model.Categories       = responseContent.Items.ToObject <List <Category> >();
                model.SelectedCategory = model.Categories.Find(o => o.Id == obj.Id);
                model.DisplayMode      = "ReadOnly";
                return(View("Index", model));
            }

            return(View("Index"));
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> Insert(Author obj)
        {
            var resource = new AuthorPostRep
            {
                FirstName = obj.FirstName,
                LastName  = obj.LastName,
                Country   = obj.Country
            };

            var response = await _proxy.PostAsync("authors", resource : resource);

            if (response.IsSuccessStatusCode)
            {
                AuthorViewModel model        = new AuthorViewModel();
                var             responseList = await this._proxy.GetAsync("authors");

                dynamic responseContent = await responseList.Content.ReadAsAsync <Object>();

                model.Authors        = responseContent.Items.ToObject <List <Author> >();
                model.SelectedAuthor = model.Authors.Find(o => o.Id == obj.Id);
                model.DisplayMode    = "ReadOnly";
                return(View("Index", model));
            }

            return(View("Index"));
        }
Ejemplo n.º 4
0
        public async Task <ActionResult> Insert(Book obj)
        {
            var resource = new BookPostRep
            {
                Name       = obj.Name,
                AuthorId   = obj.AuthorId,
                CategoryId = obj.CategoryId
            };

            var response = await _proxy.PostAsync("books", resource : resource);

            if (response.IsSuccessStatusCode)
            {
                BookViewModel model        = new BookViewModel();
                var           responseList = await this._proxy.GetAsync("books");

                dynamic responseContent = await responseList.Content.ReadAsAsync <Object>();

                model.Books        = responseContent.Items.ToObject <List <Book> >();
                model.SelectedBook = model.Books.Find(o => o.Id == obj.Id);
                model.DisplayMode  = "ReadOnly";
                return(View("Index", model));
            }

            return(View("Index"));
        }
        public async Task CreateBuild(CMSPipelineBuildParamModel @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 queueBuildUrl = $"/{@options.VSTSAccountProjectId}/_apis/build/builds?api-version={@options.VSTSAPIVersion}";

            var queueBuild         = PipelineBuildModel.Create(@options.QueueId, @options.BuildDefinitionId, @options.ProjectExternalId, @options.SourceBranch);
            var queueBuildResponse = await _httpProxyService.PostAsync(queueBuildUrl, queueBuild, authCredentials);

            queueBuildResponse.EnsureSuccessStatusCode();
        }
Ejemplo n.º 6
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);
        }