Bind CLI release index to verified archive and Bundle inputs
ci/woodpecker/pr/main Pipeline was successful
ci/woodpecker/push/main Pipeline was successful

This commit was merged in pull request #21.
This commit is contained in:
cflmy committed 2026-11-27 22:17:56 +08:00
1 parent 13affa76dd
commit ed86796d6b
5 files changed
+271 -33

No files matched your search

+13 -11
View File
@@ -109,17 +109,16 @@ def fetch_root(pin: dict, cache: Path) -> Path:
return cache
def build_bundle(root: Path, out: Path) -> Path:
def build_bundle(root: Path, out: Path) -> str:
"""Build the Bundle; an existing output is reused only after verification.
CI jobs share one workspace, so a parallel or earlier job may already have
materialized the same pinned Bundle. The output is built in a temporary
sibling and moved into place atomically; a mismatch is rebuilt, never used.
The caller serializes jobs that share a worktree. The output is built
in a temporary sibling and moved into place.
"""
temp = out.with_name(out.name + ".partial-" + str(os.getpid()))
if temp.exists():
shutil.rmtree(temp)
run(
receipt = run(
[
sys.executable,
str(root / "scripts" / "build-stdlib.py"),
@@ -131,7 +130,7 @@ def build_bundle(root: Path, out: Path) -> Path:
if out.exists():
shutil.rmtree(out)
temp.rename(out)
return out
return receipt
def verify(pin: dict, bundle: Path) -> dict:
@@ -158,12 +157,13 @@ def main(argv: list[str]) -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--out", default=str(ROOT / "target" / "ecosystem-stdlib"))
parser.add_argument("--cache", default=str(ROOT / "target" / "ecosystem-root"))
parser.add_argument("--receipt", type=Path, help="save the pinned producer's JSON summary")
args = parser.parse_args(argv)
out = Path(args.out).resolve()
cache = Path(args.cache).resolve()
try:
pin = load_pin()
if out.is_dir():
if out.is_dir() and args.receipt is None:
try:
verify(pin, out)
except (FetchError, OSError, json.JSONDecodeError):
@@ -172,12 +172,14 @@ def main(argv: list[str]) -> int:
print(out)
return 0
root = fetch_root(pin, cache)
bundle = build_bundle(root, out)
verify(pin, bundle)
except FetchError as error:
receipt = build_bundle(root, out)
verify(pin, out)
if args.receipt is not None:
args.receipt.write_text(receipt, encoding="utf-8")
except (FetchError, OSError, json.JSONDecodeError) as error:
print(f"fetch-ecosystem-bundle: error: {error}", file=sys.stderr)
return 1
print(bundle)
print(out)
return 0
+80
View File
@@ -0,0 +1,80 @@
"""Release index ties an archive and the producer receipt to authoritative CI."""
import io
import json
import sys
import tarfile
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "release"))
import release_index
class ReleaseIndexTest(unittest.TestCase):
def setUp(self):
directory = tempfile.TemporaryDirectory()
self.addCleanup(directory.cleanup)
self.archive = Path(directory.name) / "marqdo-3.0.0-rc.6-x86_64-unknown-linux-gnu.tar.gz"
self.sha = "9" * 40
self.bundle = {"name": "marqdo-stdlib", "version": "0.2.0",
"tree_digest": "sha256:" + "1" * 64,
"aliases_digest": "sha256:" + "2" * 64}
self.receipt = {**self.bundle, "root_commit": "8" * 40,
"components": [{"id": "std-io", "revision": "7" * 40}]}
self.info = {
"schema": "marqdo.release-info", "schema_version": 1, "product": "marqdo-cli",
"build_profile": "release", "product_version": "3.0.0-rc.6",
"target_triple": "x86_64-unknown-linux-gnu", "source_sha": self.sha,
"stdlib": {**self.bundle, "source": {"kind": "ecosystem",
"repository": "cflmy/marqdo", "commit": self.receipt["root_commit"]}},
}
steps = [{"name": name, "type": "commands", "state": "success"}
for name in sorted(release_index.REQUIRED_STEPS)]
self.pipeline = {"number": 3, "commit": self.sha, "status": "success", "event": "push",
"workflows": [{"name": "main", "state": "success", "children": steps}]}
self.repo = {"id": 2, "forge_remote_id": "19", "full_name": release_index.REPOSITORY}
with tarfile.open(self.archive, "w:gz") as output:
top = self.archive.name.removesuffix(".tar.gz")
for name, data in (("RELEASE_INFO.json", json.dumps(self.info)), ("SOURCE_SHA", self.sha)):
member = tarfile.TarInfo(f"{top}/{name}")
contents = data.encode()
member.size = len(contents)
output.addfile(member, io.BytesIO(contents))
self.checksum = self.archive.with_name(self.archive.name + ".sha256")
self.digest = release_index.sha256_file(self.archive)
self.checksum.write_text(f"{self.digest} {self.archive.name}\n", encoding="utf-8")
def test_assets_and_dependencies_are_derived_from_the_inputs(self):
with patch.object(release_index, "read_json", side_effect=[{"id": 19}, self.repo, self.pipeline]):
index = release_index.evidence(self.archive, self.receipt, 3)
self.assertEqual(index["ci"], {"pipeline_number": 3, "source_sha": self.sha,
"url": f"{release_index.CI}/repos/2/pipeline/3"})
self.assertEqual(index["assets"][0], {"name": self.archive.name, "sha256": self.digest,
"size": self.archive.stat().st_size,
"target": self.info["target_triple"]})
self.assertEqual(index["dependencies"], [{"id": "std-io", "source_sha": "7" * 40}])
def test_index_rejects_green_pipeline_without_exact_sha_and_gates(self):
wrong_commit = {**self.pipeline, "commit": "0" * 40}
skipped_gate = {**self.pipeline["workflows"][0]}
skipped_gate["children"] = skipped_gate["children"][1:]
for pipeline in (wrong_commit, {**self.pipeline, "workflows": [skipped_gate]}):
with (patch.object(release_index, "read_json", side_effect=[{"id": 19}, self.repo, pipeline]),
self.assertRaisesRegex(ValueError, "exact SHA")):
release_index.evidence(self.archive, self.receipt, 3)
def test_index_rejects_mismatched_receipt_or_archive_checksum(self):
receipt = {**self.receipt, "root_commit": "0" * 40}
with self.assertRaisesRegex(ValueError, "mismatches the Bundle"):
release_index.evidence(self.archive, receipt, 3)
self.checksum.write_text(f"{'0' * 64} {self.archive.name}\n", encoding="utf-8")
with self.assertRaisesRegex(ValueError, "checksum disagrees"):
release_index.evidence(self.archive, self.receipt, 3)
if __name__ == "__main__":
unittest.main()