From 763ec5dfd31542a3b06cb963caa6448af94e14f7 Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Sun, 6 Dec 2026 17:01:55 +0000 Subject: [PATCH] ci(lint): validate renovate.json in the pinned Renovate image Add a renovate-config job to Lint that runs renovate-config-validator --strict --no-global in a copy of renovate.yml's Renovate image, so the config is checked by the Renovate that runs it and the check can be required. The ci image stays without Node. check_bot_pr_diff.py now lets a bot pull request move the tag and digest of the renovate/renovate image line in renovate.yml and lint.yml, in place, keeping the name and the -full flavour. A test holds the two pins equal. Refs: #188 Assisted-by: Claude Code --- .gitea/workflows/AGENTS.md | 9 ++- .gitea/workflows/lint.yml | 28 ++++++++- scripts/check_bot_pr_diff.py | 44 ++++++++++++--- tests/scripts/test_check_bot_pr_diff.py | 75 ++++++++++++++++++++++++- 4 files changed, 145 insertions(+), 11 deletions(-) diff --git a/.gitea/workflows/AGENTS.md b/.gitea/workflows/AGENTS.md index 4a1cc10..35f8368 100644 --- a/.gitea/workflows/AGENTS.md +++ b/.gitea/workflows/AGENTS.md @@ -39,7 +39,8 @@ issue. while a content check must see the content it is given. *Held:* review (`CODEOWNERS`). - **A pull request from a `renovate/` branch changes only version, tag, digest and pin lines** (tool pins with their lock entries and hashes, hook `rev:` lines, Dockerfile `FROM` tags and - digests, `job_image:` pins, consumer image pins). In `pyproject.toml` only a pin's version + digests, `job_image:` pins, consumer image pins, and the tag and digest of the Renovate image + line of `renovate.yml` and of its copy in `lint.yml`). In `pyproject.toml` only a pin's version moves; in `uv.lock` only apm-cli moves, to that pin, with its files from PyPI by sha256, and every other package stays as it is. *Why:* the bot's minor and patch pull requests automerge, so they must not be able to change @@ -71,6 +72,12 @@ issue. - **Triggers:** both run on pull requests to `main` and on manual dispatch; Build also runs on `v*` tags, where it checks the tag against `galaxy.yml`. *Why:* the tag is what consumers install. *Held:* review. +- **Lint's `renovate-config` job runs `renovate-config-validator --strict --no-global` on + `renovate.json`, in the same Renovate image as `renovate.yml`, on every run with no `if:`.** + Renovate moves both pins in one pull request. *Why:* the config is checked by the Renovate + that runs it, and a check that always reports can be required. *Held:* control: + `test_the_renovate_image_pins_are_the_same` in `tests/scripts/test_check_bot_pr_diff.py` for + the equal pins; review for the job. ## Molecule diff --git a/.gitea/workflows/lint.yml b/.gitea/workflows/lint.yml index 472fc80..2dc1d77 100644 --- a/.gitea/workflows/lint.yml +++ b/.gitea/workflows/lint.yml @@ -3,7 +3,7 @@ name: Lint # Runs only on the Collection's own runner (label kamino-docker, scoped to the mortis org). # No org secret is used: the only credential is the # job token (secrets.GITHUB_TOKEN), which is read-only for a fork pull request. No pull_request_target. -# The tools come from the ci image (images/ci, built by image.yml): the pinned Python +# The lint job's tools come from the ci image (images/ci, built by image.yml): the pinned Python # tools of requirements-dev.txt, the collections of requirements.yml and the hook # environments of .pre-commit-config.yaml are baked in. When a PR changes one of those # files the checksum check below fails and the job installs the pinned tools and @@ -148,3 +148,29 @@ jobs: echo "Checking $sha" pre-commit run --hook-stage commit-msg --commit-msg-filename "$MSG_FILE" done + + # renovate.json checked by the validator of the Renovate that runs it. The image is a copy of + # renovate.yml's pin (test_the_renovate_image_pins_are_the_same); the ci image has no Node. + renovate-config: + runs-on: kamino-docker + timeout-minutes: 5 + container: + image: renovate/renovate:44.161.0-full@sha256:ddbf02e44b542343c9df696b88fd953f04cc3ee5f771221805ff8efb26595416 + steps: + # The image runs as a non-root user, so the clone goes to a directory it owns. + - name: Validate renovate.json + env: + GIT_CONFIG_COUNT: "1" + GIT_CONFIG_KEY_0: http.extraheader + GIT_CONFIG_VALUE_0: "AUTHORIZATION: bearer ${{ secrets.GITHUB_TOKEN }}" + SHA: ${{ github.event.pull_request.head.sha || github.sha }} + REF: ${{ github.ref }} + REPO_URL: ${{ github.server_url }}/${{ github.repository }} + run: | + src="$(mktemp -d)" + git clone "$REPO_URL" "$src" + cd "$src" + git checkout "$SHA" || { git fetch origin "$REF" && git checkout "$SHA"; } + echo "Checked out $(git rev-parse HEAD)" + # --no-global: renovate.json is a repository config, not the bot's global one. + renovate-config-validator --strict --no-global renovate.json diff --git a/scripts/check_bot_pr_diff.py b/scripts/check_bot_pr_diff.py index f456029..d3d2676 100644 --- a/scripts/check_bot_pr_diff.py +++ b/scripts/check_bot_pr_diff.py @@ -26,7 +26,10 @@ When starts with `renovate/`, the diff of ...HEAD may only and a human sets the matrix `base:`, so neither is the bot's to change; * .gitea/workflows/{lint,build,molecule,gitea-release-key}.yml: the version and digest of a `git.rkdr.net/mortis/:@sha256:` pin (the name stays), including - the MOLECULE_IMAGE value. + the MOLECULE_IMAGE value; + * .gitea/workflows/renovate.yml, and lint.yml's copy of it: the version and digest of the + `image: renovate/renovate:-full@sha256:` line, in place; the name and the + `-full` flavour stay (the job's report check needs the python3 that only -full ships). Every other file, every other line, an added, deleted or mode-changed file fails, with one `::error::` line per offending file. renovate.json itself is not allowed: the bot does not @@ -73,6 +76,11 @@ UV_TOKEN = re.compile(r"\buv==[0-9][0-9A-Za-z.+!_-]*") UBUNTU_LINE = re.compile( rf"^(\s*job_image:\s*ubuntu)(?::{TAG})?@sha256:[0-9a-f]{{64}}\s*$" ) +RENOVATE_LINE = re.compile( + r"^(\s*image:\s*renovate/renovate:)[0-9]+(?:\.[0-9]+)*(-full)@sha256:[0-9a-f]{64}\s*$" +) +RENOVATE_WORKFLOW = ".gitea/workflows/renovate.yml" +LINT_WORKFLOW = ".gitea/workflows/lint.yml" DOCKERFILE = re.compile(r"images/[^/]+/Dockerfile") COLLECTION_PINS = {"requirements.yml", "extensions/molecule/requirements.yml"} # Lint's and the molecule-controller image's requirements, each with its pip-compile lock. @@ -80,7 +88,6 @@ REQUIREMENTS = {"requirements-dev.txt", "requirements-molecule.txt"} LOCKS = {"requirements-dev.lock", "requirements-molecule.lock"} UV_LOCK = "uv.lock" CONSUMERS = { - ".gitea/workflows/lint.yml", ".gitea/workflows/build.yml", ".gitea/workflows/molecule.yml", ".gitea/workflows/gitea-release-key.yml", @@ -183,6 +190,28 @@ def reduce_pin(line): return reduced if PIN.search(line) else None +def reduce_renovate_image(line): + m = RENOVATE_LINE.match(line) + return m[1] + "X" + m[2] if m else None + + +def reduce_lint_yml(line): + # Lint also validates renovate.json in a copy of renovate.yml's image. + return reduce_pin(line) or reduce_renovate_image(line) + + +MORTIS_PIN = (reduce_pin, "an image pin of the mortis registry") +# The workflows whose image lines a bot moves: (reduce_line, what may change). +WORKFLOW_PINS = { + **dict.fromkeys(CONSUMERS, MORTIS_PIN), + LINT_WORKFLOW: ( + reduce_lint_yml, + "an image pin of the mortis registry or the Renovate image line", + ), + RENOVATE_WORKFLOW: (reduce_renovate_image, "the Renovate image line"), +} + + def file_problem(path, removed, added): """One-line reason why the change to `path` is not allowed for a bot, or None.""" if path in REQUIREMENTS: @@ -216,10 +245,8 @@ def file_problem(path, removed, added): reduce_image_yml, "a `job_image:` ubuntu pin", ) - if path in CONSUMERS: - return same_shape( - removed, added, reduce_pin, "an image pin of the mortis registry" - ) + if path in WORKFLOW_PINS: + return same_shape(removed, added, *WORKFLOW_PINS[path]) return "is not a file a bot pull request may change" @@ -268,7 +295,8 @@ def parse_patch(text): def moved_problem(removed, added): """A reason when a changed line changes position, else None; both lists are (line number, line). - A `job_image:` pin moved to another matrix entry would change which job runs on it.""" + A `job_image:` pin moved to another matrix entry, or the Renovate image line moved out of its + `container:`, would change which job runs on it.""" if [n for n, unused in removed] != [n for n, unused in added]: return "moves or deletes a line (the changed lines must stay where they are)" return None @@ -319,7 +347,7 @@ def check(entries, patch_of, show=None): [line for unused, line in removed], [line for unused, line in added], ) - if not reason and path == civ.MATRIX_FILE: + if not reason and path in (civ.MATRIX_FILE, RENOVATE_WORKFLOW): reason = moved_problem(removed, added) if reason: problems.append(f"{path}: {reason}") diff --git a/tests/scripts/test_check_bot_pr_diff.py b/tests/scripts/test_check_bot_pr_diff.py index d70891f..6d43fd3 100644 --- a/tests/scripts/test_check_bot_pr_diff.py +++ b/tests/scripts/test_check_bot_pr_diff.py @@ -16,6 +16,8 @@ import check_bot_pr_diff as cbd from test_check_uv_lock import APM_NEW, APM_OLD, CLICK_OLD, UV_LOCK, uv_files D1, D2 = "sha256:" + "a" * 64, "sha256:" + "b" * 64 +RENOVATE_OLD = f" image: renovate/renovate:44.161.0-full@{D1}" +RENOVATE_NEW = f" image: renovate/renovate:44.170.2-full@{D2}" FILES = { "requirements-dev.txt": "# pins\nansible-core==2.19.14\nreuse==6.2.0\n", "requirements.yml": "---\ncollections:\n - name: community.general\n version: 14.0.0\n", @@ -28,7 +30,14 @@ FILES = { " container:\n image: ${{ matrix.job_image }}\n" " steps:\n - run: echo ok\n" ), - ".gitea/workflows/lint.yml": f"container:\n image: git.rkdr.net/mortis/ci:0.2.0@{D1}\nrun: echo a\n", + ".gitea/workflows/lint.yml": ( + f"container:\n image: git.rkdr.net/mortis/ci:0.2.0@{D1}\nrun: echo a\n" + f"jobs:\n renovate-config:\n container:\n{RENOVATE_OLD}\n" + ), + ".gitea/workflows/renovate.yml": ( + f"jobs:\n renovate:\n container:\n{RENOVATE_OLD}\n" + " steps:\n - env:\n LOG_LEVEL: info\n run: renovate\n" + ), ".gitea/workflows/molecule.yml": f"env:\n MOLECULE_IMAGE: >-\n git.rkdr.net/mortis/mol:0.1.0@{D1}\n", "renovate.json": "{}\n", "pyproject.toml": '[project]\nrequires-python = ">=3.11"\n\n[dependency-groups]\ndev = ["apm-cli==0.39.3"]\n', @@ -228,6 +237,8 @@ class RuleTest(unittest.TestCase): f" image: git.rkdr.net/mortis/ci:0.2.0@{D1}", f" image: git.rkdr.net/mortis/ci:0.2.1@{D2}", ), + (".gitea/workflows/renovate.yml", RENOVATE_OLD, RENOVATE_NEW), + (".gitea/workflows/lint.yml", RENOVATE_OLD, RENOVATE_NEW), ] for path, old, new in cases: self.assertIsNone(self.problem(path, old, new), (path, old, new)) @@ -311,11 +322,54 @@ class RuleTest(unittest.TestCase): f" image: git.rkdr.net/defame1297/ci:0.2.0@{D1}", ), ("renovate.json", "{}", '{"a": 1}'), + ( + ".gitea/workflows/renovate.yml", + " run: renovate", + " run: evil", + ), + ( + ".gitea/workflows/renovate.yml", + " LOG_LEVEL: info", + " LOG_LEVEL: debug", + ), + ( + ".gitea/workflows/renovate.yml", + f" image: git.rkdr.net/mortis/ci:0.2.0@{D1}", + f" image: git.rkdr.net/mortis/ci:0.2.1@{D2}", + ), + ( + ".gitea/workflows/lint.yml", + f" image: git.rkdr.net/mortis/ci:0.2.0@{D1}", + RENOVATE_NEW, + ), ("roles/x/tasks/main.yml", "a", "b"), ] for path, old, new in cases: self.assertIsNotNone(self.problem(path, old, new), (path, old, new)) + def test_the_renovate_image_pins_are_the_same(self): + # Lint validates renovate.json in the image renovate.yml runs; one bot PR moves both copies. + pins = [] + for name in ("renovate.yml", "lint.yml"): + text = (SCRIPTS.parent / ".gitea/workflows" / name).read_text() + lines = [line for line in text.splitlines() if "renovate/renovate:" in line] + self.assertEqual(len(lines), 1, (name, lines)) + self.assertIsNotNone(cbd.RENOVATE_LINE.match(lines[0]), (name, lines[0])) + pins.append(lines[0].strip()) + self.assertEqual(pins[0], pins[1]) + + def test_the_renovate_image_keeps_its_name_flavour_and_digest(self): + for path in (".gitea/workflows/renovate.yml", ".gitea/workflows/lint.yml"): + for new in ( + RENOVATE_NEW.replace("renovate/renovate", "evil/renovate"), + RENOVATE_NEW.replace("-full@", "@"), + RENOVATE_NEW.replace("-full@", "-slim@"), + RENOVATE_NEW.replace(f"@{D2}", ""), + RENOVATE_NEW.replace("44.170.2", "latest"), + RENOVATE_NEW + " # evil", + ): + self.assertIsNotNone(self.problem(path, RENOVATE_OLD, new), (path, new)) + def test_job_image_lines_cannot_be_added_removed_or_moved(self): path = ".gitea/workflows/image.yml" line = f" job_image: ubuntu:24.04@{D1}" @@ -565,6 +619,25 @@ class RepoTest(unittest.TestCase): self.assertEqual(code, 0, err) self.assertIn("5 file(s)", out) + def test_a_renovate_self_bump_passes(self): + self.edit(".gitea/workflows/renovate.yml", RENOVATE_OLD, RENOVATE_NEW) + self.edit(".gitea/workflows/lint.yml", RENOVATE_OLD, RENOVATE_NEW) + self.commit("bot") + code, out, err = self.gate() + self.assertEqual(code, 0, err) + self.assertIn("2 file(s)", out) + + def test_a_moved_renovate_image_line_is_refused(self): + path = ".gitea/workflows/renovate.yml" + self.edit(path, f"{RENOVATE_OLD}\n", "") + self.edit( + path, " run: renovate\n", f" run: renovate\n{RENOVATE_NEW}\n" + ) + self.commit("bot") + code, _unused, err = self.gate() + self.assertEqual(code, 1, err) + self.assertIn(path, err) + def bump_apm_cli(self): """The change Renovate makes for apm-cli 0.39.4, as uv 0.13.1 writes it.""" self.edit("pyproject.toml", "apm-cli==0.39.3", "apm-cli==0.39.4")