Files
nmon/scripts/openapi-check.sh
jleung f85c3ef781
Some checks failed
ci / lint (push) Has been cancelled
ci / test (ubuntu-24.04-arm) (push) Has been cancelled
ci / test (ubuntu-latest) (push) Has been cancelled
ci / postgres (push) Has been cancelled
ci / openapi (push) Has been cancelled
M3: Go API per UI contract, embedded UI, openapi compat check, nmon-devtools, live gate + screenshots; M4 WP1 reachability and dependency suppression
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Lx9FNLG2CqeBbVGGd9bFsP
2026-10-22 20:34:14 +00:00

43 lines
1.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# OpenAPI compatibility check (M3): fails when the current spec is not backwards
# compatible with the one at the last m*/v* tag: removed route or method, removed
# response field, changed field type, new required request param/field, removed
# enum value. Tags that predate openapi.json pass with a note.
#
# scripts/openapi-check.sh spec from `nmon openapi`, else ./openapi.json
# OPENAPI_NEW=file.json scripts/openapi-check.sh compare this file instead
# OPENAPI_BASE_TAG=m2 ... compare against this tag instead of the latest
# Needs: go, git.
set -uo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT"
TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT
NEW="${OPENAPI_NEW:-}"
if [ -z "$NEW" ]; then
if go run ./cmd/nmon openapi >"$TMP/new.json" 2>"$TMP/gen.err" && [ -s "$TMP/new.json" ]; then
NEW="$TMP/new.json"
elif [ -f openapi.json ]; then
echo "note: 'nmon openapi' unavailable; using openapi.json"
NEW=openapi.json
else
echo "FAIL: no openapi spec (nmon openapi failed and openapi.json is missing)"; cat "$TMP/gen.err"; exit 1
fi
fi
# Latest m*/v* tag by version order (m2 < m10 < v1.0.0 is not assumed; the newest by tag date wins).
TAG="${OPENAPI_BASE_TAG:-$(git tag --list 'm*' 'v*' --sort=-creatordate 2>/dev/null | head -n 1)}"
if [ -z "$TAG" ]; then
echo "openapi-check: PASS (no m*/v* tag to compare against)"; exit 0
fi
if ! git show "$TAG:openapi.json" >"$TMP/old.json" 2>/dev/null; then
echo "openapi-check: PASS (note: $TAG has no openapi.json yet, nothing to compare against)"; exit 0
fi
if go run ./internal/openapidiff/cmd/openapidiff "$TMP/old.json" "$NEW"; then
echo "openapi-check: PASS (compatible with $TAG)"
else
echo "openapi-check: FAIL (incompatible with $TAG)"; exit 1
fi