2 # Copyright (C) 2020 rubenwardy
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
17 from flask import Blueprint, request
19 bp = Blueprint("gitlab", __name__)
22 from app.models import Package, APIToken, Permission
23 from app.blueprints.api.support import error, handleCreateRelease
26 @bp.route("/gitlab/webhook/", methods=["POST"])
32 gitlab_url = json["project"]["web_url"].replace("https://", "").replace("http://", "")
33 package = Package.query.filter(Package.repo.ilike("%{}%".format(gitlab_url))).first()
35 return error(400, "Could not find package, did you set the VCS repo in CDB correctly? Expected {}".format(gitlab_url))
37 # Get all tokens for package
38 secret = request.headers.get("X-Gitlab-Token")
40 return error(403, "Token required")
42 token = APIToken.query.filter_by(access_token=secret).first()
44 return error(403, "Invalid authentication")
46 if not package.checkPerm(token.owner, Permission.APPROVE_RELEASE):
47 return error(403, "You do not have the permission to approve releases")
53 event = json["event_name"]
57 elif event == "tag_push":
59 title = ref.replace("refs/tags/", "")
61 return error(400, "Unsupported event. Only 'push' and 'tag_push' are supported.")
67 return handleCreateRelease(token, package, title, ref)