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 | |
| parent | 28446d95471e74de364b53c3f24d6182fddac0e0 (diff) | |
backend
| -rw-r--r-- | playground/conf.yaml | 3 | ||||
| -rwxr-xr-x | playground/run.sh | 3 | ||||
| -rw-r--r-- | schemas_json/server.yaml | 24 | ||||
| -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 |
6 files changed, 42 insertions, 25 deletions
diff --git a/playground/conf.yaml b/playground/conf.yaml new file mode 100644 index 0000000..efee9fe --- /dev/null +++ b/playground/conf.yaml @@ -0,0 +1,3 @@ +--- +repositories: [] +... diff --git a/playground/run.sh b/playground/run.sh new file mode 100755 index 0000000..7302d7b --- /dev/null +++ b/playground/run.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +PYTHONPATH="../src_py" python -m hatter.main -c conf.yaml --web-path ../build/jshatter $* diff --git a/schemas_json/server.yaml b/schemas_json/server.yaml index da50e06..4a576b2 100644 --- a/schemas_json/server.yaml +++ b/schemas_json/server.yaml @@ -30,28 +30,8 @@ properties: default: '/webhook' repositories: title: Repositories - description: List of all repositories with project definitions + description: List of all repository urls type: array items: - oneOf: - - "$ref": "hatter://server.yaml#/definitions/gitlab" - - "$ref": "hatter://server.yaml#/definitions/github" -definitions: - gitlab: - type: object - required: - - type - - token - properties: - type: - enum: - - GITLAB - github: - type: object - required: - - type - properties: - type: - enum: - - GITHUB + type: string ... 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) |
