Skip to content

Comment specific preview links for changed files #4

Comment specific preview links for changed files

Comment specific preview links for changed files #4

name: "Docs preview comment"
on:
pull_request:
types: [opened, reopened, synchronize]
deployment_status:
types: [created]
jobs:
build-in-progress:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
steps:
- name: Post “build in progress” comment
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const pr = context.payload.pull_request;
const prNumber = pr.number;
const owner = context.repo.owner;
const repo = context.repo.repo;
const body = [
"⏳ **Docs preview build in progress**",
"",
"Your docs preview build is running. This comment will be updated with preview links when it completes."
].join("\n");
// Look for an existing in-progress comment
const { data: comments } = await github.rest.issues.listComments({
owner, repo, issue_number: prNumber
});
const botComment = comments.find(c =>
c.user.type === 'Bot' &&
c.body.startsWith("⏳ **Docs preview build in progress**")
);
if (botComment) {
await github.rest.issues.updateComment({
owner, repo,
comment_id: botComment.id,
body
});
} else {
await github.rest.issues.createComment({
owner, repo,
issue_number: prNumber,
body
});
}
preview-links:
if: >
github.event.deployment.environment == 'docs-preview' &&
github.event.deployment_status.state == 'success'
runs-on: ubuntu-latest
steps:
- name: Post preview links comment
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
// Get PR number from the deployment.task ("docs-preview-<PR#>")
const task = context.payload.deployment.task || '';
const prNumber = Number(task.split('-').pop());
const repo = context.repo.repo;
const baseUrl = `https://docs-v3-preview.elastic.dev/${owner}/${repo}/pull/${prNumber}`;
// List PR files
const { data: files } = await github.rest.pulls.listFiles({
owner, repo, pull_number: prNumber
});
// Filter out removed, non-.md, and anything in a _snippets folder
const changed = files
.filter(f =>
f.status !== 'removed' &&
/\.md$/i.test(f.filename) &&
!/(^|\/)_snippets\//i.test(f.filename)
)
.map(f => {
// Convert index.md ⇒ trailing slash; otherwise strip .md
let path = f.filename.replace(/\/index\.md$/i, '/');
if (path === f.filename) {
path = path.replace(/\.md$/i, '');
}
return `- [\`${f.filename}\`](${baseUrl}/${path})`;
});
if (!changed.length) return; // nothing to do
const body = [
"🔍 **Preview links for changed docs:**",
"",
...changed,
"",
`You can also browse the full preview at ${baseUrl}/`
].join("\n");
// Find existing bot comment (in-progress or previous links)
const { data: comments } = await github.rest.issues.listComments({
owner, repo, issue_number: prNumber
});
const botComment = comments.find(c =>
c.user.type === 'Bot' &&
(
c.body.startsWith("⏳ **Docs preview build in progress**") ||
c.body.startsWith("🔍 **Preview links for changed docs:**")
)
);
if (botComment) {
await github.rest.issues.updateComment({
owner, repo,
comment_id: botComment.id,
body
});
} else {
await github.rest.issues.createComment({
owner, repo,
issue_number: prNumber,
body
});
}