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