diff options
| author | bozo.kopic <bozo.kopic@gmail.com> | 2017-08-16 17:29:40 +0200 |
|---|---|---|
| committer | bozo.kopic <bozo.kopic@gmail.com> | 2017-08-16 17:29:40 +0200 |
| commit | 074b604076049fb809c22ec0afaf18f5b55cc3ca (patch) | |
| tree | aea818a57d790ef0c918e1c551f9e5913066b786 /src_py | |
| parent | 28446d95471e74de364b53c3f24d6182fddac0e0 (diff) | |
backend
Diffstat (limited to 'src_py')
| -rw-r--r-- | src_py/hatter/backend.py | 3 | ||||
| -rw-r--r-- | src_py/hatter/main.py | 4 | ||||
| -rw-r--r-- | src_py/hatter/server.py | 30 |
3 files changed, 34 insertions, 3 deletions
diff --git a/src_py/hatter/backend.py b/src_py/hatter/backend.py index b2f97e4..afb61a4 100644 --- a/src_py/hatter/backend.py +++ b/src_py/hatter/backend.py @@ -7,3 +7,6 @@ class Backend: async def async_close(self): pass + + def enqueue(self, url, commit): + pass diff --git a/src_py/hatter/main.py b/src_py/hatter/main.py index a110a4d..62ef457 100644 --- a/src_py/hatter/main.py +++ b/src_py/hatter/main.py @@ -20,7 +20,7 @@ def main(): conf = yaml.safe_load(conf_file)
hatter.json_validator.validate(conf, 'hatter://server.yaml#')
- if conf['log']:
+ if 'log' in conf:
logging.config.dictConfig(conf['log'])
if args.web_path:
@@ -62,7 +62,7 @@ def _create_parser(): named_arguments = parser.add_argument_group('required named arguments')
named_arguments.add_argument(
- '--conf', required=True, metavar='path', dest='conf_path',
+ '-c', '--conf', required=True, metavar='path', dest='conf',
help='configuration path')
return parser
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) |
