|
46 | 46 | BINDER_PATH_TMPL = BINDER_TMPL+'?filepath={path}'
|
47 | 47 |
|
48 | 48 |
|
49 |
| -def _github_url(): |
50 |
| - return os.environ.get('GITHUB_URL') if os.environ.get('GITHUB_URL', '') else "https://github.com/" |
51 |
| - |
52 | 49 | class GithubClientMixin(object):
|
| 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,
|
@@ -161,14 +171,14 @@ def get(self, user, repo, ref, path):
|
161 | 171 | # Account for possibility that GitHub API redirects us to get more accurate breadcrumbs
|
162 | 172 | # See: https://github.com/jupyter/nbviewer/issues/324
|
163 | 173 | example_file_url = contents[0]['html_url']
|
164 |
| - user, repo = re.match(r"^https://github\.com/(?P<user>[^\/]+)/(?P<repo>[^\/]+)/.*", example_file_url).group('user', 'repo') |
| 174 | + user, repo = re.match(r"^" + self.github_url + "(?P<user>[^\/]+)/(?P<repo>[^\/]+)/.*", example_file_url).group('user', 'repo') |
165 | 175 |
|
166 | 176 | base_url = u"/github/{user}/{repo}/tree/{ref}".format(
|
167 | 177 | user=user, repo=repo, ref=ref,
|
168 | 178 | )
|
169 | 179 |
|
170 | 180 | provider_url = u"{github_url}{user}/{repo}/tree/{ref}/{path}".format(
|
171 |
| - user=user, repo=repo, ref=ref, path=path, github_url = _github_url() |
| 181 | + user=user, repo=repo, ref=ref, path=path, github_url=self.github_url |
172 | 182 | )
|
173 | 183 |
|
174 | 184 | breadcrumbs = [{
|
@@ -261,7 +271,7 @@ def get(self, user, repo, ref, path):
|
261 | 271 | user=user, repo=repo, ref=ref, path=quote(path)
|
262 | 272 | )
|
263 | 273 | blob_url = u"{github_url}{user}/{repo}/blob/{ref}/{path}".format(
|
264 |
| - user=user, repo=repo, ref=ref, path=quote(path), github_url=_github_url() |
| 274 | + user=user, repo=repo, ref=ref, path=quote(path), github_url=self.github_url |
265 | 275 | )
|
266 | 276 | with self.catch_client_error():
|
267 | 277 | tree_entry = yield self.github_client.get_tree_entry(
|
|
0 commit comments