| 1 |
name: Auto Label PRs |
| 2 |
|
| 3 |
on: |
| 4 |
pull_request: |
| 5 |
types: [opened, synchronize, reopened, edited] |
| 6 |
|
| 7 |
permissions: |
| 8 |
contents: read |
| 9 |
pull-requests: write |
| 10 |
|
| 11 |
jobs: |
| 12 |
label: |
| 13 |
name: Label PRs |
| 14 |
runs-on: ubuntu-latest |
| 15 |
|
| 16 |
permissions: |
| 17 |
contents: read |
| 18 |
pull-requests: write |
| 19 |
|
| 20 |
steps: |
| 21 |
- name: Labeler |
| 22 |
uses: actions/labeler@v6 |
| 23 |
with: |
| 24 |
repo-token: "${{ secrets.GITHUB_TOKEN }}" |
| 25 |
|
| 26 |
- name: Remove unneeded autolabeler labels |
| 27 |
uses: actions/github-script@v8 |
| 28 |
with: |
| 29 |
script: | |
| 30 |
const pr = context.payload.pull_request; |
| 31 |
const labelsToCheck = ['testing', 'github', 'github_actions', 'dependencies']; |
| 32 |
const files = await github.rest.pulls.listFiles({ |
| 33 |
owner: context.repo.owner, |
| 34 |
repo: context.repo.repo, |
| 35 |
pull_number: pr.number, |
| 36 |
}); |
| 37 |
const filenames = files.data.map(f => f.filename); |
| 38 |
|
| 39 |
// Define your label rules (same as in .github/labeler.yml) |
| 40 |
const labelRules = { |
| 41 |
testing: f => f.startsWith('tests/'), |
| 42 |
github: f => f.startsWith('.github/'), |
| 43 |
github_actions: f => f.startsWith('.github/workflows/'), |
| 44 |
dependencies: f => f === 'uv.lock' || f.startsWith('requirements'), |
| 45 |
}; |
| 46 |
|
| 47 |
// Find which labels should be present |
| 48 |
const shouldHave = new Set(); |
| 49 |
for (const [label, rule] of Object.entries(labelRules)) { |
| 50 |
if (filenames.some(rule)) { |
| 51 |
shouldHave.add(label); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
// Remove labels that are present but not needed |
| 56 |
for (const label of pr.labels.map(l => l.name)) { |
| 57 |
if (labelsToCheck.includes(label) && !shouldHave.has(label)) { |
| 58 |
try { |
| 59 |
await github.rest.issues.removeLabel({ |
| 60 |
owner: context.repo.owner, |
| 61 |
repo: context.repo.repo, |
| 62 |
issue_number: pr.number, |
| 63 |
name: label, |
| 64 |
}); |
| 65 |
} catch (e) {} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
- name: Add build label if version changed |
| 70 |
uses: actions/github-script@v8 |
| 71 |
with: |
| 72 |
script: | |
| 73 |
const pr = context.payload.pull_request; |
| 74 |
const files = await github.rest.pulls.listFiles({ |
| 75 |
owner: context.repo.owner, |
| 76 |
repo: context.repo.repo, |
| 77 |
pull_number: pr.number, |
| 78 |
}); |
| 79 |
let buildLabel = false; |
| 80 |
for (const file of files.data) { |
| 81 |
if (file.filename === 'pyproject.toml' && file.patch && /^[+-]\s*version\s*=/.test(file.patch)) { |
| 82 |
buildLabel = true; |
| 83 |
break; |
| 84 |
} |
| 85 |
} |
| 86 |
if (buildLabel) { |
| 87 |
await github.rest.issues.addLabels({ |
| 88 |
owner: context.repo.owner, |
| 89 |
repo: context.repo.repo, |
| 90 |
issue_number: pr.number, |
| 91 |
labels: ['build'], |
| 92 |
}); |
| 93 |
} else { |
| 94 |
// Remove the build label if it exists |
| 95 |
try { |
| 96 |
await github.rest.issues.removeLabel({ |
| 97 |
owner: context.repo.owner, |
| 98 |
repo: context.repo.repo, |
| 99 |
issue_number: pr.number, |
| 100 |
name: 'build', |
| 101 |
}); |
| 102 |
} catch (e) { |
| 103 |
// Ignore if label does not exist |
| 104 |
} |
| 105 |
} |