Skip to content

Commit 3f0349e

Browse files
committed
ci: add openapi spec drift check against fixture schema
1 parent 1a6ba20 commit 3f0349e

2 files changed

Lines changed: 103 additions & 0 deletions

File tree

.github/workflows/openapi.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: OpenAPI
2+
3+
on:
4+
workflow_call:
5+
secrets:
6+
CACHIX_AUTH_TOKEN:
7+
required: false
8+
pull_request:
9+
branches:
10+
- main
11+
- v[0-9]+
12+
paths:
13+
- "src/PostgREST/Response/OpenAPI.hs"
14+
- "test/spec/fixtures/**"
15+
- "test/openapi-drift-check.sh"
16+
- ".github/workflows/openapi.yml"
17+
18+
concurrency:
19+
group: openapi-${{ github.head_ref || github.run_id }}
20+
cancel-in-progress: true
21+
22+
jobs:
23+
drift_check:
24+
name: OpenAPI snapshot drift check
25+
runs-on: ubuntu-24.04
26+
defaults:
27+
run:
28+
shell: script -qec "bash --noprofile --norc -eo pipefail {0}"
29+
steps:
30+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
31+
32+
- name: Setup Nix Environment
33+
uses: ./.github/actions/setup-nix
34+
with:
35+
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
36+
tools: withTools.pg-17.bin withTools.withPgrst.bin cabalTools.update.bin
37+
38+
- run: postgrest-cabal-update
39+
40+
- name: Verify OpenAPI snapshot is in sync with fixture schema
41+
run: |
42+
chmod +x test/openapi-drift-check.sh
43+
postgrest-with-pg-17 --fixtures test/spec/fixtures/load.sql \
44+
postgrest-with-pgrst test/openapi-drift-check.sh --check

test/openapi-drift-check.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env bash
2+
# Generates or verifies a snapshot of the OpenAPI (Swagger 2.0) document
3+
# emitted by PostgREST when run against the spec test fixture schema.
4+
#
5+
# Intended invocation (assumes nix devShell with withTools available):
6+
#
7+
# # Regenerate the snapshot (write mode, default):
8+
# postgrest-with-pg-17 --fixtures test/spec/fixtures/load.sql \
9+
# postgrest-with-pgrst test/openapi-drift-check.sh
10+
#
11+
# # Verify the snapshot is in sync (CI mode):
12+
# postgrest-with-pg-17 --fixtures test/spec/fixtures/load.sql \
13+
# postgrest-with-pgrst test/openapi-drift-check.sh --check
14+
#
15+
# The script expects PGRST_SERVER_UNIX_SOCKET to be set, which
16+
# postgrest-with-pgrst provides.
17+
18+
set -euo pipefail
19+
20+
MODE="${1:-write}"
21+
SNAPSHOT="test/spec/fixtures/openapi-snapshot.json"
22+
23+
: "${PGRST_SERVER_UNIX_SOCKET:?PGRST_SERVER_UNIX_SOCKET is required; run this script via postgrest-with-pgrst}"
24+
25+
TMPFILE="$(mktemp)"
26+
trap 'rm -f "$TMPFILE"' EXIT
27+
28+
curl -sSf -H "Accept: application/openapi+json" \
29+
--unix-socket "$PGRST_SERVER_UNIX_SOCKET" \
30+
http://localhost/ \
31+
| jq -S . > "$TMPFILE"
32+
33+
case "$MODE" in
34+
--check)
35+
if ! diff -u "$SNAPSHOT" "$TMPFILE"; then
36+
cat >&2 <<EOF
37+
::error::OpenAPI snapshot is out of date.
38+
39+
To regenerate it, run from inside the project nix devShell:
40+
41+
postgrest-with-pg-17 --fixtures test/spec/fixtures/load.sql \\
42+
postgrest-with-pgrst test/openapi-drift-check.sh
43+
44+
Then commit the updated $SNAPSHOT.
45+
EOF
46+
exit 1
47+
fi
48+
echo "OpenAPI snapshot is up to date."
49+
;;
50+
write|"")
51+
mv "$TMPFILE" "$SNAPSHOT"
52+
trap - EXIT
53+
echo "Wrote $SNAPSHOT"
54+
;;
55+
*)
56+
echo "Unknown mode: $MODE (expected --check or write)" >&2
57+
exit 2
58+
;;
59+
esac

0 commit comments

Comments
 (0)