|
| 1 | +name: Auto Label |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened, edited] |
| 6 | + issues: |
| 7 | + types: [opened, edited, reopened] |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + pull-requests: write |
| 12 | + issues: write |
| 13 | + |
| 14 | +jobs: |
| 15 | + pr-labeler: |
| 16 | + if: github.event_name == 'pull_request' |
| 17 | + runs-on: ubuntu-latest |
| 18 | + steps: |
| 19 | + - name: Checkout |
| 20 | + uses: actions/checkout@v4 |
| 21 | + |
| 22 | + - name: Apply path labels to PR |
| 23 | + uses: actions/labeler@v5 |
| 24 | + with: |
| 25 | + repo-token: ${{ secrets.GITHUB_TOKEN }} |
| 26 | + configuration-path: .github/labeler.yml |
| 27 | + |
| 28 | + issue-keyword-labeler: |
| 29 | + if: github.event_name == 'issues' |
| 30 | + runs-on: ubuntu-latest |
| 31 | + steps: |
| 32 | + - name: Label issue by keywords |
| 33 | + uses: actions/github-script@v7 |
| 34 | + with: |
| 35 | + script: | |
| 36 | + const issueNumber = context.payload.issue.number; |
| 37 | + const title = (context.payload.issue.title || '').toLowerCase(); |
| 38 | + const body = (context.payload.issue.body || '').toLowerCase(); |
| 39 | + const text = `${title}\n${body}`; |
| 40 | +
|
| 41 | + const labelsToEnsure = [ |
| 42 | + { name: 'type: bug', color: 'd73a4a', description: 'Something is not working' }, |
| 43 | + { name: 'type: feature', color: 'a2eeef', description: 'New feature or request' }, |
| 44 | + { name: 'type: docs', color: '0075ca', description: 'Documentation updates' }, |
| 45 | + { name: 'type: security', color: 'b60205', description: 'Security related' }, |
| 46 | + { name: 'type: question', color: 'd876e3', description: 'Further information is requested' }, |
| 47 | + { name: 'area: backend', color: '0e8a16', description: 'Backend/API changes' }, |
| 48 | + { name: 'area: docs', color: '1d76db', description: 'Documentation files' }, |
| 49 | + { name: 'area: github', color: '5319e7', description: 'GitHub templates/workflows/config' }, |
| 50 | + { name: 'area: security', color: 'b60205', description: 'Security/auth concerns' }, |
| 51 | + ]; |
| 52 | +
|
| 53 | + for (const label of labelsToEnsure) { |
| 54 | + try { |
| 55 | + await github.rest.issues.createLabel({ |
| 56 | + owner: context.repo.owner, |
| 57 | + repo: context.repo.repo, |
| 58 | + name: label.name, |
| 59 | + color: label.color, |
| 60 | + description: label.description, |
| 61 | + }); |
| 62 | + } catch (error) { |
| 63 | + if (error.status !== 422) throw error; |
| 64 | + } |
| 65 | + } |
| 66 | +
|
| 67 | + const labels = new Set(); |
| 68 | + if (/(bug|error|fail|failing|crash|exception|traceback|issue)/.test(text)) { |
| 69 | + labels.add('type: bug'); |
| 70 | + labels.add('area: backend'); |
| 71 | + } |
| 72 | + if (/(feature|enhancement|improve|proposal|request)/.test(text)) { |
| 73 | + labels.add('type: feature'); |
| 74 | + } |
| 75 | + if (/(docs|documentation|readme|install|guide)/.test(text)) { |
| 76 | + labels.add('type: docs'); |
| 77 | + labels.add('area: docs'); |
| 78 | + } |
| 79 | + if (/(security|vulnerability|auth|token|xss|csrf|injection)/.test(text)) { |
| 80 | + labels.add('type: security'); |
| 81 | + labels.add('area: security'); |
| 82 | + } |
| 83 | + if (/(how to|help|question|clarify|what is)/.test(text)) { |
| 84 | + labels.add('type: question'); |
| 85 | + } |
| 86 | +
|
| 87 | + if (labels.size > 0) { |
| 88 | + await github.rest.issues.addLabels({ |
| 89 | + owner: context.repo.owner, |
| 90 | + repo: context.repo.repo, |
| 91 | + issue_number: issueNumber, |
| 92 | + labels: Array.from(labels), |
| 93 | + }); |
| 94 | + } |
0 commit comments