Exemple #1
0
        public static async Task <string> CreateVSTSProject(
            [ActivityTrigger] VSTSIntegrationContext vstsIntegrationContext,
            ILogger log
            )
        {
            string Url     = string.Format(@"https://{0}.visualstudio.com/_apis/projects?api-version=4.0-preview", vstsIntegrationContext.VstsInstance);
            var    content = JsonConvert.SerializeObject(
                new
            {
                name         = $"{vstsIntegrationContext.ChallengeProjectName}",
                description  = vstsIntegrationContext.ChallengeProjectDescription,
                capabilities = new
                {
                    versioncontrol = new
                    {
                        sourceControlType = vstsIntegrationContext.SourceControlType
                    },
                    processTemplate = new
                    {
                        templateTypeId = vstsIntegrationContext.TemplateTypeId
                    }
                }
            });
            var response = await VSTSHelpers.CallVSTSAPI(vstsIntegrationContext.VstsInstance, vstsIntegrationContext.VstsPAT, Url, "POST", content);

            response.EnsureSuccessStatusCode();
            dynamic data = await response.Content.ReadAsAsync <object>();

            return(data.url);
        }
Exemple #2
0
        public static async Task <string> ImportChallengeSourceGitRepository(
            [ActivityTrigger] VSTSIntegrationContext vstsIntegrationContext,
            ILogger log
            )
        {
            string Url     = $@"https://{vstsIntegrationContext.VstsInstance}.visualstudio.com/{vstsIntegrationContext.ChallengeProjectName}/_apis/git/repositories/{vstsIntegrationContext.ChallengeProjectName}/importRequests?api-version=4.0-preview";
            var    content = JsonConvert.SerializeObject(
                new
            {
                parameters = new
                {
                    gitSource = new
                    {
                        url = vstsIntegrationContext.ChallengeSourceGitUrl
                    },
                    serviceEndpointId = vstsIntegrationContext.ChallengeSourceGitEndPointId,
                    deleteServiceEndpointAfterImportIsDone = true
                }
            });
            var response = await VSTSHelpers.CallVSTSAPI(vstsIntegrationContext.VstsInstance, vstsIntegrationContext.VstsPAT, Url, "POST", content);

            response.EnsureSuccessStatusCode();
            dynamic data = await response.Content.ReadAsAsync <object>();

            return(data.url);
        }
        public static async Task <string> CreateChallengeSourceGitServiceEndpoint(
            [ActivityTrigger] VSTSIntegrationContext vstsIntegrationContext,
            ILogger log
            )
        {
            string Url     = string.Format($@"https://{vstsIntegrationContext.VstsInstance}.visualstudio.com/{vstsIntegrationContext.ChallengeProjectName}/_apis/distributedtask/serviceendpoints?api-version=4.1-preview");
            var    content = JsonConvert.SerializeObject(
                new
            {
                name          = vstsIntegrationContext.ChallengeProjectName,
                type          = "git",
                url           = vstsIntegrationContext.ChallengeSourceGitUrl,
                authorization = new
                {
                    scheme     = "UsernamePassword",
                    parameters = new
                    {
                        username = "",
                        password = vstsIntegrationContext.VstsPAT
                    }
                },
                isReady = true
            });
            var response = await VSTSHelpers.CallVSTSAPI(vstsIntegrationContext.VstsInstance, vstsIntegrationContext.VstsPAT, Url, "POST", content);

            response.EnsureSuccessStatusCode();
            dynamic data = await response.Content.ReadAsAsync <object>();

            return(data.id);
        }
        public static async Task <VstsInstanceEntity> GetAvailableVSTSInstance(
            [ActivityTrigger] VSTSIntegrationContext vstsIntegrationContext,
            ILogger log
            )
        {
            var VstsInstanceService = new VstsInstanceService(Environment.GetEnvironmentVariable("SkillsBundleTablesConnectionsString"));

            return(await VstsInstanceService.GetAvailableInstance(vstsIntegrationContext.AzureLocation));
        }
Exemple #5
0
        public static async Task <int> GetDefaultProjectGitRepositorySize(
            [ActivityTrigger] VSTSIntegrationContext vstsIntegrationContext,
            ILogger log
            )
        {
            string Url      = string.Format($@"https://{vstsIntegrationContext.VstsInstance}.visualstudio.com/{vstsIntegrationContext.ChallengeProjectName}/_apis/git/repositories/{vstsIntegrationContext.ChallengeProjectName}?api-version=4.1-preview");
            var    response = await VSTSHelpers.CallVSTSAPI(vstsIntegrationContext.VstsInstance, vstsIntegrationContext.VstsPAT, Url, "GET", null);

            response.EnsureSuccessStatusCode();
            dynamic data = await response.Content.ReadAsAsync <object>();

            return(data.size);
        }
Exemple #6
0
        public static async Task <string> AddUserAsProjectContributor(
            [ActivityTrigger] VSTSIntegrationContext vstsIntegrationContext,
            ILogger log
            )
        {
            log.LogInformation("Start adding the user to VSTS");
            var    accountName = vstsIntegrationContext.VstsInstance;
            string Url         = string.Format(@"https://{0}.vsaex.visualstudio.com/_apis/userentitlements/{1}?api-version=5.0-preview.2"
                                               , vstsIntegrationContext.VstsInstance
                                               , vstsIntegrationContext.VstsUserId);
            var content = JsonConvert.SerializeObject(
                new List <object>()
            {
                new
                {
                    from  = "",
                    op    = "add",
                    path  = "/projectEntitlements",
                    value = new{
                        projectRef = new {
                            id = vstsIntegrationContext.ChallengeCreatedProjectId
                        },
                        group = new {
                            groupType = "projectContributor"
                        }
                    }
                }
            });

            var response = await VSTSHelpers.CallRestAPI(vstsIntegrationContext.VstsPAT, Url, "Patch", content, log, "application/json-patch+json");

            response.EnsureSuccessStatusCode();
            var result = await response.Content.ReadAsStringAsync();

            dynamic data = await response.Content.ReadAsAsync <object>();

            bool isSuccess = data.isSuccess;

            if (!isSuccess)
            {
                var    exceptionMessage     = "$Could not add user {vstsIntegrationContext.VstsUserId} as contributor to project {vstsIntegrationContext.ChallengeCreatedProjectId} on instance vstsIntegrationContext.VstsInstance";
                string responseErrorMessage = data.errors[0].value;
                exceptionMessage += responseErrorMessage;
                throw new Exception(exceptionMessage);
            }
            return(isSuccess.ToString());
        }
Exemple #7
0
        public static async Task <string> GetChallengeVSTSProject(
            [ActivityTrigger] VSTSIntegrationContext vstsIntegrationContext,
            ILogger log
            )
        {
            string Url      = $@"https://{vstsIntegrationContext.VstsInstance}.visualstudio.com/_apis/projects/{vstsIntegrationContext.ChallengeProjectName}?api-version=4.0-preview";
            var    response = await VSTSHelpers.CallVSTSAPI(vstsIntegrationContext.VstsInstance, vstsIntegrationContext.VstsPAT, Url, "GET", null);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                dynamic data = await response.Content.ReadAsAsync <object>();

                return(data.id);
            }
            else
            {
                return(null);
            }
        }
        public static async Task <Dictionary <string, string> > GetProjectEndpoints(
            [ActivityTrigger] VSTSIntegrationContext vstsIntegrationContext,
            ILogger log
            )
        {
            string Url      = string.Format($@"https://{vstsIntegrationContext.VstsInstance}.visualstudio.com/{vstsIntegrationContext.ChallengeProjectName}/_apis/serviceendpoint/endpoints?api-version=5.0-preview.2");
            var    response = await VSTSHelpers.CallVSTSAPI(vstsIntegrationContext.VstsInstance, vstsIntegrationContext.VstsPAT, Url, "GET", null);

            response.EnsureSuccessStatusCode();
            dynamic data = await response.Content.ReadAsAsync <object>();

            var listEP = new Dictionary <string, string>();

            foreach (var ep in data.value)
            {
                string id   = ep.id;
                string name = ep.name;
                log.LogInformation($"id EP: {id} and name : {name}");
                listEP.Add(id, name);
            }
            return(listEP);;
        }
Exemple #9
0
        public static async Task <string> AddUserEntitlment(
            [ActivityTrigger] VSTSIntegrationContext vstsIntegrationContext,
            ILogger log
            )
        {
            try
            {
                var    accountName = vstsIntegrationContext.VstsInstance;
                string Url         = string.Format(@"https://{0}.vsaex.visualstudio.com/_apis/userentitlements?api-version=4.1-preview"
                                                   , vstsIntegrationContext.VstsInstance);
                var content = JsonConvert.SerializeObject(
                    new
                {
                    accessLevel = new
                    {
                        accountLicenseType = "express"
                    },
                    user = new
                    {
                        principalName = vstsIntegrationContext.Email,
                        subjectKind   = "user"
                    }
                });
                log.LogInformation("===========PAT: vstsIntegrationContext.VstsPAT");
                var response = await VSTSHelpers.CallVSTSAPI(vstsIntegrationContext.VstsInstance, vstsIntegrationContext.VstsPAT, Url, "POST", content);

                log.LogInformation("====response:" + response);
                response.EnsureSuccessStatusCode();
                dynamic data = await response.Content.ReadAsAsync <object>();

                return(data.operationResult.userId);
            }
            catch (Exception ex)
            {
                log.LogError(ex.ToString());
                throw;
            }
        }