From 7fe5e1a8e2494e4dc6b960ece990a0ff8329e78b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Pi=C3=A9dallu?= Date: Sat, 24 Feb 2024 22:15:28 +0100 Subject: [PATCH] Fix some gitlab instances that return 404 on url-encoded project paths, by calling search --- autoupdate_app_sources/rest_api.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/autoupdate_app_sources/rest_api.py b/autoupdate_app_sources/rest_api.py index bf3f0c1..4685cfb 100644 --- a/autoupdate_app_sources/rest_api.py +++ b/autoupdate_app_sources/rest_api.py @@ -25,7 +25,7 @@ class GithubAPI: def internal_api(self, uri: str) -> Any: url = f"https://api.github.com/{uri}" r = requests.get(url, auth=self.auth) - assert r.status_code == 200, r + r.raise_for_status() return r.json() def tags(self) -> list[dict[str, str]]: @@ -66,7 +66,16 @@ class GitlabAPI: return match.group(1) def find_project_id(self, project: str) -> int: - project = self.internal_api(f"projects/{project.replace('/', '%2F')}") + try: + project = self.internal_api(f"projects/{project.replace('/', '%2F')}") + except requests.exceptions.HTTPError as err: + if err.response.status_code != 404: + raise + # Second chance for some buggy gitlab instances... + name = self.project_path.split("/")[-1] + projects = self.internal_api(f"projects?search={name}") + project = next(filter(lambda x: x.get("path_with_namespace") == self.project_path, projects)) + assert isinstance(project, dict) project_id = project.get("id", None) return project_id @@ -74,7 +83,7 @@ class GitlabAPI: def internal_api(self, uri: str) -> Any: url = f"{self.forge_root}/api/v4/{uri}" r = requests.get(url) - assert r.status_code == 200, r + r.raise_for_status() return r.json() def tags(self) -> list[dict[str, str]]: @@ -142,7 +151,7 @@ class GiteaForgejoAPI: def internal_api(self, uri: str): url = f"{self.forge_root}/api/v1/{uri}" r = requests.get(url) - assert r.status_code == 200, r + r.raise_for_status() return r.json() def tags(self) -> list[dict[str, Any]]: