Skip to content

feat: infer gitea api_domain from domain, if unspecified. #675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions semantic_release/hvcs/gitea.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}"

Expand Down
33 changes: 18 additions & 15 deletions tests/unit/semantic_release/hvcs/test_gitea.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
),
],
)
Expand Down Expand Up @@ -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)

Expand Down