]> git.lizzy.rs Git - cheatdb.git/blob - app/blueprints/gitlab/__init__.py
Add ability to edit comments
[cheatdb.git] / app / blueprints / gitlab / __init__.py
1 # ContentDB
2 # Copyright (C) 2020  rubenwardy
3 #
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.
8 #
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.
13 #
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/>.
16
17 from flask import Blueprint, request
18
19 bp = Blueprint("gitlab", __name__)
20
21 from app import csrf
22 from app.models import Package, APIToken, Permission
23 from app.blueprints.api.support import error, handleCreateRelease
24
25
26 @bp.route("/gitlab/webhook/", methods=["POST"])
27 @csrf.exempt
28 def webhook():
29         json = request.json
30
31         # Get package
32         gitlab_url = json["project"]["web_url"].replace("https://", "").replace("http://", "")
33         package = Package.query.filter(Package.repo.ilike("%{}%".format(gitlab_url))).first()
34         if package is None:
35                 return error(400, "Could not find package, did you set the VCS repo in CDB correctly? Expected {}".format(gitlab_url))
36
37         # Get all tokens for package
38         secret = request.headers.get("X-Gitlab-Token")
39         if secret is None:
40                 return error(403, "Token required")
41
42         token = APIToken.query.filter_by(access_token=secret).first()
43         if secret is None:
44                 return error(403, "Invalid authentication")
45
46         if not package.checkPerm(token.owner, Permission.APPROVE_RELEASE):
47                 return error(403, "You do not have the permission to approve releases")
48
49         #
50         # Check event
51         #
52
53         event = json["event_name"]
54         if event == "push":
55                 ref = json["after"]
56                 title = ref[:5]
57         elif event == "tag_push":
58                 ref = json["ref"]
59                 title = ref.replace("refs/tags/", "")
60         else:
61                 return error(400, "Unsupported event. Only 'push' and 'tag_push' are supported.")
62
63         #
64         # Perform release
65         #
66
67         return handleCreateRelease(token, package, title, ref)