|
| 1 | +# .github/workflows/copy-issue-labels.yml |
| 2 | +name: Copy issue labels to PR |
| 3 | + |
| 4 | +on: |
| 5 | + pull_request: |
| 6 | + types: [opened, reopened, synchronize] |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: read |
| 10 | + issues: read |
| 11 | + pull-requests: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + copy-issue-labels: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + - name: Copy labels from linked issue |
| 18 | + uses: actions/github-script@v7 |
| 19 | + with: |
| 20 | + script: | |
| 21 | + const prNumber = context.payload.pull_request.number; |
| 22 | + const body = context.payload.pull_request.body || ''; |
| 23 | +
|
| 24 | + const match = body.match(/(?:Fixes|Closes|Resolves):?\s+#(\d+)/i); |
| 25 | + if (!match) { |
| 26 | + core.info('No issue reference found in PR body.'); |
| 27 | + return; |
| 28 | + } |
| 29 | +
|
| 30 | + const issueNumber = parseInt(match[1], 10); |
| 31 | + core.info(`Found linked issue #${issueNumber}`); |
| 32 | +
|
| 33 | + const issue = await github.rest.issues.get({ |
| 34 | + owner: context.repo.owner, |
| 35 | + repo: context.repo.repo, |
| 36 | + issue_number: issueNumber |
| 37 | + }); |
| 38 | +
|
| 39 | + const labels = issue.data.labels.map(label => label.name); |
| 40 | + if (labels.length === 0) { |
| 41 | + core.info('No labels found on linked issue.'); |
| 42 | + return; |
| 43 | + } |
| 44 | +
|
| 45 | + await github.rest.issues.addLabels({ |
| 46 | + owner: context.repo.owner, |
| 47 | + repo: context.repo.repo, |
| 48 | + issue_number: prNumber, |
| 49 | + labels |
| 50 | + }); |
| 51 | +
|
| 52 | + core.info(`Copied labels to PR: ${labels.join(', ')}`); |
0 commit comments