|
| 1 | +# Cross-stack OpenAPI contract snapshot check. |
| 2 | +# |
| 3 | +# Why this exists: today's prod login broke for ~24h because /auth/exchange |
| 4 | +# (AUTH-004) shipped server-side without the dashboard's client being updated. |
| 5 | +# Per-repo unit tests on both sides stayed green because each repo only saw |
| 6 | +# its own half of the contract. This workflow makes the contract a committed |
| 7 | +# artifact (api/openapi.snapshot.json) that the dashboard + instanode-web |
| 8 | +# repos consume to generate typed clients. A drift here at PR time forces |
| 9 | +# the engineer to address client-side regeneration in the same change. |
| 10 | +# |
| 11 | +# Gate logic (small + cheap, < 30s): |
| 12 | +# 1. Build the openapi-snapshot tool. |
| 13 | +# 2. Regenerate openapi.snapshot.json from internal/handlers/openapi.go. |
| 14 | +# 3. Diff against the committed file. If different → fail with the exact |
| 15 | +# `make openapi-snapshot` command the engineer must run. |
| 16 | +# |
| 17 | +# Observability (rule 25): a single structured log line per run so we can |
| 18 | +# query CI artifacts for the rate at which contract drift is being caught: |
| 19 | +# {"event":"cross_stack_contract_drift","detected":true|false,"repo":"api"} |
| 20 | +# Captured by the GitHub Actions log forwarder into NR (instanode-reliability |
| 21 | +# dashboard tile: "Contract drift caught at PR time, 30d"). |
| 22 | + |
| 23 | +name: openapi-snapshot |
| 24 | + |
| 25 | +on: |
| 26 | + push: |
| 27 | + branches: [master] |
| 28 | + paths: |
| 29 | + - 'internal/handlers/openapi.go' |
| 30 | + - 'cmd/openapi-snapshot/**' |
| 31 | + - 'openapi.snapshot.json' |
| 32 | + - '.github/workflows/openapi-snapshot.yml' |
| 33 | + pull_request: |
| 34 | + branches: [master] |
| 35 | + paths: |
| 36 | + - 'internal/handlers/openapi.go' |
| 37 | + - 'cmd/openapi-snapshot/**' |
| 38 | + - 'openapi.snapshot.json' |
| 39 | + - '.github/workflows/openapi-snapshot.yml' |
| 40 | + workflow_dispatch: |
| 41 | + |
| 42 | +concurrency: |
| 43 | + group: openapi-snapshot-${{ github.workflow }}-${{ github.ref }} |
| 44 | + cancel-in-progress: true |
| 45 | + |
| 46 | +jobs: |
| 47 | + snapshot-drift: |
| 48 | + runs-on: ubuntu-latest |
| 49 | + steps: |
| 50 | + - uses: actions/checkout@v6 |
| 51 | + |
| 52 | + - name: Checkout proto sibling (for go.mod replace ../proto) |
| 53 | + uses: actions/checkout@v6 |
| 54 | + with: |
| 55 | + repository: ${{ vars.PROTO_REPO || format('{0}/proto', github.repository_owner) }} |
| 56 | + token: ${{ secrets.REPO_ACCESS_TOKEN || secrets.GITHUB_TOKEN }} |
| 57 | + path: _proto_ci |
| 58 | + |
| 59 | + - name: Place ../proto for Go replace directive |
| 60 | + run: mv _proto_ci ../proto |
| 61 | + |
| 62 | + - name: Checkout common sibling (for go.mod replace ../common) |
| 63 | + uses: actions/checkout@v6 |
| 64 | + with: |
| 65 | + repository: ${{ vars.COMMON_REPO || format('{0}/common', github.repository_owner) }} |
| 66 | + token: ${{ secrets.REPO_ACCESS_TOKEN || secrets.GITHUB_TOKEN }} |
| 67 | + path: _common_ci |
| 68 | + |
| 69 | + - name: Place ../common for Go replace directive |
| 70 | + run: mv _common_ci ../common |
| 71 | + |
| 72 | + - uses: actions/setup-go@v6 |
| 73 | + with: |
| 74 | + go-version: '1.25' |
| 75 | + |
| 76 | + - name: Regenerate openapi.snapshot.json |
| 77 | + id: regen |
| 78 | + run: | |
| 79 | + go run ./cmd/openapi-snapshot/ -out /tmp/openapi.snapshot.regenerated.json |
| 80 | + if diff -q openapi.snapshot.json /tmp/openapi.snapshot.regenerated.json >/dev/null; then |
| 81 | + echo "drift=false" >> "$GITHUB_OUTPUT" |
| 82 | + else |
| 83 | + echo "drift=true" >> "$GITHUB_OUTPUT" |
| 84 | + fi |
| 85 | +
|
| 86 | + - name: Emit observability line (rule 25) |
| 87 | + # Single structured line so NR log forwarder can chart drift rate. |
| 88 | + # Runs regardless of drift status so the absence of drift is also |
| 89 | + # a data point ("we ran, we passed"). |
| 90 | + run: | |
| 91 | + printf '{"event":"cross_stack_contract_drift","detected":%s,"repo":"api","pr":"%s","sha":"%s"}\n' \ |
| 92 | + "${{ steps.regen.outputs.drift }}" \ |
| 93 | + "${{ github.event.pull_request.number || 'none' }}" \ |
| 94 | + "${GITHUB_SHA:0:7}" |
| 95 | +
|
| 96 | + - name: Fail if snapshot is out of date |
| 97 | + if: steps.regen.outputs.drift == 'true' |
| 98 | + run: | |
| 99 | + echo "::error::openapi.snapshot.json is out of date." |
| 100 | + echo "::error::An edit to internal/handlers/openapi.go changed the production OpenAPI surface but the snapshot was not regenerated." |
| 101 | + echo "::error::Run \`make openapi-snapshot\` and commit the updated file in this PR." |
| 102 | + echo "::error::This is rule 22 (contract surface checklist): dashboard + instanode-web depend on this snapshot to generate typed clients." |
| 103 | + echo "" |
| 104 | + echo "Drift (committed → regenerated, first 200 lines):" |
| 105 | + diff -u openapi.snapshot.json /tmp/openapi.snapshot.regenerated.json | head -200 || true |
| 106 | + exit 1 |
| 107 | +
|
| 108 | + - name: Snapshot is current |
| 109 | + if: steps.regen.outputs.drift == 'false' |
| 110 | + run: echo "openapi.snapshot.json matches handlers.OpenAPISpecProduction() — dashboards can regenerate clients deterministically." |
0 commit comments