|
| 1 | +name: Auto Label PR |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, edited, closed] |
| 6 | + |
| 7 | +concurrency: |
| 8 | + group: pr-labeler-${{ github.workflow }}-${{ github.ref }} |
| 9 | + cancel-in-progress: true |
| 10 | + |
| 11 | +jobs: |
| 12 | + auto-label: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + if: github.event.action != 'closed' || github.event.pull_request.merged == true |
| 15 | + permissions: |
| 16 | + pull-requests: write |
| 17 | + contents: read |
| 18 | + |
| 19 | + steps: |
| 20 | + - name: Checkout |
| 21 | + uses: actions/checkout@v4 |
| 22 | + |
| 23 | + - name: Auto Label Based on Checklist |
| 24 | + uses: actions/github-script@v7 |
| 25 | + with: |
| 26 | + script: | |
| 27 | + const { owner, repo, number } = context.issue; |
| 28 | +
|
| 29 | + const pr = await github.rest.pulls.get({ |
| 30 | + owner, |
| 31 | + repo, |
| 32 | + pull_number: number |
| 33 | + }) |
| 34 | +
|
| 35 | + const prBody = pr.data.body || '' |
| 36 | +
|
| 37 | + const checklistToLabel = { |
| 38 | + 'Polish': 'PR: Polish :nail_care:', |
| 39 | + 'Bugfix':'PR: Bug Fix :bug:', |
| 40 | + 'New feature': 'PR: New Feature :rocket:', |
| 41 | + 'Breaking change': 'PR: Breaking Change :boom:', |
| 42 | + 'Documentation update':'PR: Docs :memo:', |
| 43 | + 'Internal updates': 'PR: Internal :house:', |
| 44 | + } |
| 45 | +
|
| 46 | + const currentLabels = await github.rest.issues.listLabelsOnIssue({ |
| 47 | + owner, |
| 48 | + repo, |
| 49 | + issue_number: number |
| 50 | + }) |
| 51 | +
|
| 52 | + const currentLabelNames = currentLabels.data.map(label => label.name) |
| 53 | + console.log('Current labels:', currentLabelNames) |
| 54 | +
|
| 55 | + const labelsToAdd = [] |
| 56 | + const labelsToRemove = [] |
| 57 | +
|
| 58 | + for (const [checklistKeyword, labelName] of Object.entries(checklistToLabel)) { |
| 59 | + const checkedPattern = new RegExp(`- \\[[Xx]\\]\\s+${checklistKeyword.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}`, 'i') |
| 60 | + const uncheckedPattern = new RegExp(`- \\[\\s\\]\\s+${checklistKeyword.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}`, 'i') |
| 61 | +
|
| 62 | + if (checkedPattern.test(prBody)) { |
| 63 | + if (!currentLabelNames.includes(labelName)) { |
| 64 | + labelsToAdd.push(labelName) |
| 65 | + } |
| 66 | + } else if (uncheckedPattern.test(prBody)) { |
| 67 | + if (currentLabelNames.includes(labelName)) { |
| 68 | + labelsToRemove.push(labelName) |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +
|
| 73 | + if (labelsToAdd.length > 0) { |
| 74 | + labelsToAdd.forEach((label)=>console.log('Adding label:', label)) |
| 75 | + await github.rest.issues.addLabels({ |
| 76 | + owner, |
| 77 | + repo, |
| 78 | + issue_number: number, |
| 79 | + labels: labelsToAdd |
| 80 | + }) |
| 81 | + } |
| 82 | +
|
| 83 | + for (const labelName of labelsToRemove) { |
| 84 | + console.log('Removing label:', labelName) |
| 85 | + try { |
| 86 | + await github.rest.issues.removeLabel({ |
| 87 | + owner, |
| 88 | + repo, |
| 89 | + issue_number: number, |
| 90 | + name: labelName |
| 91 | + }) |
| 92 | + } catch (error) { |
| 93 | + if (error.status !== 404) { |
| 94 | + throw error |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +
|
| 99 | + if (labelsToAdd.length > 0 || labelsToRemove.length > 0) { |
| 100 | + console.log(`Labels updated: +${labelsToAdd.length}, -${labelsToRemove.length}`) |
| 101 | + } else { |
| 102 | + console.log('No label changes needed') |
| 103 | + } |
0 commit comments