Skip to content

Commit 468485f

Browse files
authored
Merge branch 'master' into step5
2 parents 46e1447 + d8366d8 commit 468485f

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
@@ -35,9 +35,6 @@
3535

3636
from .. import _load_handler_from_location
3737

38-
def _github_url():
39-
return os.environ.get('GITHUB_URL') if os.environ.get('GITHUB_URL', '') else "https://github.com/"
40-
4138
class GithubClientMixin(object):
4239

4340
# PROVIDER_CTX is a dictionary whose entries are passed as keyword arguments
@@ -61,6 +58,19 @@ class GithubClientMixin(object):
6158
BINDER_TMPL = '{binder_base_url}/gh/{org}/{repo}/{ref}'
6259
BINDER_PATH_TMPL = BINDER_TMPL+'?filepath={path}'
6360

61+
@property
62+
def github_url(self):
63+
if getattr(self, "_github_url", None) is None:
64+
if os.environ.get('GITHUB_URL', ''):
65+
self._github_url = os.environ.get('GITHUB_URL')
66+
elif self.github_client.github_api_url == 'https://api.github.com/':
67+
self._github_url = "https://github.com/"
68+
else:
69+
# Github Enterprise
70+
# https://developer.github.com/enterprise/2.18/v3/enterprise-admin/#endpoint-urls
71+
self._github_url = re.sub(r'api/v3/$', '', self.github_client.github_api_url)
72+
return self._github_url
73+
6474
@property
6575
def github_client(self):
6676
"""Create an upgraded github API client from the HTTP client"""
@@ -118,7 +128,7 @@ def get(self, user):
118128
name=repo['name'],
119129
))
120130

121-
provider_url = u"{github_url}{user}".format(user=user, github_url = _github_url())
131+
provider_url = u"{github_url}{user}".format(user=user, github_url=self.github_url)
122132
html = self.render_template("userview.html",
123133
entries=entries, provider_url=provider_url,
124134
next_url=next_url, prev_url=prev_url,
@@ -188,14 +198,14 @@ def get(self, user, repo, ref, path, **namespace):
188198
# Account for possibility that GitHub API redirects us to get more accurate breadcrumbs
189199
# See: https://github.com/jupyter/nbviewer/issues/324
190200
example_file_url = contents[0]['html_url']
191-
user, repo = re.match(r"^https://github\.com/(?P<user>[^\/]+)/(?P<repo>[^\/]+)/.*", example_file_url).group('user', 'repo')
201+
user, repo = re.match(r"^" + self.github_url + "(?P<user>[^\/]+)/(?P<repo>[^\/]+)/.*", example_file_url).group('user', 'repo')
192202

193203
base_url = u"/github/{user}/{repo}/tree/{ref}".format(
194204
user=user, repo=repo, ref=ref,
195205
)
196206

197207
provider_url = u"{github_url}{user}/{repo}/tree/{ref}/{path}".format(
198-
user=user, repo=repo, ref=ref, path=path, github_url = _github_url()
208+
user=user, repo=repo, ref=ref, path=path, github_url=self.github_url
199209
)
200210

201211
breadcrumbs = [{
@@ -284,7 +294,7 @@ def get_notebook_data(self, user, repo, ref, path):
284294
user=user, repo=repo, ref=ref, path=quote(path)
285295
)
286296
blob_url = u"{github_url}{user}/{repo}/blob/{ref}/{path}".format(
287-
user=user, repo=repo, ref=ref, path=quote(path), github_url=_github_url()
297+
user=user, repo=repo, ref=ref, path=quote(path), github_url=self.github_url
288298
)
289299
with self.catch_client_error():
290300
tree_entry = yield self.github_client.get_tree_entry(

0 commit comments

Comments
 (0)