Skip to content

Commit ee16c25

Browse files
authored
Merge pull request #163 from manics/autocreate-pages-branch
Create new empty gh-pages branch if missing
2 parents 83ca3b4 + 581ae66 commit ee16c25

File tree

3 files changed

+150
-5
lines changed

3 files changed

+150
-5
lines changed

chartpress.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,7 @@ def publish_pages(
969969
extra_message="",
970970
force=False,
971971
chart_path=None,
972+
push=True,
972973
):
973974
"""
974975
Update a Helm chart registry hosted in the gh-pages branch of a GitHub git
@@ -1020,7 +1021,13 @@ def publish_pages(
10201021
)
10211022
else:
10221023
_check_call(["git", "fetch"], cwd=checkout_dir, echo=True)
1023-
_check_call(["git", "checkout", "gh-pages"], cwd=checkout_dir, echo=True)
1024+
try:
1025+
_check_call(["git", "checkout", "gh-pages"], cwd=checkout_dir, echo=True)
1026+
except subprocess.CalledProcessError as e:
1027+
_log("Failed to checkout gh-pages branch, creating new local empty branch.")
1028+
_check_call(
1029+
["git", "switch", "--orphan", "gh-pages"], cwd=checkout_dir, echo=True
1030+
)
10241031

10251032
# check if a chart with the same name and version has already been published. If
10261033
# there is, the behaviour depends on `--force-publish-chart`
@@ -1085,7 +1092,10 @@ def publish_pages(
10851092

10861093
_check_call(["git", "add", "."], cwd=checkout_dir)
10871094
_check_call(["git", "commit", "-m", message], cwd=checkout_dir)
1088-
_check_call(["git", "push", "origin", "gh-pages"], cwd=checkout_dir)
1095+
if push:
1096+
_check_call(["git", "push", "origin", "gh-pages"], cwd=checkout_dir)
1097+
else:
1098+
_log(f"Push disabled. Run `cd {checkout_dir} && git push origin gh-pages`")
10891099

10901100

10911101
def _increment_semver(version, field):

tests/conftest.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ def git_repo(monkeypatch):
6060
yield r
6161

6262

63+
@pytest.fixture
64+
def git_repo_mainonly(monkeypatch, git_repo):
65+
"""
66+
This fixture provides a temporary git repo with just a main branch
67+
"""
68+
r = git_repo
69+
r.delete_head("gh-pages", force=True)
70+
assert [b.name for b in r.branches] == ["main"]
71+
yield r
72+
73+
6374
@pytest.fixture
6475
def git_repo_bare_minimum(monkeypatch, git_repo):
6576
"""
@@ -174,12 +185,16 @@ def git_repo_base_version(monkeypatch, git_repo):
174185
yield r
175186

176187

177-
class MockCheckCall:
178-
def __init__(self):
188+
class CheckCallWrapper:
189+
def __init__(self, mock):
179190
self.commands = []
191+
self._mock = mock
192+
self._check_call = chartpress._check_call
180193

181194
def __call__(self, cmd, **kwargs):
182195
self.commands.append((cmd, kwargs))
196+
if not self._mock:
197+
return self._check_call(cmd, **kwargs)
183198

184199

185200
def cache_clear():
@@ -204,7 +219,26 @@ def mock_check_call(monkeypatch, _cache_clear):
204219
Replace chartpress._check_call with a no-op version that records all commands
205220
Also disable lru_cache to prevent cached information being kept across test calls
206221
"""
207-
mock_call = MockCheckCall()
222+
mock_call = CheckCallWrapper(mock=True)
223+
monkeypatch.setattr(chartpress, "_check_call", mock_call)
224+
225+
# Need to clear @lru_cache since we test multiple temporary repositories
226+
chartpress._get_latest_commit_tagged_or_modifying_paths.cache_clear()
227+
# Other @lru_cache functions, in case it's needed in future:
228+
# chartpress._get_docker_client.cache_clear()
229+
# chartpress._image_needs_pushing.cache_clear()
230+
# chartpress._image_needs_building.cache_clear()
231+
232+
yield mock_call
233+
234+
235+
@pytest.fixture(scope="function")
236+
def record_check_call(monkeypatch):
237+
"""
238+
Replace chartpress._check_call with a version that records all commands
239+
Also disable lcu_cache to prevent cached information being kept across test calls
240+
"""
241+
mock_call = CheckCallWrapper(mock=False)
208242
monkeypatch.setattr(chartpress, "_check_call", mock_call)
209243

210244
yield mock_call

tests/test_commands.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Unit tests for some chartpress methods
22
from os import mkdir
33

4+
import git
45
import pytest
56
from ruamel.yaml import YAML
67

@@ -405,3 +406,103 @@ def test_publish_pages(git_repo, mock_check_call):
405406
["git", "push", "origin", "gh-pages"],
406407
{"cwd": "test-chart-1.2.3"},
407408
)
409+
410+
411+
def test_publish_pages_firsttime(git_repo_mainonly, record_check_call):
412+
chartpress.publish_pages(
413+
"testchart",
414+
"1.2.3",
415+
"https://github.com/jupyterhub/chartpress",
416+
"https://example.org/chartpress",
417+
"<foo>",
418+
push=False,
419+
)
420+
421+
assert len(record_check_call.commands) == 7
422+
423+
assert record_check_call.commands[0] == (
424+
[
425+
"git",
426+
"clone",
427+
"--no-checkout",
428+
"https://github.com/jupyterhub/chartpress",
429+
"testchart-1.2.3",
430+
],
431+
{"echo": True},
432+
)
433+
assert record_check_call.commands[1] == (
434+
["git", "checkout", "gh-pages"],
435+
{"cwd": "testchart-1.2.3", "echo": True},
436+
)
437+
438+
assert record_check_call.commands[2] == (
439+
["git", "switch", "--orphan", "gh-pages"],
440+
{"cwd": "testchart-1.2.3", "echo": True},
441+
)
442+
443+
helm_package_cmd = record_check_call.commands[3][0]
444+
assert record_check_call.commands[3][1] == {}
445+
# Skip the temporary directory
446+
assert (helm_package_cmd[:5] + helm_package_cmd[6:]) == [
447+
"helm",
448+
"package",
449+
"testchart",
450+
"--dependency-update",
451+
"--destination",
452+
]
453+
454+
helm_index_cmd = record_check_call.commands[4][0]
455+
assert record_check_call.commands[4][1] == {}
456+
# Skip the temporary directory
457+
assert (helm_index_cmd[:3] + helm_index_cmd[4:]) == [
458+
"helm",
459+
"repo",
460+
"index",
461+
"--url",
462+
"https://example.org/chartpress",
463+
"--merge",
464+
"testchart-1.2.3/index.yaml",
465+
]
466+
467+
assert record_check_call.commands[5] == (
468+
["git", "add", "."],
469+
{"cwd": "testchart-1.2.3"},
470+
)
471+
assert record_check_call.commands[6] == (
472+
[
473+
"git",
474+
"commit",
475+
"-m",
476+
"[testchart] Automatic update for commit 1.2.3\n\n<foo>",
477+
],
478+
{"cwd": "testchart-1.2.3"},
479+
)
480+
481+
with open("testchart-1.2.3/index.yaml") as f:
482+
chart = YAML(typ="safe").load(f)
483+
484+
chart["entries"]["testchart"][0]["created"] = "DATETIME"
485+
chart["entries"]["testchart"][0]["digest"] = "DIGEST"
486+
chart["generated"] = "DATETIME"
487+
488+
assert chart == {
489+
"apiVersion": "v1",
490+
"entries": {
491+
"testchart": [
492+
{
493+
"apiVersion": "v1",
494+
"created": "DATETIME",
495+
"digest": "DIGEST",
496+
"name": "testchart",
497+
"urls": [
498+
"https://example.org/chartpress/testchart-0.0.1-test.reset.version.tgz"
499+
],
500+
"version": "0.0.1-test.reset.version",
501+
}
502+
]
503+
},
504+
"generated": "DATETIME",
505+
}
506+
507+
r = git.Repo("./testchart-1.2.3")
508+
assert sorted(b.name for b in r.branches) == ["gh-pages", "main"]

0 commit comments

Comments
 (0)