Skip to content

Commit 879d44e

Browse files
authored
chore(ci): add API package sync workflow (#5523)
Adds a scheduled workflow that checks the Management API OpenAPI document hourly and regenerates `@supabase/api` when upstream changes are detected. The workflow keeps the no-change path lightweight by comparing normalized JSON before installing dependencies, then opens an automated sync PR against `develop` when generation produces changes.
1 parent 82e6d06 commit 879d44e

1 file changed

Lines changed: 115 additions & 0 deletions

File tree

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
name: API Package Sync
2+
3+
on:
4+
schedule:
5+
- cron: "17 * * * *"
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: read
10+
11+
jobs:
12+
detect:
13+
name: Detect OpenAPI changes
14+
runs-on: blacksmith-8vcpu-ubuntu-2404
15+
outputs:
16+
has_changes: ${{ steps.compare.outputs.has_changes }}
17+
steps:
18+
- uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
19+
with:
20+
persist-credentials: false
21+
22+
- name: Compare upstream OpenAPI spec
23+
id: compare
24+
shell: bash
25+
run: |
26+
set -euo pipefail
27+
28+
remote_spec="$RUNNER_TEMP/openapi.remote.json"
29+
remote_normalized="$RUNNER_TEMP/openapi.remote.normalized.json"
30+
tracked_normalized="$RUNNER_TEMP/openapi.tracked.normalized.json"
31+
normalize_filter="$RUNNER_TEMP/normalize-openapi.jq"
32+
33+
curl -fsS https://api.supabase.com/api/v1-json -o "$remote_spec"
34+
35+
cat > "$normalize_filter" <<'JQ'
36+
def pointer_path($p): $p | split("/")[1:] | map(gsub("~1"; "/") | gsub("~0"; "~"));
37+
reduce ($overrides[0] // [])[] as $op (.;
38+
if $op.op == "test" then
39+
if getpath(pointer_path($op.path)) == $op.value then
40+
.
41+
else
42+
error("OpenAPI override test failed at \($op.path)")
43+
end
44+
elif $op.op == "replace" then
45+
setpath(pointer_path($op.path); $op.value)
46+
else
47+
error("Unsupported OpenAPI override op \($op.op)")
48+
end
49+
)
50+
JQ
51+
52+
jq -S --slurpfile overrides packages/api/scripts/openapi-overrides.json \
53+
-f "$normalize_filter" "$remote_spec" > "$remote_normalized"
54+
jq -S . packages/api/src/generated/openapi.json > "$tracked_normalized"
55+
56+
if cmp -s "$remote_normalized" "$tracked_normalized"; then
57+
echo "No upstream OpenAPI changes detected."
58+
echo "has_changes=false" >> "$GITHUB_OUTPUT"
59+
else
60+
echo "Upstream OpenAPI changes detected."
61+
echo "has_changes=true" >> "$GITHUB_OUTPUT"
62+
diff -u "$tracked_normalized" "$remote_normalized" | sed -n '1,160p' || true
63+
fi
64+
65+
sync:
66+
name: Sync API package
67+
needs: detect
68+
if: needs.detect.outputs.has_changes == 'true'
69+
runs-on: blacksmith-8vcpu-ubuntu-2404
70+
steps:
71+
- uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
72+
with:
73+
persist-credentials: false
74+
75+
- name: Setup
76+
uses: ./.github/actions/setup
77+
78+
- name: Regenerate API package
79+
run: pnpm generate
80+
working-directory: packages/api
81+
82+
- name: Check for generated changes
83+
id: check
84+
run: |
85+
if git diff --ignore-space-at-eol --exit-code --quiet packages/api/src/generated; then
86+
echo "No generated changes detected."
87+
echo "has_changes=false" >> "$GITHUB_OUTPUT"
88+
else
89+
echo "Generated changes detected."
90+
echo "has_changes=true" >> "$GITHUB_OUTPUT"
91+
fi
92+
93+
- name: Generate token
94+
if: steps.check.outputs.has_changes == 'true'
95+
id: app-token
96+
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
97+
with:
98+
client-id: ${{ vars.GH_APP_CLIENT_ID }}
99+
private-key: ${{ secrets.GH_APP_PRIVATE_KEY }}
100+
permission-pull-requests: write
101+
permission-contents: write
102+
103+
- name: Create Pull Request
104+
if: steps.check.outputs.has_changes == 'true'
105+
uses: peter-evans/create-pull-request@5f6978faf089d4d20b00c7766989d076bb2fc7f1 # v8.1.1
106+
with:
107+
token: ${{ steps.app-token.outputs.token }}
108+
commit-message: "chore(api): sync Management API OpenAPI spec"
109+
title: "chore(api): sync Management API OpenAPI spec"
110+
body: |
111+
This PR was automatically created to sync the generated `@supabase/api` package with the latest Management API OpenAPI document.
112+
113+
Changes were detected in the upstream OpenAPI document exposed by `https://api.supabase.com/api/v1-json`.
114+
branch: sync/api-package
115+
base: develop

0 commit comments

Comments
 (0)