#!/usr/bin/env python3 import json import os import urllib.request import bottle @bottle.get('/webhook') @bottle.post('/webhook') def webhook(): project_id = int(bottle.request.query.project_id) payload = { 'project_id': project_id, 'environment': '{}', } if 'template_id' in bottle.request.query: payload['template_id'] = int(bottle.request.query.template_id) if 'debug' in bottle.request.query: payload['debug'] = bottle.request.query.debug == 'true' if 'dry_run' in bottle.request.query: payload['dry_run'] = bottle.request.query.dry_run == 'true' if 'diff' in bottle.request.query: payload['diff'] = bottle.request.query.diff == 'true' baseurl = f'https://{bottle.request.urlparts.netloc}' url = os.path.join(baseurl, 'api/project', str(project_id), 'tasks') headers = { 'Content-Type': 'application/json', 'Authorization': bottle.request.headers['Authorization'], 'X-Forwarded-For': bottle.request.headers['X-Forwarded-For'], 'X-Forwarded-Host': bottle.request.headers['X-Forwarded-Host'], 'X-Forwarded-Server': bottle.request.headers['X-Forwarded-Server'], } req = urllib.request.Request(url, data=json.dumps(payload).encode(), method='POST', headers=headers) try: resp = urllib.request.urlopen(req) except urllib.error.HTTPError as e: bottle.abort(e.code, e.reason) bottle.abort(204, 'No Content') if __name__ == '__main__': bottle.run(host='::1', port=3042)