From 9de2db8c58dc36995ddaaf42a68c0dbca4c9631c Mon Sep 17 00:00:00 2001 From: Liam Thompson <32779855+leemthompo@users.noreply.github.com> Date: Mon, 16 Jun 2025 15:48:00 +0200 Subject: [PATCH 1/2] [DOCS] Add docs-preview-comment.yml MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Copies workflow added to `docs-content` repo in https://github.com/elastic/docs-content/pull/1341 • triggers on pull request events (open, reopen, sync) • comments with URL preview links for changed docs • fetches all files in the pr using github api • filters for added/modified .md files, excluding removed files and _snippets/ directory • transforms file paths into preview urls on docs-v3-preview.elastic.dev • checks for existing bot comment • updates existing comment or creates new one if none exists --- .github/workflows/docs-preview-comment.yml | 66 ++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 .github/workflows/docs-preview-comment.yml diff --git a/.github/workflows/docs-preview-comment.yml b/.github/workflows/docs-preview-comment.yml new file mode 100644 index 0000000000000..ec652d25806fd --- /dev/null +++ b/.github/workflows/docs-preview-comment.yml @@ -0,0 +1,66 @@ +name: "Docs preview comment" + +on: + pull_request: + types: [opened, reopened, synchronize] + +jobs: + preview-links: + runs-on: ubuntu-latest + steps: + - name: Comment preview links for changed docs + uses: actions/github-script@v7 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const pr = context.payload.pull_request; + const prNum = pr.number; + const owner = context.repo.owner; + const repo = context.repo.repo; + const base = `https://docs-v3-preview.elastic.dev/${owner}/${repo}/pull/${prNum}`; + // 1) List all files in this PR + const { data: files } = await github.rest.pulls.listFiles({ + owner, repo, pull_number: prNum + }); + // 2) Filter to only added/modified .md files (skip removed and _snippets/) + const links = files + .filter(f => + f.status !== 'removed' && + /\.md$/i.test(f.filename) && + !/(^|\/)_snippets\//i.test(f.filename) + ) + .map(f => { + let p = f.filename.replace(/\/index\.md$/i, '/'); + if (p === f.filename) p = p.replace(/\.md$/i, ''); + return `- [\`${f.filename}\`](${base}/${p})`; + }); + if (!links.length) return; // nothing to do + // 3) Build the comment body + const body = [ + "🔍 **Preview links for changed docs:**", + "", + ...links, + "", + "🔔 *The preview site may take up to **3 minutes** to finish building. These links will become live once it completes.*" + ].join("\n"); + // 4) Post or update a single bot comment + const { data: comments } = await github.rest.issues.listComments({ + owner, repo, issue_number: prNum + }); + const existing = comments.find(c => + c.user.type === 'Bot' && + c.body.startsWith("🔍 **Preview links for changed docs:**") + ); + if (existing) { + await github.rest.issues.updateComment({ + owner, repo, + comment_id: existing.id, + body + }); + } else { + await github.rest.issues.createComment({ + owner, repo, + issue_number: prNum, + body + }); + } From 298f22e0ac8a294ab2e0000fdffe1f419bbe118f Mon Sep 17 00:00:00 2001 From: Liam Thompson <32779855+leemthompo@users.noreply.github.com> Date: Mon, 16 Jun 2025 16:33:52 +0200 Subject: [PATCH 2/2] use pull_request_target, add perms - changed event trigger from pull_request to pull_request_target - added permissions for contents: read, issues: write, pull-requests: write --- .github/workflows/docs-preview-comment.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/docs-preview-comment.yml b/.github/workflows/docs-preview-comment.yml index ec652d25806fd..f821e402dc865 100644 --- a/.github/workflows/docs-preview-comment.yml +++ b/.github/workflows/docs-preview-comment.yml @@ -1,9 +1,14 @@ name: "Docs preview comment" on: - pull_request: + pull_request_target: types: [opened, reopened, synchronize] +permissions: + contents: read + issues: write + pull-requests: write + jobs: preview-links: runs-on: ubuntu-latest