From cb318bfc8c2934ecc14cde2eea665f6c4dffa41e Mon Sep 17 00:00:00 2001 From: Adrian Malacoda Date: Sun, 19 Jun 2016 04:52:14 -0500 Subject: [PATCH] Update gitlab-listener to correctly handle gitlab events --- config.py | 1 + gitlab-listener | 44 +++++++++++++++++++++++++++----------------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/config.py b/config.py index 9f7bd03..fa9342e 100644 --- a/config.py +++ b/config.py @@ -4,3 +4,4 @@ GITLAB_SSH_PORT = 2200 LISTENER_ADDRESS = "0.0.0.0" LISTENER_PORT = 9001 +LISTENER_DELAY = 5 diff --git a/gitlab-listener b/gitlab-listener index ef8518e..c644168 100755 --- a/gitlab-listener +++ b/gitlab-listener @@ -2,6 +2,7 @@ import os import sys import time +import json from subprocess import call import config @@ -10,22 +11,31 @@ MY_DIR = os.path.dirname(os.path.abspath(__file__)) try: from flask import Flask, request + + app = Flask(__name__) + + COMMAND = os.path.join(MY_DIR, "update-keys") + EVENTS = ["key_create", "key_destroy"] + + @app.route("/", methods=['POST']) + def commit (): + key = request.headers.get('X-Gitlab-Token', '') + if not key == config.GITLAB_KEY: + return "wrong key" + + event = json.loads(request.data) + print ">> received event: {}".format(event['event_name']) + + if not event['event_name'] in EVENTS: + return "wrong event" + + time.sleep(config.LISTENER_DELAY) + print ">> updating keys" + call([COMMAND]) + return "ok" + + if __name__ == "__main__": + app.run(host=config.LISTENER_ADDRESS, port=config.LISTENER_PORT) + except ImportError: call(". " + os.path.join(MY_DIR, "venv", "bin", "activate") + "; " + " ".join(sys.argv), shell=True) - -app = Flask(__name__) - -COMMAND = os.path.join(MY_DIR, "update-keys") - -@app.route("/", methods=['POST']) -def commit (): - key = request.args.get('key', '') - if not key == config.GITLAB_KEY: - return "not ok" - - time.sleep(1) - call([COMMAND]) - return "ok" - -if __name__ == "__main__": - app.run(host=config.LISTENER_ADDRESS, port=config.LISTENER_PORT)