|
1 | 1 | name: Autolabel PRs |
2 | 2 |
|
3 | 3 | on: |
4 | | - pull_request: |
| 4 | + pull_request_target: |
5 | 5 | types: [opened] |
6 | 6 | push: |
7 | 7 | paths: |
8 | 8 | - scripts/autolabel.lean |
9 | 9 | - .github/workflows/add_label_from_diff.yaml |
10 | 10 |
|
| 11 | +# Limit permissions for GITHUB_TOKEN for the entire workflow |
| 12 | +permissions: |
| 13 | + contents: read |
| 14 | + pull-requests: write # Only allow PR comments/labels |
| 15 | + # All other permissions are implicitly 'none' |
| 16 | + |
11 | 17 | jobs: |
12 | 18 | add_topic_label: |
13 | 19 | name: Add topic label |
14 | 20 | runs-on: ubuntu-latest |
15 | 21 | # Don't run on forks, where we wouldn't have permissions to add the label anyway. |
16 | 22 | if: github.repository == 'leanprover-community/mathlib4' |
17 | | - permissions: |
18 | | - issues: write |
19 | | - checks: write |
20 | | - pull-requests: write |
21 | | - contents: read |
22 | 23 | steps: |
23 | | - - name: Checkout code |
24 | | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
25 | | - with: |
26 | | - fetch-depth: 0 |
27 | | - - name: Configure Lean |
28 | | - uses: leanprover/lean-action@f807b338d95de7813c5c50d018f1c23c9b93b4ec # 2025-04-24 |
29 | | - with: |
30 | | - auto-config: false |
31 | | - use-github-cache: false |
32 | | - use-mathlib-cache: false |
33 | | - - name: lake exe autolabel |
34 | | - run: | |
35 | | - # the checkout dance, to avoid a detached head |
36 | | - git checkout master |
37 | | - git checkout - |
38 | | - lake exe autolabel "$NUMBER" |
39 | | - env: |
40 | | - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
41 | | - GH_REPO: ${{ github.repository }} |
42 | | - NUMBER: ${{ github.event.number }} |
| 24 | + - name: Checkout code |
| 25 | + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 26 | + with: |
| 27 | + ref: ${{ github.event.pull_request.head.sha }} |
| 28 | + fetch-depth: 0 |
| 29 | + - name: Configure Lean |
| 30 | + uses: leanprover/lean-action@f807b338d95de7813c5c50d018f1c23c9b93b4ec # 2025-04-24 |
| 31 | + with: |
| 32 | + auto-config: false |
| 33 | + use-github-cache: false |
| 34 | + use-mathlib-cache: false |
| 35 | + - name: lake exe autolabel |
| 36 | + run: | |
| 37 | + # the checkout dance, to avoid a detached head |
| 38 | + git checkout master |
| 39 | + git checkout - |
| 40 | + labels="$(lake exe autolabel)" |
| 41 | + printf '%s\n' "${labels}" |
| 42 | + # extract |
| 43 | + label="$(printf '%s' "${labels}" | sed -n 's=.*#\[\([^,]*\)\].*=\1=p')" |
| 44 | + printf 'label: "%s"\n' "${label}" |
| 45 | + if [ -n "${label}" ] |
| 46 | + then |
| 47 | + printf 'Applying label %s\n' "${label}" |
| 48 | + # we use curl rather than octokit/request-action so that the job won't fail |
| 49 | + # (and send an annoying email) if the labels don't exist |
| 50 | + url="https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels" |
| 51 | + printf 'url: %s\n' "${url}" |
| 52 | + jsonLabel="$(printf '{"labels":["%s"]}' "${label}")" |
| 53 | + printf 'jsonLabel: %s\n' "${jsonLabel}" |
| 54 | + curl --request POST \ |
| 55 | + --header 'Accept: application/vnd.github+json' \ |
| 56 | + --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \ |
| 57 | + --header 'X-GitHub-Api-Version: 2022-11-28' \ |
| 58 | + --url "${url}" \ |
| 59 | + --data "${jsonLabel}" |
| 60 | + else |
| 61 | + echo "There is no single label that we could apply, so we are not applying any label." |
| 62 | + fi |
| 63 | + env: |
| 64 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments