Sync SDK #1401
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Sync SDK | |
on: | |
push: | |
branches: | |
- main | |
paths: | |
- '.github/workflows/generate.yml' | |
- 'openapi.json' | |
- 'scripts/generate-types.mjs' | |
- 'scripts/prettify-base-json.mjs' | |
schedule: | |
# At 07:23 on every day-of-week from Monday through Friday. | |
- cron: '23 7 * * 1-5' | |
workflow_dispatch: | |
inputs: | |
force: | |
description: 'Force regeneration even if no changes detected' | |
required: false | |
default: false | |
type: boolean | |
permissions: | |
contents: write | |
pull-requests: write | |
jobs: | |
fetch_and_update: | |
name: Sync OpenAPI definition | |
runs-on: ubuntu-latest | |
outputs: | |
has_changes: ${{ steps.check.outputs.has_changes }} | |
steps: | |
- name: Random delay | |
if: github.event_name == 'schedule' | |
run: | | |
# Add random delay between 0-10 minutes for scheduled runs | |
delay=$((RANDOM % 600)) | |
echo "Sleeping for $delay seconds..." | |
sleep $delay | |
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
- uses: SocketDev/socket-registry/.github/actions/setup-and-install@a912e5bd8ec469d2ee13abf592a6b2e5898c006c # main | |
- name: Fetch latest OpenAPI definition | |
id: fetch | |
run: | | |
echo "Fetching latest OpenAPI definition..." | |
curl -sSL https://api.socket.dev/v0/openapi -o openapi-new.json | |
# Check if file changed | |
if [ -f openapi.json ]; then | |
if diff -q openapi.json openapi-new.json > /dev/null; then | |
echo "No changes detected" | |
echo "changed=false" >> $GITHUB_OUTPUT | |
else | |
echo "Changes detected" | |
echo "changed=true" >> $GITHUB_OUTPUT | |
mv openapi-new.json openapi.json | |
fi | |
else | |
echo "OpenAPI file doesn't exist, creating new one" | |
echo "changed=true" >> $GITHUB_OUTPUT | |
mv openapi-new.json openapi.json | |
fi | |
- name: Run post-sync script | |
if: steps.fetch.outputs.changed == 'true' || github.event.inputs.force == 'true' | |
run: pnpm run generate-sdk | |
- name: Configure git | |
if: steps.fetch.outputs.changed == 'true' || github.event.inputs.force == 'true' | |
run: | | |
git config --global user.name "github-actions[bot]" | |
git config --global user.email "github-actions[bot]@users.noreply.github.com" | |
- name: Check for changes | |
id: check | |
if: steps.fetch.outputs.changed == 'true' || github.event.inputs.force == 'true' | |
run: | | |
if [ -n "$(git status --porcelain)" ]; then | |
echo "has_changes=true" >> $GITHUB_OUTPUT | |
else | |
echo "has_changes=false" >> $GITHUB_OUTPUT | |
fi | |
- name: Commit and push changes | |
if: steps.check.outputs.has_changes == 'true' | |
run: | | |
git checkout -b automated/open-api | |
git add . | |
git commit -m "fix(openapi): sync with openapi definition" | |
git push origin automated/open-api -fu --no-verify | |
- name: Create Pull Request | |
if: steps.check.outputs.has_changes == 'true' | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
# Check if PR already exists | |
existing_pr=$(gh pr list --head automated/open-api --json number --jq '.[0].number' || echo "") | |
if [ -z "$existing_pr" ]; then | |
gh pr create \ | |
--head automated/open-api \ | |
--base main \ | |
--title "Sync with OpenAPI definition" \ | |
--body "## 🔄 OpenAPI Sync | |
The OpenAPI definition in the API has been updated. This PR automatically: | |
- Downloads the latest OpenAPI specification | |
- Regenerates TypeScript types | |
- Updates SDK method signatures if needed | |
### What's Changed | |
See the file changes below for specific updates to the API types and methods. | |
⚠️ **Please review carefully for any breaking changes in the API.**" \ | |
--label "dependencies" \ | |
--label "automated" | |
else | |
echo "PR #$existing_pr already exists, skipping creation" | |
fi | |
validate: | |
name: Validate generated SDK | |
needs: fetch_and_update | |
if: needs.fetch_and_update.outputs.has_changes == 'true' | |
runs-on: ubuntu-latest | |
outputs: | |
has_changes: ${{ steps.check.outputs.has_changes }} | |
steps: | |
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 | |
with: | |
ref: automated/open-api | |
- uses: SocketDev/socket-registry/.github/actions/setup-and-install@a912e5bd8ec469d2ee13abf592a6b2e5898c006c # main | |
- name: Build SDK | |
run: pnpm run build | |
- name: Type check | |
run: pnpm run type-ci | |
- name: Lint | |
run: pnpm run lint-ci | |
- name: Test | |
run: pnpm run test-ci | |
- name: Check for API breaking changes | |
run: | | |
if git diff origin/main...HEAD --name-only | grep -q 'types/api.d.ts'; then | |
echo "⚠️ API types have changed. Please review for breaking changes." | |
git diff origin/main...HEAD types/api.d.ts | head -100 | |
fi |