|
| 1 | +name: Sync Issue Labels to PR |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [ opened, synchronize, edited ] # Trigger when a PR is created, updated, or edited |
| 6 | + |
| 7 | +jobs: |
| 8 | + sync-labels: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + |
| 11 | + steps: |
| 12 | + # Checkout the repository |
| 13 | + - name: Checkout Repository |
| 14 | + uses: actions/checkout@v3 |
| 15 | + |
| 16 | + # Sync labels from the linked issue to the PR |
| 17 | + - name: Sync Labels from Linked Issue |
| 18 | + uses: actions/github-script@v7 |
| 19 | + with: |
| 20 | + github-token: ${{ secrets.BOT_TOKEN }} |
| 21 | + script: | |
| 22 | + // Extract linked issue numbers from the PR description |
| 23 | + const issueNumbers = context.payload.pull_request.body |
| 24 | + ?.match(/(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?)?(?:\s*)?#(\d+)/gi) |
| 25 | + ?.map(match => parseInt(match.match(/\d+/)[0])) || []; |
| 26 | + |
| 27 | + if (issueNumbers.length === 0) { |
| 28 | + console.log("No linked issues found in the PR description."); |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + for (const issueNumber of issueNumbers) { |
| 33 | + console.log(`Processing linked issue: #${issueNumber}`); |
| 34 | + |
| 35 | + // Fetch labels from the linked issue |
| 36 | + const issueResponse = await github.rest.issues.get({ |
| 37 | + owner: context.repo.owner, |
| 38 | + repo: context.repo.repo, |
| 39 | + issue_number: issueNumber, |
| 40 | + }); |
| 41 | + |
| 42 | + const issueLabels = issueResponse.data.labels.map(label => label.name); |
| 43 | + |
| 44 | + if (issueLabels.length === 0) { |
| 45 | + console.log(`No labels found on Issue #${issueNumber}`); |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + console.log(`Labels from Issue #${issueNumber}: ${issueLabels.join(', ')}`); |
| 50 | + |
| 51 | + // Add labels to the PR |
| 52 | + await github.rest.issues.addLabels({ |
| 53 | + owner: context.repo.owner, |
| 54 | + repo: context.repo.repo, |
| 55 | + issue_number: context.payload.pull_request.number, |
| 56 | + labels: issueLabels, |
| 57 | + }); |
| 58 | + |
| 59 | + console.log(`Labels synced to PR: ${issueLabels.join(', ')}`); |
| 60 | + } |
0 commit comments