gitlab: Deal with missing or trailing / in url input

This commit is contained in:
Antoine R. Dumont (@ardumont) 2021-01-27 18:02:08 +01:00
parent 3bede83d0f
commit 72be074a79
No known key found for this signature in database
GPG key ID: 52E2E9840D10C3B8
2 changed files with 15 additions and 2 deletions

View file

@ -106,7 +106,10 @@ class GitLabLister(Lister[GitLabListerState, PageResult]):
if instance is None:
instance = parse_url(url).host
super().__init__(
scheduler=scheduler, url=url, instance=instance, credentials=credentials,
scheduler=scheduler,
url=url.rstrip("/"),
instance=instance,
credentials=credentials,
)
self.incremental = incremental
self.last_page: Optional[str] = None
@ -161,7 +164,7 @@ class GitLabLister(Lister[GitLabListerState, PageResult]):
}
if id_after is not None:
parameters["id_after"] = str(id_after)
return f"{self.url}projects?{urlencode(parameters)}"
return f"{self.url}/projects?{urlencode(parameters)}"
def get_pages(self) -> Iterator[PageResult]:
next_page: Optional[str]

View file

@ -216,6 +216,16 @@ def test_lister_gitlab_credentials(swh_scheduler):
assert lister.session.headers["Authorization"] == "Bearer api-token"
@pytest.mark.parametrize("url", [api_url("gitlab").rstrip("/"), api_url("gitlab"),])
def test_lister_gitlab_url_computation(url, swh_scheduler):
lister = GitLabLister(scheduler=swh_scheduler, url=url)
assert not lister.url.endswith("/")
page_url = lister.page_url()
# ensure the generated url contains the separated /
assert page_url.startswith(f"{lister.url}/projects")
@pytest.mark.parametrize(
"url,expected_result",
[