Bash · 2006 bytes Raw Blame History
1 #!/usr/bin/env bash
2 # SPDX-License-Identifier: AGPL-3.0-or-later
3 #
4 # Verify that every /api/v1 route registered in
5 # internal/web/handlers/api/ appears in docs/public/api/.
6 #
7 # We don't enforce on a per-page basis — the route just needs to
8 # show up *somewhere* under docs/public/api/. The page-organization
9 # is the human's call; the script catches the most common drift
10 # bug (a new endpoint shipped, no doc updated).
11 #
12 # Excluded by design: planned-only endpoints in the docs that
13 # the source doesn't yet implement (routes mentioned in tables
14 # under "## Planned routes" headings). The verifier is route →
15 # doc, not doc → route.
16
17 set -euo pipefail
18
19 ROOT="$(cd "$(dirname "$0")/.." && pwd)"
20 cd "$ROOT"
21
22 # Collect every /api/v1 route literal from chi route registrations.
23 # Pattern: r.<Method>("/api/v1/<path>", ...)
24 ROUTES_FILE="$(mktemp)"
25 trap 'rm -f "$ROUTES_FILE"' EXIT
26
27 grep -rhoE 'r\.(Get|Post|Put|Patch|Delete|Head|Options)\("/api/v1[^"]*"' \
28 internal/web/handlers/api 2>/dev/null \
29 | sed -E 's/^r\.[A-Z][a-z]+\("([^"]+)".*$/\1/' \
30 | sort -u > "$ROUTES_FILE"
31
32 route_count=$(wc -l < "$ROUTES_FILE" | tr -d ' ')
33 if [[ "$route_count" -eq 0 ]]; then
34 echo "verify-api-docs: no API routes found — is the package layout right?" >&2
35 exit 2
36 fi
37
38 # Concatenate every doc page into one searchable blob.
39 DOC_BLOB="$(cat docs/public/api/*.md 2>/dev/null || true)"
40 if [[ -z "$DOC_BLOB" ]]; then
41 echo "verify-api-docs: docs/public/api/ is empty" >&2
42 exit 2
43 fi
44
45 missing_count=0
46 while IFS= read -r route; do
47 if ! grep -qF "$route" <<<"$DOC_BLOB"; then
48 if [[ "$missing_count" -eq 0 ]]; then
49 echo "verify-api-docs: the following routes are not documented under docs/public/api/:" >&2
50 fi
51 echo " $route" >&2
52 missing_count=$((missing_count + 1))
53 fi
54 done < "$ROUTES_FILE"
55
56 if [[ "$missing_count" -gt 0 ]]; then
57 echo "" >&2
58 echo "Add a section that mentions the route literal verbatim." >&2
59 exit 1
60 fi
61
62 echo "verify-api-docs: all ${route_count} routes documented"