Exemple #1
0
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "POST")] HttpRequestMessage request,
            ILogger logger,
            ExecutionContext executionContext)
        {
            var services = GetServices(executionContext);

            string eventName  = request.Headers.GetValueOrDefault("X-GitHub-Event");
            string deliveryId = request.Headers.GetValueOrDefault("X-GitHub-Delivery");
            string signature  = request.Headers.GetValueOrDefault("X-Hub-Signature");

            logger.LogInformation("Webhook delivery: Delivery id = '{DeliveryId}', Event name = '{EventName}'", deliveryId, eventName);

            var payloadBytes = await request.Content.ReadAsByteArrayAsync();

            if (!services.PayloadValidator.IsPayloadSignatureValid(payloadBytes, signature))
            {
                return(request.CreateResponse(HttpStatusCode.BadRequest, "Invalid signature"));
            }

            if (eventName == "pull_request")
            {
                var payload = await DeserializeBody <PullRequestEventPayload>(request.Content);

                if (PullRequestActions.Contains(payload.Action))
                {
                    try
                    {
                        logger.LogInformation("Handling action '{PayloadAction}' for pull request #{PullRequestNumber}", payload.Action, payload.Number);
                        var connection = await services.GithubConnectionCache.GetConnectionAsync(payload.Installation.Id);

                        var context = new PullRequestContext(payload, connection, logger);
                        await services.PullRequestHandler.HandleWebhookEventAsync(context);

                        logger.LogInformation("Finished handling action '{PayloadAction}' for pull request #{PullRequestNumber}", payload.Action, payload.Number);
                    }
                    catch (Exception ex)
                    {
                        logger.LogError(ex, "Error processing pull request webhook event {DeliveryId}", deliveryId);
                        return(request.CreateErrorResponse(HttpStatusCode.InternalServerError, new HttpError(ex.Message)));
                    }
                }
                else
                {
                    logger.LogInformation("Ignoring pull request action '{PayloadAction}'", payload.Action);
                }
            }
            else
            {
                logger.LogInformation("Unknown event '{EventName}', ignoring", eventName);
            }

            return(request.CreateResponse(HttpStatusCode.NoContent));
        }
Exemple #2
0
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger("POST", WebHookType = "github")] HttpRequestMessage request,
            TraceWriter log)
        {
            string eventName  = request.Headers.GetValueOrDefault("X-GitHub-Event");
            string deliveryId = request.Headers.GetValueOrDefault("X-GitHub-Delivery");

            log.Info($"Webhook delivery: Delivery id = '{deliveryId}', Event name = '{eventName}'");

            if (eventName == "pull_request")
            {
                var payload = await DeserializeBody <PullRequestPayload>(request.Content);

                if (PullRequestActions.Contains(payload.Action))
                {
                    try
                    {
                        log.Info($"Handling action '{payload.Action}' for pull request #{payload.Number}");
                        var connection = await GithubConnectionCache.GetConnectionAsync(payload.Installation.Id);

                        var context = new PullRequestContext(payload, connection, log);
                        await PullRequestHandler.HandleWebhookEventAsync(context);

                        log.Info($"Finished handling action '{payload.Action}' for pull request #{payload.Number}");
                    }
                    catch (Exception ex)
                    {
                        log.Error($"Error processing pull request webhook event {deliveryId}", ex);
                        return(request.CreateErrorResponse(HttpStatusCode.InternalServerError, new HttpError(ex.Message)));
                    }
                }
                else
                {
                    log.Info($"Ignoring pull request action '{payload.Action}'");
                }
            }
            else
            {
                log.Info($"Unknown event '{eventName}', ignoring");
            }

            return(request.CreateResponse(HttpStatusCode.NoContent));
        }