From 35e1659b770db5456db3a0ffbb5ac70d3c0f018a Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sun, 6 Dec 2020 04:54:32 +0000 Subject: [PATCH] Fix crash on missing GitLab field --- app/blueprints/gitlab/__init__.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/app/blueprints/gitlab/__init__.py b/app/blueprints/gitlab/__init__.py index 8d5b3d4..fb565f2 100644 --- a/app/blueprints/gitlab/__init__.py +++ b/app/blueprints/gitlab/__init__.py @@ -23,16 +23,15 @@ from app.models import Package, APIToken, Permission from app.blueprints.api.support import error, handleCreateRelease -@bp.route("/gitlab/webhook/", methods=["POST"]) -@csrf.exempt -def webhook(): +def webhook_impl(): json = request.json # Get package gitlab_url = json["project"]["web_url"].replace("https://", "").replace("http://", "") package = Package.query.filter(Package.repo.ilike("%{}%".format(gitlab_url))).first() if package is None: - return error(400, "Could not find package, did you set the VCS repo in CDB correctly? Expected {}".format(gitlab_url)) + return error(400, + "Could not find package, did you set the VCS repo in CDB correctly? Expected {}".format(gitlab_url)) # Get all tokens for package secret = request.headers.get("X-Gitlab-Token") @@ -65,3 +64,12 @@ def webhook(): # return handleCreateRelease(token, package, title, ref) + + +@bp.route("/gitlab/webhook/", methods=["POST"]) +@csrf.exempt +def webhook(): + try: + return webhook_impl() + except KeyError as err: + return error(400, "Missing field: {}".format(err.args[0]))