diff --git a/CHANGELOG.md b/CHANGELOG.md index 211af7d..2ff91da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,17 @@ All notable changes to this project are documented here, following ## [Unreleased] +### Fixed + +- **`convert_repo.py` floats a shared-file `sdlc ci` step to exactly + `run: .sdlc/bin/sdlc ci`** (a227). It used to keep `set -eu` above the + launcher in a `run: |` block, which constitution v1.46.0's exact-run check + fails (gatehouse needed a hand fix). A block an earlier run left that way is + rewritten too, a pinned step with more in its block is refused, and the + final verify reads the run as the validator does. The pinned form now + accepts a quoted launcher path, `"$RUNNER_TEMP/sdlc/bin/sdlc" ci`, which + made the fleet wave refuse gatehouse. + ## [0.9.0] - 2026-11-17 ### Added diff --git a/scripts/convert_repo.py b/scripts/convert_repo.py index 051915f..2460cef 100755 --- a/scripts/convert_repo.py +++ b/scripts/convert_repo.py @@ -70,20 +70,36 @@ SDLC_CI_LINE = re.compile(r"^\s*[\"']?(?:\S*/)?sdlc[\"']?\s+ci\b", re.M) # The step every converted repo's `sdlc ci` job runs today, rev read from the -# hooks file and the launcher cloned at it. Matched exactly, indent aside: a -# step that says anything else is not one this script will guess at. -PINNED_STEP = re.compile( - r"^(?P[ \t]*)rev=\$\(awk " +# hooks file and the launcher cloned at it, the launcher's path quoted or not. +# Matched exactly, indent aside: a step that says anything else is not one this +# script will guess at. +PINNED_BODY = ( + r"[ \t]*rev=\$\(awk " r"'/repo: https:\\/\\/github\.com\\/crunchtools\\/python-sdlc/ \{f=1; next\}\n" r"[ \t]*f && /rev:/ \{print \$2; exit\}' \.pre-commit-config\.yaml\)\n" r"[ \t]*\[ -n \"\$rev\" \] \|\| " r"\{ echo \"no python-sdlc rev in \.pre-commit-config\.yaml\" >&2; exit 2; \}\n" r"[ \t]*git clone -q --depth 1 --branch \"\$rev\" " r"https://github\.com/crunchtools/python-sdlc \"\$RUNNER_TEMP/sdlc\"\n" - r"[ \t]*\$RUNNER_TEMP/sdlc/bin/sdlc ci\n", - re.M, + r"[ \t]*(?P\"?)\$RUNNER_TEMP/sdlc/bin/sdlc(?P=q) ci\n" ) +PINNED_STEP = re.compile(r"^" + PINNED_BODY, re.M) STEP_NAME = re.compile(r"^(?P[ \t]*)- name: sdlc ci\n(?P[ \t]*)run: \|\n", re.M) +# The whole pinned step: its name, a `set -eu` the old form opened with, and +# the pinned body, with no line after it still inside the `run: |` block. The +# validator wants the step's run to be exactly the launcher, so the block goes. +WHOLE_STEP = re.compile( + r"^(?P[ \t]*)- name: sdlc ci\n(?P[ \t]*)run: \|\n" + r"(?:[ \t]*set -eu\n)?" + PINNED_BODY + r"(?!(?P=j)[ \t]+\S)", + re.M, +) +# What an earlier convert_repo.py left: the launcher still inside a block that +# opens with `set -eu`, which v1.46.0's exact-run check fails. +LEFTOVER_RUN = re.compile( + rf"^(?P[ \t]*)run: \|\n[ \t]*set -eu\n[ \t]*{re.escape(FLOATING_CI_LINE)}\n" + r"(?!(?P=j)[ \t]+\S)", + re.M, +) class RefusedError(Exception): @@ -362,21 +378,23 @@ def is_covered_workflow(parsed: dict, jobs: list) -> bool: def float_sdlc_step(name: str, text: str) -> str: """The pinned `sdlc ci` step of a workflow the stub cannot replace, floated.""" if not PINNED_STEP.search(text): - if re.search(rf"^\s*{re.escape(FLOATING_CI_LINE)}\s*$", text, re.M): + if LEFTOVER_RUN.search(text): + return LEFTOVER_RUN.sub(lambda m: f"{m.group('j')}run: {FLOATING_CI_LINE}\n", text) + if re.search(rf"^\s*run: {re.escape(FLOATING_CI_LINE)}\s*$", text, re.M): return text # already floating raise RefusedError( f"{name}: its `sdlc ci` step is not the pinned form this script rewrites" ) - found = STEP_NAME.search(text) + found = WHOLE_STEP.search(text) if found is None or len(STEP_NAME.findall(text)) != 1: raise RefusedError( - f"{name}: no single `- name: sdlc ci` step directly followed by `run: |`" + f"{name}: no single `- name: sdlc ci` step whose `run: |` is the pinned " + f"form and nothing else" ) - j = found.group("j") + i, j = found.group("i"), found.group("j") env = f"{j}env:\n{j} SDLC_IMAGE: ${{{{ vars.SDLC_IMAGE }}}}\n" - text = text.replace(found.group(0), f"{found.group('i')}- name: sdlc ci\n{env}{j}run: |\n", 1) - # `set -eu` stays the step's first line; the clone becomes the bootstrap. - return PINNED_STEP.sub(lambda m: f"{m.group('i')}{FLOATING_CI_LINE}\n", text, count=1) + floated = f"{i}- name: sdlc ci\n{env}{j}run: {FLOATING_CI_LINE}\n" + return text[: found.start()] + floated + text[found.end() :] # gatehouse#150's change to the guard's script, from new back to old: the one @@ -696,10 +714,13 @@ def verify(repo: Path, planned: dict[Path, str | None]) -> None: f"{rel(repo, path)} still fetches python-sdlc by the rev this removes; " f"convert that step by hand" ) - floating = re.compile(rf"^\s*{re.escape(FLOATING_CI_LINE)}\s*$", re.M) + # Exactly the launcher, as constitution v1.46.0's check_floating_ci reads it: + # a `set -eu` beside it fails the required check. if not any( is_sdlc_job(job) - and any(floating.search(str(step.get("run", ""))) for step in job.get("steps") or []) + and any( + str(step.get("run", "")).strip() == FLOATING_CI_LINE for step in job.get("steps") or [] + ) for path, text in texts.items() for job in jobs_of(parse(rel(repo, path), text)).values() ): diff --git a/tests/test_convert_repo.py b/tests/test_convert_repo.py index e3bcc8c..a0eeec0 100755 --- a/tests/test_convert_repo.py +++ b/tests/test_convert_repo.py @@ -324,7 +324,8 @@ def runs_sdlc_ci(text: str) -> bool: check("mixed: gourmand.yml is kept, no stub took it", ".github/workflows/gourmand.yml" in after) check( "mixed: the step floats, reading SDLC_IMAGE from the repository variable", - "\n .sdlc/bin/sdlc ci\n" in tests_yml + "\n run: .sdlc/bin/sdlc ci\n" in tests_yml + and "set -eu" not in tests_yml and "SDLC_IMAGE: ${{ vars.SDLC_IMAGE }}" in tests_yml and "git clone" not in tests_yml, ) @@ -404,11 +405,41 @@ def runs_sdlc_ci(text: str) -> bool: ) +# constitution v1.46.0 wants the step's run to be exactly the launcher: no +# `set -eu` left above it. gatehouse's ci.yml quoted the launcher's path, and an +# earlier convert_repo.py left `set -eu` in the block; both float to the exact line. +for label, text in ( + ( + "a quoted launcher path", + MIXED.replace("$RUNNER_TEMP/sdlc/bin/sdlc ci", '"$RUNNER_TEMP/sdlc/bin/sdlc" ci'), + ), + ( + "the leftover `set -eu` block", + MIXED.replace( + PINNED_STEP.split("run: |\n", 1)[1], " set -eu\n .sdlc/bin/sdlc ci\n" + ), + ), +): + repo = make_repo({".pre-commit-config.yaml": PINNED_HOOKS, ".github/workflows/tests.yml": text}) + done = convert(repo, apply=True) + steps = yaml.safe_load(snapshot(repo)[".github/workflows/tests.yml"])["jobs"]["sdlc"]["steps"] + check( + f"mixed: {label} floats to a run that is exactly `.sdlc/bin/sdlc ci`", + done.returncode == 0 and [s.get("run") for s in steps] == [".sdlc/bin/sdlc ci"], + f"{steps} {done.stderr}", + ) + check(f"mixed: {label}: a second run finds nothing to do", convert(repo).stdout == "") + # A mixed workflow whose `sdlc ci` step is not the exact pinned form, or has # two of them, is not rewritten by guesswork. for label, text in ( ("an edited pinned step", MIXED.replace("--depth 1", "--depth 2")), ("two sdlc ci steps", MIXED + PINNED_STEP), + ("a line after the pinned body", MIXED + " echo done\n"), + ( + "mismatched quotes", + MIXED.replace("$RUNNER_TEMP/sdlc/bin/sdlc ci", '"$RUNNER_TEMP/sdlc/bin/sdlc ci'), + ), ): repo = make_repo({".pre-commit-config.yaml": PINNED_HOOKS, ".github/workflows/tests.yml": text}) before = snapshot(repo) @@ -462,7 +493,7 @@ def runs_sdlc_ci(text: str) -> bool: "a richer sdlc ci job keeps its extra step, floated in place", done.returncode == 0 and "make generated-files" in after[".github/workflows/ci.yml"] - and "\n .sdlc/bin/sdlc ci\n" in after[".github/workflows/ci.yml"], + and "\n run: .sdlc/bin/sdlc ci\n" in after[".github/workflows/ci.yml"], done.stderr, ) for extra in (" env:\n FOO: bar\n", " services:\n db:\n image: x\n"): @@ -512,7 +543,7 @@ def runs_sdlc_ci(text: str) -> bool: f"a ci.yml with {label} keeps it, its step floated in place", done.returncode == 0 and new_text.strip() in kept - and "\n .sdlc/bin/sdlc ci\n" in kept, + and "\n run: .sdlc/bin/sdlc ci\n" in kept, done.stderr, )