|
| 1 | +name: Advance Staged QPK Pin |
| 2 | + |
| 3 | +"on": |
| 4 | + workflow_run: |
| 5 | + workflows: ["CI"] |
| 6 | + types: [completed] |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: read |
| 10 | + pull-requests: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + advance: |
| 14 | + if: >- |
| 15 | + github.event.workflow_run.conclusion == 'success' && |
| 16 | + github.event.workflow_run.head_branch == 'auto/qpk-pin-update' |
| 17 | + runs-on: ubuntu-latest |
| 18 | + timeout-minutes: 10 |
| 19 | + steps: |
| 20 | + - name: Resolve the completed staged pin PR |
| 21 | + id: pr |
| 22 | + env: |
| 23 | + GH_TOKEN: ${{ github.token }} |
| 24 | + COMPLETED_HEAD_SHA: ${{ github.event.workflow_run.head_sha }} |
| 25 | + COMPLETED_HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }} |
| 26 | + run: | |
| 27 | + set -euo pipefail |
| 28 | + pr_payload=$(gh pr list \ |
| 29 | + --repo "$GITHUB_REPOSITORY" \ |
| 30 | + --state open \ |
| 31 | + --head "$COMPLETED_HEAD_BRANCH" \ |
| 32 | + --json number,headRefOid \ |
| 33 | + --jq '.[0] // {}') |
| 34 | + pr_number=$(python3 -c 'import json,sys; print(json.load(sys.stdin).get("number", ""))' <<<"$pr_payload") |
| 35 | + pr_head_sha=$(python3 -c 'import json,sys; print(json.load(sys.stdin).get("headRefOid", ""))' <<<"$pr_payload") |
| 36 | +
|
| 37 | + if [ -z "$pr_number" ]; then |
| 38 | + echo "No open staged QPK pin PR found." >> "$GITHUB_STEP_SUMMARY" |
| 39 | + exit 0 |
| 40 | + fi |
| 41 | + if [ "$pr_head_sha" != "$COMPLETED_HEAD_SHA" ]; then |
| 42 | + echo "Skipping stale CI result for PR #$pr_number." >> "$GITHUB_STEP_SUMMARY" |
| 43 | + exit 0 |
| 44 | + fi |
| 45 | +
|
| 46 | + echo "number=$pr_number" >> "$GITHUB_OUTPUT" |
| 47 | + echo "head_sha=$pr_head_sha" >> "$GITHUB_OUTPUT" |
| 48 | +
|
| 49 | + - name: Enforce the staged-pin trust boundary |
| 50 | + id: guard |
| 51 | + if: steps.pr.outputs.number != '' |
| 52 | + env: |
| 53 | + GH_TOKEN: ${{ github.token }} |
| 54 | + PR_NUMBER: ${{ steps.pr.outputs.number }} |
| 55 | + PR_HEAD_SHA: ${{ steps.pr.outputs.head_sha }} |
| 56 | + run: | |
| 57 | + set -euo pipefail |
| 58 | + gh pr view "$PR_NUMBER" --repo "$GITHUB_REPOSITORY" \ |
| 59 | + --json author,baseRefName,changedFiles,headRefName,isCrossRepository,isDraft,title,url > pr.json |
| 60 | + python3 - <<'PY' |
| 61 | + import json |
| 62 | + import os |
| 63 | + from pathlib import Path |
| 64 | +
|
| 65 | + pr = json.loads(Path("pr.json").read_text(encoding="utf-8")) |
| 66 | + allowed = ( |
| 67 | + (pr.get("author") or {}).get("login") == "Pigbibi" |
| 68 | + and pr.get("baseRefName") == "main" |
| 69 | + and pr.get("headRefName") == "auto/qpk-pin-update" |
| 70 | + and pr.get("title") == "chore: advance staged QPK pin" |
| 71 | + and pr.get("changedFiles") == 1 |
| 72 | + and not pr.get("isCrossRepository") |
| 73 | + and not pr.get("isDraft") |
| 74 | + ) |
| 75 | + reason = "ready" if allowed else "unexpected_pr_metadata" |
| 76 | + summary = [ |
| 77 | + "## Staged QPK Pin Gate", |
| 78 | + f"- PR: {pr.get('url', '<unknown>')}", |
| 79 | + f"- Author: `{(pr.get('author') or {}).get('login', '<unknown>')}`", |
| 80 | + f"- Changed files: `{pr.get('changedFiles', '<unknown>')}`", |
| 81 | + f"- Cross-repository: `{'yes' if pr.get('isCrossRepository') else 'no'}`", |
| 82 | + f"- Decision: `{'advance' if allowed else 'skip'}`", |
| 83 | + f"- Reason: `{reason}`", |
| 84 | + ] |
| 85 | + Path("staged-pin-summary.md").write_text("\n".join(summary) + "\n", encoding="utf-8") |
| 86 | + with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output: |
| 87 | + print(f"allowed={'true' if allowed else 'false'}", file=output) |
| 88 | + PY |
| 89 | +
|
| 90 | + files=$(gh api --paginate "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER/files" --jq '.[].filename') |
| 91 | + if [ "$files" != "QPK_PIN" ]; then |
| 92 | + echo "::error title=Staged QPK pin gate::unexpected_changed_files" |
| 93 | + exit 1 |
| 94 | + fi |
| 95 | +
|
| 96 | + candidate=$(gh api "repos/$GITHUB_REPOSITORY/contents/QPK_PIN?ref=$PR_HEAD_SHA" --jq '.content' \ |
| 97 | + | base64 --decode | tr -d '\r\n') |
| 98 | + if ! [[ "$candidate" =~ ^[0-9a-f]{40}$ ]]; then |
| 99 | + echo "::error title=Staged QPK pin gate::invalid_qpk_pin" |
| 100 | + exit 1 |
| 101 | + fi |
| 102 | +
|
| 103 | + main_sha=$(gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/main" --jq '.object.sha') |
| 104 | + comparison=$(gh api "repos/$GITHUB_REPOSITORY/compare/$candidate...$main_sha" --jq '.status') |
| 105 | + if [ "$comparison" != "ahead" ] && [ "$comparison" != "identical" ]; then |
| 106 | + echo "::error title=Staged QPK pin gate::candidate_is_not_on_main_history" |
| 107 | + exit 1 |
| 108 | + fi |
| 109 | +
|
| 110 | + - name: Append gate summary |
| 111 | + if: steps.pr.outputs.number != '' |
| 112 | + run: cat staged-pin-summary.md >> "$GITHUB_STEP_SUMMARY" |
| 113 | + |
| 114 | + - name: Queue validated staged pin for merge |
| 115 | + if: steps.guard.outputs.allowed == 'true' |
| 116 | + env: |
| 117 | + GH_TOKEN: ${{ github.token }} |
| 118 | + run: | |
| 119 | + gh pr merge "${{ steps.pr.outputs.number }}" \ |
| 120 | + --repo "$GITHUB_REPOSITORY" \ |
| 121 | + --auto --rebase --delete-branch \ |
| 122 | + --match-head-commit "${{ steps.pr.outputs.head_sha }}" |
0 commit comments