Files
coder/.github/workflows/docs-preview.yaml
Nick Vigilante 13e673e9c2 ci: build docs PR previews on per-PR preview sites (#30698)
Docs previews at `coder.com/docs/@<branch>` share coder.com's origin and
cookies with the real site. This publishes each pull request's docs as
its own site on `docs.cdr-preview.dev`, a domain used only for previews
(repository variable `DOCS_PREVIEW_DOMAIN`), and points the existing
`docs-preview` comment's per-page links and checkboxes at it.

## URLs

- `https://pr-<n>.docs.cdr-preview.dev`: the PR head, re-aliased on
every build. A slower build of an older commit doesn't take it back.
- `https://pr-<n>-<first 9 of sha>.docs.cdr-preview.dev`: one per built
commit, listed newest first in a collapsible "Preview sites" list in the
PR's docs preview comment, so reviewers can step through the branch.
- Every URL is built from `vars.DOCS_PREVIEW_DOMAIN` (aliases, comment
links, cleanup); nothing hard-codes the domain. Previews moved off
`coderite.app`, which also hosts login-protected internal sites, so a
preview page whose content any coder/coder writer controls would have
been same-site with them. `cdr-preview.dev` hosts nothing else, and the
docs previews sit under `docs.` so other preview kinds can get their own
subdomain later. The jobs fail with a clear error if it is unset or a
`vercel.app` name, because `*.vercel.app` names are first-come across
all Vercel accounts and anyone could claim a preview's name before our
deploy or after cleanup frees it.
- Every preview carries the docs CSP and the other headers from
docs-engine's `apps/offlinedocs/vercel.json`, plus `X-Robots-Tag:
noindex`, which the deploy job prepends to the Build Output API
`config.json` routes without touching the existing ones.
- On merge or close, every deployment tagged `pr=<n>` and its aliases
are deleted. A weekly groom (Mondays 07:23 UTC, also
`workflow_dispatch`) deletes previews of any closed PR, in case a close
event was missed.

## Who gets previews

Only PRs from a branch in coder/coder, on every push that touches
`docs/**`. Every job in the deploy workflow, and the close path of the
cleanup, requires `github.event.pull_request.head.repo.full_name ==
github.repository`.

Fork PRs get no preview for now. Building fork content in a
`pull_request_target` run would share the base branch's Actions cache
scope, so a fork could poison main's cache, and it would sit beside the
private engine source. Fork previews come in a follow-up once the build
runs in a no-network sandbox (or the public builder kit ships). That
follow-up will gate on a maintainer stating the exact approved commit,
not on a label, so a push after approval can't slip in.

## One comment, posted only after a deploy

`docs-preview.yaml` is now a reusable workflow (`workflow_call` only).
The deploy workflow's `comment` job calls it after `deploy` succeeds, so
the per-page links never point at a preview that doesn't exist yet. I
chose this over moving the per-page logic into the deploy job: the
existing comment logic and its mapper test stay as they are, and only
the trigger changes.

Each PR gets at most one bot comment. The deploy job posts nothing; it
passes the built commit (`short_sha`) to `docs-preview.yaml`, which adds
a collapsible "Preview sites for each built commit" list to its comment,
newest first, read back from the comment on each run and capped at 100.
That comment is only posted when a non-generated page or an image
changed: a PR that changes only `docs/reference/**` (generated CLI and
API pages), or nothing previewable, gets no comment, and an earlier one
is deleted. Engineers found the earlier two-comment version too noisy.
If the comment is deleted that way and a later push brings it back, its
commit list starts over; the earlier previews still exist until the PR
closes.

## Three jobs, so PR content never runs beside a secret

1. `engine`: checks `DOCS_PREVIEW_DOMAIN` first, reads the docs-engine
commit pinned in `docs/.engine-ref` (from the PR head, falling back to
the base for a branch that predates the pin), mints a short-lived token
from a GitHub App installed only on coder/docs-engine, downscoped to
`contents: read` on that one repository, checks out with
`persist-credentials: false`, and uploads a tarball plus its sha256 as a
job output. Never touches PR content.
2. `build`: no secrets, `contents: read` only. Verifies the engine
tarball against the digest, sparse-checks out the PR's `docs/`, refuses
a `docs/` that is or contains a symbolic link (one could pull runner
files, such as the private engine source, into a public preview), builds
`apps/offlinedocs` with `DOCS_ROOT` as docs-engine's own
`docs-pr-preview.yml` does, and uploads only the static `out/` export.
3. `deploy`: in the `docs-preview` environment with
`VERCEL_PREVIEW_TOKEN`. Verifies the engine tarball against the digest
again (a compromised build job could replace an artifact, but not a job
output), refuses an export holding anything but regular files and
directories, lays the static files out with the engine's `build:output`
script, adds noindex, runs `vercel deploy --prebuilt` with meta `pr` and
`sha`, and sets both aliases through the Vercel API. It holds only
`pull-requests: read`, for the check that the PR head hasn't moved
before re-aliasing; the `comment` job posts the comment. It runs the
engine's trusted script rather than taking `config.json` from the build
job, because a Build Output directory from another job could carry
serverless functions or rewritten routes.

`docs-pr-preview-cleanup.yaml` keeps `pull_request_target: closed` so a
PR can't change its own cleanup; it checks out nothing and runs no PR
content, only Vercel and GitHub API calls. Fork PRs have no previews, so
their close is skipped.

PR-chosen strings never reach a log line unfiltered: the comment
workflow prints changed reference-page paths with every character
outside a path allowlist as `?`, so a file name can't forge a `::` or
`##[` workflow command, and the deploy job's errors name no path.

## Engine pin

`docs/.engine-ref` now pins docs-engine `main` at `eadb477`, the engine
`coder.com/docs` launched with on 2026-10-28 (was `e3c55c2`; an earlier
revision of this PR pinned `5584d67`). The commands the workflow runs
are unchanged between them. `e3c55c2` already had the `build:output`
script, but the newer engine refuses a symlinked docs root, renders
search hits without raw HTML, drops author-chosen heading ids that could
break hydration, and pins Node 22.23.4, which the build and deploy jobs
now use too. The commands the workflow runs (`pnpm turbo run build
--filter=@coder/offlinedocs` and
`apps/offlinedocs/scripts/build-output.mjs`) are unchanged at `5584d67`.

The pin must be a commit on docs-engine `main`. The `engine` job checks
docs-engine out with full history and fails before packing unless `git
merge-base --is-ancestor "$REF" origin/main` passes. Once docs-engine is
public, a commit pushed to any fork of it can be fetched by SHA through
`coder/docs-engine`, so the 40-hex format check alone would let a PR
build with unreviewed engine code. `test-docs-preview-mapper.sh` pins
that the check runs in the `engine` job, before the pack step, against a
full-history checkout.

## Before this can run

Checked on 2026-11-12: IT registered `cdr-preview.dev` in Vercel on team
`team_tGkWfhEGGelkkqUUm9nXq17r`, with Vercel nameservers, so
`*.docs.cdr-preview.dev` needs no DNS or wildcard step. IT also set
`docs.cdr-preview.dev` as the `coder-docs-pr-preview` project's own
preview deployment suffix and removed `coderite.app` from it, so the
project's generated deployment URLs land on the preview domain too, not
on the team's `coderite.app` suffix; the first deploy will confirm that.
The project has Vercel Authentication and password protection off. The
secrets `DOCS_ENGINE_APP_ID` and `DOCS_ENGINE_APP_PRIVATE_KEY` exist.
`DOCS_PREVIEW_DOMAIN` is set to `docs.cdr-preview.dev`. The
`docs-preview` environment exists (created 2026-10-28) with no
protection rules and no branch policy, and holds `VERCEL_PREVIEW_ORG_ID`
and `VERCEL_PREVIEW_PROJECT_ID`, and `VERCEL_PREVIEW_TOKEN` was stored
on 2026-11-12.

- [ ] **GitHub App** (org owner; requested in DOCS-1739). Confirm the
app behind `DOCS_ENGINE_APP_ID` is installed on coder/docs-engine only
("Only select repositories") with Repository permissions Contents:
Read-only and Metadata: Read-only and nothing else, at
https://github.com/organizations/coder/settings/installations. The
workflow also downscopes each token to `contents: read` on docs-engine,
so the installation is the outer bound.
- [x] **Repository variable**, set on 2026-11-12. Set it at repository
level, not on the environment, because the `engine` job and
`docs-preview.yaml` run outside it:
  ```sh
gh variable set DOCS_PREVIEW_DOMAIN -R coder/coder --body
docs.cdr-preview.dev
  ```
- [x] **Vercel Authentication off** on `coder-docs-pr-preview`
(`prj_RiVa1voPHmsfbEXkasFDXmYqAr0L`): checked on 2026-11-12,
`ssoProtection` and `passwordProtection` both report `enabled: false`.
- [x] **Preview domain** (IT): `cdr-preview.dev` registered with Vercel
nameservers, and `docs.cdr-preview.dev` set as the project's preview
deployment suffix.
- [x] **Project-scoped Vercel token**, 90-day expiry (created
2026-11-12, expires about 2027-02-10; a rotation reminder is set for
2027-02-09). The three checks below returned `200`, `404` and `403`.
Create it at https://vercel.com/account/settings/tokens, scoped to the
`coder-docs-pr-preview` project only (`vercel tokens create --project`
is refused for non-owners). Then check, with the new token in
`$VERCEL_PREVIEW_TOKEN`, that it can list the project's deployments
(`200`), reach the team-level alias API (`404`, not `403`), and cannot
see the production `coder-docs` project (`403` or `404`, never `200`):
  ```sh
curl -sS -o /dev/null -w '%{http_code}\n' -H "Authorization: Bearer
$VERCEL_PREVIEW_TOKEN" \

"https://api.vercel.com/v6/deployments?teamId=team_tGkWfhEGGelkkqUUm9nXq17r&projectId=prj_RiVa1voPHmsfbEXkasFDXmYqAr0L&limit=1"
curl -sS -o /dev/null -w '%{http_code}\n' -X DELETE -H "Authorization:
Bearer $VERCEL_PREVIEW_TOKEN" \

"https://api.vercel.com/v2/aliases/pr-0.docs.cdr-preview.dev?teamId=team_tGkWfhEGGelkkqUUm9nXq17r"
curl -sS -o /dev/null -w '%{http_code}\n' -H "Authorization: Bearer
$VERCEL_PREVIEW_TOKEN" \

"https://api.vercel.com/v6/deployments?teamId=team_tGkWfhEGGelkkqUUm9nXq17r&projectId=prj_pC4y7hjUgcsYYNoSDL7pNJnafzh3&limit=1"
  ```
- [x] **`docs-preview` environment**: it exists with no branch policy
(because `pull_request` runs on the merge ref) and its two variables are
set. Only the token is left:
  ```sh
  gh secret set VERCEL_PREVIEW_TOKEN -R coder/coder --env docs-preview
  ```
`gh secret set` prompts for the token so it stays out of shell history.
- [ ] **Security sign-off** (Lisa), on these points:
- **Preview domain.** Previews now live on `docs.cdr-preview.dev`, which
hosts nothing else, so they are no longer same-site with the
login-protected sites on `coderite.app`. Previews are same-site with
each other, so one PR's preview could set a cookie the others receive.
Anything added to `cdr-preview.dev` later should stay outside `docs.`
and not rely on cookies scoped to the parent domain. The docs CSP allows
inline scripts, and any coder/coder writer controls a preview's content.
- Any coder/coder writer can reach `VERCEL_PREVIEW_TOKEN` by editing the
workflow on a branch, as with any secret a `pull_request` deploy uses.
The token is scoped to the one preview project to bound that; required
reviewers on the environment would close it, at the cost of an approval
per preview.
- The engine commit comes from the PR's `docs/.engine-ref`, so the
engine code that lays out the export in the deploy job is PR-chosen, but
only among commits on docs-engine `main`. It runs before, not in, the
step that holds the token.
- [ ] **Mark ready for review** once the items above are done and a docs
push to this PR has produced a working preview.

## Testing

`test-docs-preview-mapper.sh` checks that `docs-preview.yaml` reads the
domain from `vars.DOCS_PREVIEW_DOMAIN`, builds links on
`pr-<n>.<domain>` from the validated value with no `coder.com/docs/@` or
`vercel.app` link, and runs only as a reusable workflow called after
`deploy`. It also checks the per-commit list (this commit first, earlier
ones carried, repeats and other PRs' lines dropped, capped at 100), that
the list round-trips through the comment it renders without touching
checkbox state, and that the deploy job posts no comment and passes its
commit on. All 58 checks pass (run with GNU coreutils, sed, grep and awk
on macOS); the new workflow check failed before the change. `actionlint`
at the repo's pinned commit, with ShellCheck, reports nothing, and so
does the repo's pinned zizmor 1.11.0 (`--strict-collection
--persona=regular`). The noindex step was run against a `config.json`
from docs-engine's real `build-output.mjs`: the route count went from 11
to 12 with the CSP route intact. With the variable, environment and
token in place, the `engine` and `build` jobs pass on this PR, including
the check that the pinned engine commit is on docs-engine `main`. The
first deploy failed at `vercel pull` with "Could not retrieve Project
Settings": Vercel CLI 59.3.0 looks up the token's team (403 for a
project-scoped token) and gives up, although the token reads the project
(200). Run locally with the same token, 65.11.0 skips that lookup and
pulls the settings, so the workflow now pins 65.11.0, the newest release
more than a week old. The deploy, alias and cleanup calls have not
completed in CI yet.

---
🤖 Built with AI assistance.
2026-11-12 14:10:21 -05:00

604 lines
31 KiB
YAML

# Posts a docs-preview comment on docs PRs. For every changed page with a
# docs/manifest.json route it lists a preview link on the PR's own preview
# (pr-<number>.<DOCS_PREVIEW_DOMAIN>) and a Markdown checkbox.
# docs-pr-preview-deploy.yaml calls this workflow only after it has deployed
# and aliased that preview, so the comment never links to a site that does
# not exist yet. Checkbox state round-trips
# across pushes but resets when a page's content changes, so a checked box
# means "reviewed the current revision." Pages with no manifest route
# (docs/.style/** tooling, or not-yet-navigable pages) are dropped, since
# they 404 on the docs site. The comment is updated in place on later
# pushes and deleted when nothing previewable remains.
#
# Generated reference pages (docs/reference/**) are excluded from the
# comment too: a review checkbox for machine-generated CLI/API docs is
# noise. Still logged to the job output, so nothing is silently lost.
#
# Changed images get an informational section (no checkboxes: the image
# changed, the embedding pages did not). Each image lists the manifest-
# routed pages that embed it; mapping needs the referencing Markdown on
# disk, so the job checks out the PR head. An image no navigable page
# embeds (e.g. an icon referenced only from manifest.json) is still listed
# on its own, so an image-only PR always gets a comment.
#
# This is the PR's only preview comment. A collapsible list at its end keeps
# every built commit's own preview, newest first, read back from the comment
# on each run. A PR that changes only generated reference pages, or no page or
# image at all, gets no comment.
name: docs-preview
on:
# Called with the caller's pull_request event, whose docs/** paths filter
# (minus docs/.style/**, contributor tooling that never deploys) applies.
workflow_call:
inputs:
short_sha:
description: The first 9 characters of the deployed commit, whose preview is pr-<number>-<short_sha>.<domain>
type: string
required: true
concurrency:
group: docs-preview-${{ github.event.pull_request.number }}
cancel-in-progress: true
permissions:
contents: read
jobs:
docs-preview:
if: github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
permissions:
# Job-level permissions replace the workflow defaults, so contents:
# read is repeated here for the checkout below.
contents: read
pull-requests: write # needed for commenting on PRs
steps:
# Checked out only to resolve image references (relative Markdown/HTML
# links) to their pages. Pinned to the PR head so refs and
# docs/manifest.json match the file list; sparse to docs/, the only
# tree this job reads.
- name: Check out docs sources
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ github.event.pull_request.head.sha }}
persist-credentials: false
sparse-checkout: docs
- name: Post docs preview comment
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ github.event.pull_request.number }}
REPO: ${{ github.repository }}
DOCS_PREVIEW_DOMAIN: ${{ vars.DOCS_PREVIEW_DOMAIN }}
SHORT_SHA: ${{ inputs.short_sha }}
run: |
set -euo pipefail
if ! [[ "$SHORT_SHA" =~ ^[0-9a-f]{9}$ ]]; then
echo "::error::short_sha must be 9 lowercase hex characters." >&2
exit 1
fi
# A vercel.app name is first-come across every Vercel account, so
# only a Coder-owned domain is accepted.
domain=$(printf '%s' "$DOCS_PREVIEW_DOMAIN" | tr '[:upper:]' '[:lower:]')
if [ -z "$domain" ] || [[ "$domain" =~ (^|\.)vercel\.app\.?$ ]] || ! [[ "$domain" =~ ^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+$ ]]; then
echo "::error::Set the repository variable DOCS_PREVIEW_DOMAIN to a Coder-owned domain (not vercel.app)." >&2
exit 1
fi
# DOCS_PREVIEW_MARKER locates this workflow's own comments;
# STATE_PREFIX carries the last-seen `path -> blob sha` map. Keep
# map_doc_path, the manifest filter, and the carryover logic in
# sync with test-docs-preview-mapper.sh.
DOCS_PREVIEW_MARKER='<!-- docs-preview -->'
STATE_PREFIX='docs-preview-state:'
# Bound the always-rendered image section: at most
# IMAGE_SECTION_BUDGET bytes and IMAGE_PAGES_MAX pages per image,
# with "and N more" notes on overflow. Only matters in pathological
# cases. Keep in sync with test-docs-preview-mapper.sh.
IMAGE_SECTION_BUDGET=20000
IMAGE_PAGES_MAX=25
# Per-commit preview lines kept in the collapsible list.
COMMITS_MAX=100
# The per-commit preview list, newest first: this run's commit, then
# the earlier commits' lines read back from the existing comment,
# minus a repeat of this commit. awk caps it without the SIGPIPE a
# head would raise under pipefail. Keep in sync with
# test-docs-preview-mapper.sh.
merge_commit_lines() {
local body="$1" domain_re
domain_re=${domain//./\\.}
{
# shellcheck disable=SC2016 # backticks are literal Markdown code-span delimiters.
printf -- '- `%s` https://pr-%s-%s.%s\n' "$SHORT_SHA" "$PR_NUMBER" "$SHORT_SHA" "$domain"
printf '%s\n' "$body" | grep -E "^- \`[0-9a-f]{9}\` https://pr-${PR_NUMBER}-[0-9a-f]{9}\.${domain_re}\$" | grep -vF "\`${SHORT_SHA}\`" || true
} | awk -v max="$COMMITS_MAX" 'NR <= max'
}
# Returns IDs of github-actions[bot] comments on the PR whose
# body contains DOCS_PREVIEW_MARKER.
list_docs_preview_comments() {
gh api --paginate \
"repos/${REPO}/issues/${PR_NUMBER}/comments" \
--jq ".[] | select(.user.login == \"github-actions[bot]\") | select(.body | contains(\"${DOCS_PREVIEW_MARKER}\")) | .id"
}
# Deletes the existing comment (existing_id) and exits 0 so no
# stale dead-link remains. A failed delete is cosmetic: log and
# exit clean, next push retries. (The upsert path propagates
# failures instead, to avoid duplicate comments.)
cleanup_stale_and_exit() {
if [ -n "$existing_id" ]; then
if gh api --method DELETE \
"repos/${REPO}/issues/comments/${existing_id}"; then
echo "Deleted stale docs preview comment (id=${existing_id})."
else
echo "Failed to delete stale docs preview comment (id=${existing_id}); leaving in place. This is usually a transient API error, and the next push retries the cleanup." >&2
fi
fi
exit 0
}
# Maps a repo path to the docs site URL path.
# docs/README.md -> "" (docs root)
# docs/<dir>/index.md -> "<dir>" (directory index)
# docs/<dir>/README.md -> "<dir>" (directory index)
# docs/<dir>/<file>.md -> "<dir>/<file>"
map_doc_path() {
local doc_path="$1"
local rel="${doc_path#docs/}"
local page_path
case "$rel" in
README.md)
page_path=""
;;
*)
local base dir stripped
base="$(basename "$rel")"
dir="$(dirname "$rel")"
if [ "$dir" = "." ]; then
dir=""
fi
case "$base" in
index.md | README.md)
page_path="$dir"
;;
*)
stripped="${base%.md}"
if [ -z "$dir" ]; then
page_path="$stripped"
else
page_path="${dir}/${stripped}"
fi
;;
esac
;;
esac
printf '%s' "$page_path"
}
# Page preview URL: branch prefix ($url_prefix) plus the page's
# manifest route. Shared by the checklist and image section.
page_url() {
local filename="$1" page_path url
page_path=$(map_doc_path "$filename")
url="$url_prefix"
if [ -n "$page_path" ]; then
url="${url}/${page_path}"
fi
printf '%s' "$url"
}
# Emits the raw path token of every ![alt](path ...) and
# <img ... src="path"> in a Markdown file, one per line, as written
# (resolve_ref decides which are repo images). `|| true` keeps a
# no-match grep from tripping set -e.
extract_ref_tokens() {
local file="$1"
# Markdown: capture to the first space or ) to drop a title.
grep -oE '!\[[^]]*\]\([^)[:space:]]+' "$file" 2>/dev/null \
| sed -E 's/^!\[[^]]*\]\(//' || true
# HTML: single- or double-quoted src, case-insensitive tag/attr.
grep -oiE '<img[^>]+src=("[^"]+"|'\''[^'\'']+'\'')' "$file" 2>/dev/null \
| sed -E 's/.*src=//I; s/^["'\'']//; s/["'\'']$//' || true
}
# Normalizes a ref from Markdown file $1 to a repo-root-relative
# path (stripping a #fragment, ?query, or <...> wrapper), or
# nothing when external or protocol-relative. realpath -m resolves
# ../ without requiring the target to exist.
resolve_ref() {
local file="$1" ref="$2" dir
ref="${ref%%#*}"
ref="${ref%%\?*}"
ref="${ref#<}"
ref="${ref%>}"
[ -z "$ref" ] && return 0
case "$ref" in
*://* | //* | mailto:* | data:*) return 0 ;;
esac
dir=$(dirname "$file")
realpath -m --relative-to="$PWD" "${PWD}/${dir}/${ref}" 2>/dev/null || true
}
# Pages that embed the given image, one per line. A page matches
# only when one of its refs resolves to exactly this image, so a
# prose basename mention or a same-basename image in another dir
# won't. grep -rlF is a coarse basename pre-filter.
pages_for_image() {
local image="$1" base candidates file token
base=$(basename "$image")
# -e so a basename beginning with '-' is treated as a pattern,
# not as grep options.
candidates=$(grep -rlF -e "$base" docs --include='*.md' 2>/dev/null \
| grep -v '^docs/\.style/' || true)
[ -z "$candidates" ] && return 0
while IFS= read -r file; do
[ -z "$file" ] && continue
while IFS= read -r token; do
[ -z "$token" ] && continue
[ "$(basename "$token")" = "$base" ] || continue
if [ "$(resolve_ref "$file" "$token")" = "$image" ]; then
printf '%s\n' "$file"
break
fi
done < <(extract_ref_tokens "$file")
done <<< "$candidates"
}
# Find the existing comment id once, for both the cleanup and
# upsert paths. The list must propagate a real failure under set -e
# (else the upsert sees "no comment" and posts a duplicate); the
# `|| true` only absorbs head's SIGPIPE.
all_comment_ids=$(list_docs_preview_comments)
existing_id=$(printf '%s\n' "$all_comment_ids" | head -n 1) || true
# Computed early: the reference-page log below needs it too.
url_prefix="https://pr-${PR_NUMBER}.${domain}"
# Fetch the PR's changed files once, then derive the Markdown and
# image sets from the same payload: one read halves the rate-limit
# cost and avoids two paginated reads straddling a synchronize.
# Captured (not piped) so a gh failure propagates under set -e; jq
# `.[]` streams the per-page arrays, so no --slurp. pulls/files
# caps at 3000 files, which docs PRs never approach.
files_json=$(gh api --paginate "repos/${REPO}/pulls/${PR_NUMBER}/files")
# Non-removed docs/*.md. The blob sha lets a later run detect a
# page changed since it was last listed.
changed_tsv=$(printf '%s' "$files_json" \
| jq -r '.[] | select(.status != "removed") | select(.filename | test("^docs/.*\\.md$")) | select((.filename | test("^docs/\\.style/")) | not) | select((.filename | test("^docs/reference/")) | not) | [.filename, .sha] | @tsv')
# Logged instead of listed on the comment; see header note.
reference_tsv=$(printf '%s' "$files_json" \
| jq -r '.[] | select(.status != "removed") | select(.filename | test("^docs/reference/.*\\.md$")) | [.filename, .sha] | @tsv')
if [ -n "$reference_tsv" ]; then
echo "Generated reference page(s) changed (excluded from the docs-preview comment):"
# File names come from the PR, and the runner reads "##[" as a
# workflow command anywhere in a line, so every character
# outside a path allowlist prints as "?".
printf '%s\n' "$reference_tsv" | while IFS=$'\t' read -r filename _; do
[ -z "$filename" ] && continue
printf ' %s -> %s\n' "$filename" "$(page_url "$filename")" | LC_ALL=C tr -c 'A-Za-z0-9._/:>\n -' '?'
done
fi
# Changed docs/ images (outside docs/.style/), extensions matched
# case-insensitively. No sha: the image section is stateless. Keep
# the extension set in sync with test-docs-preview-mapper.sh.
changed_images=$(printf '%s' "$files_json" \
| jq -r '.[] | select(.status != "removed") | select(.filename | test("^docs/.*\\.(png|jpe?g|gif|svg|webp|avif|bmp|ico)$"; "i")) | select((.filename | test("^docs/\\.style/")) | not) | .filename')
# Nothing previewable changed at all: drop any stale comment and
# stop before the manifest read and reference resolution below.
if [ -z "$changed_tsv" ] && [ -z "$changed_images" ]; then
echo "No added/modified Markdown or image files under docs/ (outside docs/.style/ and docs/reference/) on this push."
cleanup_stale_and_exit
fi
# Navigable pages from the PR-head manifest: every object with a
# "path" key, normalized to docs/... to compare against the PR-file
# and resolved-image paths. This makes the manifest a hard
# dependency: rename the "path" key and allowed_paths comes back
# empty, nothing is eligible, and the comment is deleted, so a
# future manifest refactor must keep this extraction in step.
allowed_paths=$(jq -r '[.. | objects | select(has("path")) | .path] | .[]' docs/manifest.json \
| sed -E 's#^\./##; s#^#docs/#')
# Keep only changed pages with a manifest route; the rest 404.
eligible_tsv=$(printf '%s\n' "$changed_tsv" | while IFS=$'\t' read -r filename sha; do
[ -z "$filename" ] && continue
if printf '%s\n' "$allowed_paths" | grep -qxF "$filename"; then
printf '%s\t%s\n' "$filename" "$sha"
fi
done)
# Map each changed image to the manifest-routed pages that embed
# it, one "<image>\t<page>" per line. An image with no such page
# yields no pair but is still listed (link-less) below.
image_pairs_tsv=$(printf '%s\n' "$changed_images" | while IFS= read -r image; do
[ -z "$image" ] && continue
pages_for_image "$image" | while IFS= read -r page; do
[ -z "$page" ] && continue
if printf '%s\n' "$allowed_paths" | grep -qxF "$page"; then
printf '%s\t%s\n' "$image" "$page"
fi
done
done)
# Nothing to preview: no manifest-routed changed page and no
# changed image (a changed image always renders, so only a
# Markdown-only PR with nothing routed lands here). Drop and stop.
if [ -z "$eligible_tsv" ] && [ -z "$changed_images" ]; then
echo "No changed Markdown pages resolve to a docs/manifest.json route, and no images changed under docs/ (outside docs/.style/)."
echo "(If pages you expect are missing, check docs/manifest.json's schema: an empty allowlist looks identical to no eligible pages.)"
cleanup_stale_and_exit
fi
# May be an empty array: an image-only PR has no changed pages,
# in which case the checklist section is omitted and only the
# image section renders.
eligible_json=$(printf '%s\n' "$eligible_tsv" \
| jq -R -s '[splits("\n") | select(length > 0) | split("\t") | {filename: .[0], sha: .[1]}]')
# [{image, pages:[...]}] sorted by image, pages per image deduped
# and sorted. An image with no embedding page keeps pages:[] so it
# still renders link-less (icons live only in manifest icon_path;
# some images are embedded only by non-navigable pages). No changed
# image yields [], so no image section. Keep in sync with
# test-docs-preview-mapper.sh.
image_section_json=$(jq -n \
--argjson images "$(printf '%s\n' "$changed_images" | jq -R -s '[splits("\n") | select(length > 0)]')" \
--argjson pairs "$(printf '%s\n' "$image_pairs_tsv" | jq -R -s '[splits("\n") | select(length > 0) | split("\t") | {image: .[0], page: .[1]}]')" \
'($pairs | group_by(.image) | map({key: .[0].image, value: (map(.page) | unique)}) | from_entries) as $by
| [$images[] | {image: ., pages: ($by[.] // [])}]
| sort_by(.image)')
# Fetch the body right before reading its checkbox state, so a
# reviewer's toggle isn't overwritten by a stale read from several
# API calls earlier. `|| true`: a transient error degrades to
# treating every page as new. A toggle landing between this read
# and the PATCH below is lost but reappears on the next push.
existing_body=""
if [ -n "$existing_id" ]; then
existing_body=$(gh api "repos/${REPO}/issues/comments/${existing_id}" --jq '.body' || true)
if [ -z "$existing_body" ]; then
# A docs-preview comment always has a body, so an empty read
# against a known id is a transient fetch failure. Log the
# resulting checkbox reset so it isn't silent; self-heals next
# push.
echo "Could not read existing comment ${existing_id}; checkbox state resets this push (transient, self-heals)." >&2
fi
fi
commit_lines=$(merge_commit_lines "$existing_body")
commit_count=$(printf '%s\n' "$commit_lines" | grep -c .)
# Recover state from the existing comment, if any:
# - old_state: path -> sha map this workflow wrote last time
# (hidden marker).
# - old_checked: path -> checked map from the live checkbox
# glyphs, where a reviewer's clicks land (GitHub persists a
# toggle as a comment-body edit).
old_state_json="{}"
old_checked_json="{}"
if [ -n "$existing_body" ]; then
old_state_b64=$(printf '%s\n' "$existing_body" | grep -oE "${STATE_PREFIX}[A-Za-z0-9+/=]+" | sed "s/^${STATE_PREFIX}//") || true
if [ -n "$old_state_b64" ]; then
# Guard the decode: a truncated/corrupt marker must degrade to
# "treat every page as new", not kill the run (base64 -d and jq
# run under set -e). The non-empty check matters because on
# jq < 1.7 `jq -e` exits 0 on empty input, so the type check
# alone would accept "" and `--argjson old_state ""` would
# abort. Else keep {}.
decoded=$(printf '%s' "$old_state_b64" | base64 -d 2>/dev/null || true)
if [ -n "$decoded" ] && printf '%s' "$decoded" | jq -e 'type == "object"' >/dev/null 2>&1; then
old_state_json="$decoded"
fi
fi
# shellcheck disable=SC2016 # backticks below are literal Markdown code-span delimiters, not command substitution.
old_checked_json=$(printf '%s\n' "$existing_body" \
| grep -oE '^[[:space:]]*- \[[ xX]\] \[`[^`]+`\]' \
| sed -E 's/^[[:space:]]*- \[([ xX])\] \[`([^`]+)`\]/\1\t\2/' \
| jq -R -s '[splits("\n") | select(length > 0) | split("\t") | {(.[1]): (.[0] | test("x"; "i"))}] | add // {}') || true
fi
# Each page's checked state: carry the live checkbox forward only
# if its blob sha is unchanged since the last state marker. New
# pages, and pages whose sha moved, start unchecked.
final_rows=$(jq -n \
--argjson eligible "$eligible_json" \
--argjson old_state "$old_state_json" \
--argjson old_checked "$old_checked_json" \
'[
$eligible[] | . as $f |
($old_state[$f.filename] // null) as $prev_sha |
(if $prev_sha != null and $prev_sha == $f.sha
then ($old_checked[$f.filename] // false)
else false
end) as $checked |
{filename: $f.filename, sha: $f.sha, checked: $checked}
] | sort_by(.filename)')
total_pages=$(printf '%s' "$final_rows" | jq 'length')
# Render the "Changed images" section from image_section_json: each
# image as a bullet with a nested preview link per embedding page
# (an image with no page renders link-less). Self-limits to a byte
# budget at image granularity (always emits the first image) and to
# IMAGE_PAGES_MAX pages per image, with "and N more" notes on
# overflow. Reads $image_section_json, $url_prefix (via page_url),
# REPO, PR_NUMBER, IMAGE_* caps. Keep in sync with
# test-docs-preview-mapper.sh.
render_image_section() {
local budget="$1" count intro header out="" shown=0 i=0
local image page_count entry j page url dropped candidate
count=$(printf '%s' "$image_section_json" | jq 'length')
if [ "$count" -eq 0 ]; then
return 0
fi
intro="These images changed. Each is listed with the navigable page(s) that embed it, so you can open the preview and see the new image in context. An image with no page listed isn't embedded by any navigable docs page. These links are informational and have no review checkboxes."
header="#### Changed images"$'\n\n'"${intro}"$'\n\n'
while [ "$i" -lt "$count" ]; do
image=$(printf '%s' "$image_section_json" | jq -r --argjson i "$i" '.[$i].image')
page_count=$(printf '%s' "$image_section_json" | jq --argjson i "$i" '.[$i].pages | length')
# The backticks are literal Markdown code-span delimiters.
entry="- \`${image}\`"$'\n'
j=0
while [ "$j" -lt "$page_count" ] && [ "$j" -lt "$IMAGE_PAGES_MAX" ]; do
page=$(printf '%s' "$image_section_json" | jq -r --argjson i "$i" --argjson j "$j" '.[$i].pages[$j]')
url=$(page_url "$page")
entry="${entry} - [\`${page}\`](${url})"$'\n'
j=$((j + 1))
done
if [ "$page_count" -gt "$IMAGE_PAGES_MAX" ]; then
entry="${entry} - _and $((page_count - IMAGE_PAGES_MAX)) more page(s) embedding this image_"$'\n'
fi
# Keep the first image unconditionally (an empty section under a
# header is self-contradicting); measure each later image's
# would-be section and stop before it exceeds budget.
if [ "$shown" -gt 0 ]; then
candidate="${header}${out}${entry}"
if [ "$(printf '%s' "$candidate" | LC_ALL=C wc -c)" -gt "$budget" ]; then
break
fi
fi
out="${out}${entry}"
shown=$((shown + 1))
i=$((i + 1))
done
dropped=$((count - shown))
if [ "$dropped" -gt 0 ]; then
out="${out}"$'\n'"_and ${dropped} more changed image(s) not listed to stay under GitHub's comment size limit. See the [Files tab](https://github.com/${REPO}/pull/${PR_NUMBER}/files) for the full list._"$'\n'
fi
printf '%s%s' "$header" "$out"
}
# Rendered once (independent of page count) so the checklist binary
# search below sizes pages against a body that already includes it.
image_section=$(render_image_section "$IMAGE_SECTION_BUDGET")
# Assemble the body for the first N pages: optional checklist,
# optional image section, hidden base64 state marker. Checklist and
# marker derive from the same N rows, so this prints exactly the
# posted bytes, letting the caller size by measuring not estimating.
# An image-only PR has zero pages: only images render.
build_comment_body() {
local n="$1" rows state_json state_b64 checklist="" intro page_block=""
local filename checked url box omitted
rows=$(printf '%s' "$final_rows" | jq -c --argjson n "$n" '.[:$n]')
state_json=$(printf '%s' "$rows" | jq -c 'map({(.filename): .sha}) | add // {}')
state_b64=$(printf '%s' "$state_json" | base64 -w0)
if [ "$total_pages" -gt 0 ]; then
while IFS=$'\t' read -r filename checked; do
[ -z "$filename" ] && continue
url=$(page_url "$filename")
box=" "
if [ "$checked" = "true" ]; then
box="x"
fi
# The backticks are literal Markdown code-span delimiters.
checklist="${checklist}- [${box}] [\`${filename}\`](${url})"$'\n'
done < <(printf '%s' "$rows" | jq -r '.[] | [.filename, (.checked | tostring)] | @tsv')
omitted=$((total_pages - n))
if [ "$omitted" -gt 0 ]; then
checklist="${checklist}"$'\n'"_and ${omitted} more changed page(s) not listed to stay under GitHub's comment size limit. See the [Files tab](https://github.com/${REPO}/pull/${PR_NUMBER}/files) for the full list._"$'\n'
fi
intro="Check off each page once it's been reviewed. If a page changes in a later push, its checkbox clears automatically so it gets a fresh look. Pages not yet wired into the docs navigation aren't listed here."
page_block="${intro}"$'\n\n'"${checklist}"
fi
# The identity and state markers are always present so the comment
# stays discoverable and round-trippable, even on an image-only PR
# whose state map is {}.
{
printf '## Docs preview\n\n'
# shellcheck disable=SC2016 # backticks are literal Markdown code-span delimiters.
printf 'Links open this pull request'"'"'s preview at %s, which updates with each push.\n\n' "$url_prefix"
if [ -n "$page_block" ]; then
printf '%s\n' "$page_block"
fi
if [ -n "$image_section" ]; then
printf '%s\n' "$image_section"
fi
if [ -n "$commit_lines" ]; then
printf '<details>\n<summary>Preview sites for each built commit (%s)</summary>\n\n' "$commit_count"
printf "Every built commit keeps its own preview, newest first. Previews aren't indexed by search engines and are deleted when this pull request is merged or closed.\n\n"
printf '%s\n\n</details>\n\n' "$commit_lines"
fi
printf '%s\n' "$DOCS_PREVIEW_MARKER"
printf '<!-- %s%s -->' "$STATE_PREFIX" "$state_b64"
}
}
# GitHub caps a comment body at 65536 chars. Per-page cost varies
# with path length, so measure the real body instead of estimating:
# keep every page if all fit, else binary search the largest leading
# prefix under budget (body size grows monotonically with page
# count, so the search is well defined). 65000 leaves headroom.
comment_budget=65000
body_bytes() { LC_ALL=C wc -c; }
if [ "$(build_comment_body "$total_pages" | body_bytes)" -le "$comment_budget" ]; then
keep_pages=$total_pages
else
lo=0
hi=$((total_pages - 1))
keep_pages=0
while [ "$lo" -le "$hi" ]; do
mid=$(((lo + hi) / 2))
if [ "$(build_comment_body "$mid" | body_bytes)" -le "$comment_budget" ]; then
keep_pages=$mid
lo=$((mid + 1))
else
hi=$((mid - 1))
fi
done
fi
# List at least one page when any page changed: the search floors at
# 0 only if a single line exceeds budget (impossible at real path
# lengths), and an empty list under an "and N more" summary is
# self-contradicting. Gated on total_pages so an image-only PR keeps
# its legitimate zero.
if [ "$total_pages" -ge 1 ] && [ "$keep_pages" -lt 1 ]; then
keep_pages=1
fi
omitted_pages=$((total_pages - keep_pages))
echo "Listing ${keep_pages} of ${total_pages} changed page(s); ${omitted_pages} omitted for comment size."
comment_body=$(build_comment_body "$keep_pages")
# Upsert: PATCH the existing comment if found, else create.
# existing_id is re-derived from a live list each run (never
# persisted), so a deleted comment lands in the create branch. A
# PATCH failure against a known id therefore almost always means the
# comment still exists and the error is transient: fail instead of
# creating a duplicate; the next push retries.
if [ -n "$existing_id" ]; then
if gh api --method PATCH \
"repos/${REPO}/issues/comments/${existing_id}" \
--raw-field body="$comment_body"; then
echo "Updated existing docs preview comment (id=${existing_id})."
else
echo "Failed to update docs preview comment ${existing_id}; leaving it in place to avoid a duplicate. This is usually a transient API error, and the next push will retry." >&2
exit 1
fi
else
gh pr comment "${PR_NUMBER}" \
--repo "${REPO}" \
--body "$comment_body"
echo "Created new docs preview comment."
fi