|
| 1 | +#!/usr/bin/env node |
| 2 | +// Enforces the OpenAPI spec-versioning policy (CONTRIBUTING.md, issue #233): |
| 3 | +// fails CI when public/openapi.json's paths or response schemas change without |
| 4 | +// an info.version bump, and when a version bump lacks a CHANGELOG.md entry. |
| 5 | +// Compares info only by version — the build stamps info["x-generated-at"] / |
| 6 | +// info["x-commit-sha"], which must not count as a semantic change. |
| 7 | +// |
| 8 | +// Usage: node scripts/check-openapi-version.mjs <base-spec.json> <head-spec.json> |
| 9 | + |
| 10 | +import { readFileSync, existsSync } from 'node:fs' |
| 11 | + |
| 12 | +const [, , basePath, headPath] = process.argv |
| 13 | +if (!basePath || !headPath) { |
| 14 | + console.error('usage: check-openapi-version.mjs <base-spec> <head-spec>') |
| 15 | + process.exit(2) |
| 16 | +} |
| 17 | + |
| 18 | +function load (p) { |
| 19 | + try { |
| 20 | + const raw = readFileSync(p, 'utf8').trim() |
| 21 | + return raw ? JSON.parse(raw) : null |
| 22 | + } catch (err) { |
| 23 | + console.error(`[openapi-version] cannot parse ${p}: ${err.message}`) |
| 24 | + process.exit(2) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// Stable stringify: recursively sort object keys so key-order churn isn't |
| 29 | +// mistaken for a semantic change. |
| 30 | +function canonical (value) { |
| 31 | + if (Array.isArray(value)) return value.map(canonical) |
| 32 | + if (value && typeof value === 'object') { |
| 33 | + return Object.keys(value).sort().reduce((acc, k) => { |
| 34 | + acc[k] = canonical(value[k]) |
| 35 | + return acc |
| 36 | + }, {}) |
| 37 | + } |
| 38 | + return value |
| 39 | +} |
| 40 | + |
| 41 | +function semantic (spec) { |
| 42 | + return JSON.stringify({ |
| 43 | + paths: canonical(spec?.paths ?? {}), |
| 44 | + schemas: canonical(spec?.components?.schemas ?? {}) |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +const base = load(basePath) |
| 49 | +const head = load(headPath) |
| 50 | + |
| 51 | +if (!head) { |
| 52 | + console.error('[openapi-version] head spec missing or empty') |
| 53 | + process.exit(2) |
| 54 | +} |
| 55 | + |
| 56 | +// New spec (no base on the target branch) — nothing to compare against. |
| 57 | +if (!base || Object.keys(base).length === 0) { |
| 58 | + console.log('[openapi-version] no base spec to compare; skipping.') |
| 59 | + process.exit(0) |
| 60 | +} |
| 61 | + |
| 62 | +const baseVersion = base?.info?.version |
| 63 | +const headVersion = head?.info?.version |
| 64 | + |
| 65 | +if (semantic(base) === semantic(head)) { |
| 66 | + console.log('[openapi-version] no path/schema changes detected; OK.') |
| 67 | + process.exit(0) |
| 68 | +} |
| 69 | + |
| 70 | +if (baseVersion === headVersion) { |
| 71 | + console.error( |
| 72 | + `[openapi-version] paths or schemas changed but info.version is still ${headVersion}.\n` + |
| 73 | + ' Bump info.version per the SemVer policy in CONTRIBUTING.md:\n' + |
| 74 | + ' MAJOR (x.0.0) backward-incompatible redesign / removed-renamed field\n' + |
| 75 | + ' MINOR (2.x.0) new path or field, or a shape fix aligning the spec to the live response\n' + |
| 76 | + ' PATCH (2.1.x) description-only edits\n' + |
| 77 | + ' Removed paths and renamed fields bump the MINOR at minimum.' |
| 78 | + ) |
| 79 | + process.exit(1) |
| 80 | +} |
| 81 | + |
| 82 | +// Version bumped — require a CHANGELOG.md entry for the new version. |
| 83 | +const CHANGELOG = 'CHANGELOG.md' |
| 84 | +if (existsSync(CHANGELOG)) { |
| 85 | + const log = readFileSync(CHANGELOG, 'utf8') |
| 86 | + const escaped = headVersion.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') |
| 87 | + const hasEntry = new RegExp(`^##\\s.*${escaped}(\\s|$)`, 'm').test(log) |
| 88 | + if (!hasEntry) { |
| 89 | + console.error( |
| 90 | + `[openapi-version] info.version bumped ${baseVersion} -> ${headVersion} but ` + |
| 91 | + `CHANGELOG.md has no "## ... ${headVersion}" entry. Add one describing the change.` |
| 92 | + ) |
| 93 | + process.exit(1) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +console.log(`[openapi-version] info.version ${baseVersion} -> ${headVersion}; OK.`) |
| 98 | +process.exit(0) |
0 commit comments