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

Create new empty gh-pages branch if missing #163

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 12 additions & 2 deletions chartpress.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ def publish_pages(
chart_repo_url,
extra_message="",
force=False,
push=True,
):
"""
Update a Helm chart registry hosted in the gh-pages branch of a GitHub git
Expand Down Expand Up @@ -821,7 +822,13 @@ def publish_pages(
)
else:
_check_call(["git", "fetch"], cwd=checkout_dir, echo=True)
_check_call(["git", "checkout", "gh-pages"], cwd=checkout_dir, echo=True)
try:
_check_call(["git", "checkout", "gh-pages"], cwd=checkout_dir, echo=True)
except subprocess.CalledProcessError as e:
_log("Failed to checkout gh-pages branch, creating new local empty branch.")
_check_call(
["git", "switch", "--orphan", "gh-pages"], cwd=checkout_dir, echo=True
)

# check if a chart with the same name and version has already been published. If
# there is, the behaviour depends on `--force-publish-chart`
Expand Down Expand Up @@ -886,7 +893,10 @@ def publish_pages(

_check_call(["git", "add", "."], cwd=checkout_dir)
_check_call(["git", "commit", "-m", message], cwd=checkout_dir)
_check_call(["git", "push", "origin", "gh-pages"], cwd=checkout_dir)
if push:
_check_call(["git", "push", "origin", "gh-pages"], cwd=checkout_dir)
else:
_log(f"Push disabled. Run `cd {checkout_dir} && git push origin gh-pages`")


class ActionStoreDeprecated(argparse.Action):
Expand Down
40 changes: 37 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ def git_repo(monkeypatch):
yield r


@pytest.fixture
def git_repo_mainonly(monkeypatch, git_repo):
"""
This fixture provides a temporary git repo with just a main branch
"""
r = git_repo
r.delete_head("gh-pages", force=True)
assert [b.name for b in r.branches] == ["main"]
yield r


@pytest.fixture
def git_repo_bare_minimum(monkeypatch, git_repo):
"""
Expand Down Expand Up @@ -146,12 +157,16 @@ def git_repo_alternative(monkeypatch, git_repo):
yield r


class MockCheckCall:
def __init__(self):
class CheckCallWrapper:
def __init__(self, mock):
self.commands = []
self._mock = mock
self._check_call = chartpress._check_call

def __call__(self, cmd, **kwargs):
self.commands.append((cmd, kwargs))
if not self._mock:
return self._check_call(cmd, **kwargs)


@pytest.fixture(scope="function")
Expand All @@ -160,7 +175,26 @@ def mock_check_call(monkeypatch):
Replace chartpress._check_call with a no-op version that records all commands
Also disable lcu_cache to prevent cached information being kept across test calls
"""
mock_call = MockCheckCall()
mock_call = CheckCallWrapper(mock=True)
monkeypatch.setattr(chartpress, "_check_call", mock_call)

# Need to clear @lru_cache since we test multiple temporary repositories
chartpress._get_latest_commit_tagged_or_modifying_paths.cache_clear()
# Other @lru_cache functions, in case it's needed in future:
# chartpress._get_docker_client.cache_clear()
# chartpress._image_needs_pushing.cache_clear()
# chartpress._image_needs_building.cache_clear()

yield mock_call


@pytest.fixture(scope="function")
def record_check_call(monkeypatch):
"""
Replace chartpress._check_call with a version that records all commands
Also disable lcu_cache to prevent cached information being kept across test calls
"""
mock_call = CheckCallWrapper(mock=False)
monkeypatch.setattr(chartpress, "_check_call", mock_call)

# Need to clear @lru_cache since we test multiple temporary repositories
Expand Down
101 changes: 101 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Unit tests for some chartpress methods
from os import mkdir

import git
import pytest
from ruamel.yaml import YAML

Expand Down Expand Up @@ -404,3 +405,103 @@ def test_publish_pages(git_repo, mock_check_call):
["git", "push", "origin", "gh-pages"],
{"cwd": "test-chart-1.2.3"},
)


def test_publish_pages_firsttime(git_repo_mainonly, record_check_call):
chartpress.publish_pages(
"testchart",
"1.2.3",
"https://github.com/jupyterhub/chartpress",
"https://example.org/chartpress",
"<foo>",
push=False,
)

assert len(record_check_call.commands) == 7

assert record_check_call.commands[0] == (
[
"git",
"clone",
"--no-checkout",
"https://github.com/jupyterhub/chartpress",
"testchart-1.2.3",
],
{"echo": True},
)
assert record_check_call.commands[1] == (
["git", "checkout", "gh-pages"],
{"cwd": "testchart-1.2.3", "echo": True},
)

assert record_check_call.commands[2] == (
["git", "switch", "--orphan", "gh-pages"],
{"cwd": "testchart-1.2.3", "echo": True},
)

helm_package_cmd = record_check_call.commands[3][0]
assert record_check_call.commands[3][1] == {}
# Skip the temporary directory
assert (helm_package_cmd[:5] + helm_package_cmd[6:]) == [
"helm",
"package",
"testchart",
"--dependency-update",
"--destination",
]

helm_index_cmd = record_check_call.commands[4][0]
assert record_check_call.commands[4][1] == {}
# Skip the temporary directory
assert (helm_index_cmd[:3] + helm_index_cmd[4:]) == [
"helm",
"repo",
"index",
"--url",
"https://example.org/chartpress",
"--merge",
"testchart-1.2.3/index.yaml",
]

assert record_check_call.commands[5] == (
["git", "add", "."],
{"cwd": "testchart-1.2.3"},
)
assert record_check_call.commands[6] == (
[
"git",
"commit",
"-m",
"[testchart] Automatic update for commit 1.2.3\n\n<foo>",
],
{"cwd": "testchart-1.2.3"},
)

with open("testchart-1.2.3/index.yaml") as f:
chart = YAML(typ="safe").load(f)

chart["entries"]["testchart"][0]["created"] = "DATETIME"
chart["entries"]["testchart"][0]["digest"] = "DIGEST"
chart["generated"] = "DATETIME"

assert chart == {
"apiVersion": "v1",
"entries": {
"testchart": [
{
"apiVersion": "v1",
"created": "DATETIME",
"digest": "DIGEST",
"name": "testchart",
"urls": [
"https://example.org/chartpress/testchart-0.0.1-test.reset.version.tgz"
],
"version": "0.0.1-test.reset.version",
}
]
},
"generated": "DATETIME",
}

r = git.Repo("./testchart-1.2.3")
assert sorted(b.name for b in r.branches) == ["gh-pages", "main"]