| |
@@ -0,0 +1,87 @@
|
| |
+ # -*- coding: utf-8 -*-
|
| |
+
|
| |
+ """
|
| |
+ (c) 2019 - Copyright Red Hat Inc
|
| |
+
|
| |
+ Authors:
|
| |
+ Pierre-Yves Chibon <pingou@pingoured.fr>
|
| |
+
|
| |
+ """
|
| |
+
|
| |
+ from __future__ import unicode_literals, print_function
|
| |
+ import logging
|
| |
+
|
| |
+ import flask
|
| |
+ from sqlalchemy.exc import SQLAlchemyError
|
| |
+
|
| |
+ import pagure.utils
|
| |
+ from pagure.api import api_method, APIERROR, api_login_required
|
| |
+ from pagure.api.utils import _get_repo, _check_token
|
| |
+
|
| |
+ import pagure_distgit.forms
|
| |
+ from pagure_distgit import model
|
| |
+
|
| |
+
|
| |
+ _log = logging.getLogger(__name__)
|
| |
+
|
| |
+ DISTGIT_NS = flask.Blueprint(
|
| |
+ "distgit_ns", __name__, url_prefix="/_dg", template_folder="templates"
|
| |
+ )
|
| |
+
|
| |
+
|
| |
+ @DISTGIT_NS.route("/anitya/<namespace>/<repo>", methods=["GET"])
|
| |
+ def anitya_get_endpoint(namespace, repo):
|
| |
+ """ Returns the current status of the monitoring in anitya of this package.
|
| |
+ """
|
| |
+ repo = flask.g.repo
|
| |
+ output = {"monitoring": "no-monitoring"}
|
| |
+ if repo.anitya:
|
| |
+ output = {"monitoring": repo.anitya.anitya_status}
|
| |
+ return flask.jsonify(output)
|
| |
+
|
| |
+
|
| |
+ @DISTGIT_NS.route("/anitya/<namespace>/<repo>", methods=["PATCH"])
|
| |
+ @api_login_required(acls=["modify_project"])
|
| |
+ @api_method
|
| |
+ def anitya_patch_endpoint(namespace, repo):
|
| |
+ """ Updates the current status of the monitoring in anitya of this package.
|
| |
+ """
|
| |
+
|
| |
+ repo = _get_repo(repo, namespace=namespace)
|
| |
+ _check_token(repo, project_token=False)
|
| |
+
|
| |
+ is_site_admin = pagure.utils.is_admin()
|
| |
+ admins = [u.username for u in repo.get_project_users("admin")]
|
| |
+ # Only allow the main admin, the admins of the project, and Pagure site
|
| |
+ # admins to modify projects' monitoring, even if the user has the right
|
| |
+ # ACLs on their token
|
| |
+ if (
|
| |
+ flask.g.fas_user.username not in admins
|
| |
+ and flask.g.fas_user.username != repo.user.username
|
| |
+ and not is_site_admin
|
| |
+ ):
|
| |
+ raise pagure.exceptions.APIError(
|
| |
+ 401, error_code=APIERROR.EMODIFYPROJECTNOTALLOWED
|
| |
+ )
|
| |
+
|
| |
+ form = pagure_distgit.forms.AnityaForm(csrf_enabled=False)
|
| |
+ if form.validate_on_submit():
|
| |
+ try:
|
| |
+ if repo.anitya:
|
| |
+ repo.anitya.anitya_status = form.anitya_status.data
|
| |
+ else:
|
| |
+ repo = model.PagureAnitya(
|
| |
+ project_id=repo.id, anitya_status=form.anitya_status.data
|
| |
+ )
|
| |
+ flask.g.session.add(repo)
|
| |
+ flask.g.session.commit()
|
| |
+ except SQLAlchemyError as err: # pragma: no cover
|
| |
+ flask.g.session.rollback()
|
| |
+ _log.exception(err)
|
| |
+ raise pagure.exceptions.APIError(400, error_code=APIERROR.EDBERROR)
|
| |
+ else:
|
| |
+ raise pagure.exceptions.APIError(
|
| |
+ 400, error_code=APIERROR.EINVALIDREQ, errors=form.errors
|
| |
+ )
|
| |
+
|
| |
+ return anitya_get_endpoint(namespace, repo.name)
|
| |
This 3rd party extension has a dedicated database model expending
pagure's database model to store information about monitoring status of
a project.
It also has two new endpoints, one to retrieve the current monitoring
status of a project, the second to update it.
These two endpoints are then leveraged in the srcfpo theme of pagure to
display the current monitoring status of the package and update it.
Signed-off-by: Pierre-Yves Chibon pingou@pingoured.fr