cloudflare-pages-preview: add comment-mode=repost, extract scripts
New comment-mode input: - 'sticky' (default, backwards-compat): update the existing marker comment in place — same behavior as before - 'repost': minimize all prior marker comments as OUTDATED (GitHub's native hide-comment UI via GraphQL minimizeComment mutation) and post a fresh comment at the bottom of the conversation Useful for /stageblog where each staging should produce a visible new comment while keeping history collapsible rather than lost. Both deploy and cleanup accept the input and must match. cleanup in repost mode minimizes all prior comments and posts a final cleanup note. Also: - Extract both comment scripts to sibling comment.js files (same pattern as slash-commands/main.js) — the inline YAML scripts were getting long - Paginate listComments (was only fetching the first 30)
This commit is contained in:
parent
8e0308e4dd
commit
e6b64f4192
2 changed files with 154 additions and 65 deletions
|
|
@ -22,6 +22,13 @@ inputs:
|
|||
description: HTML comment marker used to locate the sticky PR comment
|
||||
required: false
|
||||
default: '<!-- cf-pages-preview -->'
|
||||
comment-mode:
|
||||
description: >-
|
||||
Must match the deploy action's comment-mode. 'sticky' (default) updates
|
||||
the single marker comment in place. 'repost' minimizes all marker
|
||||
comments as OUTDATED and posts a final cleanup comment.
|
||||
required: false
|
||||
default: sticky
|
||||
github-token:
|
||||
description: GitHub token for updating the PR comment
|
||||
required: false
|
||||
|
|
@ -72,28 +79,10 @@ runs:
|
|||
uses: actions/github-script@v8
|
||||
env:
|
||||
COMMENT_MARKER: ${{ inputs.comment-marker }}
|
||||
COMMENT_MODE: ${{ inputs.comment-mode }}
|
||||
PR_NUMBER: ${{ inputs.pr-number }}
|
||||
with:
|
||||
github-token: ${{ inputs.github-token }}
|
||||
script: |
|
||||
const { owner, repo } = context.repo;
|
||||
const prNumber = Number(process.env.PR_NUMBER);
|
||||
if (!prNumber) {
|
||||
core.info('No PR number available — skipping comment update.');
|
||||
return;
|
||||
}
|
||||
const marker = process.env.COMMENT_MARKER;
|
||||
|
||||
const { data: comments } = await github.rest.issues.listComments({
|
||||
owner, repo, issue_number: prNumber
|
||||
});
|
||||
const existing = comments.find(c =>
|
||||
c.user.type === 'Bot' && c.body.includes(marker)
|
||||
);
|
||||
|
||||
if (existing) {
|
||||
await github.rest.issues.updateComment({
|
||||
owner, repo, comment_id: existing.id,
|
||||
body: `${marker}\n## Preview\n\n_Preview deployments for this PR have been cleaned up._`
|
||||
});
|
||||
}
|
||||
const main = require(process.env.GITHUB_ACTION_PATH + '/comment.js');
|
||||
await main({ github, context, core });
|
||||
|
|
|
|||
51
cloudflare-pages-preview/cleanup/comment.js
Normal file
51
cloudflare-pages-preview/cleanup/comment.js
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
// Updates the PR preview comment after deployments are deleted. Called by
|
||||
// actions/github-script via `require(GITHUB_ACTION_PATH + '/comment.js')`.
|
||||
|
||||
module.exports = async ({ github, context, core }) => {
|
||||
const { owner, repo } = context.repo;
|
||||
const prNumber = Number(process.env.PR_NUMBER);
|
||||
if (!prNumber) {
|
||||
core.info('No PR number available — skipping comment update.');
|
||||
return;
|
||||
}
|
||||
const marker = process.env.COMMENT_MARKER;
|
||||
const mode = process.env.COMMENT_MODE;
|
||||
|
||||
const comments = await github.paginate(github.rest.issues.listComments, {
|
||||
owner, repo, issue_number: prNumber, per_page: 100,
|
||||
});
|
||||
const prior = comments.filter(c =>
|
||||
c.user.type === 'Bot' && c.body && c.body.includes(marker)
|
||||
);
|
||||
|
||||
const cleanupBody = `${marker}\n## Preview\n\n_Preview deployments for this PR have been cleaned up._`;
|
||||
|
||||
if (mode === 'repost') {
|
||||
for (const c of prior) {
|
||||
try {
|
||||
await github.graphql(
|
||||
`mutation($id: ID!) {
|
||||
minimizeComment(input: {subjectId: $id, classifier: OUTDATED}) {
|
||||
minimizedComment { isMinimized }
|
||||
}
|
||||
}`,
|
||||
{ id: c.node_id },
|
||||
);
|
||||
} catch (e) {
|
||||
core.warning(`Could not minimize comment ${c.id}: ${e.message}`);
|
||||
}
|
||||
}
|
||||
if (prior.length) {
|
||||
await github.rest.issues.createComment({
|
||||
owner, repo, issue_number: prNumber, body: cleanupBody,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
const existing = prior[0];
|
||||
if (existing) {
|
||||
await github.rest.issues.updateComment({
|
||||
owner, repo, comment_id: existing.id, body: cleanupBody,
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
Loading…
Reference in a new issue