diff --git a/semantic_release/hvcs/gitea.py b/semantic_release/hvcs/gitea.py index 23adc3594..a8800f8f4 100644 --- a/semantic_release/hvcs/gitea.py +++ b/semantic_release/hvcs/gitea.py @@ -8,6 +8,7 @@ import os from requests import HTTPError +from urllib3.util.url import Url, parse_url from semantic_release.helpers import logged_function from semantic_release.hvcs._base import HvcsBase @@ -49,13 +50,34 @@ def __init__( ) -> None: self._remote_url = remote_url - self.hvcs_domain = hvcs_domain or os.getenv( - "GITEA_SERVER_URL", self.DEFAULT_DOMAIN - ).replace("https://", "") + domain_url = parse_url( + hvcs_domain + or os.getenv("GITEA_SERVER_URL", "") + or self.DEFAULT_DOMAIN + ) + + # Strip any scheme, query or fragment from the domain + self.hvcs_domain = Url( + host=domain_url.host, port=domain_url.port, path=domain_url.path + ).url.rstrip("/") + + api_domain_parts = parse_url( + hvcs_api_domain + or os.getenv("GITEA_API_URL", "") + or Url( + # infer from Domain url and append the default api path + scheme=domain_url.scheme, + host=self.hvcs_domain, + path=self.DEFAULT_API_PATH + ).url + ) - self.hvcs_api_domain = hvcs_api_domain or os.getenv( - "GITEA_API_URL", self.DEFAULT_API_DOMAIN - ).replace("https://", "") + # Strip any scheme, query or fragment from the api domain + self.hvcs_api_domain = Url( + host=api_domain_parts.host, + port=api_domain_parts.port, + path=api_domain_parts.path + ).url.rstrip("/") self.api_url = f"https://{self.hvcs_api_domain}" diff --git a/tests/unit/semantic_release/hvcs/test_gitea.py b/tests/unit/semantic_release/hvcs/test_gitea.py index 9c47d7550..fe29c6e2d 100644 --- a/tests/unit/semantic_release/hvcs/test_gitea.py +++ b/tests/unit/semantic_release/hvcs/test_gitea.py @@ -29,39 +29,42 @@ def default_gitea_client(): @pytest.mark.parametrize( - ( - "patched_os_environ, hvcs_domain, hvcs_api_domain, " - "expected_hvcs_domain, expected_hvcs_api_domain" - ), + str.join(", ", [ + "patched_os_environ", + "hvcs_domain", + "hvcs_api_domain", + "expected_hvcs_domain", + "expected_hvcs_api_domain", + ]), [ ({}, None, None, Gitea.DEFAULT_DOMAIN, Gitea.DEFAULT_API_DOMAIN), ( {"GITEA_SERVER_URL": "https://special.custom.server/vcs/"}, None, None, - "special.custom.server/vcs/", - Gitea.DEFAULT_API_DOMAIN, + "special.custom.server/vcs", + "special.custom.server/vcs/api/v1", ), ( {"GITEA_API_URL": "https://api.special.custom.server/"}, None, None, Gitea.DEFAULT_DOMAIN, - "api.special.custom.server/", + "api.special.custom.server", ), ( {"GITEA_SERVER_URL": "https://special.custom.server/vcs/"}, "https://example.com", None, - "https://example.com", - Gitea.DEFAULT_API_DOMAIN, + "example.com", + "example.com/api/v1", ), ( {"GITEA_API_URL": "https://api.special.custom.server/"}, None, "https://api.example.com", Gitea.DEFAULT_DOMAIN, - "https://api.example.com", + "api.example.com", ), ], ) @@ -90,11 +93,11 @@ def test_gitea_client_init( token=token, ) - assert client.hvcs_domain == expected_hvcs_domain - assert client.hvcs_api_domain == expected_hvcs_api_domain - assert client.api_url == f"https://{client.hvcs_api_domain}" - assert client.token == token - assert client._remote_url == remote_url + assert expected_hvcs_domain == client.hvcs_domain + assert expected_hvcs_api_domain == client.hvcs_api_domain + assert f"https://{expected_hvcs_api_domain}" == client.api_url + assert token == client.token + assert remote_url == client._remote_url assert hasattr(client, "session") assert isinstance(getattr(client, "session", None), Session)