Skip to content

Commit 774d0a6

Browse files
authored
Create link-to-changed-files.yml
1 parent ea451a6 commit 774d0a6

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: "Comment preview links for changed docs"
2+
3+
on:
4+
pull_request:
5+
types: [opened, reopened, synchronize, edited]
6+
7+
jobs:
8+
preview-links:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- uses: actions/checkout@v3
13+
14+
- name: Comment preview URLs for changed .md files
15+
uses: actions/github-script@v7
16+
with:
17+
github-token: ${{ secrets.GITHUB_TOKEN }}
18+
script: |
19+
const pr = context.payload.pull_request;
20+
const prNumber = pr.number;
21+
const owner = context.repo.owner;
22+
const repo = context.repo.repo;
23+
const previewBase = `https://docs-v3-preview.elastic.dev/${owner}/${repo}/pull/${prNumber}`;
24+
25+
// 1) Gather changed .md files
26+
const { data: files } = await github.rest.pulls.listFiles({
27+
owner, repo, pull_number: prNumber
28+
});
29+
const changed = files
30+
.filter(f => f.status !== 'removed' && /\.md$/i.test(f.filename))
31+
.map(f => {
32+
let path = f.filename.replace(/\/index\.md$/i, '/');
33+
if (path === f.filename) path = path.replace(/\.md$/i, '');
34+
return `- [\`${f.filename}\`](${previewBase}/${path})`;
35+
});
36+
if (!changed.length) return;
37+
38+
// 2) Build comment body
39+
const body = [
40+
"🔍 **Preview links for changed docs:**",
41+
"",
42+
...changed,
43+
"",
44+
`You can also browse the full preview at ${previewBase}/`
45+
].join("\n");
46+
47+
// 3) Look for an existing bot comment to update
48+
const { data: comments } = await github.rest.issues.listComments({
49+
owner, repo, issue_number: prNumber
50+
});
51+
const botComment = comments.find(c =>
52+
c.user.type === 'Bot' &&
53+
c.body.startsWith("🔍 **Preview links for changed docs:**")
54+
);
55+
56+
if (botComment) {
57+
// update the old comment
58+
await github.rest.issues.updateComment({
59+
owner,
60+
repo,
61+
comment_id: botComment.id,
62+
body
63+
});
64+
} else {
65+
// create a new one
66+
await github.rest.issues.createComment({
67+
owner,
68+
repo,
69+
issue_number: prNumber,
70+
body
71+
});
72+
}

0 commit comments

Comments
 (0)