@@ -132,16 +132,17 @@ def get_datafile_contents(
132132
133133
134134def get_raw_file_url (
135+ github_host : str ,
135136 repository : str ,
136137 branch : str ,
137138 path : pathlib .Path ,
138139 is_public : bool ,
139140):
140- if not is_public :
141- # If the repository is private, then the real links to raw.githubusercontents.com
142- # will be short-lived. In this case, it's better to keep an URL that will
143- # redirect to the correct URL just when asked.
144- return f"https://github.com /{ repository } /raw/{ branch } /{ path } "
141+ if ( not is_public ) or ( not github_host . endswith ( "github.com" )) :
142+ # If the repository is private or hosted on a github enterprise instance,
143+ # then the real links to raw.githubusercontents.com will be short-lived.
144+ # In this case, it's better to keep an URL that will redirect to the correct URL just when asked.
145+ return f"{ github_host } /{ repository } /raw/{ branch } /{ path } "
145146
146147 # Otherwise, we can access the file directly. (shields.io doesn't like the
147148 # github.com domain)
@@ -154,7 +155,9 @@ def get_raw_file_url(
154155 # seconds.
155156
156157
157- def get_repo_file_url (repository : str , branch : str , path : str = "/" ) -> str :
158+ def get_repo_file_url (
159+ github_host : str , repository : str , branch : str , path : str = "/"
160+ ) -> str :
158161 """
159162 Computes the GitHub Web UI URL for a given path:
160163 If the path is empty or ends with a slash, it will be interpreted as a folder,
@@ -166,11 +169,39 @@ def get_repo_file_url(repository: str, branch: str, path: str = "/") -> str:
166169 # See test_get_repo_file_url for precise specifications
167170 path = "/" + path .lstrip ("/" )
168171 part = "tree" if path .endswith ("/" ) else "blob"
169- return f"https://github.com /{ repository } /{ part } /{ branch } { path } " .rstrip ("/" )
172+ return f"{ github_host } /{ repository } /{ part } /{ branch } { path } " .rstrip ("/" )
170173
171174
172- def get_html_report_url (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"
173191 readme_url = get_repo_file_url (
174- repository = repository , branch = branch , path = "/htmlcov/index.html"
192+ github_host , repository = repository , branch = branch , path = html_report_path
175193 )
176- return f"https://htmlpreview.github.io/?{ readme_url } "
194+
195+ if github_host .endswith ("github.com" ):
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
207+ return readme_url
0 commit comments