Skip to content

Commit fdd2bd2

Browse files
daniel-anyaewjoachim
authored andcommitted
Updates extract_github_host function + test
1 parent 4f95ed0 commit fdd2bd2

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

coverage_comment/github.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,29 +59,25 @@ def extract_github_host(api_url: str) -> str:
5959
The base GitHub web host URL (e.g., 'https://github.com',
6060
'https://my-ghe.company.com').
6161
"""
62-
try:
63-
parsed_url = urlparse(api_url)
64-
scheme = parsed_url.scheme
65-
netloc = parsed_url.netloc # This includes the domain and potentially the port
66-
67-
# Special case for GitHub.com API
68-
if netloc == "api.github.com":
69-
host_domain = "github.com"
70-
# Special case for GitHub.com with port (less common but good practice)
71-
elif netloc.startswith("api.github.com:"):
72-
# Remove 'api.' prefix but keep the port
73-
host_domain = netloc.replace("api.", "", 1)
74-
# General case for GitHub Enterprise (netloc is already the host:port)
75-
else:
76-
host_domain = netloc
62+
parsed_url = urlparse(api_url)
63+
scheme = parsed_url.scheme
64+
netloc = parsed_url.netloc # This includes the domain and potentially the port
65+
66+
# Special case for GitHub.com API
67+
if netloc == "api.github.com":
68+
host_domain = "github.com"
69+
# Special case for GitHub.com with port (less common but good practice)
70+
elif netloc.startswith("api.github.com:"):
71+
# Remove 'api.' prefix but keep the port
72+
host_domain = netloc.replace("api.", "", 1)
73+
# General case for GitHub Enterprise (netloc is already the host:port)
74+
else:
75+
host_domain = netloc
7776

78-
# Reconstruct the host URL
79-
host_url = f"{scheme}://{host_domain}"
77+
# Reconstruct the host URL
78+
host_url = f"{scheme}://{host_domain}"
8079

81-
return host_url
82-
except Exception as e:
83-
log.error(f"Error parsing URL {api_url}: {e}")
84-
return ""
80+
return host_url
8581

8682

8783
def download_artifact(

tests/integration/test_github.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ def test_get_repository_info(gh, session):
4545

4646
assert info == github.RepositoryInfo(default_branch="baz", visibility="public")
4747

48+
@pytest.mark.parametrize(
49+
"api_url, expected",
50+
[
51+
("https://api.github.com/repos/foo/bar", "https://github.com"),
52+
("https://api.github.com:8080/repos/foo/bar", "https://github.com:8080"),
53+
("https://api.github.com/repos/foo/bar/issues", "https://github.com"),
54+
("https://my-ghe.company.com/api/v3/repos/foo/bar", "https://my-ghe.company.com"),
55+
("https://my-ghe.company.com/api/v3/repos/foo/bar/issues", "https://my-ghe.company.com"),
56+
]
57+
)
58+
def test_extract_github_host(api_url, expected):
59+
result = github.extract_github_host(api_url=api_url)
60+
assert result == expected
4861

4962
def test_download_artifact(gh, session, zip_bytes):
5063
artifacts = [

0 commit comments

Comments
 (0)