diff --git a/.github/workflows/reusable-build-smoke-gate.yml b/.github/workflows/reusable-build-smoke-gate.yml index 82c6604c..ad2442ee 100755 --- a/.github/workflows/reusable-build-smoke-gate.yml +++ b/.github/workflows/reusable-build-smoke-gate.yml @@ -177,6 +177,9 @@ jobs: - name: Generate trust pack artifact if: ${{ inputs.generate_trust_pack }} + env: + # Checkout of a PR may be a synthetic merge; preserve the actual source head. + WINEBOT_SOURCE_HEAD_SHA: ${{ github.event.pull_request.head.sha || github.sha }} run: scripts/ci/generate-trust-pack.sh "${{ inputs.trust_pack_dir }}" - name: Upload trust pack diff --git a/scripts/ci/generate-trust-pack.sh b/scripts/ci/generate-trust-pack.sh index 4c229533..ede0863c 100755 --- a/scripts/ci/generate-trust-pack.sh +++ b/scripts/ci/generate-trust-pack.sh @@ -2,18 +2,29 @@ set -euo pipefail OUT_DIR="${1:-artifacts/trust-pack}" -mkdir -p "$OUT_DIR" -timestamp="$(date -u +%Y-%m-%dT%H:%M:%SZ)" -sha="$(git rev-parse --short HEAD 2>/dev/null || echo unknown)" +# The checkout can be a synthetic PR merge commit rather than the source head. +# Record both IDs with unambiguous names; refuse provenance-free evidence. +checkout_sha="$(git rev-parse HEAD 2>/dev/null || true)" +source_sha="${WINEBOT_SOURCE_HEAD_SHA:-$checkout_sha}" +for candidate in "$checkout_sha" "$source_sha"; do + if [[ ! "$candidate" =~ ^[0-9a-f]{40}$ ]]; then + echo "Trust pack requires valid full checkout/source Git SHAs" >&2 + exit 1 + fi +done branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo unknown)" +timestamp="$(date -u +%Y-%m-%dT%H:%M:%SZ)" +mkdir -p "$OUT_DIR" cat > "${OUT_DIR}/summary.md" < Path: + bin_dir = tmp_path / "bin" + bin_dir.mkdir() + fake_git = bin_dir / "git" + fake_git.write_text( + "#!/bin/sh\n" + 'if [ "$1" = "rev-parse" ] && [ "$2" = "HEAD" ]; then\n' + ' printf "%s\\n" "$FAKE_CHECKOUT_SHA"\n' + 'elif [ "$1" = "rev-parse" ] && [ "$2" = "--abbrev-ref" ]; then\n' + ' printf "HEAD\\n"\n' + "else\n" + " exit 7\n" + "fi\n", + encoding="utf-8", + ) + fake_git.chmod(0o755) + return tmp_path + + +def generate(workspace: Path, source: str | None = SOURCE, checkout: str = CHECKOUT): + env = { + **os.environ, + "PATH": f"{workspace / 'bin'}:{os.environ.get('PATH', '')}", + "FAKE_CHECKOUT_SHA": checkout, + } + if source is None: + env.pop("WINEBOT_SOURCE_HEAD_SHA", None) + else: + env["WINEBOT_SOURCE_HEAD_SHA"] = source + output = workspace / "pack" + result = subprocess.run( + ["bash", str(GENERATOR), str(output)], + cwd=workspace, + env=env, + text=True, + capture_output=True, + check=False, + timeout=30, + ) + return result, output + + +def test_merged_checkout_not_misrepresented_as_source(workspace: Path) -> None: + result, out = generate(workspace) + assert result.returncode == 0, result.stderr + summary = (out / "summary.md").read_text(encoding="utf-8") + assert f"- Tested checkout commit: {CHECKOUT}" in summary + assert f"- Source head commit: {SOURCE}" in summary + assert "- Commit: " not in summary + assert (out / "inventory.json").is_file() + + +def test_direct_checkout_defaults_source_to_tested_commit(workspace: Path) -> None: + result, out = generate(workspace, source=None) + assert result.returncode == 0, result.stderr + summary = (out / "summary.md").read_text(encoding="utf-8") + assert f"- Source head commit: {CHECKOUT}" in summary + + +@pytest.mark.parametrize( + ("source", "checkout"), + [("not-a-commit", CHECKOUT), (SOURCE, "invalid-checkout")], +) +def test_invalid_identity_fails_before_publishing( + workspace: Path, source: str, checkout: str +) -> None: + result, out = generate(workspace, source=source, checkout=checkout) + assert result.returncode != 0 + assert not (out / "summary.md").exists() + + +def test_reusable_workflow_supplies_pr_source_head() -> None: + workflow = WORKFLOW.read_text(encoding="utf-8") + assert "WINEBOT_SOURCE_HEAD_SHA: ${{ github.event.pull_request.head.sha || github.sha }}" in workflow