Skip to content

Commit

Permalink
Refactor github path construction and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
athornton committed Oct 22, 2024
1 parent dc6ea32 commit 160b528
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/ghostwriter/hooks/github_notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,38 @@ async def github_notebook(params: Parameters) -> Parameters:
# Honestly it's easier to just unconditionally rewrite the target
# than to figure out whether it needs rewriting.

return _get_new_params(serial, params)


def _get_new_params(serial: str, params: Parameters) -> Parameters:
# Start with the common fragment
target = (
f"{params.base_url}nb/user/{params.user}/lab/tree/notebooks"
"/on-demand/github.com/"
)

# Remove branch information, if any (the checkout will have handled
# it in the code we ran to get the serial).
path = params.path.split("@")[0]
if path.startswith("notebooks/github.com/"):

# Canonicalize path.
prefix = "notebooks/github.com/"
if path.startswith(prefix):
# Needs stripping
path = path[(len("notebooks/github.com/")) :]
path = path[(len(prefix)) :]
if path.endswith(".ipynb"):
# Also needs stripping
path = path[: -(len(".ipynb"))]

# Add discriminator if needed
unique_id: str | None = None
if serial and serial != "0":
# Glue in serial if it is nonzero
unique_id = serial
path += f"-{unique_id}"
path += ".ipynb" # And add the extension
target += path

new_param = Parameters(
user=params.user,
base_url=params.base_url,
Expand Down
69 changes: 69 additions & 0 deletions tests/hooks/serial_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""Test the filename construction from serial bit of the github hook."""

import json
from pathlib import Path

import pytest
import pytest_asyncio
from httpx import AsyncClient
from rubin.nublado.client import NubladoClient
from rubin.nublado.client.models import User

from ghostwriter.config import Configuration
from ghostwriter.hooks.github_notebook import _get_new_params
from ghostwriter.models.substitution import Parameters

COMMON_PREFIX = (
"https://data.example.org/nb/user/rachel/lab/tree/notebooks"
"/on-demand/github.com/lsst-sqre/system-test/Firefly"
)


@pytest_asyncio.fixture
def std_params(config: Configuration, client: AsyncClient) -> Parameters:
user_objs = json.loads(
(Path(__file__).parent.parent / "support" / "users.json").read_text()
)
token = next(iter(user_objs.keys()))
username = user_objs[token]["username"]
user = User(username=username, token=token)
nc = NubladoClient(user=user, base_url=str(config.environment_url))
return Parameters(
user=user.username,
base_url=str(config.environment_url),
token=token,
client=nc,
path="notebooks/github.com/lsst-sqre/system-test/Firefly",
target="inconsequential",
unique_id=None,
)


@pytest.mark.asyncio
async def test_basic_rewrite(std_params: Parameters) -> None:
new_p = _get_new_params("0", std_params)
expected = f"{COMMON_PREFIX}.ipynb"
assert new_p.target == expected


@pytest.mark.asyncio
async def test_serial_rewrite(std_params: Parameters) -> None:
new_p = _get_new_params("3", std_params)
expected = f"{COMMON_PREFIX}-3.ipynb"
assert new_p.target == expected


@pytest.mark.asyncio
async def test_strip_branch(std_params: Parameters) -> None:
inp = Parameters(
user=std_params.user,
base_url=std_params.base_url,
token=std_params.token,
client=std_params.client,
path=f"{std_params.path}@prod",
target=std_params.target,
unique_id=std_params.unique_id,
)
new_p = _get_new_params("0", inp)
expected = f"{COMMON_PREFIX}.ipynb"
assert new_p.target == expected

0 comments on commit 160b528

Please sign in to comment.