name: Update OpenAPI Spec on: schedule: - cron: "0 20 * * 1" # Monday 4pm EST (8pm UTC) workflow_dispatch: # Allow manual trigger permissions: contents: write pull-requests: write jobs: check-openapi-updates: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v6 - name: Install uv uses: astral-sh/setup-uv@v5 with: version: "latest" - name: Set up Python uses: actions/setup-python@v5 with: python-version: "3.11" - name: Install jq run: sudo apt-get update && sudo apt-get install -y jq - name: Check if there is already an open pull request id: check_pull_request run: | if [[ -n $(gh pr list --state open --repo ${{ github.repository }} | grep "docs: OpenAPI spec") ]]; then echo "There is already an open PR with updates to the OpenAPI spec. Merge or close that PR first. Skipping." echo "pr_exists=true" >> $GITHUB_OUTPUT else echo "pr_exists=false" >> $GITHUB_OUTPUT fi env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Generate OpenAPI spec if: steps.check_pull_request.outputs.pr_exists != 'true' run: | # Generate the new OpenAPI spec echo "Generating OpenAPI spec from Starlette routes..." uv run python docs/openapi/generate_openapi.py > /tmp/generate_output.txt 2>&1 || cat /tmp/generate_output.txt # Verify file was created if [ ! -f docs/openapi/openapi.json ]; then echo "Error: openapi.json was not generated" cat /tmp/generate_output.txt exit 1 fi echo "OpenAPI spec generated successfully" ls -la docs/openapi/openapi.json - name: Compare OpenAPI files if: steps.check_pull_request.outputs.pr_exists != 'true' id: compare run: | # Create a backup of the current spec for comparison cp docs/openapi/openapi.json docs/openapi/openapi.json.backup # Re-run generation to get fresh spec uv run python docs/openapi/generate_openapi.py # Extract versions (if they exist) NEW_VERSION=$(jq -r '.info.version // "1.0.0"' docs/openapi/openapi.json) CURRENT_VERSION=$(jq -r '.info.version // "1.0.0"' docs/openapi/openapi.json.backup) echo "Current version: $CURRENT_VERSION" echo "New version: $NEW_VERSION" # Compare file content (normalize by sorting keys) jq --sort-keys . docs/openapi/openapi.json > docs/openapi/sorted_new.json jq --sort-keys . docs/openapi/openapi.json.backup > docs/openapi/sorted_current.json if ! cmp -s docs/openapi/sorted_new.json docs/openapi/sorted_current.json; then echo "OpenAPI spec content has changed." # Compare versions (assuming semantic versioning) if [ "$(printf '%s\n' "$CURRENT_VERSION" "$NEW_VERSION" | sort -V | tail -n1)" == "$NEW_VERSION" ] && [ "$CURRENT_VERSION" != "$NEW_VERSION" ]; then echo "New version detected. Creating PR." echo "NEEDS_UPDATE=true" >> $GITHUB_ENV echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV echo "UPDATE_REASON=version upgraded from $CURRENT_VERSION to $NEW_VERSION" >> $GITHUB_ENV else echo "File content changed but version remains the same. Creating PR." echo "NEEDS_UPDATE=true" >> $GITHUB_ENV echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV echo "UPDATE_REASON=content updated (routes or endpoints changed)" >> $GITHUB_ENV fi else echo "No changes detected in OpenAPI spec content." echo "NEEDS_UPDATE=false" >> $GITHUB_ENV # Restore original if no changes mv docs/openapi/openapi.json.backup docs/openapi/openapi.json fi # Clean up rm -f docs/openapi/openapi.json.backup docs/openapi/sorted_new.json docs/openapi/sorted_current.json - name: Create Pull Request if: env.NEEDS_UPDATE == 'true' && steps.check_pull_request.outputs.pr_exists != 'true' uses: peter-evans/create-pull-request@v7 with: token: ${{ secrets.GITHUB_TOKEN }} commit-message: "docs: OpenAPI spec ${{ env.UPDATE_REASON }}" title: "docs: OpenAPI spec ${{ env.UPDATE_REASON }}" body: | This PR updates the OpenAPI spec automatically. **Update reason:** ${{ env.UPDATE_REASON }} **Version:** ${{ env.NEW_VERSION }} This PR was created automatically by the [Update OpenAPI Spec](https://github.com/${{ github.repository }}/actions/workflows/docs-update-openapi.yml) workflow. Review the changes and merge if they look correct. branch: update-openapi-spec branch-suffix: timestamp delete-branch: true