aboutsummaryrefslogtreecommitdiff
path: root/src_py/hatter/server.py
diff options
context:
space:
mode:
authorbozo.kopic <bozo.kopic@gmail.com>2017-08-16 17:29:40 +0200
committerbozo.kopic <bozo.kopic@gmail.com>2017-08-16 17:29:40 +0200
commit074b604076049fb809c22ec0afaf18f5b55cc3ca (patch)
treeaea818a57d790ef0c918e1c551f9e5913066b786 /src_py/hatter/server.py
parent28446d95471e74de364b53c3f24d6182fddac0e0 (diff)
backend
Diffstat (limited to 'src_py/hatter/server.py')
-rw-r--r--src_py/hatter/server.py30
1 files changed, 29 insertions, 1 deletions
diff --git a/src_py/hatter/server.py b/src_py/hatter/server.py
index 894d101..9e44a0b 100644
--- a/src_py/hatter/server.py
+++ b/src_py/hatter/server.py
@@ -1,4 +1,6 @@
import asyncio
+import json
+import collections
import aiohttp.web
@@ -34,7 +36,18 @@ class WebServer:
return ws
async def _webhook_handler(self, request):
- pass
+ try:
+ if not ({'X-Gitlab-Event', 'X-GitHub-Event'} &
+ set(request.headers.keys())):
+ raise Exception('unsupported webhook request')
+ body = await request.read()
+ data = json.loads(body)
+ req = _parse_webhook_request(request.headers, data)
+ for commit in req.commits:
+ self._backend.enqueue(req.url, commit)
+ except Exception:
+ pass
+ return aiohttp.web.Response()
class Client:
@@ -44,3 +57,18 @@ class Client:
async def run(self):
pass
+
+
+WebhookRequest = collections.namedtuple('WebhookRequest', ['url', 'commits'])
+
+
+def _parse_webhook_request(headers, data):
+ if headers.get('X-Gitlab-Event') == 'Push Hook':
+ url = data['repository']['git_http_url']
+ commits = [commit['id'] for commit in data['commits']]
+ elif headers.get('X-GitHub-Event') == 'push':
+ url = data['repository']['clone_url']
+ commits = [commit['id'] for commit in data['commits']]
+ else:
+ raise Exception('unsupported webhook event')
+ return WebhookRequest(url, commits)