|
1 | 1 | name: preview-build
|
2 | 2 |
|
3 | 3 | on:
|
4 |
| - pull_request_target: |
| 4 | + pull_request: |
5 | 5 | types:
|
6 | 6 | - opened
|
7 | 7 | - synchronize
|
|
43 | 43 | type: string
|
44 | 44 | default: 'false'
|
45 | 45 | required: false
|
| 46 | + disable-comments: |
| 47 | + description: 'Disable comments' |
| 48 | + type: boolean |
| 49 | + default: false |
| 50 | + required: false |
46 | 51 |
|
47 | 52 | permissions:
|
48 | 53 | id-token: write
|
49 | 54 | deployments: write
|
50 | 55 | contents: read
|
51 |
| - pull-requests: read |
| 56 | + pull-requests: write |
52 | 57 |
|
53 | 58 | jobs:
|
54 | 59 | match:
|
@@ -112,6 +117,7 @@ jobs:
|
112 | 117 | id: check-files
|
113 | 118 | uses: tj-actions/changed-files@2f7c5bfce28377bc069a65ba478de0a74aa0ca32 # v46.0.1
|
114 | 119 | with:
|
| 120 | + safe_output: false # set to false because we are using an environment variable to store the output and avoid command injection. |
115 | 121 | files: ${{ inputs.path-pattern != '' && inputs.path-pattern || '**' }}
|
116 | 122 |
|
117 | 123 | - name: Checkout
|
@@ -231,6 +237,70 @@ jobs:
|
231 | 237 | aws cloudfront create-invalidation \
|
232 | 238 | --distribution-id EKT7LT5PM8RKS \
|
233 | 239 | --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 | + } |
234 | 304 |
|
235 | 305 | - name: Update Link Index
|
236 | 306 | if: |
|
|
0 commit comments