Example #1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("a new PR has been created...");

            req.Headers.TryGetValue("X-GitHub-Event", out StringValues eventName);
            req.Headers.TryGetValue("X-Hub-Signature", out StringValues signature);
            req.Headers.TryGetValue("X-GitHub-Delivery", out StringValues delivery);

            log.LogInformation($"EventName : {eventName}");
            log.LogInformation($"Signature : {signature}");
            log.LogInformation($"Delivery : {delivery}");

            var body = await new StreamReader(req.Body).ReadToEndAsync();

            log.LogInformation($"Body has been read to end ({body.Length} characters)");

            var payload = JsonConvert.DeserializeObject <WebHookPayload>(body);

            log.LogInformation($"Body parsed and stored in the payload object (pull request number is {payload.Number})");

            var isPayloadReview = payload?.Action == "submitted";

            var isAzureRelated = payload?.PullRequest?.Labels.Count(e => e.Name == "Azure") > 0;

            var isApproved = payload?.Review?.State == "approved";

            if (!isPayloadReview || !isAzureRelated || !isApproved)
            {
                return(new OkObjectResult(""));
            }

            var repo = payload?.Repository?.FullName;

            log.LogInformation($"Repo : {repo}");

            var branch = payload?.PullRequest?.Head?.Ref;

            log.LogInformation($"Branch : {branch}");

            var pr_number = payload?.PullRequest?.Number;

            log.LogInformation($"PRNumber : {pr_number}");

            log.LogInformation("CI pipeline is about to be triggered");

            var isTriggered = await GitHubHelper.TriggerWorkflowAsync(repo, branch, pr_number);

            if (isTriggered)
            {
                log.LogInformation("CI pipeline is successfully triggered");
            }

            return(new OkObjectResult("OK"));
        }
Example #2
0
        public static async Task Run(
            [TimerTrigger("* */15 * * * *")] TimerInfo myTimer,
            [Table("zagreb")] CloudTable table,
            ILogger log)
        {
            log.LogInformation("schedule...");

            var prs = await GitHubHelper.GetPullRequestsAsync();

            foreach (var pr in prs)
            {
                log.LogInformation($"{pr.Title} -> {string.Join(',', pr.Labels.Select(e => e.Name).ToList())}");

                var partitionKey = pr.Head.Repo.Name;
                var rowKey       = pr.Number.ToString();

                pr.PartitionKey = partitionKey;
                pr.RowKey       = rowKey;

                var result = table.Execute(TableOperation.Retrieve <PullRequest>(partitionKey, rowKey));

                if (result?.Result == null)
                {
                    table.Execute(TableOperation.InsertOrMerge(pr));

                    log.LogInformation("pr has been saved");
                }

                var fromTable = result?.Result as PullRequest;

                if (fromTable == null || fromTable?.UpdatedAt != pr.UpdatedAt)
                {
                    log.LogWarning($"fromTable: {fromTable?.UpdatedAt}");
                    log.LogWarning($"pr: {pr?.UpdatedAt}");

                    table.Execute(TableOperation.InsertOrMerge(pr));

                    log.LogInformation($"{pr.Number} either detected first time or has been changed since last time");

                    var repo = pr.Head.Repo.FullName;
                    log.LogInformation($"Repo : {repo}");

                    var branch = pr.Head.Ref;
                    log.LogInformation($"Branch : {branch}");

                    var isTriggered = await GitHubHelper.TriggerWorkflowAsync(repo, branch, pr.Number);

                    if (isTriggered)
                    {
                        log.LogInformation("CI pipeline is successfully triggered");
                    }
                }
            }
        }