lister: Allow lister to build url out of the instance parameter
This pushes the rather elementary logic within the lister's scope. This will simplify and unify cli call between lister and scheduler clis. This will also allow to reduce erroneous operations which can happen for example in the add-forge-now. With the following, we will only have to provide the type and the instance, then everything will be scheduled properly. Refs. swh/devel/swh-lister#4693
This commit is contained in:
parent
596e8c6c40
commit
19bdeefb14
7 changed files with 100 additions and 18 deletions
|
@ -99,7 +99,7 @@ class GitLabLister(Lister[GitLabListerState, PageResult]):
|
|||
def __init__(
|
||||
self,
|
||||
scheduler,
|
||||
url: str,
|
||||
url: Optional[str] = None,
|
||||
name: Optional[str] = "gitlab",
|
||||
instance: Optional[str] = None,
|
||||
credentials: Optional[CredentialsType] = None,
|
||||
|
@ -113,7 +113,8 @@ class GitLabLister(Lister[GitLabListerState, PageResult]):
|
|||
self.LISTER_NAME = name
|
||||
super().__init__(
|
||||
scheduler=scheduler,
|
||||
url=url.rstrip("/"),
|
||||
# if url is not provided, url will be built in the `build_url` method
|
||||
url=url.rstrip("/") if url else None,
|
||||
instance=instance,
|
||||
credentials=credentials,
|
||||
max_origins_per_page=max_origins_per_page,
|
||||
|
@ -138,6 +139,11 @@ class GitLabLister(Lister[GitLabListerState, PageResult]):
|
|||
if api_token:
|
||||
self.session.headers["Authorization"] = f"Bearer {api_token}"
|
||||
|
||||
def build_url(self, instance: str) -> str:
|
||||
"""Build gitlab api url."""
|
||||
prefix_url = super().build_url(instance)
|
||||
return f"{prefix_url}/api/v4"
|
||||
|
||||
def state_from_dict(self, d: Dict[str, Any]) -> GitLabListerState:
|
||||
return GitLabListerState(**d)
|
||||
|
||||
|
|
|
@ -22,17 +22,30 @@ logger = logging.getLogger(__name__)
|
|||
|
||||
|
||||
def api_url(instance: str) -> str:
|
||||
return f"https://{instance}/api/v4/"
|
||||
return f"https://{instance}/api/v4"
|
||||
|
||||
|
||||
def _match_request(request, lister_name="gitlab"):
|
||||
return request.headers.get("User-Agent") == USER_AGENT_TEMPLATE % lister_name
|
||||
|
||||
|
||||
def test_lister_gitlab_fail_to_instantiate(swh_scheduler):
|
||||
"""Build a lister without its url nor its instance should raise"""
|
||||
# while instantiating a gitlab lister is fine with the url or the instance...
|
||||
assert GitLabLister(swh_scheduler, url="https://gitlab.com/api/v4") is not None
|
||||
assert GitLabLister(swh_scheduler, instance="gitlab.fr") is not None
|
||||
|
||||
# ... It will raise without any of those
|
||||
with pytest.raises(ValueError, match="'url' or 'instance'"):
|
||||
GitLabLister(swh_scheduler)
|
||||
|
||||
|
||||
def test_lister_gitlab(datadir, swh_scheduler, requests_mock):
|
||||
"""Gitlab lister supports full listing"""
|
||||
instance = "gitlab.com"
|
||||
lister = GitLabLister(swh_scheduler, url=api_url(instance), instance=instance)
|
||||
lister = GitLabLister(swh_scheduler, instance=instance)
|
||||
|
||||
assert lister.url == api_url(instance)
|
||||
|
||||
response = gitlab_page_response(datadir, instance, 1)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue