From 8a82bbf95fd23a19022541928f54057ed605ed1e Mon Sep 17 00:00:00 2001 From: "Antoine R. Dumont (@ardumont)" Date: Fri, 21 Oct 2022 17:50:49 +0200 Subject: [PATCH] gogs/lister: Allow public gogs instance listing Prior to this commit, the lister assumed authentication was required. It exists public gogs instances which do not require it. This also updates documentation to mention the usual api location. This is useful when people wants to actually trigger a listing as a pre-check flight. This drops repetitive instruction in the gitea lister as well. Co-authored with Antoine Lambert (@anlambert) . Related to infra/sysadm-environment#4644 --- swh/lister/gitea/lister.py | 7 +------ swh/lister/gogs/lister.py | 24 +++++++++++++----------- swh/lister/gogs/tests/test_lister.py | 8 ++++---- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/swh/lister/gitea/lister.py b/swh/lister/gitea/lister.py index 51084b6..e429756 100644 --- a/swh/lister/gitea/lister.py +++ b/swh/lister/gitea/lister.py @@ -1,4 +1,4 @@ -# Copyright (C) 2018-2021 The Software Heritage developers +# Copyright (C) 2018-2022 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -20,8 +20,3 @@ class GiteaLister(GogsLister): accessible at https://try.gitea.io/api/v1/ and https://codeberg.org/api/v1/.""" LISTER_NAME = "gitea" - - def on_anonymous_mode(self): - logger.warning( - "No authentication token set in configuration, using anonymous mode" - ) diff --git a/swh/lister/gogs/lister.py b/swh/lister/gogs/lister.py index f87100d..ce8a398 100644 --- a/swh/lister/gogs/lister.py +++ b/swh/lister/gogs/lister.py @@ -50,10 +50,15 @@ class GogsLister(Lister[GogsListerState, GogsListerPage]): Gogs API documentation: https://github.com/gogs/docs-api - The API is protected behind authentication so credentials/API tokens - are mandatory. It supports pagination and provides next page URL - through the 'next' value of the 'Link' header. The default value for - page size ('limit') is 10 but the maximum allowed value is 50. + The API may be protected behind authentication so credentials/API tokens can be + provided. + + The lister supports pagination and provides next page URL through the 'next' value + of the 'Link' header. The default value for page size ('limit') is 10 but the + maximum allowed value is 50. + + Api can usually be found at the location: https:///api/v1/repos/search + """ LISTER_NAME = "gogs" @@ -90,17 +95,15 @@ class GogsLister(Lister[GogsListerState, GogsListerPage]): username = cred.get("username") self.api_token = cred["password"] logger.info("Using authentication credentials from user %s", username) - else: - # Raises an error on Gogs, or a warning on Gitea - self.on_anonymous_mode() self.session.headers.update({"Accept": "application/json"}) if self.api_token: self.session.headers["Authorization"] = f"token {self.api_token}" - - def on_anonymous_mode(self): - raise ValueError("No credentials or API token provided") + else: + logger.warning( + "No authentication token set in configuration, using anonymous mode" + ) def state_from_dict(self, d: Dict[str, Any]) -> GogsListerState: return GogsListerState(**d) @@ -153,7 +156,6 @@ class GogsLister(Lister[GogsListerState, GogsListerPage]): while next_link is not None: repos = self.extract_repos(body) - assert len(links) > 0, "API changed: no Link header found" if "next" in links: next_link = links["next"]["url"] else: diff --git a/swh/lister/gogs/tests/test_lister.py b/swh/lister/gogs/tests/test_lister.py index 4f9e370..c90c2bc 100644 --- a/swh/lister/gogs/tests/test_lister.py +++ b/swh/lister/gogs/tests/test_lister.py @@ -139,16 +139,16 @@ def test_gogs_full_listing( def test_gogs_auth_instance( swh_scheduler, requests_mock, trygogs_p1, trygogs_p2, trygogs_p3_empty ): - """Covers token authentication, token from credentials, + """Covers without authentication, token authentication, token from credentials, instance inference from URL.""" api_token = "secret" instance = "try_gogs" # Test lister initialization without api_token or credentials: - with pytest.raises(ValueError, match="No credentials or API token provided"): - kwargs1 = dict(url=TRY_GOGS_URL, instance=instance) - GogsLister(scheduler=swh_scheduler, **kwargs1) + kwargs1 = dict(url=TRY_GOGS_URL, instance=instance) + lister = GogsLister(scheduler=swh_scheduler, **kwargs1) + assert "Authorization" not in lister.session.headers # Test lister initialization using api_token: kwargs2 = dict(url=TRY_GOGS_URL, api_token=api_token, instance=instance)