|
| 1 | +name: ✨ Auto-label PR |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [opened, synchronized, reopened] |
| 6 | + |
| 7 | +jobs: |
| 8 | + update-pr: |
| 9 | + name: Update PR |
| 10 | + runs-on: ubuntu-latest |
| 11 | + permissions: |
| 12 | + pull-requests: write |
| 13 | + steps: |
| 14 | + - name: Gather Info |
| 15 | + id: check |
| 16 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8 |
| 17 | + with: |
| 18 | + script: | |
| 19 | + const { data: pr } = await github.rest.pulls.get({ |
| 20 | + owner: context.repo.owner, |
| 21 | + repo: context.repo.repo, |
| 22 | + pull_number: context.payload.pull_request.number |
| 23 | + }); |
| 24 | +
|
| 25 | + // Check if PR has assignees |
| 26 | + const hasAssignees = pr.assignees && pr.assignees.length > 0; |
| 27 | + core.setOutput('has_assignees', hasAssignees); |
| 28 | + core.setOutput('author', pr.user.login); |
| 29 | +
|
| 30 | + // Get list of changed files |
| 31 | + const { data: files } = await github.rest.pulls.listFiles({ |
| 32 | + owner: context.repo.owner, |
| 33 | + repo: context.repo.repo, |
| 34 | + pull_number: context.payload.pull_request.number |
| 35 | + }); |
| 36 | +
|
| 37 | + // Find all packages that were modified |
| 38 | + const packagesRegex = /^packages\/([^\/]+)\//; |
| 39 | + const affectedPackages = new Set(); |
| 40 | +
|
| 41 | + for (const file of files) { |
| 42 | + const match = file.filename.match(packagesRegex); |
| 43 | + if (match) { |
| 44 | + affectedPackages.add(match[1]); |
| 45 | + } |
| 46 | + } |
| 47 | +
|
| 48 | + const labels = Array.from(affectedPackages).map(pkg => `pkg/${pkg}`); |
| 49 | + core.setOutput('labels', JSON.stringify(labels)); |
| 50 | + console.log('Detected package labels:', labels); |
| 51 | +
|
| 52 | + // Get current labels on the PR that match pkg/* pattern |
| 53 | + const currentPkgLabels = pr.labels |
| 54 | + .map(label => label.name) |
| 55 | + .filter(name => name.startsWith('pkg/')); |
| 56 | +
|
| 57 | + core.setOutput('current_pkg_labels', JSON.stringify(currentPkgLabels)); |
| 58 | + console.log('Current pkg labels:', currentPkgLabels); |
| 59 | +
|
| 60 | + - name: Sync Author |
| 61 | + if: steps.check.outputs.has_assignees == 'false' |
| 62 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8 |
| 63 | + with: |
| 64 | + script: | |
| 65 | + await github.rest.issues.addAssignees({ |
| 66 | + owner: context.repo.owner, |
| 67 | + repo: context.repo.repo, |
| 68 | + issue_number: context.payload.pull_request.number, |
| 69 | + assignees: ['${{ steps.check.outputs.author }}'] |
| 70 | + }); |
| 71 | +
|
| 72 | + console.log('Assigned PR author: ${{ steps.check.outputs.author }}'); |
| 73 | +
|
| 74 | + - name: Sync Labels |
| 75 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8 |
| 76 | + with: |
| 77 | + script: | |
| 78 | + const newLabels = ${{ steps.check.outputs.labels }}; |
| 79 | + const currentLabels = ${{ steps.check.outputs.current_pkg_labels }}; |
| 80 | +
|
| 81 | + // Find labels to add (in newLabels but not in currentLabels) |
| 82 | + const labelsToAdd = newLabels.filter(label => !currentLabels.includes(label)); |
| 83 | +
|
| 84 | + // Find labels to remove (in currentLabels but not in newLabels) |
| 85 | + const labelsToRemove = currentLabels.filter(label => !newLabels.includes(label)); |
| 86 | +
|
| 87 | + // Add new labels |
| 88 | + if (labelsToAdd.length > 0) { |
| 89 | + await github.rest.issues.addLabels({ |
| 90 | + owner: context.repo.owner, |
| 91 | + repo: context.repo.repo, |
| 92 | + issue_number: context.payload.pull_request.number, |
| 93 | + labels: labelsToAdd |
| 94 | + }); |
| 95 | + console.log('Added labels:', labelsToAdd); |
| 96 | + } |
| 97 | +
|
| 98 | + // Remove obsolete labels |
| 99 | + for (const label of labelsToRemove) { |
| 100 | + await github.rest.issues.removeLabel({ |
| 101 | + owner: context.repo.owner, |
| 102 | + repo: context.repo.repo, |
| 103 | + issue_number: context.payload.pull_request.number, |
| 104 | + name: label |
| 105 | + }); |
| 106 | + console.log('Removed label:', label); |
| 107 | + } |
| 108 | +
|
| 109 | + if (labelsToAdd.length === 0 && labelsToRemove.length === 0) { |
| 110 | + console.log('No label changes needed'); |
| 111 | + } |
0 commit comments