Skip to content

Commit d8366d8

Browse files
authored
Merge pull request #851 from mskimm/ISSUE-850
fix for Github Enterprise
2 parents 51317e6 + dc72153 commit d8366d8

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

nbviewer/providers/github/handlers.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@
3636
from .. import _load_handler_from_location
3737

3838

39-
def _github_url():
40-
return os.environ.get('GITHUB_URL') if os.environ.get('GITHUB_URL', '') else "https://github.com/"
41-
4239
class GithubClientMixin(object):
4340
PROVIDER_CTX = {
4441
'provider_label': 'GitHub',
@@ -50,6 +47,19 @@ class GithubClientMixin(object):
5047
BINDER_TMPL = '{binder_base_url}/gh/{org}/{repo}/{ref}'
5148
BINDER_PATH_TMPL = BINDER_TMPL+'?filepath={path}'
5249

50+
@property
51+
def github_url(self):
52+
if getattr(self, "_github_url", None) is None:
53+
if os.environ.get('GITHUB_URL', ''):
54+
self._github_url = os.environ.get('GITHUB_URL')
55+
elif self.github_client.github_api_url == 'https://api.github.com/':
56+
self._github_url = "https://github.com/"
57+
else:
58+
# Github Enterprise
59+
# https://developer.github.com/enterprise/2.18/v3/enterprise-admin/#endpoint-urls
60+
self._github_url = re.sub(r'api/v3/$', '', self.github_client.github_api_url)
61+
return self._github_url
62+
5363
@property
5464
def github_client(self):
5565
"""Create an upgraded github API client from the HTTP client"""
@@ -107,7 +117,7 @@ def get(self, user):
107117
name=repo['name'],
108118
))
109119

110-
provider_url = u"{github_url}{user}".format(user=user, github_url = _github_url())
120+
provider_url = u"{github_url}{user}".format(user=user, github_url=self.github_url)
111121
html = self.render_template("userview.html",
112122
entries=entries, provider_url=provider_url,
113123
next_url=next_url, prev_url=prev_url,
@@ -169,14 +179,14 @@ def get(self, user, repo, ref, path, **namespace):
169179
# Account for possibility that GitHub API redirects us to get more accurate breadcrumbs
170180
# See: https://github.com/jupyter/nbviewer/issues/324
171181
example_file_url = contents[0]['html_url']
172-
user, repo = re.match(r"^https://github\.com/(?P<user>[^\/]+)/(?P<repo>[^\/]+)/.*", example_file_url).group('user', 'repo')
182+
user, repo = re.match(r"^" + self.github_url + "(?P<user>[^\/]+)/(?P<repo>[^\/]+)/.*", example_file_url).group('user', 'repo')
173183

174184
base_url = u"/github/{user}/{repo}/tree/{ref}".format(
175185
user=user, repo=repo, ref=ref,
176186
)
177187

178188
provider_url = u"{github_url}{user}/{repo}/tree/{ref}/{path}".format(
179-
user=user, repo=repo, ref=ref, path=path, github_url = _github_url()
189+
user=user, repo=repo, ref=ref, path=path, github_url=self.github_url
180190
)
181191

182192
breadcrumbs = [{
@@ -266,7 +276,7 @@ def get(self, user, repo, ref, path):
266276
user=user, repo=repo, ref=ref, path=quote(path)
267277
)
268278
blob_url = u"{github_url}{user}/{repo}/blob/{ref}/{path}".format(
269-
user=user, repo=repo, ref=ref, path=quote(path), github_url=_github_url()
279+
user=user, repo=repo, ref=ref, path=quote(path), github_url=self.github_url
270280
)
271281
with self.catch_client_error():
272282
tree_entry = yield self.github_client.get_tree_entry(

0 commit comments

Comments
 (0)