Skip to content

Commit 42863d0

Browse files
committed
landoscript: refactor extension_manifest_version_bump into smaller functions (bug 2025695)
1 parent 7fe8788 commit 42863d0

2 files changed

Lines changed: 88 additions & 28 deletions

File tree

landoscript/src/landoscript/actions/extension_manifest_version_bump.py

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,54 +18,64 @@
1818
ALLOWED_MANIFEST_FILES = ("browser/extensions/newtab/manifest.json",)
1919

2020

21-
async def run(
22-
github_client: GithubClient,
23-
public_artifact_dir: str,
24-
branch: str,
25-
info: dict,
26-
) -> list[LandoAction]:
27-
file = info["file"]
21+
def _validate_file(file: str) -> None:
2822
if file not in ALLOWED_MANIFEST_FILES:
2923
raise TaskVerificationError(f"{file} is not in extension manifest version bump allowlist")
3024

25+
26+
async def _fetch_file(github_client: GithubClient, file: str, branch: str) -> str:
3127
try:
3228
log.info(f"fetching {file} from github")
3329
orig_files = await github_client.get_files([file], branch)
3430
except TransportError as e:
3531
raise LandoscriptError(f"couldn't retrieve {file} from github") from e
36-
3732
orig = orig_files.get(file)
3833
if not orig:
3934
raise LandoscriptError(f"{file} does not exist!")
35+
return orig
4036

41-
log.info(f"{file} contents:")
42-
log_file_contents(orig)
4337

38+
def _bump_manifest_version(orig: str, file: str) -> tuple[str, str] | None:
4439
manifest = json.loads(orig)
45-
version_str = manifest["version"]
46-
cur = BaseVersion.parse(version_str)
47-
next_ = cur.bump("patch_number")
48-
49-
log.info(f"{file}: bumping {cur} -> {next_}")
50-
40+
cur = BaseVersion.parse(manifest["version"])
41+
next_ = cur.bump("minor_number")
5142
modified = re.sub(
5243
r'("version":\s*")' + re.escape(str(cur)) + r'"',
5344
rf'\g<1>{next_}"',
5445
orig,
5546
)
5647
if orig == modified:
57-
raise LandoscriptError(f"{file}: version replacement produced no change, this should be impossible")
48+
log.warning(f"{file}: version replacement produced no change, skipping")
49+
return None
50+
return modified, str(next_)
5851

59-
log.info(f"{file}: new contents are:")
60-
log_file_contents(modified)
6152

53+
def _build_commit(orig: str, modified: str, file: str, next_version: str, public_artifact_dir: str) -> LandoAction:
6254
diff = diff_contents(orig, modified, file)
63-
6455
with open(os.path.join(public_artifact_dir, "extension-manifest-version-bump.diff"), "w+") as f:
6556
f.write(diff)
66-
6757
log.info("adding version bump commit! diff contents are:")
6858
log_file_contents(diff)
59+
commitmsg = f"No Bug - Bump newtab manifest.json version to {next_version} a=release CLOSED TREE DONTBUILD"
60+
return create_commit_action(commitmsg, diff)
6961

70-
commitmsg = f"No Bug - Bump newtab manifest.json version to {next_} a=release CLOSED TREE DONTBUILD"
71-
return [create_commit_action(commitmsg, diff)]
62+
63+
async def run(
64+
github_client: GithubClient,
65+
public_artifact_dir: str,
66+
branch: str,
67+
info: dict,
68+
) -> list[LandoAction]:
69+
file = info["file"]
70+
_validate_file(file)
71+
orig = await _fetch_file(github_client, file, branch)
72+
log.info(f"{file} contents:")
73+
log_file_contents(orig)
74+
result = _bump_manifest_version(orig, file)
75+
if result is None:
76+
return []
77+
modified, next_version = result
78+
log.info(f"{file}: bumping to {next_version}")
79+
log.info(f"{file}: new contents are:")
80+
log_file_contents(modified)
81+
return [_build_commit(orig, modified, file, next_version, public_artifact_dir)]

landoscript/tests/test_extension_manifest_version_bump.py

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
from pytest_scriptworker_client import get_files_payload
55
from scriptworker.client import TaskVerificationError
66

7-
from landoscript.actions.extension_manifest_version_bump import ALLOWED_MANIFEST_FILES
7+
from landoscript.actions.extension_manifest_version_bump import (
8+
ALLOWED_MANIFEST_FILES,
9+
_build_commit,
10+
_bump_manifest_version,
11+
_validate_file,
12+
)
813
from landoscript.errors import LandoscriptError
914
from landoscript.script import async_main
1015

@@ -30,6 +35,55 @@ def _manifest(version):
3035
)
3136

3237

38+
# --- Unit tests for helper functions (no mocks needed) ---
39+
40+
41+
@pytest.mark.parametrize(
42+
"initial_version,expected_version",
43+
[
44+
pytest.param("151.0.0", "151.1.0", id="initial_after_merge_day"),
45+
pytest.param("151.1.0", "151.2.0", id="normal_increment"),
46+
],
47+
)
48+
def test_bump_manifest_version(initial_version, expected_version):
49+
modified, next_version = _bump_manifest_version(_manifest(initial_version), MANIFEST_FILE)
50+
assert next_version == expected_version
51+
assert f'"version": "{expected_version}"' in modified
52+
assert f'"version": "{initial_version}"' not in modified
53+
54+
55+
def test_validate_file_allowed():
56+
_validate_file(MANIFEST_FILE)
57+
58+
59+
def test_validate_file_not_in_allowlist():
60+
with pytest.raises(TaskVerificationError, match="not in extension manifest version bump allowlist"):
61+
_validate_file("browser/extensions/webcompat/manifest.json")
62+
63+
64+
def test_bump_manifest_version_no_change():
65+
orig = _manifest("151.0.0")
66+
# Simulate a regex match failure (e.g. version format in file doesn't match
67+
# what BaseVersion.parse produces) by patching re.sub to return orig unchanged.
68+
with pytest.MonkeyPatch().context() as mp:
69+
mp.setattr("landoscript.actions.extension_manifest_version_bump.re.sub", lambda *a, **kw: orig)
70+
result = _bump_manifest_version(orig, MANIFEST_FILE)
71+
assert result is None
72+
73+
74+
def test_build_commit(tmp_path):
75+
orig = _manifest("151.0.0")
76+
modified = orig.replace('"151.0.0"', '"151.0.1"')
77+
action = _build_commit(orig, modified, MANIFEST_FILE, "151.0.1", str(tmp_path))
78+
assert action["action"] == "create-commit"
79+
assert "151.0.1" in action["commitmsg"]
80+
assert "a=release" in action["commitmsg"]
81+
assert (tmp_path / "extension-manifest-version-bump.diff").exists()
82+
83+
84+
# --- Integration tests via async_main ---
85+
86+
3387
def assert_success(req, initial_version, expected_version):
3488
assert "json" in req.kwargs
3589
create_commit_actions = [a for a in req.kwargs["json"]["actions"] if a["action"] == "create-commit"]
@@ -126,7 +180,3 @@ async def test_file_not_found(aioresponses, github_installation_responses, conte
126180
err=LandoscriptError,
127181
errmsg="does not exist",
128182
)
129-
130-
131-
def test_allowed_manifest_files():
132-
assert MANIFEST_FILE in ALLOWED_MANIFEST_FILES

0 commit comments

Comments
 (0)