|
| 1 | +name: Sync Issue Metadata to PR |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [opened, edited, synchronize, reopened] |
| 6 | + |
| 7 | +permissions: |
| 8 | + pull-requests: write |
| 9 | + issues: write |
| 10 | + contents: read |
| 11 | + repository-projects: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + sync-pr-metadata: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Checkout |
| 19 | + uses: actions/checkout@v4 |
| 20 | + |
| 21 | + - name: Extract linked issue(s) from PR |
| 22 | + id: extract-issues |
| 23 | + uses: actions/github-script@v7 |
| 24 | + with: |
| 25 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 26 | + script: | |
| 27 | + const prNumber = context.payload.pull_request.number; |
| 28 | + const prTitle = context.payload.pull_request.title || ''; |
| 29 | + const prBody = context.payload.pull_request.body || ''; |
| 30 | +
|
| 31 | + // Regex patterns for issue references |
| 32 | + const patterns = [ |
| 33 | + /(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?)\s+#(\d+)/gi, |
| 34 | + /#(\d+)/g |
| 35 | + ]; |
| 36 | +
|
| 37 | + const issueNumbers = new Set(); |
| 38 | + const text = prTitle + ' ' + prBody; |
| 39 | +
|
| 40 | + for (const pattern of patterns) { |
| 41 | + for (const match of text.matchAll(pattern)) { |
| 42 | + issueNumbers.add(match[1]); |
| 43 | + } |
| 44 | + } |
| 45 | +
|
| 46 | + return JSON.stringify({ issues: Array.from(issueNumbers), pr: prNumber }); |
| 47 | +
|
| 48 | + - name: Sync Issue Metadata to PR |
| 49 | + uses: actions/github-script@v7 |
| 50 | + with: |
| 51 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 52 | + script: | |
| 53 | + const data = JSON.parse('${{ steps.extract-issues.outputs.result }}'); |
| 54 | + const prNumber = data.pr; |
| 55 | + const issueNumbers = data.issues || []; |
| 56 | +
|
| 57 | + if (issueNumbers.length === 0) { |
| 58 | + console.log("No linked issues found"); |
| 59 | + return; |
| 60 | + } |
| 61 | +
|
| 62 | + for (const issueNumber of issueNumbers) { |
| 63 | + try { |
| 64 | + // Fetch issue |
| 65 | + const { data: issue } = await github.rest.issues.get({ |
| 66 | + owner: context.repo.owner, |
| 67 | + repo: context.repo.repo, |
| 68 | + issue_number: parseInt(issueNumber) |
| 69 | + }); |
| 70 | +
|
| 71 | + console.log(`Syncing metadata from Issue #${issueNumber} to PR #${prNumber}`); |
| 72 | +
|
| 73 | + // --- Sync Labels --- |
| 74 | + const issueLabels = issue.labels.map(l => l.name); |
| 75 | + const { data: pr } = await github.rest.pulls.get({ |
| 76 | + owner: context.repo.owner, |
| 77 | + repo: context.repo.repo, |
| 78 | + pull_number: prNumber |
| 79 | + }); |
| 80 | + const currentPRLabels = pr.labels.map(l => l.name); |
| 81 | + const combinedLabels = Array.from(new Set([...currentPRLabels, ...issueLabels])); |
| 82 | +
|
| 83 | + await github.rest.issues.addLabels({ |
| 84 | + owner: context.repo.owner, |
| 85 | + repo: context.repo.repo, |
| 86 | + issue_number: prNumber, |
| 87 | + labels: combinedLabels |
| 88 | + }); |
| 89 | + console.log(`Labels applied: ${combinedLabels.join(', ')}`); |
| 90 | +
|
| 91 | + // --- Sync Milestone --- |
| 92 | + if (issue.milestone) { |
| 93 | + await github.rest.issues.update({ |
| 94 | + owner: context.repo.owner, |
| 95 | + repo: context.repo.repo, |
| 96 | + issue_number: prNumber, |
| 97 | + milestone: issue.milestone.number |
| 98 | + }); |
| 99 | + console.log(`Milestone synced: ${issue.milestone.title}`); |
| 100 | + } |
| 101 | +
|
| 102 | + // --- Sync Projects (GitHub Projects v2) --- |
| 103 | + if(issue.project_cards_url) { |
| 104 | + // Fetch project cards of issue |
| 105 | + const cardsResponse = await github.rest.projects.listCards({ |
| 106 | + column_id: issue.project_cards_url.split('/').pop() // last part is column_id |
| 107 | + }).catch(()=>({data:[]})); |
| 108 | + |
| 109 | + for(const card of cardsResponse.data || []) { |
| 110 | + await github.rest.projects.createCard({ |
| 111 | + column_id: card.column_id, |
| 112 | + content_id: prNumber, |
| 113 | + content_type: 'PullRequest' |
| 114 | + }); |
| 115 | + console.log(`Added PR #${prNumber} to project card in column ${card.column_id}`); |
| 116 | + } |
| 117 | + } |
| 118 | +
|
| 119 | + // --- Optionally: Add a comment on PR --- |
| 120 | + await github.rest.issues.createComment({ |
| 121 | + owner: context.repo.owner, |
| 122 | + repo: context.repo.repo, |
| 123 | + issue_number: prNumber, |
| 124 | + body: `✅ Synchronized metadata from Issue #${issueNumber}:\nLabels: ${issueLabels.join(', ')}\nMilestone: ${issue.milestone ? issue.milestone.title : 'None'}` |
| 125 | + }); |
| 126 | +
|
| 127 | + } catch (error) { |
| 128 | + console.error(`Error syncing issue #${issueNumber} to PR #${prNumber}:`, error.message); |
| 129 | + } |
| 130 | + } |
0 commit comments