From a6e18d05a286a1d3bd1d2481966b6d787315de63 Mon Sep 17 00:00:00 2001 From: codejedi365 Date: Sun, 31 Mar 2024 16:22:08 -0400 Subject: [PATCH 1/3] test(gitea): add test of custom server path & custom api domain --- .../unit/semantic_release/hvcs/test_gitea.py | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) 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) From 8e3b672f7551856090a4c8515447ff2be26a3c0e Mon Sep 17 00:00:00 2001 From: Luke Elliott Date: Fri, 11 Aug 2023 20:02:29 +0100 Subject: [PATCH 2/3] feat: infer gitea api_domain from domain, if unspecified --- semantic_release/hvcs/gitea.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/semantic_release/hvcs/gitea.py b/semantic_release/hvcs/gitea.py index 23adc3594..d1a55f174 100644 --- a/semantic_release/hvcs/gitea.py +++ b/semantic_release/hvcs/gitea.py @@ -53,9 +53,13 @@ def __init__( "GITEA_SERVER_URL", self.DEFAULT_DOMAIN ).replace("https://", "") - self.hvcs_api_domain = hvcs_api_domain or os.getenv( - "GITEA_API_URL", self.DEFAULT_API_DOMAIN - ).replace("https://", "") + self.hvcs_api_domain = ( + hvcs_api_domain + or os.getenv("GITEA_API_URL", "").replace("https://", "") + or ( + f"{self.hvcs_domain}{'' if self.hvcs_domain[-1] == '/' else '/'}{self.DEFAULT_API_PATH}" + ) + ) self.api_url = f"https://{self.hvcs_api_domain}" From 14e911b2af76d4d803d9700cea0f926a6a00bb83 Mon Sep 17 00:00:00 2001 From: codejedi365 Date: Sun, 31 Mar 2024 16:18:38 -0400 Subject: [PATCH 3/3] refactor(hvcs-gitea): uniformly handle protocol prefixes --- semantic_release/hvcs/gitea.py | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/semantic_release/hvcs/gitea.py b/semantic_release/hvcs/gitea.py index d1a55f174..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,18 +50,35 @@ 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("/") - self.hvcs_api_domain = ( + api_domain_parts = parse_url( hvcs_api_domain - or os.getenv("GITEA_API_URL", "").replace("https://", "") - or ( - f"{self.hvcs_domain}{'' if self.hvcs_domain[-1] == '/' else '/'}{self.DEFAULT_API_PATH}" - ) + 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 ) + # 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}" self.token = token