private static async Task <string> CreateContent(BuildContent content, string repo, string owner)
        {
            var    buildPath = $"vsts-builds/{content.name}";
            string pat       = ConfigurationManager.AppSettings["GithubPAT"];
            string url       = ConfigurationManager.AppSettings["ContentUrl"].Replace("%%ACCESS_TOKEN%%", pat).Replace("%%REPO%%", repo).Replace("%%OWNER%%", owner).Replace("%%PATH%%", buildPath);

            string sha = string.Empty;

            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(new ProductHeaderValue("vsts2git")));
                HttpResponseMessage resp = null;
                resp = await client.PutAsJsonAsync(url, content);


                resp.EnsureSuccessStatusCode();

                string data = await resp.Content.ReadAsStringAsync();

                dynamic respContent = JsonConvert.DeserializeObject(data);

                return(respContent?.content?.html_url);
            }
        }
        private static async Task <BuildContent> SetupBuildContent(dynamic buildInfo, string repo, string owner, Binder binder)
        {
            string name   = $"{buildInfo?.resource?.definition?.id}.md";
            string result = buildInfo?.resource?.result;
            string branch = ConfigurationManager.AppSettings["ContentBranch"];

            string logsUrl = await Vsts2git.BuildContent.CopyLogsToBlob(buildInfo, binder);

            StringBuilder contentBuilder = Vsts2git.BuildContent.GetBuilderWithSummary(buildInfo);

            contentBuilder.AppendLine($"Find detailed information in the [build log files]({logsUrl})");

            string plainContent = contentBuilder.ToString();
            string base64       = Convert.ToBase64String(Encoding.UTF8.GetBytes(plainContent));

            BuildContent content = new BuildContent()
            {
                name         = name,
                result       = result,
                plainContent = plainContent,

                commiter = new Commiter()
                {
                    name  = "wasteam",
                    email = "*****@*****.**"
                },
                message = "Vsts build completed",
                content = base64,
                sha     = await GetContentSha(name, repo, owner),
                branch  = branch
            };

            return(content);
        }
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req,
            Binder binder,
            TraceWriter log)
        {
            try
            {
                string data = await req.Content.ReadAsStringAsync();

                dynamic buildInfo = JsonConvert.DeserializeObject(data);
                log.Info($"Processing {buildInfo?.message?.text}");

                var queryParams = req.RequestUri.ParseQueryString();
                var id          = buildInfo?.resource?.repository?.id?.ToString();

                var owner = id.Split('/')[0];
                var repo  = id.Split('/')[1];

                if (repo != "WindowsTemplateStudio" && repo != "Rapid-XAML-Toolkit")
                {
                    log.Info($"Invalid Repo {repo}.");
                    return(req.CreateErrorResponse(HttpStatusCode.BadRequest, $"Repo {repo} is not allowed"));
                }

                bool.TryParse(queryParams["createIssue"], out var createIssue);

                BuildContent content = await SetupBuildContent(buildInfo, repo, owner, binder);

                log.Info($"Content ready for build {buildInfo?.resource?.buildNumber}");

                Result result = new Result();
                result.url = await CreateContent(content, repo, owner);

                log.Info($"Content created for build with id {buildInfo?.resource?.id}");

                if (createIssue && (content.result == "failed" || content.result == "partiallySucceded"))
                {
                    Issue issue = SetupIssue(buildInfo?.message?.text.ToString(), content);
                    result.issueUrl = await CreateIssue(issue, repo, owner);
                }

                log.Info($"Page url: {result.url}");
                log.Info($"Issue url: {result.issueUrl}");

                return(req.CreateResponse(HttpStatusCode.OK, result));
            }
            catch (Exception ex)
            {
                log.Error("Unexpected error.", ex);
                return(req.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
        }
        private static Issue SetupIssue(string title, BuildContent buildContent)
        {
            Issue
                issue = new Issue()
            {
                title     = title,
                body      = buildContent.plainContent,
                labels    = buildContent.result == "failed" || buildContent.result == "partiallySucceeded" ? new string[] { "bug", "vsts-build" } : new string[] { "vsts-build" },
                assignees = new string[0]
            };

            return(issue);
        }