From 69672765ceb6510848c1200106d6950ed69c65a8 Mon Sep 17 00:00:00 2001 From: Ahmed Awad Date: Mon, 20 Jan 2020 11:43:58 +0000 Subject: [PATCH 1/6] Modify _get_digest to use request.get_data(). * request.data is empty when the request type is application/x-www-form-urlencoded * request.get_data contains the raw request bytes, cached by flask, and that works for both application/json and application/x-www-form-urlencoded Signed-Off-By: Ahmed Awad --- github_webhook/webhook.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github_webhook/webhook.py b/github_webhook/webhook.py index 85787d3..180b801 100644 --- a/github_webhook/webhook.py +++ b/github_webhook/webhook.py @@ -42,7 +42,7 @@ def decorator(func): def _get_digest(self): """Return message digest if a secret key was provided""" - return hmac.new(self._secret, request.data, hashlib.sha1).hexdigest() if self._secret else None + return hmac.new(self._secret, request.get_data(), hashlib.sha1).hexdigest() if self._secret else None def _postreceive(self): """Callback from Flask""" From 7a1ea2095afa4450890d279f7dfa84a03b9b204b Mon Sep 17 00:00:00 2001 From: ahawad Date: Sat, 21 Jan 2023 20:36:15 +0000 Subject: [PATCH 2/6] Support quart --- github_webhook/webhook.py | 4 ++-- setup.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/github_webhook/webhook.py b/github_webhook/webhook.py index 7e4d255..80cee71 100644 --- a/github_webhook/webhook.py +++ b/github_webhook/webhook.py @@ -4,7 +4,7 @@ import logging import json import six -from flask import abort, request +from quart import abort, request class Webhook(object): @@ -58,7 +58,7 @@ def _get_digest(self): return hmac.new(self._secret, request.get_data(), hashlib.sha1).hexdigest() if self._secret else None - def _postreceive(self): + async def _postreceive(self): """Callback from Flask""" digest = self._get_digest() diff --git a/setup.py b/setup.py index b1ebbae..fb44943 100644 --- a/setup.py +++ b/setup.py @@ -2,14 +2,14 @@ setup( name="github-webhook", - version="1.0.4", + version="1.0.5", description="Very simple, but powerful, microframework for writing Github webhooks in Python", url="https://github.com/bloomberg/python-github-webhook", author="Alex Chamberlain, Fred Phillips, Daniel Kiss, Daniel Beer", author_email="achamberlai9@bloomberg.net, fphillips7@bloomberg.net, dkiss1@bloomberg.net, dbeer1@bloomberg.net", license="Apache 2.0", packages=["github_webhook"], - install_requires=["flask", "six"], + install_requires=["quart", "six"], tests_require=["mock", "pytest"], classifiers=[ "Development Status :: 4 - Beta", From 3c45e82256cc10cd1f68f41d17b8158d017deb63 Mon Sep 17 00:00:00 2001 From: ahawad Date: Sat, 21 Jan 2023 20:47:03 +0000 Subject: [PATCH 3/6] await form --- github_webhook/webhook.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/github_webhook/webhook.py b/github_webhook/webhook.py index 80cee71..6233367 100644 --- a/github_webhook/webhook.py +++ b/github_webhook/webhook.py @@ -73,8 +73,9 @@ async def _postreceive(self): event_type = _get_header("X-Github-Event") content_type = _get_header("content-type") + form_data = await request.form data = ( - json.loads(request.form.to_dict(flat=True)["payload"]) + json.loads(form_data.to_dict(flat=True)["payload"]) if content_type == "application/x-www-form-urlencoded" else request.get_json() ) From dd6ca100b830ea40a01a6ad4488ff161995c4ee7 Mon Sep 17 00:00:00 2001 From: ahawad Date: Sat, 21 Jan 2023 20:50:35 +0000 Subject: [PATCH 4/6] test From 1abf5348d898c83cad32096f94389760287efee5 Mon Sep 17 00:00:00 2001 From: ahawad Date: Sat, 21 Jan 2023 21:09:42 +0000 Subject: [PATCH 5/6] test From f5f5ca53b9a3ee3bbe3fe1f9f71cf6c0e68ec689 Mon Sep 17 00:00:00 2001 From: ahawad Date: Sat, 21 Jan 2023 21:18:28 +0000 Subject: [PATCH 6/6] Await hook. --- github_webhook/webhook.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github_webhook/webhook.py b/github_webhook/webhook.py index 6233367..688ca44 100644 --- a/github_webhook/webhook.py +++ b/github_webhook/webhook.py @@ -86,7 +86,7 @@ async def _postreceive(self): self._logger.info("%s (%s)", _format_event(event_type, data), _get_header("X-Github-Delivery")) for hook in self._hooks.get(event_type, []): - hook(data) + await hook(data) return "", 204