semaphore-webhook-server/semaphore-webhook-server.py
2023-12-20 02:05:36 +01:00

44 lines
1.5 KiB
Python
Executable file

#!/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)