Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 206 additions & 0 deletions .github/workflows/regenerate-sdk.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
name: Regenerate SDK

# Reusable workflow called by each SDK repo when a new OAS version is released.
# Each SDK repo has a thin receiver workflow that calls this with its language.

on:
workflow_call:
inputs:
oas-version:
description: 'The @shotstack/schemas version to generate from'
required: true
type: string
language:
description: 'Target language: node, php, python, ruby'
required: true
type: string
sdk-version:
description: 'SDK version to set (defaults to oas-version)'
required: false
type: string
default: ''
secrets:
PR_TOKEN:
description: 'Token for creating PRs (must be PAT or App token to trigger CI)'
required: true

jobs:
regenerate:
name: Generate ${{ inputs.language }} SDK
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write

steps:
- name: Checkout SDK repo
uses: actions/checkout@v4
with:
fetch-depth: 1

- name: Checkout OAS repo
uses: actions/checkout@v4
with:
repository: shotstack/oas-api-definition
path: _oas
ref: main

# ---------- Setup tools ----------

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '22'

- name: Setup pnpm (for OAS repo)
uses: pnpm/action-setup@v4
with:
version: 9
run_install: false

- name: Install OAS dependencies & bundle spec
run: |
cd _oas
pnpm install --frozen-lockfile
bash scripts/sdk/bundle-spec.sh "${{ github.workspace }}/_spec.json"

- name: Setup Java (for openapi-generator)
if: inputs.language != 'node'
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'

- name: Cache openapi-generator
if: inputs.language != 'node'
uses: actions/cache@v4
with:
path: ~/.openapi-generator
key: openapi-generator-${{ inputs.language == 'python' && '5.4.0' || '7.4.0' }}

- name: Install openapi-generator-cli
if: inputs.language != 'node'
run: |
npm install -g @openapitools/openapi-generator-cli
# Set generator version based on language
# Python uses v5.4.0 due to oneOf/anyOf discriminator bugs in v7.x
if [ "${{ inputs.language }}" = "python" ]; then
npx @openapitools/openapi-generator-cli version-manager set 5.4.0
else
npx @openapitools/openapi-generator-cli version-manager set 7.4.0
fi

- name: Setup PHP
if: inputs.language == 'php'
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'

- name: Setup Python
if: inputs.language == 'python'
uses: actions/setup-python@v5
with:
python-version: '3.12'

- name: Setup Ruby
if: inputs.language == 'ruby'
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.3'

# ---------- Generate ----------

- name: Determine SDK version
id: version
run: |
VERSION="${{ inputs.sdk-version }}"
if [ -z "$VERSION" ]; then
VERSION="${{ inputs.oas-version }}"
fi
echo "sdk_version=${VERSION}" >> $GITHUB_OUTPUT

- name: Generate SDK
run: |
bash _oas/scripts/sdk/generate-${{ inputs.language }}.sh \
"${{ github.workspace }}/_spec.json" \
"${{ github.workspace }}/_generated" \
"${{ steps.version.outputs.sdk_version }}"

- name: Copy generated files to SDK repo
run: |
# Remove previously generated files (but not .git, README, .github, etc.)
if [ "${{ inputs.language }}" = "node" ]; then
rm -rf src/generated/ dist/
mkdir -p src/generated/
cp -r _generated/src/generated/* src/generated/
# Update package.json version only (preserve custom fields)
if [ -f _generated/package.json ]; then
node -e "
const pkg = JSON.parse(require('fs').readFileSync('package.json', 'utf-8'));
const gen = JSON.parse(require('fs').readFileSync('_generated/package.json', 'utf-8'));
pkg.version = gen.version;
require('fs').writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n');
"
fi
elif [ "${{ inputs.language }}" = "php" ]; then
rm -rf src/Api/ src/Model/ src/ApiException.php src/Configuration.php src/HeaderSelector.php src/ObjectSerializer.php
cp -r _generated/src/* src/ 2>/dev/null || true
cp -r _generated/lib/* src/ 2>/dev/null || true
elif [ "${{ inputs.language }}" = "python" ]; then
rm -rf shotstack_sdk/api/ shotstack_sdk/model/ shotstack_sdk/models/
cp -r _generated/shotstack_sdk/* shotstack_sdk/ 2>/dev/null || true
elif [ "${{ inputs.language }}" = "ruby" ]; then
rm -rf lib/shotstack/api/ lib/shotstack/models/
cp -r _generated/lib/shotstack/* lib/shotstack/ 2>/dev/null || true
fi

- name: Run post-generate script
if: hashFiles('scripts/post-generate.sh') != ''
run: bash scripts/post-generate.sh

# ---------- Test ----------

- name: Smoke test
run: bash _oas/scripts/sdk/smoke-test.sh "${{ inputs.language }}" "${{ github.workspace }}"

- name: Conformance check
run: |
node _oas/scripts/sdk/conformance-check.mjs \
"${{ github.workspace }}/_spec.json" \
"${{ inputs.language }}" \
"${{ github.workspace }}"

# ---------- Version tracking ----------

- name: Write .oas-version
run: echo "${{ inputs.oas-version }}" > .oas-version

# ---------- Create PR ----------

- name: Clean up temporary files
run: rm -rf _oas _generated _spec.json

- name: Create Pull Request
uses: peter-evans/create-pull-request@v8
with:
token: ${{ secrets.PR_TOKEN }}
branch: auto/oas-v${{ inputs.oas-version }}
title: "chore: regenerate SDK from @shotstack/schemas v${{ inputs.oas-version }}"
body: |
## Automated SDK Regeneration

Regenerated **${{ inputs.language }}** SDK from `@shotstack/schemas` v${{ inputs.oas-version }}.

### What changed
- Updated generated models and API clients to match OAS spec v${{ inputs.oas-version }}
- SDK version: ${{ steps.version.outputs.sdk_version }}

### Checks
- [x] Smoke tests (compile/import) passed
- [x] Spec conformance check passed

---
*Automated by [regenerate-sdk workflow](https://github.com/shotstack/oas-api-definition/blob/main/.github/workflows/regenerate-sdk.yml)*
labels: automated,sdk-regeneration
commit-message: "chore: regenerate SDK from @shotstack/schemas v${{ inputs.oas-version }}"
delete-branch: true
37 changes: 36 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,42 @@ jobs:
run: pnpm test:smoke

- name: Release
id: release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_CONFIG_PROVENANCE: true
run: pnpm exec semantic-release
run: |
pnpm exec semantic-release
# Capture the released version from package.json (semantic-release updates it)
echo "version=$(node -p 'require("./package.json").version')" >> $GITHUB_OUTPUT

- name: Dispatch SDK regeneration
if: steps.release.outcome == 'success'
env:
DISPATCH_TOKEN: ${{ secrets.SDK_DISPATCH_TOKEN }}
run: |
VERSION="${{ steps.release.outputs.version }}"
REPOS=("shotstack-sdk-node" "shotstack-sdk-php" "shotstack-sdk-python" "shotstack-sdk-ruby")

for repo in "${REPOS[@]}"; do
success=false
for attempt in 1 2 3; do
status=$(curl -s -o /dev/null -w "%{http_code}" -X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${DISPATCH_TOKEN}" \
"https://api.github.com/repos/shotstack/${repo}/dispatches" \
-d "{\"event_type\":\"oas-release\",\"client_payload\":{\"version\":\"${VERSION}\"}}")

if [ "$status" = "204" ]; then
echo "✓ Dispatched to ${repo} (attempt ${attempt})"
success=true
break
fi
echo "✗ ${repo} returned ${status}, retrying in 5s (attempt ${attempt}/3)..."
sleep 5
done

if [ "$success" = "false" ]; then
echo "::error::Failed to dispatch to ${repo} after 3 attempts"
fi
done
96 changes: 96 additions & 0 deletions .github/workflows/sdk-conformance-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
name: SDK Conformance Check

# Weekly check that all SDK repos are generated from the latest OAS version.
# Alerts via GitHub issue if any SDK repo has a stale .oas-version.

on:
schedule:
- cron: '0 9 * * 1' # Every Monday at 9:00 UTC
workflow_dispatch: {}

jobs:
check:
name: Check SDK versions
runs-on: ubuntu-latest
steps:
- name: Get latest OAS version
id: oas
run: |
VERSION=$(npm view @shotstack/schemas version 2>/dev/null || echo "unknown")
echo "version=${VERSION}" >> $GITHUB_OUTPUT
echo "Latest @shotstack/schemas: ${VERSION}"

- name: Check SDK repos
id: check
env:
GH_TOKEN: ${{ secrets.SDK_DISPATCH_TOKEN }}
run: |
OAS_VERSION="${{ steps.oas.outputs.version }}"
REPOS=("shotstack-sdk-node" "shotstack-sdk-php" "shotstack-sdk-python" "shotstack-sdk-ruby")
stale=""

for repo in "${REPOS[@]}"; do
sdk_version=$(curl -s \
-H "Authorization: Bearer ${GH_TOKEN}" \
-H "Accept: application/vnd.github.raw" \
"https://api.github.com/repos/shotstack/${repo}/contents/.oas-version" 2>/dev/null | tr -d '[:space:]')

if [ -z "$sdk_version" ] || [ "$sdk_version" = "404: Not Found" ]; then
sdk_version="not found"
fi

if [ "$sdk_version" != "$OAS_VERSION" ]; then
echo "✗ ${repo}: ${sdk_version} (expected ${OAS_VERSION})"
stale="${stale}| ${repo} | ${sdk_version} | ${OAS_VERSION} |\n"
else
echo "✓ ${repo}: ${sdk_version}"
fi
done

if [ -n "$stale" ]; then
echo "has_stale=true" >> $GITHUB_OUTPUT
# Write the table for the issue body
{
echo "stale_table<<EOF"
echo -e "$stale"
echo "EOF"
} >> $GITHUB_OUTPUT
else
echo "has_stale=false" >> $GITHUB_OUTPUT
fi

- name: Create issue if SDKs are stale
if: steps.check.outputs.has_stale == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Check if an issue already exists
existing=$(gh issue list --repo shotstack/oas-api-definition \
--label "sdk-drift" --state open --json number --jq '.[0].number' 2>/dev/null || echo "")

BODY="## SDK Version Drift Detected

The following SDK repos are not on the latest \`@shotstack/schemas\` version:

| SDK Repo | Current Version | Expected Version |
|----------|----------------|-----------------|
${{ steps.check.outputs.stale_table }}

### Resolution
Either trigger regeneration manually via \`workflow_dispatch\` on the affected repo, or investigate why the automated \`repository_dispatch\` didn't fire.

*This issue is auto-created by the [SDK Conformance Check](https://github.com/shotstack/oas-api-definition/actions/workflows/sdk-conformance-check.yml) workflow.*"

if [ -n "$existing" ]; then
gh issue comment "$existing" \
--repo shotstack/oas-api-definition \
--body "$BODY"
echo "Updated existing issue #${existing}"
else
gh issue create \
--repo shotstack/oas-api-definition \
--title "SDK drift: some repos are behind @shotstack/schemas v${{ steps.oas.outputs.version }}" \
--body "$BODY" \
--label "sdk-drift"
echo "Created new drift issue"
fi
24 changes: 24 additions & 0 deletions scripts/sdk/bundle-spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
set -euo pipefail

# Bundle the OAS YAML spec into a single JSON file for SDK generation.
# Usage: bundle-spec.sh <output-file>
#
# Must be run from the oas-api-definition root directory.

OUTPUT_FILE="${1:?Usage: bundle-spec.sh <output-file>}"

SPEC_FILE="./api.oas3.yaml"

if [ ! -f "${SPEC_FILE}" ]; then
echo "Error: ${SPEC_FILE} not found. Run from the oas-api-definition root." >&2
exit 1
fi

echo "Validating OpenAPI spec..."
npx @apidevtools/swagger-cli validate "${SPEC_FILE}"

echo "Bundling spec to ${OUTPUT_FILE}..."
npx @apidevtools/swagger-cli bundle -o "${OUTPUT_FILE}" -t json "${SPEC_FILE}"

echo "Spec bundled successfully."
Loading
Loading