Skip to content

Commit b626770

Browse files
committed
Add preview links comment to PRs
1 parent 032257f commit b626770

File tree

1 file changed

+72
-2
lines changed

1 file changed

+72
-2
lines changed

.github/workflows/preview-build.yml

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: preview-build
22

33
on:
4-
pull_request_target:
4+
pull_request:
55
types:
66
- opened
77
- synchronize
@@ -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:
@@ -112,6 +117,7 @@ jobs:
112117
id: check-files
113118
uses: tj-actions/changed-files@2f7c5bfce28377bc069a65ba478de0a74aa0ca32 # v46.0.1
114119
with:
120+
safe_output: false # set to false because we are using an environment variable to store the output and avoid command injection.
115121
files: ${{ inputs.path-pattern != '' && inputs.path-pattern || '**' }}
116122

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

0 commit comments

Comments
 (0)