From c08e7e296796c4791c832f25a204dc4ab10a7abe Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: May 14 2019 14:27:30 +0000 Subject: Check if the project is active in PDC on master If the project is not active in PDC, it is considered retired and thus the packager requesting will have to go through a releng ticket. Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure_distgit/pagure_distgit.py b/pagure_distgit/pagure_distgit.py index b919c73..3d28856 100644 --- a/pagure_distgit/pagure_distgit.py +++ b/pagure_distgit/pagure_distgit.py @@ -9,9 +9,11 @@ """ from __future__ import unicode_literals, print_function +import datetime import logging import flask +import requests from sqlalchemy.exc import SQLAlchemyError import pagure.utils @@ -87,12 +89,74 @@ def anitya_patch_endpoint(namespace, repo): return anitya_get_endpoint(namespace, repo.name) +def _is_active_in_pdc(name, namespace): + """ Queries PDC and return whether the project is active on the master + branch in PDC or not. + """ + pdc_url = flask.current_app.config.get("PDC_URL") + if not pdc_url: + raise pagure.exceptions.APIError( + 500, + error_code=APIERROR.ENOCODE, + error="This pagure instance has no PDC_URL configured, please inform" + " your pagure administrators" + ) + else: + pdc_url = "%s/rest_api/v1/component-branches/" % pdc_url.rstrip("/") + + _log.debug("Based PDC url: %s", pdc_url) + + to_pdc_namespace = { + "rpms": "rpm", + "modules": "module", + "container": "container", + } + to_pdc_namespace = flask.current_app.config.get("PDC_NAMESPACES") \ + or to_pdc_namespace + + try: + pdc_namespace = to_pdc_namespace[namespace] + except: + raise pagure.exceptions.APIError( + 500, + error_code=APIERROR.ENOCODE, + error="Namespace: %s could not be converted to a PDC namespace" % + namespace + ) + + url = "%s?global_component=%s&name=master&type=%s" % ( + pdc_url, name, pdc_namespace) + + _log.info("Querying PDC at: %s", url) + try: + req = requests.get(url, timeout=(30, 30)) + except requests.RequestException as err: + raise pagure.exceptions.APIError( + 500, + error_code=APIERROR.ENOCODE, + error="An error occured while querying pdc: %s" % err + ) + + try: + data = req.json() + except: + raise pagure.exceptions.APIError( + 500, + error_code=APIERROR.ENOCODE, + error="The output of %s could not be converted to JSON" % req.url + ) + + _log.info("%s/%s is active: %s", namespace, name, data["results"][0]["active"]) + return data["results"][0]["active"] == True + + @DISTGIT_NS.route("/take_orphan//", methods=["POST"]) @api_login_required(acls=["modify_project"]) @api_method def take_orphan_endpoint(namespace, repo): """ Updates the current point of contact of orphan packages. """ + _log.info("Received a request to unorphan: %s/%s", namespace, repo) repo = _get_repo(repo, namespace=namespace) _check_token(repo, project_token=False) @@ -117,6 +181,15 @@ def take_orphan_endpoint(namespace, repo): errors="You must be a packager to adopt a package.", ) + # Check if the project is retired in PDC + if not _is_active_in_pdc(repo.name, repo.namespace): + raise pagure.exceptions.APIError( + 400, + error_code=APIERROR.EINVALIDREQ, + errors="This project has been retired and cannot be unorphaned " + "here, please open a releng ticket for it.", + ) + try: repo.user = user_obj flask.g.session.add(repo)