Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose branch and depth options #35

Merged
merged 2 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ c.GalleryManager.exhibits = [
"token": "access-token-for-example-starting-with-github_pat_",
"title": "My private tutorial",
"description": "A tutorial which is not public.",
},
{
"git": "https://github.com/my_org/public-tutorial.git",
"title": "My tutorial",
"branch": "v2024",
"depth": 1
}
]
```
Expand Down
14 changes: 14 additions & 0 deletions binder/jupyter_gallery_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@
"title": "Empty icon",
"description": "Empty icon should show social card for GitHub repos",
"icon": ""
},
{
"git": "https://github.com/jupyterlab/jupyterlab.git",
"homepage": "https://github.com/jupyterlab/jupyterlab",
"title": "JupyterLab branch 3.6.x",
"branch": "3.6.x",
"description": "Cloning a specific branch with 'branch' argument"
},
{
"git": "https://github.com/nebari-dev/nebari.git",
"homepage": "https://github.com/nebari-dev/nebari",
"title": "Nebari with shallow clone",
"depth": 1,
"description": "Cloning with 'depth = 1' for faster checkout"
}
]
}
Expand Down
7 changes: 3 additions & 4 deletions jupyterlab_gallery/gitpuller.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def clone_task():
self.git_url,
self.repo_dir,
branch=self.branch_name,
depth=self.depth,
progress=progress,
)
except Exception as e:
Expand Down Expand Up @@ -149,6 +150,8 @@ async def _pull(
exhibit_id: int,
token: Optional[str],
account: Optional[str],
branch: Optional[str],
depth: Optional[int],
):
q = self.settings["pull_status_queues"][exhibit_id]
try:
Expand All @@ -164,10 +167,6 @@ async def _pull(
return

try:
branch = self.get_argument("branch", None)
depth = self.get_argument("depth", None)
if depth:
depth = int(depth)
# The default working directory is the directory from which Jupyter
# server is launched, which is not the same as the root notebook
# directory assuming either --notebook-dir= is used from the
Expand Down
13 changes: 10 additions & 3 deletions jupyterlab_gallery/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,21 @@ async def post(self):
self.set_status(406)
self.finish(json.dumps({"message": f"exhibit_id {exhibit_id} not found"}))
return

branch = exhibit.get("branch")
depth = exhibit.get("depth")

if depth:
depth = int(depth)

return await super()._pull(
repo=exhibit["git"],
targetpath=str(self.gallery_manager.get_local_path(exhibit)),
exhibit_id=exhibit_id,
account=exhibit.get("account"),
token=exhibit.get("token"),
# branch
# depth
targetpath=str(self.gallery_manager.get_local_path(exhibit)),
branch=branch,
depth=depth,
)

@tornado.web.authenticated
Expand Down
10 changes: 8 additions & 2 deletions jupyterlab_gallery/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from threading import Thread

from traitlets.config.configurable import LoggingConfigurable
from traitlets import Dict, List, Unicode, Bool
from traitlets import Dict, List, Unicode, Bool, Int

from .git_utils import (
extract_repository_owner,
Expand Down Expand Up @@ -49,6 +49,12 @@ def __init__(self, *args, **kwargs):
help="Path to an svg or png, or base64 encoded string",
allow_none=True,
),
"branch": Unicode(
default_value=None, help="Branch to use", allow_none=True
),
"depth": Int(
default_value=None, help="Depth of the clone", allow_none=True
),
# other ideas: `path_in_repository`, `documentation_url`
}
),
Expand All @@ -72,7 +78,7 @@ def __init__(self, *args, **kwargs):
hide_gallery_without_exhibits = Bool(
help="Hide Gallery if no exhibits are configured",
default_value=False,
config=True
config=True,
)

def get_local_path(self, exhibit) -> Path:
Expand Down
41 changes: 23 additions & 18 deletions jupyterlab_gallery/tests/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,30 @@ async def test_exhibits(jp_fetch):
assert isinstance(payload["exhibits"], list)


@pytest.mark.parametrize("exhibit", [
{
"git": "https://github.com/nebari-dev/nebari.git",
"homepage": "https://github.com/nebari-dev/nebari"
},
{
"git": "https://github.com/nebari-dev/nebari.git",
"homepage": "https://github.com/nebari-dev/nebari",
"icon": None
}
])
@pytest.mark.parametrize(
"exhibit",
[
{
"git": "https://github.com/nebari-dev/nebari.git",
"homepage": "https://github.com/nebari-dev/nebari",
},
{
"git": "https://github.com/nebari-dev/nebari.git",
"homepage": "https://github.com/nebari-dev/nebari",
"icon": None,
},
],
)
async def test_exhibit_generate_github_icon(jp_serverapp, jp_fetch, exhibit):
with mock.patch.object(GalleryManager, 'exhibits', [exhibit]):
with mock.patch.object(GalleryManager, "exhibits", [exhibit]):
response = await jp_fetch("jupyterlab-gallery", "exhibits")
assert response.code == 200
payload = json.loads(response.body)
assert len(payload["exhibits"]) == 1
assert payload["exhibits"][0]["icon"] == 'https://opengraph.githubassets.com/1/nebari-dev/nebari'
assert (
payload["exhibits"][0]["icon"]
== "https://opengraph.githubassets.com/1/nebari-dev/nebari"
)


async def test_gallery(jp_fetch):
Expand All @@ -41,16 +47,15 @@ async def test_gallery(jp_fetch):
assert payload["apiVersion"] == "1.0"


async def test_pull_token_can_be_used_instead_of_xsrf(jp_serverapp, jp_base_url, http_server_client):
async def test_pull_token_can_be_used_instead_of_xsrf(
jp_serverapp, jp_base_url, http_server_client
):
token = jp_serverapp.identity_provider.token
response = await http_server_client.fetch(
url_path_join(jp_base_url, "jupyterlab-gallery", "pull"),
body=b'{"exhibit_id": 100}',
method="POST",
headers={
"Authorization": f"token {token}",
"Cookie": ""
},
headers={"Authorization": f"token {token}", "Cookie": ""},
raise_error=False,
)
assert response.code == 406
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jupyterlab-gallery",
"version": "0.3.3",
"version": "0.4.0",
"description": "A JupyterLab gallery extension for presenting and downloading examples from remote repositories",
"keywords": [
"jupyter",
Expand Down
Loading