Skip to content

Commit 717fea2

Browse files
daniel-anyaewjoachim
authored andcommitted
Adds support for Github pages html url coverage report
1 parent fdd2bd2 commit 717fea2

File tree

6 files changed

+64
-9
lines changed

6 files changed

+64
-9
lines changed

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ inputs:
102102
notice, warning and error as annotation type. For more information look here:
103103
https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-a-notice-message
104104
default: warning
105+
USE_GH_PAGES_HTML_URL:
106+
description: >
107+
If true, will use the GitHub Pages URL for the coverage report instead of the raw URL or an htmlpreview.github.io link.
108+
default: false
105109
VERBOSE:
106110
description: >
107111
Deprecated, see https://docs.github.com/en/actions/monitoring-and-troubleshooting-workflows/enabling-debug-logging

coverage_comment/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ def save_coverage_data_files(
430430
github_host=github_host,
431431
branch=config.FINAL_COVERAGE_DATA_BRANCH,
432432
repository=config.GITHUB_REPOSITORY,
433+
use_gh_pages_html_url=config.USE_GH_PAGES_HTML_URL,
433434
)
434435
readme_file, log_message = communication.get_readme_and_log(
435436
is_public=is_public,

coverage_comment/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class Config:
6363
ANNOTATE_MISSING_LINES: bool = False
6464
ANNOTATION_TYPE: str = "warning"
6565
MAX_FILES_IN_COMMENT: int = 25
66+
USE_GH_PAGES_HTML_URL: bool = False
6667
VERBOSE: bool = False
6768
# Only for debugging, not exposed in the action:
6869
FORCE_WORKFLOW_RUN: bool = False

coverage_comment/storage.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,36 @@ def get_repo_file_url(
172172
return f"{github_host}/{repository}/{part}/{branch}{path}".rstrip("/")
173173

174174

175-
def get_html_report_url(github_host: str, repository: str, branch: str) -> str:
175+
def get_html_report_url(
176+
github_host: str,
177+
repository: str,
178+
branch: str,
179+
use_gh_pages_html_url: bool = False,
180+
) -> str:
181+
"""
182+
Computes the URL for an HTML report:
183+
- If use_gh_pages_html_url is True:
184+
* GitHub.com => https://<user_or_org>.github.io/<repo>/<pages_path>
185+
* GitHub Enterprise => https://<host>/pages/<user_or_org>/<repo>/<pages_path>
186+
- If use_gh_pages_html_url is False:
187+
* GitHub.com => https://htmlpreview.github.io/?<readme_url>
188+
* GitHub Enterprise => <readme_url>
189+
"""
190+
html_report_path = "htmlcov/index.html"
176191
readme_url = get_repo_file_url(
177-
github_host, repository=repository, branch=branch, path="/htmlcov/index.html"
192+
github_host, repository=repository, branch=branch, path=html_report_path
178193
)
194+
179195
if github_host.endswith("github.com"):
180-
return f"https://htmlpreview.github.io/?{readme_url}"
196+
if use_gh_pages_html_url:
197+
user, repo = repository.split("/", 1)
198+
return f"https://{user}.github.io/{repo}/{html_report_path}"
199+
else:
200+
return f"https://htmlpreview.github.io/?{readme_url}"
201+
else:
202+
# Assume GitHub Enterprise
203+
if use_gh_pages_html_url:
204+
return f"{github_host}/pages/{repository}/{html_report_path}"
205+
206+
# Always fallback to the raw readme_url
181207
return readme_url

tests/integration/test_github.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,28 @@ def test_get_repository_info(gh, session):
4545

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

48+
4849
@pytest.mark.parametrize(
4950
"api_url, expected",
5051
[
5152
("https://api.github.com/repos/foo/bar", "https://github.com"),
5253
("https://api.github.com:8080/repos/foo/bar", "https://github.com:8080"),
5354
("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-
]
55+
(
56+
"https://my-ghe.company.com/api/v3/repos/foo/bar",
57+
"https://my-ghe.company.com",
58+
),
59+
(
60+
"https://my-ghe.company.com/api/v3/repos/foo/bar/issues",
61+
"https://my-ghe.company.com",
62+
),
63+
],
5764
)
5865
def test_extract_github_host(api_url, expected):
5966
result = github.extract_github_host(api_url=api_url)
6067
assert result == expected
6168

69+
6270
def test_download_artifact(gh, session, zip_bytes):
6371
artifacts = [
6472
{"name": "bar", "id": 456},

tests/unit/test_storage.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,20 +227,35 @@ def test_get_repo_file_url__no_path(github_host):
227227

228228

229229
@pytest.mark.parametrize(
230-
"github_host, expected",
230+
"github_host,use_gh_pages_html_url,expected",
231231
[
232232
(
233233
"https://github.com",
234+
True,
235+
"https://foo.github.io/bar/htmlcov/index.html",
236+
),
237+
(
238+
"https://github.com",
239+
False,
234240
"https://htmlpreview.github.io/?https://github.com/foo/bar/blob/baz/htmlcov/index.html",
235241
),
236242
(
237243
"https://github.mycompany.com",
244+
True,
245+
"https://github.mycompany.com/pages/foo/bar/htmlcov/index.html",
246+
),
247+
(
248+
"https://github.mycompany.com",
249+
False,
238250
"https://github.mycompany.com/foo/bar/blob/baz/htmlcov/index.html",
239251
),
240252
],
241253
)
242-
def test_get_html_report_url(github_host, expected):
254+
def test_get_html_report_url(github_host, use_gh_pages_html_url, expected):
243255
result = storage.get_html_report_url(
244-
github_host=github_host, repository="foo/bar", branch="baz"
256+
github_host=github_host,
257+
repository="foo/bar",
258+
branch="baz",
259+
use_gh_pages_html_url=use_gh_pages_html_url,
245260
)
246261
assert result == expected

0 commit comments

Comments
 (0)