contentdb/app/blueprints/api/support.py

59 lines
2.1 KiB
Python
Raw Normal View History

# ContentDB
# Copyright (C) 2018-21 rubenwardy
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2021-01-30 18:10:38 +01:00
2020-05-19 18:24:57 +02:00
from flask import jsonify, abort, make_response, url_for
from app.logic.releases import LogicError, do_create_vcs_release, do_create_zip_release
from app.models import APIToken, Package, MinetestRelease
2021-01-30 18:10:38 +01:00
2020-01-24 21:21:40 +01:00
def error(code: int, msg: str):
abort(make_response(jsonify({ "success": False, "error": msg }), code))
2020-01-24 21:21:40 +01:00
# Catches LogicErrors and aborts with JSON error
def run_safe(f, *args, **kwargs):
try:
return f(*args, **kwargs)
except LogicError as e:
error(e.code, e.message)
2020-01-24 21:21:40 +01:00
def api_create_vcs_release(token: APIToken, package: Package, title: str, ref: str,
min_v: MinetestRelease = None, max_v: MinetestRelease = None, reason="API"):
2020-01-24 21:21:40 +01:00
if not token.canOperateOnPackage(package):
error(403, "API token does not have access to the package")
2020-01-24 21:21:40 +01:00
rel = run_safe(do_create_vcs_release, token.owner, package, title, ref, None, None, reason)
2020-01-24 21:21:40 +01:00
return jsonify({
"success": True,
"task": url_for("tasks.check", id=rel.task_id),
"release": rel.getAsDictionary()
})
2021-01-30 18:10:38 +01:00
def api_create_zip_release(token: APIToken, package: Package, title: str, file, reason="API"):
if not token.canOperateOnPackage(package):
error(403, "API token does not have access to the package")
2020-01-24 21:21:40 +01:00
rel = run_safe(do_create_zip_release, token.owner, package, title, file, None, None, reason)
2020-01-24 21:21:40 +01:00
return jsonify({
"success": True,
"task": url_for("tasks.check", id=rel.task_id),
"release": rel.getAsDictionary()
})