-
Notifications
You must be signed in to change notification settings - Fork 17
ci: add workflow to regenerate models from OpenAPI spec #694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vdusek
wants to merge
5
commits into
master
Choose a base branch
from
ci/regenerate-models-workflow
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+168
−0
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3c43378
ci: add workflow to regenerate models from OpenAPI spec changes
vdusek e5c2d39
fix: rename branch, clean up quotes, use placeholder PR title
vdusek 706eb4d
Address feedback
vdusek 30d9596
use artifacts
vdusek bdfe4bf
address feedback
vdusek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| # This workflow regenerates Pydantic models (src/apify_client/_models.py) from the OpenAPI spec whenever | ||
| # the spec changes in a apify/apify-docs PR. It is triggered via workflow_dispatch from the apify-docs CI pipeline. | ||
|
|
||
| name: Regenerate models from OpenAPI spec | ||
|
|
||
| on: | ||
| workflow_dispatch: | ||
| inputs: | ||
| docs_pr_number: | ||
| description: PR number in apify/apify-docs that triggered this workflow | ||
| required: true | ||
| type: string | ||
| docs_pr_sha: | ||
| description: Commit SHA from the apify/apify-docs PR | ||
| required: true | ||
| type: string | ||
|
|
||
| permissions: | ||
| contents: write | ||
| pull-requests: write | ||
|
|
||
| concurrency: | ||
| group: regenerate-models-${{ inputs.docs_pr_number }} | ||
| cancel-in-progress: true | ||
|
|
||
| jobs: | ||
| regenerate-models: | ||
| name: Regenerate models | ||
| runs-on: ubuntu-latest | ||
|
|
||
| env: | ||
| GITHUB_TOKEN: ${{ secrets.APIFY_SERVICE_ACCOUNT_GITHUB_TOKEN }} | ||
| DOCS_PR_NUMBER: ${{ inputs.docs_pr_number }} | ||
| DOCS_PR_SHA: ${{ inputs.docs_pr_sha }} | ||
|
|
||
| steps: | ||
| - name: Validate inputs | ||
| run: | | ||
| if ! [[ "$DOCS_PR_NUMBER" =~ ^[0-9]+$ ]]; then | ||
vdusek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| echo "::error::docs_pr_number must be a positive integer, got: $DOCS_PR_NUMBER" | ||
| exit 1 | ||
| fi | ||
| if ! [[ "$DOCS_PR_SHA" =~ ^[a-f0-9]{40}$ ]]; then | ||
| echo "::error::docs_pr_sha must be a 40-character hex SHA, got: $DOCS_PR_SHA" | ||
| exit 1 | ||
| fi | ||
|
|
||
| - name: Checkout apify-client-python | ||
| uses: actions/checkout@v6 | ||
vdusek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| - name: Checkout apify-docs at PR commit | ||
| uses: actions/checkout@v6 | ||
| with: | ||
| repository: apify/apify-docs | ||
| ref: ${{ inputs.docs_pr_sha }} | ||
| path: apify-docs | ||
| token: ${{ secrets.APIFY_SERVICE_ACCOUNT_GITHUB_TOKEN }} | ||
|
|
||
| - name: Set up Node.js | ||
| uses: actions/setup-node@v6 | ||
| with: | ||
| node-version: 24 | ||
| cache: npm | ||
| cache-dependency-path: apify-docs/package-lock.json | ||
|
|
||
| # Build the bundled OpenAPI JSON from the docs repo sources. This requires Node.js because the spec | ||
| # is assembled by the docs build tooling. | ||
| - name: Build OpenAPI spec bundle | ||
| run: | | ||
| cd apify-docs | ||
| corepack enable | ||
| npm ci --force | ||
| npm run openapi:build:json | ||
| env: | ||
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | ||
vdusek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| - name: Set up uv | ||
| uses: astral-sh/setup-uv@v6 | ||
|
|
||
vdusek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # We call datamodel-codegen with --input pointing at the locally built spec. | ||
| - name: Generate models from local spec | ||
| run: uv run datamodel-codegen --input apify-docs/static/api/openapi.json | ||
|
|
||
vdusek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - name: Check for changes | ||
| id: changes | ||
| run: | | ||
| if git diff --exit-code src/apify_client/_models.py; then | ||
| echo "No changes in generated models" | ||
| echo "changed=false" >> "$GITHUB_OUTPUT" | ||
| else | ||
| echo "Models have changed" | ||
| echo "changed=true" >> "$GITHUB_OUTPUT" | ||
| fi | ||
|
|
||
| - name: Configure git | ||
| if: steps.changes.outputs.changed == 'true' | ||
| run: | | ||
| git config user.name "apify-service-account" | ||
| git config user.email "apify-service-account@users.noreply.github.com" | ||
|
|
||
| - name: Create or update PR | ||
| if: steps.changes.outputs.changed == 'true' | ||
| id: pr | ||
| run: | | ||
| BRANCH="update-models-docs-pr-${DOCS_PR_NUMBER}" | ||
| DOCS_PR_URL="https://github.com/apify/apify-docs/pull/${DOCS_PR_NUMBER}" | ||
| TITLE="[TODO]: update generated models from apify-docs PR #${DOCS_PR_NUMBER}" | ||
|
|
||
| # -B creates the branch or resets it if it already exists (re-runs for the same docs PR). | ||
| git checkout -B "$BRANCH" | ||
| git add src/apify_client/_models.py | ||
| git commit -m "$TITLE" | ||
| git push --force origin "$BRANCH" | ||
vdusek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| EXISTING_PR=$(gh pr list --head "$BRANCH" --json url --jq '.[0].url' 2>/dev/null || true) | ||
|
|
||
| if [ -n "$EXISTING_PR" ]; then | ||
| echo "PR already exists: $EXISTING_PR" | ||
| echo "pr_url=$EXISTING_PR" >> "$GITHUB_OUTPUT" | ||
| echo "created=false" >> "$GITHUB_OUTPUT" | ||
| else | ||
| BODY=$(cat <<EOF | ||
| This PR updates the auto-generated Pydantic models based on OpenAPI specification changes in [apify-docs PR #${DOCS_PR_NUMBER}](${DOCS_PR_URL}). | ||
|
|
||
| ## Changes | ||
|
|
||
| - Regenerated \`src/apify_client/_models.py\` using \`datamodel-codegen\` | ||
|
|
||
| ## Source | ||
|
|
||
| - apify-docs PR: ${DOCS_PR_URL} | ||
| EOF | ||
| ) | ||
|
|
||
| PR_URL=$(gh pr create \ | ||
| --title "$TITLE" \ | ||
| --body "$BODY" \ | ||
| --head "$BRANCH" \ | ||
| --base master) | ||
| echo "Created PR: $PR_URL" | ||
| echo "pr_url=$PR_URL" >> "$GITHUB_OUTPUT" | ||
| echo "created=true" >> "$GITHUB_OUTPUT" | ||
| fi | ||
|
|
||
| # Post a cross-repo comment on the original docs PR so reviewers know about the corresponding client-python PR. | ||
| - name: Comment on apify-docs PR | ||
| if: steps.changes.outputs.changed == 'true' | ||
| env: | ||
| PR_CREATED: ${{ steps.pr.outputs.created }} | ||
| PR_URL: ${{ steps.pr.outputs.pr_url }} | ||
| run: | | ||
| if [ "$PR_CREATED" = "true" ]; then | ||
| COMMENT="A PR to update the Python client models has been created: ${PR_URL} | ||
|
|
||
| This was automatically triggered by OpenAPI specification changes in this PR." | ||
| else | ||
| COMMENT="The Python client model PR has been updated with the latest OpenAPI spec changes: ${PR_URL}" | ||
| fi | ||
|
|
||
| gh pr comment "$DOCS_PR_NUMBER" \ | ||
| --repo apify/apify-docs \ | ||
| --body "$COMMENT" | ||
|
|
||
| - name: Comment on failure | ||
| if: failure() | ||
| run: | | ||
| gh pr comment "$DOCS_PR_NUMBER" \ | ||
| --repo apify/apify-docs \ | ||
| --body "Python client model regeneration failed. [See workflow run](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})." | ||
vdusek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.