Skip to content

Commit da10b01

Browse files
authored
Add preview links comment to PRs (#1416)
* Add preview links comment to PRs * Update .github/workflows/preview-build.yml * Change from code review * test * Fix conditional rendering of details * Update .github/workflows/preview-build.yml * Revert "test" This reverts commit fd3d3c5. * Only try to comment if it's a PR
1 parent d4f0f7b commit da10b01

File tree

1 file changed

+81
-1
lines changed

1 file changed

+81
-1
lines changed

.github/workflows/preview-build.yml

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,17 @@ on:
4343
type: string
4444
default: 'false'
4545
required: false
46+
disable-comments:
47+
description: 'Disable comments'
48+
type: boolean
49+
default: false
50+
required: false
4651

4752
permissions:
4853
id-token: write
4954
deployments: write
5055
contents: read
51-
pull-requests: read
56+
pull-requests: write
5257

5358
jobs:
5459
match:
@@ -231,6 +236,81 @@ jobs:
231236
aws cloudfront create-invalidation \
232237
--distribution-id EKT7LT5PM8RKS \
233238
--paths "${PATH_PREFIX}" "${PATH_PREFIX}/*"
239+
240+
- name: Comment on PR
241+
continue-on-error: true
242+
if: startsWith(github.event_name, 'pull_request') && inputs.disable-comments != 'true' && env.MATCH == 'true' && steps.deployment.outputs.result && steps.check-files.outputs.all_changed_files
243+
uses: actions/github-script@v7
244+
env:
245+
ALL_CHANGED_FILES: ${{ steps.check-files.outputs.all_changed_files }}
246+
with:
247+
script: |
248+
const title = '## 🔍 Preview links for changed docs'
249+
const changedMdFiles = process.env.ALL_CHANGED_FILES
250+
.split(/\s+/)
251+
.filter(i => i.endsWith('.md'))
252+
.filter(i => !i.includes('/_snippets/'));
253+
254+
if (changedMdFiles.length === 0) {
255+
return;
256+
}
257+
258+
const toLink = (file) => {
259+
const path = file
260+
.replace('docs/', '')
261+
.replace('/index.md', '')
262+
.replace('.md', '');
263+
return `[${file}](https://docs-v3-preview.elastic.dev${process.env.PATH_PREFIX}/${path})`;
264+
}
265+
266+
const links = changedMdFiles.map(toLink)
267+
268+
const body = [
269+
title,
270+
...links.slice(0, 10).map(i => `- ${i}`),
271+
]
272+
273+
if (links.length > 10) {
274+
body.push('<details>');
275+
body.push('<summary>More links …</summary>');
276+
body.push('');
277+
for (const link of links.slice(10, 100)) {
278+
body.push(`- ${link}`);
279+
}
280+
body.push('');
281+
body.push('</details>');
282+
}
283+
284+
if (links.length > 100) {
285+
body.push('');
286+
body.push(`<sub>In total, ${links.length} files changed.</sub>`);
287+
}
288+
289+
const owner = context.repo.owner;
290+
const repo = context.repo.repo;
291+
const issue_number = context.payload.pull_request.number;
292+
293+
// Post or update a single bot comment
294+
const { data: comments } = await github.rest.issues.listComments({
295+
owner, repo, issue_number
296+
});
297+
const existing = comments.find(c =>
298+
c.user.type === 'Bot' &&
299+
c.body.startsWith(title)
300+
);
301+
if (existing) {
302+
await github.rest.issues.updateComment({
303+
owner, repo,
304+
comment_id: existing.id,
305+
body: body.join('\n'),
306+
});
307+
} else {
308+
await github.rest.issues.createComment({
309+
owner, repo,
310+
issue_number,
311+
body:body.join('\n'),
312+
});
313+
}
234314
235315
- name: Update Link Index
236316
if: |

0 commit comments

Comments
 (0)