-
Notifications
You must be signed in to change notification settings - Fork 0
63 lines (56 loc) · 2.5 KB
/
Copy pathpreview-link.yml
File metadata and controls
63 lines (56 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
name: Post preview link
# Cloudflare Pages reports previews as a Check Run, not a GitHub Deployment, so
# deployment_status never fires here. Instead we key off the comment Cloudflare
# posts on the PR — it carries the branch preview URL — and reply with the exact
# post URL (branch preview + /blog/<slug>/). Free: GitHub's built-in token, no secrets.
on:
issue_comment:
types: [created, edited]
permissions:
pull-requests: write
contents: read
jobs:
comment:
# Only Cloudflare's deploy comment, and only on pull requests.
if: >
github.event.issue.pull_request &&
startsWith(github.event.comment.user.login, 'cloudflare-workers-and-pages') &&
contains(github.event.comment.body, 'Deploy successful')
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
with:
script: |
const cf = context.payload.comment.body || '';
// Prefer the branch preview URL (stable across redeploys of this branch);
// fall back to the per-deploy hash URL if only that is present.
const branch = cf.match(/Branch Preview URL:[\s\S]*?(https:\/\/[a-z0-9-]+\.[a-z0-9-]+\.pages\.dev)/i);
const hash = cf.match(/Preview URL:[\s\S]*?(https:\/\/[a-z0-9-]+\.[a-z0-9-]+\.pages\.dev)/i);
const base = (branch && branch[1]) || (hash && hash[1]);
if (!base) return;
const pr = context.payload.issue;
const match = (pr.body || '').match(/<!--\s*post-slug:\s*([a-z0-9-]+)\s*-->/);
const link = match ? `${base}/blog/${match[1]}` : base;
const marker = '<!-- preview-link-bot -->';
const body = `${marker}\n> [!TIP]\n> 📄 **[Preview your post →](${link})**`;
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
});
const existing = comments.data.find((c) => (c.body || '').includes(marker));
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: pr.number,
body,
});
}