Skip to content

update-sdk

update-sdk #31

name: Update and Publish SDK
on:
repository_dispatch:
types: [update-sdk]
workflow_dispatch: # Allow manual trigger
jobs:
update-and-publish:
runs-on: ubuntu-latest
permissions:
contents: write
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Install OpenAPI Generator
run: |
npm install -g @openapitools/openapi-generator-cli
- name: Fetch current version from swagger
id: fetch-version
run: |
SWAGGER_URL="https://api.linkbreakers.com/internal/openapi/api/v1/api.swagger.json"
NEW_VERSION=$(curl -s "$SWAGGER_URL" | python -c "import sys, json; print(json.load(sys.stdin)['info']['version'])")
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
echo "Swagger version: $NEW_VERSION"
- name: Read current OPENAPI_VERSION
id: current-version
run: |
if [ -f OPENAPI_VERSION ]; then
CURRENT_VERSION=$(cat OPENAPI_VERSION)
else
CURRENT_VERSION="0.0.0"
fi
echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
echo "Current version: $CURRENT_VERSION"
- name: Compare versions
id: compare
run: |
if [ "${{ steps.fetch-version.outputs.new_version }}" == "${{ steps.current-version.outputs.current_version }}" ]; then
echo "needs_update=false" >> $GITHUB_OUTPUT
echo "✅ Versions match. No update needed."
else
echo "needs_update=true" >> $GITHUB_OUTPUT
echo "🔄 Version changed: ${{ steps.current-version.outputs.current_version }} → ${{ steps.fetch-version.outputs.new_version }}"
fi
- name: Download and convert OpenAPI spec to 3.1
if: steps.compare.outputs.needs_update == 'true'
run: |
# Download OpenAPI 3.2.0 spec
curl -o openapi-3.2.json https://api.linkbreakers.com/internal/openapi/api/v1/api.swagger.json
# Convert OpenAPI 3.2 to 3.1 (remove 3.2-specific fields)
python -c "
import json
with open('openapi-3.2.json', 'r') as f:
spec = json.load(f)
# Change version to 3.1.0
spec['openapi'] = '3.1.0'
# Remove 3.2-specific fields
spec.pop('jsonSchemaDialect', None)
with open('openapi-3.1.json', 'w') as f:
json.dump(spec, f, indent=2)
"
echo "✅ OpenAPI spec converted from 3.2 to 3.1"
- name: Generate SDK using OpenAPI Generator
if: steps.compare.outputs.needs_update == 'true'
run: |
NEW_VERSION="${{ steps.fetch-version.outputs.new_version }}"
# Backup custom files that we want to preserve
mkdir -p .backup
cp linkbreakers/__init__.py .backup/ 2>/dev/null || true
cp linkbreakers/safe_config.py .backup/ 2>/dev/null || true
# Remove existing generated code
find linkbreakers -mindepth 1 -delete || true
# Generate SDK using OpenAPI Generator with python generator (creates proper SDK classes)
openapi-generator-cli generate \
-i openapi-3.1.json \
-g python \
-o . \
--skip-validate-spec \
--global-property apiTests=false,modelTests=false,apiDocs=false,modelDocs=false \
--additional-properties=packageName=linkbreakers,projectName=linkbreakers,packageVersion=$NEW_VERSION,library=urllib3
# Restore custom files (overwrite the generated ones)
cp .backup/__init__.py linkbreakers/ 2>/dev/null || true
cp .backup/safe_config.py linkbreakers/ 2>/dev/null || true
# Clean up
rm -rf .backup
rm openapi-3.2.json openapi-3.1.json
echo "✅ SDK generated successfully with version $NEW_VERSION"
- name: Update version files
if: steps.compare.outputs.needs_update == 'true'
run: |
NEW_VERSION="${{ steps.fetch-version.outputs.new_version }}"
# Update OPENAPI_VERSION file
echo "$NEW_VERSION" > OPENAPI_VERSION
# Update __version__ in __init__.py
sed -i "s/__version__ = '.*'/__version__ = '$NEW_VERSION'/" linkbreakers/__init__.py
echo "✅ Version files updated to $NEW_VERSION"
- name: Build distribution
if: steps.compare.outputs.needs_update == 'true'
run: |
python setup.py sdist bdist_wheel
echo "✅ Distribution built"
- name: Configure Git
if: steps.compare.outputs.needs_update == 'true'
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Commit changes
if: steps.compare.outputs.needs_update == 'true'
run: |
NEW_VERSION="${{ steps.fetch-version.outputs.new_version }}"
git add .
git commit -m "chore: update SDK to API version $NEW_VERSION"
echo "✅ Changes committed"
- name: Create and push tag
if: steps.compare.outputs.needs_update == 'true'
run: |
NEW_VERSION="${{ steps.fetch-version.outputs.new_version }}"
git tag "v$NEW_VERSION"
git push origin main
git push origin "v$NEW_VERSION"
echo "✅ Tag v$NEW_VERSION created and pushed"
- name: Publish to PyPI
if: steps.compare.outputs.needs_update == 'true'
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
run: |
twine upload dist/*
echo "✅ Published linkbreakers==${{ steps.fetch-version.outputs.new_version }} to PyPI"
- name: Summary
if: steps.compare.outputs.needs_update == 'true'
run: |
echo "🎉 SDK successfully updated and published!"
echo "Version: ${{ steps.fetch-version.outputs.new_version }}"
echo "Package: https://pypi.org/project/linkbreakers/"