Skip to content

Commit

Permalink
feat: Support Github Enterprise server
Browse files Browse the repository at this point in the history
  • Loading branch information
ukalwa authored and relekang committed Nov 30, 2021
1 parent bf21fa6 commit b4e01f1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
13 changes: 13 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,19 @@ The name of your hvcs. Currently only ``github`` and ``gitlab`` are supported.

Default: `github`

.. _config-hvcs_domain:

``hvcs_domain``
---------------
The domain url (without https://) of your custom vcs server.

.. _config-hvcs_api_domain:

``hvcs_api_domain``
-------------------
The api url (without https://) of your custom vcs server.


.. _config-check_build_status:

``check_build_status``
Expand Down
14 changes: 11 additions & 3 deletions semantic_release/hvcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ def domain() -> str:
:return: The Github domain
"""
hvcs_domain = config.get("hvcs_domain")
# ref: https://docs.github.com/en/actions/reference/environment-variables#default-environment-variables
hvcs_domain = config.get(
"hvcs_domain", os.getenv("GITHUB_SERVER_URL", "").replace("https://", "")
)
domain = hvcs_domain if hvcs_domain else Github.DEFAULT_DOMAIN
return domain

Expand All @@ -113,8 +116,13 @@ def api_url() -> str:
"""
# not necessarily prefixed with api in the case of a custom domain, so
# can't just default DEFAULT_DOMAIN to github.com
hvcs_domain = config.get("hvcs_domain")
hostname = hvcs_domain if hvcs_domain else "api." + Github.DEFAULT_DOMAIN
# ref: https://docs.github.com/en/actions/reference/environment-variables#default-environment-variables
hvcs_api_domain = config.get(
"hvcs_api_domain", os.getenv("GITHUB_API_URL", "").replace("https://", "")
)
hostname = (
hvcs_api_domain if hvcs_api_domain else "api." + Github.DEFAULT_DOMAIN
)
return f"https://{hostname}"

@staticmethod
Expand Down
30 changes: 24 additions & 6 deletions tests/test_hvcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,25 @@ def test_check_custom_gitlab_token_should_return_false():


@pytest.mark.parametrize(
"hvcs,hvcs_domain,expected_domain,api_url,ci_server_host",
"hvcs,hvcs_domain,expected_domain,api_url,ci_server_host,hvcs_api_domain",
[
("github", None, "github.com", "https://api.github.com", None),
("gitlab", None, "gitlab.com", "https://gitlab.com", None),
("github", None, "github.com", "https://api.github.com", None, None),
("gitlab", None, "gitlab.com", "https://gitlab.com", None, None),
(
"github",
"github.example.com",
"github.example.com",
"https://github.example.com",
"https://api.github.enterprise",
None,
"api.github.enterprise",
),
(
"gitlab",
"example.gitlab.com",
"example.gitlab.com",
"https://example.gitlab.com",
None,
None,
),
(
"gitlab",
Expand All @@ -130,17 +132,20 @@ def test_check_custom_gitlab_token_should_return_false():
"example2.gitlab.com",
"https://example2.gitlab.com",
"ciserverhost.gitlab.com",
None,
),
],
)
@mock.patch("os.environ", {"GL_TOKEN": "token"})
def test_get_domain_should_have_expected_domain(
hvcs, hvcs_domain, expected_domain, api_url, ci_server_host
hvcs, hvcs_domain, expected_domain, api_url, ci_server_host, hvcs_api_domain
):

with mock.patch(
"semantic_release.hvcs.config.get",
wrapped_config_get(hvcs_domain=hvcs_domain, hvcs=hvcs),
wrapped_config_get(
hvcs_domain=hvcs_domain, hvcs=hvcs, hvcs_api_domain=hvcs_api_domain
),
):
with mock.patch(
"os.environ",
Expand All @@ -155,6 +160,19 @@ def test_get_domain_should_have_expected_domain(
assert get_hvcs().api_url() == api_url


@mock.patch("semantic_release.hvcs.config.get", wrapped_config_get(hvcs="github"))
@mock.patch(
"os.environ",
{
"GITHUB_API_URL": "api.github.enterprise",
"GITHUB_SERVER_URL": "github.enterprise",
},
)
def test_ghe_domain_should_be_retrieved_from_env():
assert get_hvcs().domain() == "github.enterprise"
assert get_hvcs().api_url() == "https://api.github.enterprise"


@mock.patch("semantic_release.hvcs.config.get", wrapped_config_get(hvcs="gitlab"))
@mock.patch("os.environ", {"GL_TOKEN": "token"})
def test_get_token():
Expand Down

0 comments on commit b4e01f1

Please sign in to comment.