Skip to content

Commit 1ec4e0b

Browse files
authored
[DOCS] Add docs-preview-comment.yml (elastic#129485)
* [DOCS] Add docs-preview-comment.yml Copies workflow added to `docs-content` repo in elastic/docs-content#1341 • triggers on pull request events (open, reopen, sync) • comments with URL preview links for changed docs • fetches all files in the pr using github api • filters for added/modified .md files, excluding removed files and _snippets/ directory • transforms file paths into preview urls on docs-v3-preview.elastic.dev • checks for existing bot comment • updates existing comment or creates new one if none exists * use pull_request_target, add perms - changed event trigger from pull_request to pull_request_target - added permissions for contents: read, issues: write, pull-requests: write
1 parent b0daa5a commit 1ec4e0b

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: "Docs preview comment"
2+
3+
on:
4+
pull_request_target:
5+
types: [opened, reopened, synchronize]
6+
7+
permissions:
8+
contents: read
9+
issues: write
10+
pull-requests: write
11+
12+
jobs:
13+
preview-links:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Comment preview links for changed docs
17+
uses: actions/github-script@v7
18+
with:
19+
github-token: ${{ secrets.GITHUB_TOKEN }}
20+
script: |
21+
const pr = context.payload.pull_request;
22+
const prNum = pr.number;
23+
const owner = context.repo.owner;
24+
const repo = context.repo.repo;
25+
const base = `https://docs-v3-preview.elastic.dev/${owner}/${repo}/pull/${prNum}`;
26+
// 1) List all files in this PR
27+
const { data: files } = await github.rest.pulls.listFiles({
28+
owner, repo, pull_number: prNum
29+
});
30+
// 2) Filter to only added/modified .md files (skip removed and _snippets/)
31+
const links = files
32+
.filter(f =>
33+
f.status !== 'removed' &&
34+
/\.md$/i.test(f.filename) &&
35+
!/(^|\/)_snippets\//i.test(f.filename)
36+
)
37+
.map(f => {
38+
let p = f.filename.replace(/\/index\.md$/i, '/');
39+
if (p === f.filename) p = p.replace(/\.md$/i, '');
40+
return `- [\`${f.filename}\`](${base}/${p})`;
41+
});
42+
if (!links.length) return; // nothing to do
43+
// 3) Build the comment body
44+
const body = [
45+
"🔍 **Preview links for changed docs:**",
46+
"",
47+
...links,
48+
"",
49+
"🔔 *The preview site may take up to **3 minutes** to finish building. These links will become live once it completes.*"
50+
].join("\n");
51+
// 4) Post or update a single bot comment
52+
const { data: comments } = await github.rest.issues.listComments({
53+
owner, repo, issue_number: prNum
54+
});
55+
const existing = comments.find(c =>
56+
c.user.type === 'Bot' &&
57+
c.body.startsWith("🔍 **Preview links for changed docs:**")
58+
);
59+
if (existing) {
60+
await github.rest.issues.updateComment({
61+
owner, repo,
62+
comment_id: existing.id,
63+
body
64+
});
65+
} else {
66+
await github.rest.issues.createComment({
67+
owner, repo,
68+
issue_number: prNum,
69+
body
70+
});
71+
}

0 commit comments

Comments
 (0)