Skip to content

Commit 53ea92a

Browse files
authored
Update PR issue sync workflow to use comments
1 parent 2167ce9 commit 53ea92a

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

.github/workflows/pr-issue-sync.yml

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ jobs:
99
runs-on: ubuntu-latest
1010

1111
permissions:
12-
pull-requests: write
13-
issues: read
14-
contents: read
12+
issues: write
1513

1614
steps:
1715
- name: Extract Linked Issues
@@ -26,17 +24,16 @@ jobs:
2624
while ((m = issuePattern.exec(body)) !== null) {
2725
matches.push(parseInt(m[1]));
2826
}
29-
// Ensure output is always a valid JSON array
3027
core.setOutput("issues", JSON.stringify(matches));
3128
32-
- name: Sync Metadata into PR Body
29+
- name: Post or Update data Comment
3330
if: steps.extract.outputs.issues && steps.extract.outputs.issues != '[]'
3431
uses: actions/github-script@v7
3532
with:
3633
script: |
3734
const issuesInput = core.getInput("issues") || "[]";
3835
const issues = JSON.parse(issuesInput);
39-
const pr = context.payload.pull_request;
36+
const prNumber = context.payload.pull_request.number;
4037
4138
let combinedLabels = [];
4239
let combinedAssignees = [];
@@ -49,16 +46,12 @@ jobs:
4946
issue_number: number
5047
});
5148
52-
const labels = issue.data.labels.map(l => l.name);
53-
const assignees = issue.data.assignees.map(a => a.login);
54-
const milestone = issue.data.milestone ? issue.data.milestone.title : null;
55-
56-
combinedLabels.push(...labels);
57-
combinedAssignees.push(...assignees);
58-
if (milestone) combinedMilestones.push(milestone);
49+
combinedLabels.push(...issue.data.labels.map(l => l.name));
50+
combinedAssignees.push(...issue.data.assignees.map(a => a.login));
51+
if (issue.data.milestone) combinedMilestones.push(issue.data.milestone.title);
5952
6053
} catch (err) {
61-
console.log(`Could not sync from issue #${number}:`, err.message);
54+
console.log(`Could not fetch issue #${number}: ${err.message}`);
6255
}
6356
}
6457
@@ -67,23 +60,33 @@ jobs:
6760
combinedAssignees = [...new Set(combinedAssignees)];
6861
combinedMilestones = [...new Set(combinedMilestones)];
6962
70-
const metadataBlock =
71-
`\n---\n` +
72-
`### Synced data from Linked Issues\n\n` +
63+
const commentBody =
64+
`### Synced data from Linked Issues\n\n` +
7365
`**Labels:**\n${combinedLabels.length ? combinedLabels.map(l => `- ${l}`).join("\n") : "- None"}\n\n` +
7466
`**Assignees:**\n${combinedAssignees.length ? combinedAssignees.map(a => `- ${a}`).join("\n") : "- None"}\n\n` +
7567
`**Milestones:**\n${combinedMilestones.length ? combinedMilestones.map(m => `- ${m}`).join("\n") : "- None"}\n`;
7668
77-
let newBody = pr.body || "";
78-
79-
// Remove old metadata block if exists
80-
newBody = newBody.replace(/---\n### Synced data[\s\S]*$/, "");
81-
82-
// Append fresh metadata block
83-
newBody += metadataBlock;
84-
85-
await github.rest.pulls.update({
69+
// Get existing comments
70+
const comments = await github.rest.issues.listComments({
8671
...context.repo,
87-
pull_number: pr.number,
88-
body: newBody
72+
issue_number: prNumber
8973
});
74+
75+
// Find existing workflow comment
76+
const existingComment = comments.data.find(c => c.body.includes("### Synced data from Linked Issues"));
77+
78+
if (existingComment) {
79+
// Update existing comment
80+
await github.rest.issues.updateComment({
81+
...context.repo,
82+
comment_id: existingComment.id,
83+
body: commentBody
84+
});
85+
} else {
86+
// Create new comment
87+
await github.rest.issues.createComment({
88+
...context.repo,
89+
issue_number: prNumber,
90+
body: commentBody
91+
});
92+
}

0 commit comments

Comments
 (0)