Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 42 additions & 19 deletions .github/scripts/post-preview-comment.cjs
Original file line number Diff line number Diff line change
@@ -1,34 +1,57 @@
const MARKER = '<!-- cf-preview -->';
const MAX_PREVIOUS_ENTRIES = 10;

const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });

const formatRelativeTime = (nowMs, thenMs) => {
const seconds = Math.max(0, Math.floor((nowMs - thenMs) / 1000));
if (seconds < 60) return rtf.format(0, 'second');
if (seconds < 3600) return rtf.format(-Math.floor(seconds / 60), 'minute');
if (seconds < 86400) return rtf.format(-Math.floor(seconds / 3600), 'hour');
return rtf.format(-Math.floor(seconds / 86400), 'day');
};

const formatEntry = ({ url, sha, iso }, repo, nowMs) => {
const commitLink = `[\`${sha.slice(0, 7)}\`](https://github.com/${repo}/commit/${sha})`;
const relative = formatRelativeTime(nowMs, new Date(iso).getTime());
return `${url} (${commitLink}) · ${relative} <!-- sha:${sha} ts:${iso} -->`;
};

const entryPattern = /(https:\/\/\S+\.pages\.dev).*?<!-- sha:([a-f0-9]+) ts:(\S+?) -->/g;

module.exports = async ({ github, context, deploymentUrl, sha }) => {
const { data: comments } = await github.rest.issues.listComments({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
per_page: 100,
});
const marker = '<!-- cf-preview -->';
const existing = comments.find((c) => c.body.includes(marker));
const existing = comments.find((c) => c.body.includes(MARKER));
const repo = `${context.repo.owner}/${context.repo.repo}`;
const shortSha = sha.slice(0, 7);
const commitLink = `[\`${shortSha}\`](https://github.com/${repo}/commit/${sha})`;
const nowMs = Date.now();

let previous = '';
const priorEntries = [];
if (existing) {
const entryPattern = /- (https:\/\/\S+\.pages\.dev)\s+\(.*?\)/g;
const currentLine = existing.body
.split('\n')
.find((l) => l.includes('.pages.dev') && !l.startsWith('-'));
const previousEntries = [];
if (currentLine) previousEntries.push(currentLine.trim());
for (const match of existing.body.matchAll(entryPattern)) {
previousEntries.push(match[0].slice(2));
}
const deduplicated = [...new Set(previousEntries)].filter((e) => !e.startsWith(deploymentUrl));
if (deduplicated.length > 0) {
const list = deduplicated.map((e) => `- ${e}`).join('\n');
previous = `\n\n<details>\n<summary>Previous previews</summary>\n\n${list}\n</details>`;
for (const [, url, prevSha, iso] of existing.body.matchAll(entryPattern)) {
if (url !== deploymentUrl) priorEntries.push({ url, sha: prevSha, iso });
}
}
const kept = priorEntries.slice(0, MAX_PREVIOUS_ENTRIES);

const previousSection =
kept.length > 0
? `\n\n<details>\n<summary>Previous previews</summary>\n\n${kept
.map((e) => `- ${formatEntry(e, repo, nowMs)}`)
.join('\n')}\n</details>`
: '';

const current = formatEntry(
{ url: deploymentUrl, sha, iso: new Date(nowMs).toISOString() },
repo,
nowMs,
);
const body = `${MARKER}\n### Preview deployment\n\n${current}${previousSection}`;

const body = `${marker}\n### Preview deployment\n\n${deploymentUrl} (${commitLink})${previous}`;
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
Expand Down
Loading