|
| 1 | +name: Cross-reference tag review |
| 2 | + |
| 3 | +# Runs after a `build` / `build_fork` workflow finishes. The build's |
| 4 | +# `post_steps` job has already produced the `crossref-context` artifact |
| 5 | +# (a TSV of cross-reference tags whose declarations live in files the |
| 6 | +# PR touched, plus a small JSON with the PR number and head SHA). This |
| 7 | +# workflow downloads that artifact in a privileged `workflow_run` |
| 8 | +# context, fetches snippets, posts/updates/deletes the bot comment, |
| 9 | +# and exits non-zero on any tag that's missing upstream so the |
| 10 | +# resulting check status can gate merges. |
| 11 | +# |
| 12 | +# Privilege model: the post_steps job runs in the PR's permission |
| 13 | +# scope and writes only a machine-readable TSV. The TSV is parsed by |
| 14 | +# the orchestrator from a trusted mathlib-ci checkout; we never blindly |
| 15 | +# post Markdown that originated from PR code. |
| 16 | + |
| 17 | +on: |
| 18 | + workflow_run: |
| 19 | + workflows: |
| 20 | + - "continuous integration" |
| 21 | + - "continuous integration (mathlib forks)" |
| 22 | + types: |
| 23 | + - completed |
| 24 | + |
| 25 | +permissions: |
| 26 | + contents: read |
| 27 | + pull-requests: write |
| 28 | + actions: read |
| 29 | + |
| 30 | +jobs: |
| 31 | + crossref-review: |
| 32 | + name: post-or-update-crossref-comment |
| 33 | + if: github.repository == 'leanprover-community/mathlib4' |
| 34 | + runs-on: ubuntu-latest |
| 35 | + env: |
| 36 | + # Until mathlib-ci#39's successor lands in mathlib-ci master, pin to |
| 37 | + # the branch tip containing the new TSV-driven orchestrator. |
| 38 | + MATHLIB_CI_REF: b77378efba2bccad82103c9cd4ae08c3bd2c768d |
| 39 | + |
| 40 | + steps: |
| 41 | + - name: Download crossref artifact |
| 42 | + id: download |
| 43 | + uses: dawidd6/action-download-artifact@bf251b5aa9c2f7eeb574a96ee720e24f801b7c11 # v6 |
| 44 | + with: |
| 45 | + name: crossref-context |
| 46 | + run_id: ${{ github.event.workflow_run.id }} |
| 47 | + path: artifact |
| 48 | + if_no_artifact_found: warn |
| 49 | + |
| 50 | + - name: Resolve PR number |
| 51 | + id: pr |
| 52 | + env: |
| 53 | + HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }} |
| 54 | + HEAD_REPO: ${{ github.event.workflow_run.head_repository.full_name }} |
| 55 | + HEAD_SHA: ${{ github.event.workflow_run.head_sha }} |
| 56 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 57 | + run: | |
| 58 | + set -euo pipefail |
| 59 | + PR="" |
| 60 | + # Preferred: the JSON embedded by post_steps. |
| 61 | + if [ -f artifact/crossref-context.json ]; then |
| 62 | + PR=$(jq -r '.pr_number // empty' artifact/crossref-context.json) |
| 63 | + ART_SHA=$(jq -r '.head_sha // empty' artifact/crossref-context.json) |
| 64 | + if [ -n "$ART_SHA" ] && [ "$ART_SHA" != "$HEAD_SHA" ]; then |
| 65 | + echo "::warning::Artifact head_sha ($ART_SHA) differs from workflow_run head_sha ($HEAD_SHA)." |
| 66 | + fi |
| 67 | + fi |
| 68 | + # Fallback: workflow_run.pull_requests[0].number (often unreliable). |
| 69 | + if [ -z "$PR" ]; then |
| 70 | + PR=$(jq -r '.workflow_run.pull_requests[0].number // empty' \ |
| 71 | + "$GITHUB_EVENT_PATH") |
| 72 | + fi |
| 73 | + # Last-resort fallback: gh pr list with head sha matching. |
| 74 | + if [ -z "$PR" ] && [ -n "$HEAD_REPO" ] && [ -n "$HEAD_BRANCH" ]; then |
| 75 | + PR=$(gh pr list --repo "${{ github.repository }}" \ |
| 76 | + --head "${HEAD_REPO%%/*}:${HEAD_BRANCH}" \ |
| 77 | + --state open --json number,headRefOid \ |
| 78 | + --jq "map(select(.headRefOid == \"$HEAD_SHA\")) | first | .number // empty") |
| 79 | + fi |
| 80 | + echo "number=$PR" >> "$GITHUB_OUTPUT" |
| 81 | + if [ -z "$PR" ]; then |
| 82 | + echo "Could not resolve PR number. Skipping." |
| 83 | + else |
| 84 | + echo "Resolved PR number: $PR" |
| 85 | + fi |
| 86 | +
|
| 87 | + - name: Checkout workflow actions |
| 88 | + if: steps.pr.outputs.number != '' |
| 89 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 90 | + with: |
| 91 | + ref: ${{ github.workflow_sha }} |
| 92 | + fetch-depth: 1 |
| 93 | + sparse-checkout: .github/actions |
| 94 | + path: workflow-actions |
| 95 | + |
| 96 | + - name: Get mathlib-ci |
| 97 | + if: steps.pr.outputs.number != '' |
| 98 | + uses: ./workflow-actions/.github/actions/get-mathlib-ci |
| 99 | + with: |
| 100 | + ref: ${{ env.MATHLIB_CI_REF }} |
| 101 | + |
| 102 | + - name: Compose comment body |
| 103 | + if: steps.pr.outputs.number != '' |
| 104 | + id: compose |
| 105 | + run: | |
| 106 | + set +e |
| 107 | + python3 "${CI_SCRIPTS_DIR}/crossref_review/crossref-pr-comment.py" \ |
| 108 | + --tsv "$(pwd)/artifact/crossref-added.tsv" \ |
| 109 | + --output "$(pwd)/comment.md" |
| 110 | + ec=$? |
| 111 | + if [ ! -f comment.md ]; then : > comment.md; fi |
| 112 | + echo "exit_code=$ec" >> "$GITHUB_OUTPUT" |
| 113 | + echo "---- comment.md ----" |
| 114 | + cat comment.md |
| 115 | + echo "---- end ----" |
| 116 | +
|
| 117 | + - name: Post / update / delete bot comment |
| 118 | + if: steps.pr.outputs.number != '' |
| 119 | + env: |
| 120 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 121 | + GITHUB_REPOSITORY: ${{ github.repository }} |
| 122 | + run: | |
| 123 | + set -euo pipefail |
| 124 | + PR="${{ steps.pr.outputs.number }}" |
| 125 | + EXIT="${{ steps.compose.outputs.exit_code }}" |
| 126 | + TITLE='### Cross-reference tags added by this PR' |
| 127 | + if [ "$EXIT" = "0" ] || [ ! -s comment.md ]; then |
| 128 | + existing=$(gh api "repos/${{ github.repository }}/issues/$PR/comments" --paginate \ |
| 129 | + --jq 'map(select(.body | startswith("### Cross-reference tags added by this PR"))) | first | .id // empty') |
| 130 | + if [ -n "${existing:-}" ]; then |
| 131 | + echo "Removing stale bot comment $existing." |
| 132 | + gh api -X DELETE "repos/${{ github.repository }}/issues/comments/$existing" |
| 133 | + fi |
| 134 | + else |
| 135 | + "${CI_SCRIPTS_DIR}/pr_summary/update_PR_comment.sh" \ |
| 136 | + comment.md "$TITLE" "$PR" |
| 137 | + fi |
| 138 | +
|
| 139 | + - name: Fail on missing tags |
| 140 | + if: steps.pr.outputs.number != '' |
| 141 | + env: |
| 142 | + EXIT: ${{ steps.compose.outputs.exit_code }} |
| 143 | + run: | |
| 144 | + set -euo pipefail |
| 145 | + # Exit codes from crossref-pr-comment.py: |
| 146 | + # 0 = no tags reported (no comment posted; not a failure) |
| 147 | + # 1 = tags reported, all resolved |
| 148 | + # 2 = some tag missing upstream → fail this check |
| 149 | + # 3 = transient network failure → warn but pass |
| 150 | + case "$EXIT" in |
| 151 | + 0|1) echo "OK." ;; |
| 152 | + 2) echo "::error::One or more cross-reference tags are missing upstream." |
| 153 | + exit 1 ;; |
| 154 | + 3) echo "::warning::Some cross-reference tags could not be resolved due to a network error." |
| 155 | + exit 0 ;; |
| 156 | + *) echo "::error::Unexpected exit code $EXIT from crossref-pr-comment.py" |
| 157 | + exit 1 ;; |
| 158 | + esac |
0 commit comments