36
36
from .. import _load_handler_from_location
37
37
38
38
39
- def _github_url ():
40
- return os .environ .get ('GITHUB_URL' ) if os .environ .get ('GITHUB_URL' , '' ) else "https://github.com/"
41
-
42
39
class GithubClientMixin (object ):
43
40
PROVIDER_CTX = {
44
41
'provider_label' : 'GitHub' ,
@@ -50,6 +47,19 @@ class GithubClientMixin(object):
50
47
BINDER_TMPL = '{binder_base_url}/gh/{org}/{repo}/{ref}'
51
48
BINDER_PATH_TMPL = BINDER_TMPL + '?filepath={path}'
52
49
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
+
53
63
@property
54
64
def github_client (self ):
55
65
"""Create an upgraded github API client from the HTTP client"""
@@ -107,7 +117,7 @@ def get(self, user):
107
117
name = repo ['name' ],
108
118
))
109
119
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 )
111
121
html = self .render_template ("userview.html" ,
112
122
entries = entries , provider_url = provider_url ,
113
123
next_url = next_url , prev_url = prev_url ,
@@ -169,14 +179,14 @@ def get(self, user, repo, ref, path, **namespace):
169
179
# Account for possibility that GitHub API redirects us to get more accurate breadcrumbs
170
180
# See: https://github.com/jupyter/nbviewer/issues/324
171
181
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' )
173
183
174
184
base_url = u"/github/{user}/{repo}/tree/{ref}" .format (
175
185
user = user , repo = repo , ref = ref ,
176
186
)
177
187
178
188
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
180
190
)
181
191
182
192
breadcrumbs = [{
@@ -266,7 +276,7 @@ def get(self, user, repo, ref, path):
266
276
user = user , repo = repo , ref = ref , path = quote (path )
267
277
)
268
278
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
270
280
)
271
281
with self .catch_client_error ():
272
282
tree_entry = yield self .github_client .get_tree_entry (
0 commit comments