|
| 1 | +name: Sync PR data from Linked Issues |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, edited, synchronize] |
| 6 | + |
| 7 | +jobs: |
| 8 | + sync-metadata: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + |
| 11 | + permissions: |
| 12 | + issues: write |
| 13 | + pull-requests: write |
| 14 | + |
| 15 | + steps: |
| 16 | + - name: Extract Linked Issues |
| 17 | + id: extract |
| 18 | + uses: actions/github-script@v7 |
| 19 | + with: |
| 20 | + script: | |
| 21 | + const body = context.payload.pull_request.body || ""; |
| 22 | + const issuePattern = /#(\d+)/g; |
| 23 | + let matches = []; |
| 24 | + let m; |
| 25 | + while ((m = issuePattern.exec(body)) !== null) { |
| 26 | + matches.push(parseInt(m[1])); |
| 27 | + } |
| 28 | + core.setOutput("issues", JSON.stringify(matches)); |
| 29 | +
|
| 30 | + - name: Post or Update data Comment |
| 31 | + if: steps.extract.outputs.issues && steps.extract.outputs.issues != '[]' |
| 32 | + uses: actions/github-script@v7 |
| 33 | + with: |
| 34 | + script: | |
| 35 | + const issuesInput = core.getInput("issues") || "[]"; |
| 36 | + const issues = JSON.parse(issuesInput); |
| 37 | + const prNumber = context.payload.pull_request.number; |
| 38 | +
|
| 39 | + let combinedLabels = []; |
| 40 | + let combinedAssignees = []; |
| 41 | + let combinedMilestones = []; |
| 42 | +
|
| 43 | + for (const number of issues) { |
| 44 | + try { |
| 45 | + const issue = await github.rest.issues.get({ |
| 46 | + ...context.repo, |
| 47 | + issue_number: number |
| 48 | + }); |
| 49 | +
|
| 50 | + combinedLabels.push(...issue.data.labels.map(l => l.name)); |
| 51 | + combinedAssignees.push(...issue.data.assignees.map(a => a.login)); |
| 52 | + if (issue.data.milestone) combinedMilestones.push(issue.data.milestone.title); |
| 53 | +
|
| 54 | + } catch (err) { |
| 55 | + console.log(`Could not fetch issue #${number}: ${err.message}`); |
| 56 | + } |
| 57 | + } |
| 58 | +
|
| 59 | + // Deduplicate |
| 60 | + combinedLabels = [...new Set(combinedLabels)]; |
| 61 | + combinedAssignees = [...new Set(combinedAssignees)]; |
| 62 | + combinedMilestones = [...new Set(combinedMilestones)]; |
| 63 | +
|
| 64 | + const commentBody = |
| 65 | + `### Synced data from Linked Issues\n\n` + |
| 66 | + `**Labels:**\n${combinedLabels.length ? combinedLabels.map(l => `- ${l}`).join("\n") : "- None"}\n\n` + |
| 67 | + `**Assignees:**\n${combinedAssignees.length ? combinedAssignees.map(a => `- ${a}`).join("\n") : "- None"}\n\n` + |
| 68 | + `**Milestones:**\n${combinedMilestones.length ? combinedMilestones.map(m => `- ${m}`).join("\n") : "- None"}\n`; |
| 69 | +
|
| 70 | + // Get existing comments |
| 71 | + const comments = await github.rest.issues.listComments({ |
| 72 | + ...context.repo, |
| 73 | + issue_number: prNumber |
| 74 | + }); |
| 75 | +
|
| 76 | + // Find existing workflow comment |
| 77 | + const existingComment = comments.data.find(c => c.body.includes("### Synced data from Linked Issues")); |
| 78 | +
|
| 79 | + if (existingComment) { |
| 80 | + // Update existing comment |
| 81 | + await github.rest.issues.updateComment({ |
| 82 | + ...context.repo, |
| 83 | + comment_id: existingComment.id, |
| 84 | + body: commentBody |
| 85 | + }); |
| 86 | + } else { |
| 87 | + // Create new comment |
| 88 | + await github.rest.issues.createComment({ |
| 89 | + ...context.repo, |
| 90 | + issue_number: prNumber, |
| 91 | + body: commentBody |
| 92 | + }); |
| 93 | + } |
0 commit comments