Skip to content
Open
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
43 changes: 39 additions & 4 deletions .github/workflows/preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ jobs:
- name: Comment PR with preview URL
if: success()
uses: actions/github-script@v9
env:
WRANGLER_OUTPUT: ${{ steps.upload.outputs.command-output }}
with:
script: |
const prNumber = parseInt('${{ steps.get-pr.outputs.pr-number }}');
Expand All @@ -83,19 +85,52 @@ jobs:
`$1${alias}-`,
);

// wrangler lists each newly uploaded asset as "+ /some/path/index.html"
const uploaded = (process.env.WRANGLER_OUTPUT || '')
.split('\n')
.map(line => line.replace(/\[[0-9;]*m/g, '').trim())
.map(line => /^\+\s+(\/\S*)$/.exec(line)?.[1])
.filter(path => path && path.endsWith('.html'));

const pages = [...new Set(uploaded)]
.map(path => path.replace(/index\.html$/, '').replace(/\.html$/, '/'))
.sort();

const pageList = pages.length
? pages
.map(path => `- [${path}](${previewUrl}${path})`)
.join('\n')
: '_No changed pages detected._';

const marker = '<!-- preview-url-comment -->';
const body = [
marker,
`🌐 Preview URL: ${previewUrl}`,
'',
'**Changed pages**',
pageList,
].join('\n');

const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const alreadyCommented = comments.some(
c => c.user?.type === 'Bot' && c.body?.includes(previewUrl)
const existing = comments.find(
c => c.user?.type === 'Bot' && c.body?.includes(marker)
);
if (!alreadyCommented) {
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: `🌐 Preview URL: ${previewUrl}`,
body,
});
}
Loading