From 7c9071a998f397e386750309173a5d2d31da1740 Mon Sep 17 00:00:00 2001 From: David Nguyen Date: Wed, 19 Aug 2026 23:45:54 +0700 Subject: [PATCH] Add pull request web preview --- .github/workflows/pr-preview.yml | 52 ++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 .github/workflows/pr-preview.yml diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml new file mode 100644 index 0000000..1ecadad --- /dev/null +++ b/.github/workflows/pr-preview.yml @@ -0,0 +1,52 @@ +name: PR Preview + +on: + pull_request: + types: [opened, synchronize, reopened] + +permissions: + contents: read + pull-requests: write + +jobs: + preview-link: + runs-on: ubuntu-latest + steps: + - name: Post preview link + uses: actions/github-script@v7 + with: + script: | + const marker = ''; + const owner = context.repo.owner; + const repo = context.repo.repo; + const pr = context.payload.pull_request; + const sha = pr.head.sha; + const preview = `https://rawcdn.githack.com/${owner}/${repo}/${sha}/index.html`; + const body = `${marker}\n## 🌍 Web preview\n\n[Open this PR as a live website](${preview})\n\nPreview is pinned to commit \`${sha.slice(0, 7)}\` and updates automatically when the PR changes.`; + + const comments = await github.paginate(github.rest.issues.listComments, { + owner, + repo, + issue_number: pr.number, + per_page: 100, + }); + + const existing = comments.find(comment => + comment.user?.type === 'Bot' && comment.body?.includes(marker) + ); + + if (existing) { + await github.rest.issues.updateComment({ + owner, + repo, + comment_id: existing.id, + body, + }); + } else { + await github.rest.issues.createComment({ + owner, + repo, + issue_number: pr.number, + body, + }); + }