def get_contents(self, user, repo, path, callback=None, ref=None, **kwargs): """Make contents API request - either file contents or directory listing""" path = quote(u'repos/{user}/{repo}/contents/{path}'.format( **locals() )) if ref is not None: params = kwargs.setdefault('params', {}) params['ref'] = ref return self.api_request(path, callback, **kwargs)
def get_contents(self, user, repo, path, callback=None, ref=None, **kwargs): """Make contents API request - either file contents or directory listing""" path = quote(u'repos/{user}/{repo}/contents/{path}'.format(**locals())) if ref is not None: params = kwargs.setdefault('params', {}) params['ref'] = ref return self.api_request(path, callback, **kwargs)
def test_quote(): tests = [ ('hi', u'hi'), (u'hi', u'hi'), (b'hi', u'hi'), (' /#', u'%20/%23'), (b' /#', u'%20/%23'), (u' /#', u'%20/%23'), (u'ü /é#/', u'%C3%BC%20/%C3%A9%23/'), (u'ü /é#/'.encode('utf8'), u'%C3%BC%20/%C3%A9%23/'), ('ü /é#/', u'%C3%BC%20/%C3%A9%23/'), ] for s, expected in tests: quoted = utils.quote(s) assert quoted == expected assert type(quoted) == type(expected)
def test_quote(): tests = [ ("hi", "hi"), ("hi", "hi"), ("hi", "hi"), (" /#", "%20/%23"), (" /#", "%20/%23"), (" /#", "%20/%23"), ("ü /é#/", "%C3%BC%20/%C3%A9%23/"), ("ü /é#/", "%C3%BC%20/%C3%A9%23/"), ("ü /é#/", "%C3%BC%20/%C3%A9%23/"), ] for s, expected in tests: quoted = utils.quote(s) assert quoted == expected assert type(quoted) == type(expected)
def test_quote(): tests = [ ("hi", u"hi"), (u"hi", u"hi"), ("hi", u"hi"), (" /#", u"%20/%23"), (" /#", u"%20/%23"), (u" /#", u"%20/%23"), (u"ü /é#/", u"%C3%BC%20/%C3%A9%23/"), ("ü /é#/", u"%C3%BC%20/%C3%A9%23/"), ("ü /é#/", u"%C3%BC%20/%C3%A9%23/"), ] for s, expected in tests: quoted = utils.quote(s) assert quoted == expected assert type(quoted) == type(expected)
def get(self, user, repo, ref, path): raw_url = u"https://raw.gitlabusercontent.com/{user}/{repo}/{ref}/{path}".format( user=user, repo=repo, ref=ref, path=quote(path) ) blob_url = u"https://gitlab.com/{user}/{repo}/blob/{ref}/{path}".format( user=user, repo=repo, ref=ref, path=quote(path), ) with self.catch_client_error(): tree_entry = yield self.gitlab_client.get_tree_entry( user, repo, path=path, ref=ref ) if tree_entry['type'] == 'tree': tree_url = "/gitlab/{user}/{repo}/tree/{ref}/{path}/".format( user=user, repo=repo, ref=ref, path=quote(path), ) app_log.info("%s is a directory, redirecting to %s", self.request.path, tree_url) self.redirect(tree_url) return # fetch file data from the blobs API with self.catch_client_error(): response = yield self.gitlab_client.fetch(tree_entry['url']) data = json.loads(response_text(response)) contents = data['content'] if data['encoding'] == 'base64': # filedata will be bytes filedata = base64_decode(contents) else: # filedata will be unicode filedata = contents if path.endswith('.ipynb'): dir_path = path.rsplit('/', 1)[0] base_url = "/gitlab/{user}/{repo}/tree/{ref}".format( user=user, repo=repo, ref=ref, ) breadcrumbs = [{ 'url' : base_url, 'name' : repo, }] breadcrumbs.extend(self.breadcrumbs(dir_path, base_url)) try: # filedata may be bytes, but we need text if isinstance(filedata, bytes): nbjson = filedata.decode('utf-8') else: nbjson = filedata except Exception as e: app_log.error("Failed to decode notebook: %s", raw_url, exc_info=True) raise web.HTTPError(400) yield self.finish_notebook(nbjson, raw_url, provider_url=blob_url, breadcrumbs=breadcrumbs, msg="file from GitLab: %s" % raw_url, public=True, format=self.format, request=self.request, **PROVIDER_CTX ) else: mime, enc = mimetypes.guess_type(path) self.set_header("Content-Type", mime or 'text/plain') self.cache_and_finish(filedata)
def get(self, user, repo, ref, path): if not self.request.uri.endswith('/'): self.redirect(self.request.uri + '/') return path = path.rstrip('/') with self.catch_client_error(): response = yield self.gitlab_client.get_contents(user, repo, path, ref=ref) contents = json.loads(response_text(response)) branches, tags = yield self.refs(user, repo) for nav_ref in branches + tags: nav_ref["url"] = (u"/gitlab/{user}/{repo}/tree/{ref}/{path}" .format( ref=nav_ref["name"], user=user, repo=repo, path=path )) if not isinstance(contents, list): app_log.info( "{format}/{user}/{repo}/{ref}/{path} not tree, redirecting to blob", extra=dict(format=self.format_prefix, user=user, repo=repo, ref=ref, path=path) ) self.redirect( u"{format}/gitlab/{user}/{repo}/blob/{ref}/{path}".format( format=self.format_prefix, user=user, repo=repo, ref=ref, path=path, ) ) return base_url = u"/gitlab/{user}/{repo}/tree/{ref}".format( user=user, repo=repo, ref=ref, ) provider_url = u"https://gitlab.com/{user}/{repo}/tree/{ref}/{path}".format( user=user, repo=repo, ref=ref, path=path, ) breadcrumbs = [{ 'url' : base_url, 'name' : repo, }] breadcrumbs.extend(self.breadcrumbs(path, base_url)) entries = [] dirs = [] ipynbs = [] others = [] for file in contents: e = {} e['name'] = file['name'] if file['type'] == 'dir': e['url'] = u'/gitlab/{user}/{repo}/tree/{ref}/{path}'.format( user=user, repo=repo, ref=ref, path=file['path'] ) e['url'] = quote(e['url']) e['class'] = 'fa-folder-open' dirs.append(e) elif file['name'].endswith('.ipynb'): e['url'] = u'/gitlab/{user}/{repo}/blob/{ref}/{path}'.format( user=user, repo=repo, ref=ref, path=file['path'] ) e['url'] = quote(e['url']) e['class'] = 'fa-book' ipynbs.append(e) elif file['html_url']: e['url'] = file['html_url'] e['class'] = 'fa-share' others.append(e) else: # submodules don't have html_url e['url'] = '' e['class'] = 'fa-folder-close' others.append(e) entries.extend(dirs) entries.extend(ipynbs) entries.extend(others) html = self.render_template("treelist.html", entries=entries, breadcrumbs=breadcrumbs, provider_url=provider_url, user=user, repo=repo, ref=ref, path=path, branches=branches, tags=tags, tree_type="gitlab", **PROVIDER_CTX ) yield self.cache_and_finish(html)