|
| 1 | +name: Export cross-reference data |
| 2 | + |
| 3 | +# Generates a JSON dictionary of every declaration tagged with `@[wikidata]`, |
| 4 | +# `@[stacks]`, or `@[kerodon]` (name, file, line, and the referenced ids) and |
| 5 | +# pushes it to a versioned repository (`CROSSREFS_REPO`), where it is publicly |
| 6 | +# available at `https://raw.githubusercontent.com/<CROSSREFS_REPO>/master/crossrefs.json`. |
| 7 | +# |
| 8 | +# The data is read from the fully-imported `Mathlib` environment by |
| 9 | +# `scripts/export_crossrefs.lean`, like the `#stacks_tags` command. |
| 10 | + |
| 11 | +on: |
| 12 | + workflow_run: |
| 13 | + workflows: ["continuous integration"] |
| 14 | + branches: [master] |
| 15 | + types: [completed] |
| 16 | + workflow_dispatch: |
| 17 | + inputs: |
| 18 | + dry_run: |
| 19 | + description: "Dry run: mint the app token and verify push permission, but don't push to the crossrefs repo" |
| 20 | + type: boolean |
| 21 | + default: true |
| 22 | + |
| 23 | +concurrency: |
| 24 | + group: export-crossrefs |
| 25 | + cancel-in-progress: true |
| 26 | + |
| 27 | +permissions: |
| 28 | + contents: read |
| 29 | + # Required for the token-minting action to request a GitHub OIDC token, |
| 30 | + # which it exchanges (via Azure Key Vault) for a GitHub App installation token. |
| 31 | + id-token: write |
| 32 | + |
| 33 | +env: |
| 34 | + CROSSREFS_REPO: leanprover-community/crossref-exports |
| 35 | + BUILT_SHA: ${{ github.event.workflow_run.head_sha || github.sha }} |
| 36 | + |
| 37 | +jobs: |
| 38 | + export: |
| 39 | + name: Export cross-reference JSON |
| 40 | + runs-on: ubuntu-latest |
| 41 | + environment: |
| 42 | + name: crossref-exports |
| 43 | + deployment: false |
| 44 | + if: >- |
| 45 | + github.repository == 'leanprover-community/mathlib4' && |
| 46 | + (github.event_name == 'workflow_dispatch' |
| 47 | + || github.event.workflow_run.conclusion == 'success') |
| 48 | + steps: |
| 49 | + - name: Checkout the built commit |
| 50 | + uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 |
| 51 | + with: |
| 52 | + persist-credentials: false |
| 53 | + # The exact commit CI validated; for manual runs, the dispatched ref. |
| 54 | + ref: ${{ env.BUILT_SHA }} |
| 55 | + |
| 56 | + - name: Configure Lean and fetch the Mathlib cache |
| 57 | + uses: leanprover/lean-action@38fbc41a8c28c4cbaec22d7f7de508ec2e7c0dd9 # v1.5.0 |
| 58 | + with: |
| 59 | + auto-config: false |
| 60 | + use-github-cache: false |
| 61 | + use-mathlib-cache: true |
| 62 | + reinstall-transient-toolchain: true |
| 63 | + |
| 64 | + - name: Generate crossrefs.json |
| 65 | + env: |
| 66 | + CROSSREFS_OUT: crossrefs.json |
| 67 | + CROSSREFS_COMMIT: ${{ env.BUILT_SHA }} |
| 68 | + run: lake env lean scripts/export_crossrefs.lean |
| 69 | + |
| 70 | + - name: Sanity-check the output |
| 71 | + run: | |
| 72 | + jq -e '.entries | length > 0' crossrefs.json > /dev/null |
| 73 | + echo "Exported $(jq '.entries | length' crossrefs.json) entries." |
| 74 | +
|
| 75 | + # Split `owner/repo` so the token can be scoped to the crossrefs repo. The |
| 76 | + # app is installed on the crossrefs repo's owner, not on this repository, |
| 77 | + # so the token-minting action must resolve the installation from there. |
| 78 | + - name: Resolve crossrefs repository owner and name |
| 79 | + id: repo |
| 80 | + run: | |
| 81 | + echo "owner=${CROSSREFS_REPO%%/*}" >> "$GITHUB_OUTPUT" |
| 82 | + echo "name=${CROSSREFS_REPO#*/}" >> "$GITHUB_OUTPUT" |
| 83 | +
|
| 84 | + # Mint a short-lived GitHub App installation token scoped to the crossrefs |
| 85 | + # repo, instead of relying on a long-lived personal access token. |
| 86 | + - name: Generate app token |
| 87 | + id: app-token |
| 88 | + uses: leanprover-community/mathlib-ci/.github/actions/azure-create-github-app-token@3bb576208589a435eeaeac9b144a1b7c3e948760 |
| 89 | + with: |
| 90 | + app-id: ${{ secrets.MATHLIB_CROSSREFS_APP_ID }} |
| 91 | + key-vault-name: ${{ vars.MATHLIB_AZ_KEY_VAULT_NAME }} |
| 92 | + key-name: crossref-exports-app-pk |
| 93 | + azure-client-id: ${{ vars.GH_APP_AZURE_CLIENT_ID_CROSSREFS }} |
| 94 | + azure-tenant-id: ${{ secrets.LPC_AZ_TENANT_ID }} |
| 95 | + owner: ${{ steps.repo.outputs.owner }} |
| 96 | + repositories: ${{ steps.repo.outputs.name }} |
| 97 | + |
| 98 | + - name: Push to the crossrefs repository |
| 99 | + env: |
| 100 | + CROSSREFS_PUSH_TOKEN: ${{ steps.app-token.outputs.token }} |
| 101 | + # On manual runs this defaults to true (see the workflow_dispatch |
| 102 | + # inputs); on the automatic workflow_run trigger `inputs` is empty, so |
| 103 | + # this is false and the push happens for real. |
| 104 | + DRY_RUN: ${{ inputs.dry_run || false }} |
| 105 | + run: | |
| 106 | + git clone --depth 1 \ |
| 107 | + "https://x-access-token:${CROSSREFS_PUSH_TOKEN}@github.com/${CROSSREFS_REPO}.git" \ |
| 108 | + crossrefs-repo |
| 109 | + cd crossrefs-repo |
| 110 | + git config user.name "leanprover-community-bot" |
| 111 | + git config user.email "leanprover-community-bot@users.noreply.github.com" |
| 112 | + # Commit only when the entries themselves change. The `generated` |
| 113 | + # timestamp and source `commit` change on every run, so comparing the |
| 114 | + # whole file would produce a commit even when nothing of substance did. |
| 115 | + # Consequence: the published `generated`/`commit` fields record when the |
| 116 | + # entries last changed, not the latest commit the export ran against, so |
| 117 | + # consumers should not read `generated` as a "data freshness" signal. |
| 118 | + # Read each file fully into a string and compare; using `diff -q` on two |
| 119 | + # `jq` process substitutions makes `diff` close the pipes at the first |
| 120 | + # difference, which leaves the `jq` writers with a SIGPIPE ("broken pipe") |
| 121 | + # and noisy spurious errors in the log. |
| 122 | + if [ -f crossrefs.json ] && \ |
| 123 | + [ "$(jq -Sc '.entries' crossrefs.json)" = "$(jq -Sc '.entries' ../crossrefs.json)" ]; then |
| 124 | + echo "No change in entries." |
| 125 | + changed=false |
| 126 | + else |
| 127 | + cp ../crossrefs.json crossrefs.json |
| 128 | + git add crossrefs.json |
| 129 | + git commit -m "Update crossrefs (mathlib4@${BUILT_SHA})" |
| 130 | + changed=true |
| 131 | + fi |
| 132 | +
|
| 133 | + if [ "${DRY_RUN}" = "true" ]; then |
| 134 | + # Verify the minted token can actually push, without mutating the |
| 135 | + # remote: `git push --dry-run` performs the authenticated |
| 136 | + # git-receive-pack handshake (which GitHub gates on write access) but |
| 137 | + # never sends the update. If nothing changed, add a throwaway local |
| 138 | + # commit so there is a ref update to negotiate. |
| 139 | + if [ "${changed}" != "true" ]; then |
| 140 | + git commit --allow-empty -m "Dry-run push permission check (never sent)" |
| 141 | + fi |
| 142 | + echo "Dry run: verifying push permission against ${CROSSREFS_REPO} without pushing." |
| 143 | + git push --dry-run |
| 144 | + elif [ "${changed}" = "true" ]; then |
| 145 | + git push |
| 146 | + else |
| 147 | + echo "No change in entries; nothing to commit." |
| 148 | + fi |
| 149 | +
|
| 150 | + - name: Post failure message on Zulip |
| 151 | + if: failure() |
| 152 | + uses: zulip/github-actions-zulip/send-message@bd8ec52de371d139ae8313661b7d8318c19266aa # v2.0.1 |
| 153 | + with: |
| 154 | + api-key: ${{ secrets.ZULIP_API_KEY }} |
| 155 | + email: 'github-mathlib4-bot@leanprover.zulipchat.com' |
| 156 | + organization-url: 'https://leanprover.zulipchat.com' |
| 157 | + to: 'nightly-testing-mathlib' |
| 158 | + type: 'stream' |
| 159 | + topic: 'crossrefs export failure' |
| 160 | + content: | |
| 161 | + ❌ Cross-reference export [failed](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) on ${{ env.BUILT_SHA }} |
0 commit comments