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