|
| 1 | +# Post-build workflow that emits a Lean-aware declarations-diff in the Actions |
| 2 | +# step summary for each successful PR build. Consumes the `import-graph` |
| 3 | +# artifact uploaded by `build_template.yml` on both sides (PR head + master |
| 4 | +# merge-base) and computes the diff via mathlib-ci's `decls-diff` action. |
| 5 | +# |
| 6 | +# Stage-2 scope: visibility only. No `### PR summary` comment patching; that |
| 7 | +# behaviour ships in a follow-up PR. |
| 8 | + |
| 9 | +name: Declarations diff (post-build) |
| 10 | + |
| 11 | +on: |
| 12 | + workflow_run: |
| 13 | + # Match the build workflows by their `name:` — `build.yml` (non-fork PRs, |
| 14 | + # push-triggered) and `build_fork.yml` (fork PRs, pull_request_target). |
| 15 | + workflows: ["continuous integration", "continuous integration (mathlib forks)"] |
| 16 | + types: [completed] |
| 17 | + |
| 18 | +permissions: |
| 19 | + contents: read |
| 20 | + actions: read # for cross-workflow artifact downloads |
| 21 | + |
| 22 | +jobs: |
| 23 | + diff: |
| 24 | + # The build never runs on a `pull_request` event: non-fork PRs build via |
| 25 | + # `build.yml` on `push` (head_branch = the PR branch), fork PRs via |
| 26 | + # `build_fork.yml` on `pull_request_target`. Accept both, and for `push` |
| 27 | + # exclude master and the non-PR maintenance branches. |
| 28 | + if: >- |
| 29 | + github.repository == 'leanprover-community/mathlib4' |
| 30 | + && github.event.workflow_run.conclusion == 'success' |
| 31 | + && ( |
| 32 | + github.event.workflow_run.event == 'pull_request_target' |
| 33 | + || ( |
| 34 | + github.event.workflow_run.event == 'push' |
| 35 | + && github.event.workflow_run.head_branch != 'master' |
| 36 | + && github.event.workflow_run.head_branch != 'nightly-testing' |
| 37 | + && !startsWith(github.event.workflow_run.head_branch, 'lean-pr-testing-') |
| 38 | + ) |
| 39 | + ) |
| 40 | + runs-on: ubuntu-latest |
| 41 | + steps: |
| 42 | + - name: Resolve Build run + SHA |
| 43 | + id: meta |
| 44 | + run: | |
| 45 | + set -euo pipefail |
| 46 | + RUN_ID="${{ github.event.workflow_run.id }}" |
| 47 | + NEW_SHA="${{ github.event.workflow_run.head_sha }}" |
| 48 | + { |
| 49 | + echo "run-id=$RUN_ID" |
| 50 | + echo "new-sha=$NEW_SHA" |
| 51 | + } | tee -a "$GITHUB_OUTPUT" |
| 52 | +
|
| 53 | + - name: Checkout new commit |
| 54 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 55 | + with: |
| 56 | + ref: ${{ steps.meta.outputs.new-sha }} |
| 57 | + fetch-depth: 0 |
| 58 | + |
| 59 | + - name: Resolve merge-base against master |
| 60 | + id: resolve |
| 61 | + env: |
| 62 | + NEW_SHA: ${{ steps.meta.outputs.new-sha }} |
| 63 | + run: | |
| 64 | + set -euo pipefail |
| 65 | + git fetch --quiet origin master |
| 66 | + MB="$(git merge-base "$NEW_SHA" origin/master)" |
| 67 | + echo "merge-base=$MB" | tee -a "$GITHUB_OUTPUT" |
| 68 | +
|
| 69 | + - name: Download new-side artifact |
| 70 | + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 |
| 71 | + with: |
| 72 | + name: import-graph |
| 73 | + path: ./new-artifact |
| 74 | + run-id: ${{ steps.meta.outputs.run-id }} |
| 75 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 76 | + |
| 77 | + - name: Find master Build for the merge-base |
| 78 | + id: master-run |
| 79 | + env: |
| 80 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 81 | + REPO: ${{ github.repository }} |
| 82 | + MB: ${{ steps.resolve.outputs.merge-base }} |
| 83 | + run: | |
| 84 | + set -euo pipefail |
| 85 | + RUN="$(gh api "repos/$REPO/actions/runs?head_sha=$MB&event=push&status=success&branch=master" \ |
| 86 | + --jq '[.workflow_runs[] | select(.name=="continuous integration")] | .[0].id // ""')" |
| 87 | + # A successful run is not enough: it must also carry the `import-graph` artifact. |
| 88 | + HAS_ARTIFACT="" |
| 89 | + if [ -n "$RUN" ]; then |
| 90 | + HAS_ARTIFACT="$(gh api "repos/$REPO/actions/runs/$RUN/artifacts" \ |
| 91 | + --jq '[.artifacts[] | select(.name=="import-graph")] | length')" |
| 92 | + fi |
| 93 | + if [ -n "$RUN" ] && [ "${HAS_ARTIFACT:-0}" -gt 0 ]; then |
| 94 | + { |
| 95 | + echo "found=true" |
| 96 | + echo "run-id=$RUN" |
| 97 | + } | tee -a "$GITHUB_OUTPUT" |
| 98 | + else |
| 99 | + echo "found=false" | tee -a "$GITHUB_OUTPUT" |
| 100 | + echo "No usable master Build for merge-base $MB (no run, or run lacks the import-graph artifact)." |
| 101 | + fi |
| 102 | +
|
| 103 | + - name: Download master-side artifact |
| 104 | + if: steps.master-run.outputs.found == 'true' |
| 105 | + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 |
| 106 | + with: |
| 107 | + name: import-graph |
| 108 | + path: ./ref-artifact |
| 109 | + run-id: ${{ steps.master-run.outputs.run-id }} |
| 110 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 111 | + |
| 112 | + - name: Checkout local actions |
| 113 | + if: steps.master-run.outputs.found == 'true' |
| 114 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 115 | + with: |
| 116 | + ref: ${{ github.workflow_sha }} |
| 117 | + fetch-depth: 1 |
| 118 | + sparse-checkout: .github/actions |
| 119 | + path: workflow-actions |
| 120 | + |
| 121 | + - name: Get mathlib-ci |
| 122 | + if: steps.master-run.outputs.found == 'true' |
| 123 | + uses: ./workflow-actions/.github/actions/get-mathlib-ci |
| 124 | + |
| 125 | + - name: Compute Lean-aware declarations diff |
| 126 | + if: steps.master-run.outputs.found == 'true' |
| 127 | + uses: ./ci-tools/.github/actions/decls-diff |
| 128 | + with: |
| 129 | + reference-decls-file: ${{ github.workspace }}/ref-artifact/decls.txt |
| 130 | + reference-imports-file: ${{ github.workspace }}/ref-artifact/imports.json |
| 131 | + new-decls-file: ${{ github.workspace }}/new-artifact/decls.txt |
| 132 | + new-imports-file: ${{ github.workspace }}/new-artifact/imports.json |
| 133 | + new-sha: ${{ steps.meta.outputs.new-sha }} |
| 134 | + |
| 135 | + - name: Note cache miss |
| 136 | + if: steps.master-run.outputs.found != 'true' |
| 137 | + run: | |
| 138 | + { |
| 139 | + echo "## Declarations diff" |
| 140 | + echo |
| 141 | + echo "⚠️ The Mathlib cache for this PR's merge-base \`${{ steps.resolve.outputs.merge-base }}\` isn't on the server (typically because the merge-base is a bors-batch intermediate that CI never built). Merge \`master\` into this PR and push to retrigger the build." |
| 142 | + } >> "$GITHUB_STEP_SUMMARY" |
0 commit comments