From e91e0bf09c0e17c754833358482dc703054361ac Mon Sep 17 00:00:00 2001 From: "Antoine R. Dumont (@ardumont)" Date: Mon, 22 May 2023 16:51:51 +0200 Subject: [PATCH] cgit: Allow url to be optional Some cgit instances are at a domain's root path so we can build their url directly from their 'instance' parameter. This unifies further the cli to register a lister and the cli to schedule the listed origins from a forge. [1] ``` https://git.kernel.org https://source.codeaurora.org https://git.trueelena.org https://dev.sanctum.geek.nz https://git.trueelena.org https://git.dpkg.org https://anongit.mindrot.org https://git.aurel32.net https://gitweb.gentoo.org https://git.joeyh.name https://git.adrian.geek.nz ``` Refs. swh/devel/swh-lister#4693 --- swh/lister/cgit/lister.py | 9 +++++---- swh/lister/cgit/tests/test_lister.py | 21 ++++++++++++++++++++- swh/lister/pattern.py | 8 +++++--- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/swh/lister/cgit/lister.py b/swh/lister/cgit/lister.py index 4a9aeab..1ab17cb 100644 --- a/swh/lister/cgit/lister.py +++ b/swh/lister/cgit/lister.py @@ -1,4 +1,4 @@ -# Copyright (C) 2019-2022 The Software Heritage developers +# Copyright (C) 2019-2023 The Software Heritage developers # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -46,7 +46,7 @@ class CGitLister(StatelessLister[Repositories]): def __init__( self, scheduler: SchedulerInterface, - url: str, + url: Optional[str] = None, instance: Optional[str] = None, credentials: Optional[CredentialsType] = None, base_git_url: Optional[str] = None, @@ -57,8 +57,9 @@ class CGitLister(StatelessLister[Repositories]): """Lister class for CGit repositories. Args: - url: main URL of the CGit instance, i.e. url of the index - of published git repositories on this instance. + url: (Optional) Root URL of the CGit instance, i.e. url of the index of + published git repositories on this instance. Defaults to + :file:`https://{instance}` if unset. instance: Name of cgit instance. Defaults to url's network location if unset. base_git_url: Optional base git url which allows the origin url diff --git a/swh/lister/cgit/tests/test_lister.py b/swh/lister/cgit/tests/test_lister.py index c6ffcf2..bcf0e8d 100644 --- a/swh/lister/cgit/tests/test_lister.py +++ b/swh/lister/cgit/tests/test_lister.py @@ -1,4 +1,4 @@ -# Copyright (C) 2019-2022 The Software Heritage developers +# Copyright (C) 2019-2023 The Software Heritage developers # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -155,6 +155,25 @@ requests_mock_datadir_missing_url = requests_mock_datadir_factory( ) +def test_lister_cgit_instantiate(swh_scheduler): + """Build a lister with either an url or an instance is fine""" + url = "https://source.codeaurora.org" + lister = CGitLister(swh_scheduler, url=url) + assert lister is not None + assert lister.url == url + + assert CGitLister(swh_scheduler, instance="source.codeaurora.org") is not None + assert lister is not None + assert lister.url == url + + +def test_lister_cgit_fail_to_instantiate(swh_scheduler): + """Build a lister without its url nor its instance should raise""" + # ... It will raise without any of those + with pytest.raises(ValueError, match="'url' or 'instance'"): + CGitLister(swh_scheduler) + + def test_lister_cgit_get_origin_from_repo_failing( requests_mock_datadir_missing_url, swh_scheduler ): diff --git a/swh/lister/pattern.py b/swh/lister/pattern.py index 7fb8680..dd3295c 100644 --- a/swh/lister/pattern.py +++ b/swh/lister/pattern.py @@ -116,12 +116,14 @@ class Lister(Generic[StateType, PageType]): raise ValueError("Must set the LISTER_NAME attribute on Lister classes") self.url: str + # lister can be instantiated using directly their 'url' (the default behavior) + # or derive their url through an 'instance' (their domain's root path) parameter if url is not None: - # Retro-compability with lister already instantiated out of a provided url + # direct url instantiation self.url = url elif url is None and instance is not None: - # Allow lister to be instantiated simply with their type and instance - # (as in their domain like "gitlab.com", "git.garbaye.fr", ...) + # Allow instantiation through their instance parameter (domain's root path) + # (e.g. "gitlab.com", "git.garbaye.fr", ...) self.url = self.build_url(instance) else: raise ValueError(