Generate Python Client #411
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: Generate Python Client | |
| on: | |
| workflow_dispatch: | |
| schedule: | |
| - cron: "0 0 * * *" # Runs daily at midnight UTC | |
| jobs: | |
| generate-client: | |
| runs-on: ubuntu-latest | |
| env: | |
| OPENAPI_SPEC_URL: https://app.fieldmanager.io/api/location/openapi.json | |
| steps: | |
| # Step 1: Initialize Log File | |
| - name: 📝 Initialize Log File | |
| run: | | |
| mkdir -p logs | |
| echo "" > logs/log | |
| # Step 2: Checkout the code | |
| - name: 📥 Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: trunk | |
| # Step 3: Set up Python environment | |
| - name: 🐍 Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: "3.12" | |
| # Step 4: Set up Node.js | |
| - name: 🟢 Set up Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: "22" | |
| # Step 5: Install dependencies | |
| - name: 📦 Install Dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install openapi-python-client yq | |
| sudo apt-get update && sudo apt-get install -y jq | |
| echo "Recording openapi-python-client version..." >> logs/log | |
| openapi-python-client --version >> logs/log 2>&1 | |
| # Step 6: Download OpenAPI Specification | |
| - name: ⬇️ Download OpenAPI Specification | |
| run: | | |
| echo "Downloading OpenAPI specification..." | |
| mkdir -p openapi_specification | |
| curl -s ${{ env.OPENAPI_SPEC_URL }} > ./openapi_specification/openapi.json | |
| # Step 7: Format OpenAPI Specification | |
| - name: 🔧 Format OpenAPI Specification | |
| run: | | |
| echo "Formatting OpenAPI specification..." | |
| npx prettier --write ./openapi_specification/openapi.json | |
| # Step 8: Get API Version | |
| - name: 🔢 Get API Version | |
| run: | | |
| if yq -e '.package_version_override' config.yaml > /dev/null; then | |
| VERSION=$(yq -r '.package_version_override' config.yaml) | |
| else | |
| VERSION=$(jq -r '.info.version' ./openapi_specification/openapi.json) | |
| fi | |
| if [ -f openapi_specification/version ]; then | |
| OLD_VERSION=$(cat openapi_specification/version) | |
| if [ "$VERSION" = "$OLD_VERSION" ]; then | |
| echo "API version has not changed. Skipping update." | |
| echo "version_changed=false" >> $GITHUB_ENV | |
| exit 0 | |
| else | |
| echo "API version has changed from $OLD_VERSION to $VERSION." | |
| echo "version_changed=true" >> $GITHUB_ENV | |
| fi | |
| fi | |
| echo "$VERSION" > openapi_specification/version | |
| echo "VERSION=$VERSION" >> $GITHUB_ENV | |
| # Step 9: Check for existing PR | |
| - name: 🔍 Check for existing PR | |
| if: env.version_changed == 'true' | |
| run: | | |
| PR_EXISTS=$(gh pr list --repo ${{ github.repository }} --base trunk --search "update-python-client-${{ env.VERSION }} in:title" --json number --jq 'length') | |
| if [ "$PR_EXISTS" -gt 0 ]; then | |
| echo "A pull request for version ${{ env.VERSION }} already exists. Skipping update." | |
| echo "pr_exists=true" >> $GITHUB_ENV | |
| else | |
| echo "No pull request found for version ${{ env.VERSION }}. Proceeding with update." | |
| echo "pr_exists=false" >> $GITHUB_ENV | |
| fi | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| # Step 10: Generate Python Client | |
| - name: ⚙️ Generate Python Client | |
| if: env.version_changed == 'true' && env.pr_exists == 'false' | |
| run: | | |
| echo "Generating API client..." >> logs/log | |
| openapi-python-client generate \ | |
| --path ./openapi_specification/openapi.json \ | |
| --overwrite \ | |
| --custom-template-path=templates \ | |
| --config config.yaml >> logs/log 2>&1 | |
| cat logs/log | |
| # Step 11: Check for Changes | |
| - name: 👀 Check for Changes | |
| if: env.version_changed == 'true' && env.pr_exists == 'false' | |
| id: git_diff | |
| run: | | |
| git add . | |
| if git diff --cached --quiet; then | |
| echo "changes_detected=false" >> $GITHUB_OUTPUT | |
| else | |
| echo "changes_detected=true" >> $GITHUB_OUTPUT | |
| fi | |
| # Step 12: Configure Git | |
| - name: 🔨 Configure Git | |
| if: env.version_changed == 'true' && env.pr_exists == 'false' && steps.git_diff.outputs.changes_detected == 'true' | |
| run: | | |
| git config --global user.name "github-actions[bot]" | |
| git config --global user.email "github-actions[bot]@users.noreply.github.com" | |
| # Step 13: Create Pull Request | |
| - name: 🚀 Create Pull Request | |
| if: env.version_changed == 'true' && env.pr_exists == 'false' && steps.git_diff.outputs.changes_detected == 'true' | |
| uses: peter-evans/create-pull-request@v7 | |
| with: | |
| token: ${{ secrets.PULL_REQUEST_TOKEN }} | |
| branch: update-python-client-${{ env.VERSION }} | |
| base: trunk | |
| title: "Update Python client to API version ${{ env.VERSION }}" | |
| body: | | |
| This PR updates the generated Python client to match API version ${{ env.VERSION }}. | |
| branch-suffix: timestamp |