Skip to content
Snippets Groups Projects
Verified Commit 8a82bbf9 authored by Antoine R. Dumont's avatar Antoine R. Dumont
Browse files

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) <anlambert@softwareheritage.org>.

Related to infra/sysadm-environment#4644
parent 0baaf68c
No related branches found
No related tags found
No related merge requests found
# 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"
)
......@@ -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://<host>/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:
......
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment