35
35
36
36
from .. import _load_handler_from_location
37
37
38
- def _github_url ():
39
- return os .environ .get ('GITHUB_URL' ) if os .environ .get ('GITHUB_URL' , '' ) else "https://github.com/"
40
-
41
38
class GithubClientMixin (object ):
42
39
43
40
# PROVIDER_CTX is a dictionary whose entries are passed as keyword arguments
@@ -61,6 +58,19 @@ class GithubClientMixin(object):
61
58
BINDER_TMPL = '{binder_base_url}/gh/{org}/{repo}/{ref}'
62
59
BINDER_PATH_TMPL = BINDER_TMPL + '?filepath={path}'
63
60
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
+
64
74
@property
65
75
def github_client (self ):
66
76
"""Create an upgraded github API client from the HTTP client"""
@@ -118,7 +128,7 @@ def get(self, user):
118
128
name = repo ['name' ],
119
129
))
120
130
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 )
122
132
html = self .render_template ("userview.html" ,
123
133
entries = entries , provider_url = provider_url ,
124
134
next_url = next_url , prev_url = prev_url ,
@@ -188,14 +198,14 @@ def get(self, user, repo, ref, path, **namespace):
188
198
# Account for possibility that GitHub API redirects us to get more accurate breadcrumbs
189
199
# See: https://github.com/jupyter/nbviewer/issues/324
190
200
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' )
192
202
193
203
base_url = u"/github/{user}/{repo}/tree/{ref}" .format (
194
204
user = user , repo = repo , ref = ref ,
195
205
)
196
206
197
207
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
199
209
)
200
210
201
211
breadcrumbs = [{
@@ -284,7 +294,7 @@ def get_notebook_data(self, user, repo, ref, path):
284
294
user = user , repo = repo , ref = ref , path = quote (path )
285
295
)
286
296
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
288
298
)
289
299
with self .catch_client_error ():
290
300
tree_entry = yield self .github_client .get_tree_entry (
0 commit comments