Skip to content

Commit 701f08e

Browse files
committed
Update existing comments when possible
1 parent eccf49c commit 701f08e

File tree

2 files changed

+72
-10
lines changed

2 files changed

+72
-10
lines changed

.github/workflows/cloudflare-preview.yml

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,39 @@ jobs:
7070
script: |
7171
const prNumber = ${{ env.PR_NUMBER }};
7272
const url = "${{ steps.deploy.outputs.deployment-url }}";
73+
const commentMarker = "🚀 Preview deployment available at:";
7374
74-
await github.rest.issues.createComment({
75+
// Get all comments on the PR
76+
const comments = await github.rest.issues.listComments({
7577
issue_number: prNumber,
7678
owner: context.repo.owner,
7779
repo: context.repo.repo,
78-
body: `🚀 Preview deployment available at: [${url}](${url})`
80+
per_page: 100
7981
});
82+
83+
// Look for our previous bot comment
84+
const existingComment = comments.data.find(comment =>
85+
comment.body.includes(commentMarker)
86+
);
87+
88+
const commentBody = `${commentMarker} [${url}](${url})`;
89+
90+
if (existingComment) {
91+
// Update existing comment
92+
await github.rest.issues.updateComment({
93+
comment_id: existingComment.id,
94+
owner: context.repo.owner,
95+
repo: context.repo.repo,
96+
body: commentBody
97+
});
98+
console.log("Updated existing preview comment");
99+
} else {
100+
// Create new comment
101+
await github.rest.issues.createComment({
102+
issue_number: prNumber,
103+
owner: context.repo.owner,
104+
repo: context.repo.repo,
105+
body: commentBody
106+
});
107+
console.log("Created new preview comment");
108+
}

.github/workflows/pr-preview-comment.yml

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,56 @@ jobs:
1313
- name: Comment on PR with manual deployment instructions
1414
uses: actions/github-script@v7
1515
with:
16-
script: |
16+
script: |-
1717
const prNumber = context.payload.pull_request.number;
1818
const workflowUrl = `https://github.com/ruby/rdoc/actions/workflows/cloudflare-preview.yml`;
1919
const branch = context.payload.pull_request.head.ref;
20+
const commentMarker = "## Cloudflare Preview Deployment";
2021
2122
// Create a direct link that pre-fills the PR number input
2223
const dispatchUrl = `${workflowUrl}/dispatch?ref=main&inputs%5Bpull_request_number%5D=${prNumber}`;
2324
24-
await github.rest.issues.createComment({
25+
// Get all comments on the PR
26+
const comments = await github.rest.issues.listComments({
2527
issue_number: prNumber,
2628
owner: context.repo.owner,
2729
repo: context.repo.repo,
28-
body: `## Cloudflare Preview Deployment
29-
⚠️ This PR is from a fork, so the preview deployment workflow doesn't run automatically for security reasons.
30-
If you're a maintainer and want to preview this PR:
30+
per_page: 100
31+
});
3132
32-
[➡️ Click here to run the workflow with PR #${prNumber} pre-filled](${dispatchUrl})
33+
// Look for our previous bot comment
34+
const existingComment = comments.data.find(comment =>
35+
comment.body.includes(commentMarker)
36+
);
3337
34-
This will trigger a Cloudflare Pages preview deployment for this PR.`
35-
});
38+
const messageLines = [
39+
`${commentMarker}`,
40+
`⚠️ This PR is from a fork, so the preview deployment workflow doesn't run automatically for security reasons.`,
41+
`If you're a maintainer and want to preview this PR:`,
42+
``,
43+
`[➡️ Click here to run the workflow with PR #${prNumber} pre-filled](${dispatchUrl})`,
44+
``,
45+
`This will trigger a Cloudflare Pages preview deployment for this PR.`
46+
];
47+
48+
const commentBody = messageLines.join('\n');
49+
50+
if (existingComment) {
51+
// Update existing comment
52+
await github.rest.issues.updateComment({
53+
comment_id: existingComment.id,
54+
owner: context.repo.owner,
55+
repo: context.repo.repo,
56+
body: commentBody
57+
});
58+
console.log("Updated existing fork PR comment");
59+
} else {
60+
// Create new comment
61+
await github.rest.issues.createComment({
62+
issue_number: prNumber,
63+
owner: context.repo.owner,
64+
repo: context.repo.repo,
65+
body: commentBody
66+
});
67+
console.log("Created new fork PR comment");
68+
}

0 commit comments

Comments
 (0)