From e60b548b5316e2586cba44b2b59d8b2a04c6c73a Mon Sep 17 00:00:00 2001 From: FrantiĊĦek Zatloukal Date: May 04 2021 08:21:59 +0000 Subject: Fetch package versions from oraculum --- diff --git a/README.rst b/README.rst index 97e8d6b..a829c7d 100644 --- a/README.rst +++ b/README.rst @@ -53,6 +53,7 @@ Fedora 'refs/heads/epel[0-9]+', 'refs/heads/el[0-9]+', 'refs/heads/olpc[0-9]+'] + ORACULUM_URL = 'https://packager-dashboard.fedoraproject.org/api/' CentOS ------ diff --git a/pagure_distgit/plugin.py b/pagure_distgit/plugin.py index facdcca..43dd6b6 100644 --- a/pagure_distgit/plugin.py +++ b/pagure_distgit/plugin.py @@ -395,24 +395,82 @@ def take_orphan_endpoint(namespace, repo): return flask.jsonify(output) -@DISTGIT_NS.route("/bodhi_updates//", methods=["GET"]) -@api_method -def bodhi_updates_endpoint(namespace, repo): - """Retrieves the current updates in bodhi for the specified package.""" - if namespace not in ["rpms"]: +def query_oraculum_versions(package): + """ + Receives package versions from oraculum + Returns triplet: fedora-releases, updates-for-package, F%number-of-current-rawhide + """ + + oraculum_base_url = "https://packager-dashboard.fedoraproject.org/api/" + # easier debugging when running directly outside of flask context + try: + oraculum_url = flask.current_app.config.get("ORACULUM_URL", oraculum_base_url) + except RuntimeError: + oraculum_url = oraculum_base_url + + if not oraculum_url: raise pagure.exceptions.APIError( - 400, - error_code=APIERROR.EINVALIDREQ, - errors=["Namespace not supported"], + 500, + error_code=APIERROR.ENOCODE, + error="This pagure instance has no ORACULUM_URL configured, please " + "inform your pagure administrators", ) - _log.info( - "Received request for the bodhi updates of: %s/%s", namespace, repo - ) + _log.debug("Based ORACULUM url: %s", oraculum_url) + req_versions = requests.get("%sv1/package_versions/%s" % (oraculum_url, package)) + req_releases = requests.get("%sv1/releases" % oraculum_url) - repo = _get_repo(repo, namespace=namespace) - html = pagure.utils.is_true(flask.request.args.get("html", False)) + if not req_releases.ok: + raise pagure.exceptions.APIError( + 400, + error_code=APIERROR.EINVALIDREQ, + errors=["Could not call oraculum to query current Fedora Releases."], + ) + + if not req_versions.ok: + raise pagure.exceptions.APIError( + 400, + error_code=APIERROR.EINVALIDREQ, + errors=["Could not call oraculum to query current package versions."], + ) + + versions_resp = req_versions.json() + releases_resp = req_releases.json() + + # Releases mapping ({"FXX": "Fedora XX", "EPEL X": "Fedora EPEL X"}) + releases = {} + releases["ELN"] = "Fedora ELN" + for epel_release in releases_resp["epel"]: + releases["EPEL-%d" % epel_release] = "Fedora EPEL %d" % epel_release + for release in releases_resp["fedora"]["values"]: + releases["F%d" % release] = "Fedora %d" % release + + rawhide_num = max(releases_resp["fedora"]["values"]) + rawhide = "F" + str(rawhide_num) + # Rename fedora releases to what pagure's dist-git api uses now + # (Fedora XX => FXX; Fedora Rawhide => FXX, EPEL X => EPEL-X) + versions = {} + + for release in list(versions_resp.keys()): + if release in ["Fedora ELN", "Fedora Rawhide"]: + continue + if release.startswith("Fedora"): + versions[release.replace("Fedora ", "F")] = versions_resp[release] + elif release.startswith("EPEL"): + versions[release.replace(" ", "-")] = versions_resp[release] + + # And finally Rawhide and ELN + versions[rawhide] = versions_resp["Fedora Rawhide"] + versions["ELN"] = versions_resp["Fedora ELN"] + + return releases, versions, rawhide + +def query_bodhi_versions(repo): + """ + Receives package versions from bodhi + Returns triplet: fedora-releases, updates-for-package, F%number-of-current-rawhide + """ bodhi_base_url = "https://bodhi.fedoraproject.org/" # Retrieves the active releases from bodhi @@ -465,7 +523,7 @@ def bodhi_updates_endpoint(namespace, repo): # Retrieves the updates of that package in bodhi update_url = "%s/updates?packages=%s" % ( bodhi_base_url.rstrip("/"), - repo.name, + repo, ) updates = collections.defaultdict(dict) @@ -479,18 +537,51 @@ def bodhi_updates_endpoint(namespace, repo): ) data = req.json() + + # To be consistent with oraculum + for release in releases: + updates[release] = {"pending": None, "stable": None, "testing": None} + for update in data["updates"]: if update["release"]["name"] not in releases: continue + if update["status"] == "obsolete": + # To be consistent with oraculum + continue name = update["title"] for build in update["builds"]: - if repo.name in build["nvr"]: + if repo in build["nvr"]: name = build["nvr"] break if not updates[update["release"]["name"]].get(update["status"]): updates[update["release"]["name"]][update["status"]] = name + return releases, updates, rawhide + +@DISTGIT_NS.route("/bodhi_updates//", methods=["GET"]) +@api_method +def bodhi_updates_endpoint(namespace, repo): + """Retrieves the current updates in bodhi for the specified package.""" + if namespace not in ["rpms"]: + raise pagure.exceptions.APIError( + 400, + error_code=APIERROR.EINVALIDREQ, + errors=["Namespace not supported"], + ) + + _log.info( + "Received request for the bodhi updates of: %s/%s", namespace, repo + ) + + repo = _get_repo(repo, namespace=namespace) + html = pagure.utils.is_true(flask.request.args.get("html", False)) + + try: + releases, updates, rawhide = query_oraculum_versions(repo.name) + except pagure.exceptions.APIError: + releases, updates, rawhide = query_bodhi_versions(repo.name) + if html: html_output = ( '\n'