From 5b51f1823249a8410697a6363280dbddd9a8733f Mon Sep 17 00:00:00 2001 From: Lenka Segura Date: Jan 18 2024 14:03:43 +0000 Subject: [PATCH 1/3] Remove pdc query from is_supported_branch function Signed-off-by: Lenka Segura --- diff --git a/dist_git_auth.py b/dist_git_auth.py index d17c08d..71f35bd 100644 --- a/dist_git_auth.py +++ b/dist_git_auth.py @@ -34,7 +34,7 @@ try: except ImportError: # From pagure 5.2, code has been moved to pagure.lib.query from pagure.lib.query import get_user -from pagure.lib.git import is_forced_push +from pagure.lib.git import is_forced_push, read_git_output from pagure.lib.git_auth import GitAuthHelper, _read_file from pagure.utils import is_repo_collaborator @@ -73,16 +73,22 @@ class DistGitAuth(GitAuthHelper): self.protected_namespaces = pagure_config.get( "ACL_PROTECTED_NAMESPACES", ["rpms"] ) + self.bodhi_url = pagure_config.get("BODHI_URL") - self.pdc_url = pagure_config.get("PDC_URL") + def is_not_retired_package(self, branch, abspath): + """Returns if the package is NOT retired on the branch, + that means it does not contain dead.package""" + cmd = ["ls-tree", branch, "--name-only", "--", "dead.package"] + return read_git_output(cmd, abspath) != "dead.package" - def is_supported_branch(self, project, refname): + def is_supported_branch(self, project, refname, repodir): """Returns whether a specific branch is currently supported for Fedora This retrieves the information about EOL status from PDC, to prevent EOL branches being pushed to. """ - if not self.pdc_url: + + if not self.bodhi_url: # No way to confirm this is a supported branch, not supported return None if not refname.startswith("refs/heads/"): @@ -90,29 +96,23 @@ class DistGitAuth(GitAuthHelper): return None refname = refname[len("refs/heads/") :] - namespace2pdctype = { - "rpms": "rpm", - "modules": "module", - "container": "container", - } - name = urllib.parse.quote(project.name) - resp = requests.get( - f"{self.pdc_url}component-branches/?global_component={name}" - f"&name={refname}&type={namespace2pdctype[project.namespace]}&fields=active" - ) + # Check if the branch is active on Bodhi + if refname not in ["main", "rawhide"]: + resp = requests.get(f"{self.bodhi_url}releases/{refname}") - res = [] - if resp.ok: - res = resp.json().get("results") + if resp.ok: + resp = resp.json().get("state") + if not resp: # case when response is empty + return None + if resp not in ["current", "pending", "frozen"]: + return False + else: + return None - if len(res) == 0: - # No status - return None - if len(res) != 1: - # PDC couldn't make up its mind.... - # Should never happen, but just in case... - raise ValueError("PDC was unable to make up its mind") - return res[0]["active"] + # Branch can be supported, but package can be retired, + # in that case don't push + active = self.is_not_retired_package(refname, repodir) + return active def info(self, msg): """Function to print information. @@ -245,9 +245,12 @@ class DistGitAuth(GitAuthHelper): branch_overrides = pagure_config.get("PDC_BRANCH_OVERRIDES") or {} if refname in (branch_overrides.get(project.namespace) or []): pdc_ref = branch_overrides[project.namespace][refname] - is_supported = self.is_supported_branch(project, pdc_ref) + is_supported = self.is_supported_branch(project, pdc_ref, repodir) if is_supported is False: - self.info("Branch %s is unsupported. Cannot push to a disabled branch (maybe eol?)." % refname) + self.info( + "Branch %s is unsupported. Cannot push to a disabled branch (maybe eol?)." + % refname + ) return False elif is_supported is True: self.debug("Branch %s is supported" % refname) @@ -257,9 +260,7 @@ class DistGitAuth(GitAuthHelper): # This allows to block anything that is not allowed, so no # random branch creation. if self.block_unspecified: - self.info( - "Access to namespace %s is restricted" % project.namespace - ) + self.info("Access to namespace %s is restricted" % project.namespace) return False # For branches that are not explicitely active in PDC, check diff --git a/pagure_distgit_tests/test_dist_git_auth.py b/pagure_distgit_tests/test_dist_git_auth.py index a39d34b..f276b65 100644 --- a/pagure_distgit_tests/test_dist_git_auth.py +++ b/pagure_distgit_tests/test_dist_git_auth.py @@ -1,4 +1,5 @@ from __future__ import print_function +import os import dist_git_auth @@ -405,6 +406,7 @@ class DistGitAuthTestsFedora(DistGitAuthTests): "RCM_BRANCHES": ["refs/heads/f[0-9]+"], "ACL_PROTECTED_NAMESPACES": ["rpms", "modules", "container"], "PDC_URL": "invalid://", + "BODHI_URL": "invalid://", } def test_protected_blacklisted_ref(self): @@ -451,7 +453,7 @@ class DistGitAuthTestsFedora(DistGitAuthTests): def test_protected_unsupported_branch(self, mock_requests): res = Mock() res.ok = True - res.json.return_value = {"results": [{"active": False}]} + res.json.return_value = {'name': 'F34', 'long_name': 'Fedora 34', 'version': '34', 'id_prefix': 'FEDORA', 'branch': 'f34', 'dist_tag': 'f34', 'stable_tag': 'f34-updates', 'testing_tag': 'f34-updates-testing', 'candidate_tag': 'f34-updates-candidate', 'pending_signing_tag': 'f34-signing-pending', 'pending_testing_tag': 'f34-updates-testing-pending', 'pending_stable_tag': 'f34-updates-pending', 'override_tag': 'f34-override', 'mail_template': 'fedora_errata_template', 'state': 'archived', 'composed_by_bodhi': True, 'create_automatic_updates': False, 'package_manager': 'dnf', 'testing_repository': 'updates-testing', 'eol': None} mock_requests.get.return_value = res project = self.create_namespaced_project("rpms", "test") @@ -477,7 +479,7 @@ class DistGitAuthTestsFedora(DistGitAuthTests): project = self.create_namespaced_project("rpms", "test") res = Mock() res.ok = True - res.json.return_value = {"results": [{"active": True}]} + res.json.return_value = {'name': 'F39', 'long_name': 'Fedora 39', 'version': '39', 'id_prefix': 'FEDORA', 'branch': 'f39', 'dist_tag': 'f39', 'stable_tag': 'f39-updates', 'testing_tag': 'f39-updates-testing', 'candidate_tag': 'f39-updates-candidate', 'pending_signing_tag': 'f39-signing-pending', 'pending_testing_tag': 'f39-updates-testing-pending', 'pending_stable_tag': 'f39-updates-pending', 'override_tag': 'f39-override', 'mail_template': 'fedora_errata_template', 'state': 'current', 'composed_by_bodhi': True, 'create_automatic_updates': False, 'package_manager': 'dnf', 'testing_repository': 'updates-testing', 'eol': '2024-11-12'} mock_requests.get.return_value = res self.assertTrue( @@ -502,7 +504,7 @@ class DistGitAuthTestsFedora(DistGitAuthTests): project = self.create_namespaced_project("rpms", "test") res = Mock() res.ok = True - res.json.return_value = {"results": [{"active": True}]} + res.json.return_value = {'name': 'F39', 'long_name': 'Fedora 39', 'version': '39', 'id_prefix': 'FEDORA', 'branch': 'f39', 'dist_tag': 'f39', 'stable_tag': 'f39-updates', 'testing_tag': 'f39-updates-testing', 'candidate_tag': 'f39-updates-candidate', 'pending_signing_tag': 'f39-signing-pending', 'pending_testing_tag': 'f39-updates-testing-pending', 'pending_stable_tag': 'f39-updates-pending', 'override_tag': 'f39-override', 'mail_template': 'fedora_errata_template', 'state': 'current', 'composed_by_bodhi': True, 'create_automatic_updates': False, 'package_manager': 'dnf', 'testing_repository': 'updates-testing', 'eol': '2024-11-12'} mock_requests.get.return_value = res self.assertFalse( @@ -527,7 +529,7 @@ class DistGitAuthTestsFedora(DistGitAuthTests): project = self.create_namespaced_project("rpms", "test") res = Mock() res.ok = True - res.json.return_value = {"results": []} + res.json.return_value = {} mock_requests.get.return_value = res self.assertFalse( @@ -547,12 +549,31 @@ class DistGitAuthTestsFedora(DistGitAuthTests): self.expect_info_msg("Unspecified ref refs/heads/f28 is blocked") + def test_is_not_retired_package(self): + projects = tests.create_projects_git( + os.path.join(self.path, "repos"), bare=True + ) + tests.add_content_git_repo(projects[0]) + self.assertTrue( + self.dga.is_not_retired_package("master", projects[0]) + ) + + def test_is_not_retired_package_false(self): + projects = tests.create_projects_git( + os.path.join(self.path, "repos"), bare=True + ) + tests.add_content_git_repo(projects[0]) + tests.add_readme_git_repo(projects[0], readme_name="dead.package") + self.assertFalse( + self.dga.is_not_retired_package("master", projects[0]) + ) + @patch("dist_git_auth.requests") def test_protected_unspecified_branch_normal_committer(self, mock_requests): project = self.create_namespaced_project("rpms", "test") res = Mock() res.ok = True - res.json.return_value = {"results": []} + res.json.return_value = {} mock_requests.get.return_value = res self.assertTrue( @@ -577,7 +598,7 @@ class DistGitAuthTestsFedora(DistGitAuthTests): project = self.create_namespaced_project("rpms", "test") res = Mock() res.ok = True - res.json.return_value = {"results": []} + res.json.return_value = {} mock_requests.get.return_value = res self.assertFalse( @@ -608,6 +629,8 @@ class DistGitAuthTestsFedoraCommitterAccess(DistGitAuthTests): "RCM_BRANCHES": ["refs/heads/f[0-9]+"], "ACL_PROTECTED_NAMESPACES": ["rpms", "modules", "container"], "PDC_URL": "invalid://", + "BODHI_URL": "invalid://", + } def setUp(self): @@ -633,7 +656,7 @@ class DistGitAuthTestsFedoraCommitterAccess(DistGitAuthTests): project = get_project(self.session, name="test", namespace="rpms") res = Mock() res.ok = True - res.json.return_value = {"results": []} + res.json.return_value = {} mock_requests.get.return_value = res self.assertFalse( @@ -659,7 +682,7 @@ class DistGitAuthTestsFedoraCommitterAccess(DistGitAuthTests): project = get_project(self.session, name="test", namespace="rpms") res = Mock() res.ok = True - res.json.return_value = {"results": []} + res.json.return_value = {} mock_requests.get.return_value = res self.assertTrue( @@ -801,7 +824,7 @@ class DistGitAuthTestsCentOS(DistGitAuthTests): project = self.create_namespaced_project("rpms", "test") res = Mock() res.ok = True - res.json.return_value = {"results": []} + res.json.return_value = {} mock_requests.get.return_value = res self.assertFalse( @@ -826,7 +849,7 @@ class DistGitAuthTestsCentOS(DistGitAuthTests): project = self.create_namespaced_project("rpms", "test") res = Mock() res.ok = True - res.json.return_value = {"results": []} + res.json.return_value = {} mock_requests.get.return_value = res self.assertFalse( @@ -857,6 +880,7 @@ class DistGitAuthTestsFedoraBranchOverride(DistGitAuthTests): "RCM_BRANCHES": ["refs/heads/f[0-9]+"], "ACL_PROTECTED_NAMESPACES": ["rpms", "modules", "container"], "PDC_URL": "invalid://", + "BODHI_URL": "invalid://", "PDC_BRANCH_OVERRIDES": {"rpms": {"refs/heads/main": "refs/heads/rawhide"}}, } @@ -880,11 +904,11 @@ class DistGitAuthTestsFedoraBranchOverride(DistGitAuthTests): self.assertEqual(msg, "User added") @patch("dist_git_auth.requests") - def test_pushing_to_rpms_main_calls_pdc_correctly(self, mock_requests): + def test_pushing_to_rpms_main_calls_bodhi_correctly(self, mock_requests): project = get_project(self.session, name="test", namespace="rpms") res = Mock() res.ok = True - res.json.return_value = {"results": []} + res.json.return_value = {} mock_requests.get.return_value = res self.assertFalse( @@ -905,17 +929,14 @@ class DistGitAuthTestsFedoraBranchOverride(DistGitAuthTests): self.expect_info_msg("Committer: False") self.expect_info_msg("Fall-through deny") - mock_requests.get.assert_called_with( - "invalid://component-branches/?global_component=test" - "&name=rawhide&type=rpm&fields=active" - ) + mock_requests.get.assert_not_called() @patch("dist_git_auth.requests") def test_pushing_to_rpms_rawhide_calls_pdc_correctly(self, mock_requests): project = get_project(self.session, name="test", namespace="rpms") res = Mock() res.ok = True - res.json.return_value = {"results": []} + res.json.return_value = {} mock_requests.get.return_value = res self.assertFalse( @@ -936,17 +957,14 @@ class DistGitAuthTestsFedoraBranchOverride(DistGitAuthTests): self.expect_info_msg("Committer: False") self.expect_info_msg("Fall-through deny") - mock_requests.get.assert_called_with( - "invalid://component-branches/?global_component=test" - "&name=rawhide&type=rpm&fields=active" - ) + mock_requests.get.assert_not_called() @patch("dist_git_auth.requests") - def test_pushing_to_containers_calls_pdc_correctly(self, mock_requests): + def test_pushing_to_containers_calls_bodhi_correctly(self, mock_requests): project = get_project(self.session, name="cockpit", namespace="container") res = Mock() res.ok = True - res.json.return_value = {"results": []} + res.json.return_value = {} mock_requests.get.return_value = res self.assertTrue( @@ -968,6 +986,5 @@ class DistGitAuthTestsFedoraBranchOverride(DistGitAuthTests): self.expect_info_msg("Committer push") mock_requests.get.assert_called_with( - "invalid://component-branches/?global_component=cockpit" - "&name=epel8&type=container&fields=active" + "invalid://releases/epel8" ) From 5ed1105fb879c0b72d00d825a43d47db799edb00 Mon Sep 17 00:00:00 2001 From: Lenka Segura Date: Jan 18 2024 14:03:58 +0000 Subject: [PATCH 2/3] Style changes to make distgit tests happy --- diff --git a/pagure_distgit/plugin.py b/pagure_distgit/plugin.py index 94e0917..8b1a86d 100644 --- a/pagure_distgit/plugin.py +++ b/pagure_distgit/plugin.py @@ -439,10 +439,13 @@ def give_orphan_endpoint(namespace, repo): errors="You must be in rel-eng or admin group to assign a package.", ) - user=flask.request.values.get('user') + user = flask.request.values.get("user") if not user: - raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOUSER, - errors="You have to specify a user as point_of_contact.") + raise pagure.exceptions.APIError( + 404, + error_code=APIERROR.ENOUSER, + errors="You have to specify a user as point_of_contact.", + ) user_obj = pagure.lib.query.get_user(flask.g.session, user) diff --git a/pagure_distgit_tests/test_dist_git_auth.py b/pagure_distgit_tests/test_dist_git_auth.py index f276b65..33fbfc3 100644 --- a/pagure_distgit_tests/test_dist_git_auth.py +++ b/pagure_distgit_tests/test_dist_git_auth.py @@ -453,7 +453,28 @@ class DistGitAuthTestsFedora(DistGitAuthTests): def test_protected_unsupported_branch(self, mock_requests): res = Mock() res.ok = True - res.json.return_value = {'name': 'F34', 'long_name': 'Fedora 34', 'version': '34', 'id_prefix': 'FEDORA', 'branch': 'f34', 'dist_tag': 'f34', 'stable_tag': 'f34-updates', 'testing_tag': 'f34-updates-testing', 'candidate_tag': 'f34-updates-candidate', 'pending_signing_tag': 'f34-signing-pending', 'pending_testing_tag': 'f34-updates-testing-pending', 'pending_stable_tag': 'f34-updates-pending', 'override_tag': 'f34-override', 'mail_template': 'fedora_errata_template', 'state': 'archived', 'composed_by_bodhi': True, 'create_automatic_updates': False, 'package_manager': 'dnf', 'testing_repository': 'updates-testing', 'eol': None} + res.json.return_value = { + "name": "F34", + "long_name": "Fedora 34", + "version": "34", + "id_prefix": "FEDORA", + "branch": "f34", + "dist_tag": "f34", + "stable_tag": "f34-updates", + "testing_tag": "f34-updates-testing", + "candidate_tag": "f34-updates-candidate", + "pending_signing_tag": "f34-signing-pending", + "pending_testing_tag": "f34-updates-testing-pending", + "pending_stable_tag": "f34-updates-pending", + "override_tag": "f34-override", + "mail_template": "fedora_errata_template", + "state": "archived", + "composed_by_bodhi": True, + "create_automatic_updates": False, + "package_manager": "dnf", + "testing_repository": "updates-testing", + "eol": None, + } mock_requests.get.return_value = res project = self.create_namespaced_project("rpms", "test") @@ -472,14 +493,37 @@ class DistGitAuthTestsFedora(DistGitAuthTests): ) ) - self.expect_info_msg("Branch refs/heads/f26 is unsupported. Cannot push to a disabled branch (maybe eol?).") + self.expect_info_msg( + "Branch refs/heads/f26 is unsupported. Cannot push to a disabled branch (maybe eol?)." + ) @patch("dist_git_auth.requests") def test_protected_supported_branch_committer(self, mock_requests): project = self.create_namespaced_project("rpms", "test") res = Mock() res.ok = True - res.json.return_value = {'name': 'F39', 'long_name': 'Fedora 39', 'version': '39', 'id_prefix': 'FEDORA', 'branch': 'f39', 'dist_tag': 'f39', 'stable_tag': 'f39-updates', 'testing_tag': 'f39-updates-testing', 'candidate_tag': 'f39-updates-candidate', 'pending_signing_tag': 'f39-signing-pending', 'pending_testing_tag': 'f39-updates-testing-pending', 'pending_stable_tag': 'f39-updates-pending', 'override_tag': 'f39-override', 'mail_template': 'fedora_errata_template', 'state': 'current', 'composed_by_bodhi': True, 'create_automatic_updates': False, 'package_manager': 'dnf', 'testing_repository': 'updates-testing', 'eol': '2024-11-12'} + res.json.return_value = { + "name": "F39", + "long_name": "Fedora 39", + "version": "39", + "id_prefix": "FEDORA", + "branch": "f39", + "dist_tag": "f39", + "stable_tag": "f39-updates", + "testing_tag": "f39-updates-testing", + "candidate_tag": "f39-updates-candidate", + "pending_signing_tag": "f39-signing-pending", + "pending_testing_tag": "f39-updates-testing-pending", + "pending_stable_tag": "f39-updates-pending", + "override_tag": "f39-override", + "mail_template": "fedora_errata_template", + "state": "current", + "composed_by_bodhi": True, + "create_automatic_updates": False, + "package_manager": "dnf", + "testing_repository": "updates-testing", + "eol": "2024-11-12", + } mock_requests.get.return_value = res self.assertTrue( @@ -504,7 +548,28 @@ class DistGitAuthTestsFedora(DistGitAuthTests): project = self.create_namespaced_project("rpms", "test") res = Mock() res.ok = True - res.json.return_value = {'name': 'F39', 'long_name': 'Fedora 39', 'version': '39', 'id_prefix': 'FEDORA', 'branch': 'f39', 'dist_tag': 'f39', 'stable_tag': 'f39-updates', 'testing_tag': 'f39-updates-testing', 'candidate_tag': 'f39-updates-candidate', 'pending_signing_tag': 'f39-signing-pending', 'pending_testing_tag': 'f39-updates-testing-pending', 'pending_stable_tag': 'f39-updates-pending', 'override_tag': 'f39-override', 'mail_template': 'fedora_errata_template', 'state': 'current', 'composed_by_bodhi': True, 'create_automatic_updates': False, 'package_manager': 'dnf', 'testing_repository': 'updates-testing', 'eol': '2024-11-12'} + res.json.return_value = { + "name": "F39", + "long_name": "Fedora 39", + "version": "39", + "id_prefix": "FEDORA", + "branch": "f39", + "dist_tag": "f39", + "stable_tag": "f39-updates", + "testing_tag": "f39-updates-testing", + "candidate_tag": "f39-updates-candidate", + "pending_signing_tag": "f39-signing-pending", + "pending_testing_tag": "f39-updates-testing-pending", + "pending_stable_tag": "f39-updates-pending", + "override_tag": "f39-override", + "mail_template": "fedora_errata_template", + "state": "current", + "composed_by_bodhi": True, + "create_automatic_updates": False, + "package_manager": "dnf", + "testing_repository": "updates-testing", + "eol": "2024-11-12", + } mock_requests.get.return_value = res self.assertFalse( @@ -550,23 +615,15 @@ class DistGitAuthTestsFedora(DistGitAuthTests): self.expect_info_msg("Unspecified ref refs/heads/f28 is blocked") def test_is_not_retired_package(self): - projects = tests.create_projects_git( - os.path.join(self.path, "repos"), bare=True - ) + projects = tests.create_projects_git(os.path.join(self.path, "repos"), bare=True) tests.add_content_git_repo(projects[0]) - self.assertTrue( - self.dga.is_not_retired_package("master", projects[0]) - ) + self.assertTrue(self.dga.is_not_retired_package("master", projects[0])) def test_is_not_retired_package_false(self): - projects = tests.create_projects_git( - os.path.join(self.path, "repos"), bare=True - ) + projects = tests.create_projects_git(os.path.join(self.path, "repos"), bare=True) tests.add_content_git_repo(projects[0]) tests.add_readme_git_repo(projects[0], readme_name="dead.package") - self.assertFalse( - self.dga.is_not_retired_package("master", projects[0]) - ) + self.assertFalse(self.dga.is_not_retired_package("master", projects[0])) @patch("dist_git_auth.requests") def test_protected_unspecified_branch_normal_committer(self, mock_requests): @@ -630,7 +687,6 @@ class DistGitAuthTestsFedoraCommitterAccess(DistGitAuthTests): "ACL_PROTECTED_NAMESPACES": ["rpms", "modules", "container"], "PDC_URL": "invalid://", "BODHI_URL": "invalid://", - } def setUp(self): @@ -985,6 +1041,4 @@ class DistGitAuthTestsFedoraBranchOverride(DistGitAuthTests): self.expect_info_msg("Committer: True") self.expect_info_msg("Committer push") - mock_requests.get.assert_called_with( - "invalid://releases/epel8" - ) + mock_requests.get.assert_called_with("invalid://releases/epel8") diff --git a/pagure_distgit_tests/test_plugin.py b/pagure_distgit_tests/test_plugin.py index ec1d1a3..c685fa4 100644 --- a/pagure_distgit_tests/test_plugin.py +++ b/pagure_distgit_tests/test_plugin.py @@ -542,12 +542,12 @@ class PagureFlaskApiTakeOrphanEndpointTests(tests.Modeltests): namespace="rpms", ) pagure.lib.query.delete_user_of_group( - self.session, - "pingou", - "rel-eng", - "pingou", - False, - force=True, + self.session, + "pingou", + "rel-eng", + "pingou", + False, + force=True, ) self.session.commit() @@ -558,5 +558,7 @@ class PagureFlaskApiTakeOrphanEndpointTests(tests.Modeltests): data = json.loads(output.get_data(as_text=True)) assert output.status_code == 403 - assert data["errors"] == "You must be in rel-eng or admin group to assign a package." + assert ( + data["errors"] == "You must be in rel-eng or admin group to assign a package." + ) assert data["error_code"] == "ENOTHIGHENOUGH" From 377ac31b54483aa5114f55649b4137f249b2b9e3 Mon Sep 17 00:00:00 2001 From: Lenka Segura Date: Jan 23 2024 15:28:42 +0000 Subject: [PATCH 3/3] Remove pdc from plugin.py --- diff --git a/pagure_distgit/plugin.py b/pagure_distgit/plugin.py index 8b1a86d..1bcf1e7 100644 --- a/pagure_distgit/plugin.py +++ b/pagure_distgit/plugin.py @@ -108,68 +108,38 @@ def get_actived_status(namespace, repo): 401, error_code=APIERROR.EMODIFYPROJECTNOTALLOWED ) - # Check if the project is retired in PDC - active = _is_active_in_pdc(repo.name, repo.namespace) + # Check if the project is retired + active = _is_active_project(repo.name, repo.namespace) output = {"active": active} return flask.jsonify(output) -def _is_active_in_pdc(name, namespace): - """Queries PDC and return whether the project is active on the master - branch in PDC or not. +def _is_active_project(name, namespace): + """Queries distgit/lookaside and returns whether the project is not retired + (=active) on the master branch in distgit lookaside or not. """ - pdc_url = flask.current_app.config.get("PDC_URL") - if not pdc_url: + distgit_url = flask.current_app.config.get("APP_URL") + branch = "rawhide" + if not distgit_url: raise pagure.exceptions.APIError( 500, error_code=APIERROR.ENOCODE, - error="This pagure instance has no PDC_URL configured, please " + error="This pagure instance has no APP_URL configured, please " "inform your pagure administrators", ) else: - pdc_url = "%s/component-branches/" % pdc_url.rstrip("/") + _log.debug("Based distgit lookaside url: %s", distgit_url) + distgit_url = f"{distgit_url}lookaside/retired_in_{branch}.json" - _log.debug("Based PDC url: %s", pdc_url) - - to_pdc_namespace = { - "rpms": "rpm", - "modules": "module", - "container": "container", - "flatpaks": "flatpak", - } - to_pdc_namespace = flask.current_app.config.get("PDC_NAMESPACES") or to_pdc_namespace - - try: - pdc_namespace = to_pdc_namespace[namespace] - except Exception: - raise pagure.exceptions.APIError( - 500, - error_code=APIERROR.ENOCODE, - error="Namespace: %s could not be converted to a PDC namespace" % namespace, - ) - - branch = "master" - if namespace in ["rpms", "container"]: - branch = "rawhide" - elif namespace == "flatpaks": - branch = "stable" - - url = "%s?global_component=%s&name=%s&type=%s" % ( - pdc_url, - name, - branch, - pdc_namespace, - ) - - _log.info("Querying PDC at: %s", url) + _log.info("Querying distgit lookaside at: {distgit_url} (PDC is retired!)") try: - req = requests.get(url, timeout=(30, 30)) + req = requests.get(distgit_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, + error="An error occured while querying distgit lookaside: %s" % err, ) try: @@ -181,8 +151,8 @@ def _is_active_in_pdc(name, namespace): 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"] is True + _log.info("%s/%s is active: %s", namespace, name, name not in data[branch]) + return name not in req @DISTGIT_NS.route("/orphan//", methods=["GET"]) @@ -355,8 +325,8 @@ 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): + # Check if the project is not retired + if not _is_active_project(repo.name, repo.namespace): raise pagure.exceptions.APIError( 400, error_code=APIERROR.EINVALIDREQ, @@ -463,8 +433,8 @@ def give_orphan_endpoint(namespace, repo): errors="The new POC 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): + # Check if the project is retired + if not _is_active_project(repo.name, repo.namespace): raise pagure.exceptions.APIError( 400, error_code=APIERROR.EINVALIDREQ, diff --git a/pagure_distgit_tests/test_plugin.py b/pagure_distgit_tests/test_plugin.py index c685fa4..d75bee2 100644 --- a/pagure_distgit_tests/test_plugin.py +++ b/pagure_distgit_tests/test_plugin.py @@ -366,11 +366,11 @@ class PagureFlaskApiTakeOrphanEndpointTests(tests.Modeltests): ) assert output.status_code == 401 - @patch("pagure_distgit.plugin._is_active_in_pdc") + @patch("pagure_distgit.plugin._is_active_project") @patch("pagure_distgit.plugin.pagure.lib.notify.log") - def test_take_orphan(self, mock_log, mock_pdc): + def test_take_orphan(self, mock_log, mock_is_active_project): """Assert that package is correctly adopted.""" - mock_pdc.return_value = True + mock_is_active_project.return_value = True headers = {"Authorization": "token aaabbbcccddd"} repo = pagure.lib.query.get_authorized_project( self.session, @@ -393,7 +393,7 @@ class PagureFlaskApiTakeOrphanEndpointTests(tests.Modeltests): assert mock_log.call_count == 1 - @patch.dict("pagure.config.config", {"PDC_URL": "invalid://"}) + @patch.dict("pagure.config.config", {"APP_URL": "invalid://"}) @patch("pagure_distgit.plugin.requests") @patch("pagure_distgit.plugin.pagure.lib.notify.log") def test_take_orphan_no_reason(self, mock_log, mock_req): @@ -403,7 +403,9 @@ class PagureFlaskApiTakeOrphanEndpointTests(tests.Modeltests): """ resp = MagicMock() resp.url = "http://localhost" - resp.json.return_value = {"results": [{"active": True}]} + resp.json.return_value = { + "rawhide": ["advancecomp", "ahc-tools", "airsched", "amavisd-new"] + } mock_req.get.return_value = resp headers = {"Authorization": "token aaabbbcccddd"} repo = pagure.lib.query.get_authorized_project( @@ -430,15 +432,14 @@ class PagureFlaskApiTakeOrphanEndpointTests(tests.Modeltests): assert mock_req.get.call_count == 1 mock_req.get.assert_called_with( - "invalid:/component-branches/?" - "global_component=test4&name=rawhide&type=rpm", + "invalid://lookaside/retired_in_rawhide.json", timeout=(30, 30), ) - @patch("pagure_distgit.plugin._is_active_in_pdc") - def test_give_orphan_no_user(self, mock_pdc): + @patch("pagure_distgit.plugin._is_active_project") + def test_give_orphan_no_user(self, mock_is_active_project): """Assert that the point_of_contact cannot be updated without specifying user.""" - mock_pdc.return_value = True + mock_is_active_project.return_value = True headers = {"Authorization": "token aaabbbcccddd"} repo = pagure.lib.query.get_authorized_project( self.session, @@ -455,11 +456,11 @@ class PagureFlaskApiTakeOrphanEndpointTests(tests.Modeltests): assert data["error_code"] == "ENOUSER" assert data["errors"] == "You have to specify a user as point_of_contact." - @patch("pagure_distgit.plugin._is_active_in_pdc") + @patch("pagure_distgit.plugin._is_active_project") @patch("pagure_distgit.plugin.pagure.lib.notify.log") - def test_give_orphan(self, mock_log, mock_pdc): + def test_give_orphan(self, mock_log, mock_is_active_project): """Assert that point of contact is correctly updated.""" - mock_pdc.return_value = True + mock_is_active_project.return_value = True headers = {"Authorization": "token aaabbbcccddd"} orphan_user_obj = pagure.lib.query.get_user(self.session, "orphan") repo = pagure.lib.query.get_authorized_project( @@ -476,11 +477,13 @@ class PagureFlaskApiTakeOrphanEndpointTests(tests.Modeltests): data = json.loads(output.get_data(as_text=True)) self.assertDictEqual(data, {"point_of_contact": "pingou"}) - @patch("pagure_distgit.plugin._is_active_in_pdc") + @patch("pagure_distgit.plugin._is_active_project") @patch("pagure_distgit.plugin.pagure.lib.notify.log") - def test_give_orphan_user_not_in_packager_group(self, mock_log, mock_pdc): + def test_give_orphan_user_not_in_packager_group( + self, mock_log, mock_is_active_project + ): """Assert that point of contact is correctly updated.""" - mock_pdc.return_value = True + mock_is_active_project.return_value = True headers = {"Authorization": "token aaabbbcccddd"} orphan_user_obj = pagure.lib.query.get_user(self.session, "orphan") repo = pagure.lib.query.get_authorized_project( @@ -505,11 +508,11 @@ class PagureFlaskApiTakeOrphanEndpointTests(tests.Modeltests): assert data["error_code"] == "ENOTHIGHENOUGH" assert data["errors"] == "The new POC must be a packager to adopt a package." - @patch("pagure_distgit.plugin._is_active_in_pdc") + @patch("pagure_distgit.plugin._is_active_project") @patch("pagure_distgit.plugin.pagure.lib.notify.log") - def test_give_orphan_repo_user_not_orphan(self, mock_log, mock_pdc): + def test_give_orphan_repo_user_not_orphan(self, mock_log, mock_is_active_project): """Assert that point of contact is correctly updated.""" - mock_pdc.return_value = True + mock_is_active_project.return_value = True headers = {"Authorization": "token aaabbbcccddd"} orphan_user_obj = pagure.lib.query.get_user(self.session, "pingou") repo = pagure.lib.query.get_authorized_project( @@ -530,11 +533,13 @@ class PagureFlaskApiTakeOrphanEndpointTests(tests.Modeltests): assert data["error"] == "You are not allowed to modify this project" assert data["error_code"] == "EMODIFYPROJECTNOTALLOWED" - @patch("pagure_distgit.plugin._is_active_in_pdc") + @patch("pagure_distgit.plugin._is_active_project") @patch("pagure_distgit.plugin.pagure.lib.notify.log") - def test_give_orphan_user_not_in_releng_or_admin_group(self, mock_log, mock_pdc): + def test_give_orphan_user_not_in_releng_or_admin_group( + self, mock_log, mock_is_active_project + ): """Assert that point of contact is correctly updated.""" - mock_pdc.return_value = True + mock_is_active_project.return_value = True headers = {"Authorization": "token aaabbbcccddd"} repo = pagure.lib.query.get_authorized_project( self.session,