예제 #1
0
        public static async Task Run([QueueTrigger("scheduledprsqueue", Connection = "AzureWebJobsStorage")] string myQueueItem, ILogger log)
        {
            MergeData mdQueueObject = Newtonsoft.Json.JsonConvert.DeserializeObject <MergeData>(myQueueItem);

            log.LogInformation($"Queue trigger function processed: Merge time " + mdQueueObject.MergeTime + " and branch name " + mdQueueObject.BranchName);

            // this env var should have an xml body containing an RSA key
            var xmlGHPrivateKey = Environment.GetEnvironmentVariable("GitHubPrivateKey");
            var handler         = new GitHubEventHandlers(log, xmlGHPrivateKey, GitHubWebhook.AppId);

            try
            {
                await handler.PassMergeAsync(mdQueueObject);

                await handler.MergePRAsync(mdQueueObject);

                if (!string.IsNullOrWhiteSpace(mdQueueObject.BranchName))
                {
                    // branchname specified, then create new PR from this PR's dest to the specified dest
                    await handler.CreateNewPR(mdQueueObject);
                }
            }
            catch (Exception e)
            {
                log.LogInformation(e, "Caught exception when handling queue item.");
            }
        }
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("Got GitHub Webhook.");

            string eventType = string.Empty;

            if (req.Headers.ContainsKey(EventType))
            {
                eventType = req.Headers[EventType];
                log.LogInformation($"Rec. event of type: {eventType}");
            }

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

            log.LogInformation($"Request body: {requestBody}");

            // Establishing connectivity to queue
            var azureStorageConnectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
            CloudStorageAccount csAccount    = CloudStorageAccount.Parse(azureStorageConnectionString);
            CloudQueueClient    cQueueClient = csAccount.CreateCloudQueueClient();
            CloudQueue          cQueue       = cQueueClient.GetQueueReference("scheduledprsqueue");

            // this env var should have an xml body containing an RSA key
            var xmlGHPrivateKey = Environment.GetEnvironmentVariable("GitHubPrivateKey");
            var handler         = new GitHubEventHandlers(log, xmlGHPrivateKey, AppId);

            // deserialize the payload
            var payload = JsonConvert.DeserializeObject <WebhookPayload>(requestBody);

            bool isHuman = payload?.Sender?.Type == "User"; // don't respond in an infinite loop

            if (payload != null && isHuman)
            {
                log.LogDebug("Deserialized the payload.");

                try
                {
                    switch (eventType)
                    {
                    case PullRequestEvent:
                        var prresult = CheckPRHasCommand(payload);
                        if (prresult != null)
                        {
                            log.LogInformation($"Got PR with command: {prresult.BranchName} {prresult.MergeTime}");
                            log.LogInformation($"Message insert result: " + InsertMessageToQueue(cQueue, prresult, TimeSpan.FromMinutes(5)));
                        }
                        break;

                    case IssueCommentEvent:     // same event as a PR comment, need to check that this comment is made on a PR and not an issue
                        // do a thing, but differently
                        var result = CheckCommentHasCommand(payload);
                        if (result != null)
                        {
                            // ack to the comment
                            await handler.AckAddToQueueAsync(result);

                            await handler.BlockMergeAsync(result);

                            log.LogInformation($"Got comment with command: {result.BranchName} {result.MergeTime}");
                            log.LogInformation($"Message insert result: " + InsertMessageToQueue(cQueue, result, TimeSpan.FromMinutes(5)));
                        }

                        // debug, this should be done in QueueExecutor
                        // MergePRAsync(log, xmlGHPrivateKey, result).GetAwaiter().GetResult();
                        break;
                    }
                }
                catch (Exception e)
                {
                    log.LogInformation(e, "Caught exception when processing payload.");
                }
            }
            return(new OkResult());
        }