@@ -277,15 +277,56 @@ jobs:
277277 console.log(`README updated with preview URL: ${previewUrl}`);
278278 }
279279
280- - name : 🌐 Update GitHub repo homepage URL
281- if : github.event_name == 'push' && steps.vars.outputs.is_main == 'true'
280+ - name : 📌 Update PR description with preview URL
281+ if : github.event_name == 'push'
282282 uses : actions/github-script@v7
283283 with :
284284 script : |
285+ const owner = context.repo.owner;
286+ const repo = context.repo.repo;
287+ const branch = '${{ steps.vars.outputs.branch }}';
285288 const previewUrl = '${{ steps.vars.outputs.preview_url }}';
286- await github.rest.repos.update({
287- owner: context.repo.owner,
288- repo: context.repo.repo,
289- homepage: previewUrl,
289+ const sha = context.sha.substring(0, 7);
290+
291+ // Find open PR for this branch
292+ const { data: prs } = await github.rest.pulls.list({
293+ owner, repo, head: `${owner}:${branch}`, state: 'open',
290294 });
291- console.log(`Repository homepage updated to: ${previewUrl}`);
295+
296+ if (prs.length === 0) {
297+ console.log(`No open PR found for branch ${branch}, skipping.`);
298+ return;
299+ }
300+
301+ const pr = prs[0];
302+ const body = pr.body || '';
303+
304+ const marker = {
305+ start: '<!-- GH-PAGES-PREVIEW:START -->',
306+ end: '<!-- GH-PAGES-PREVIEW:END -->',
307+ };
308+
309+ const section = [
310+ marker.start,
311+ `---`,
312+ `🌐 **Preview:** [${previewUrl}](${previewUrl}) `,
313+ `_Deployed from \`${sha}\`_`,
314+ `---`,
315+ marker.end,
316+ ].join('\n');
317+
318+ let updated;
319+ if (body.includes(marker.start)) {
320+ const re = new RegExp(`${marker.start}[\\s\\S]*?${marker.end}`);
321+ updated = body.replace(re, section);
322+ } else {
323+ // Prepend to top of PR description
324+ updated = section + '\n\n' + body;
325+ }
326+
327+ if (updated !== body) {
328+ await github.rest.pulls.update({
329+ owner, repo, pull_number: pr.number, body: updated,
330+ });
331+ console.log(`PR #${pr.number} description updated with preview URL: ${previewUrl}`);
332+ }
0 commit comments