Skip to content

Commit dc4ba2a

Browse files
committed
ci: add workflow to regenerate models from OpenAPI spec changes
1 parent 3fee70a commit dc4ba2a

File tree

3 files changed

+133
-2
lines changed

3 files changed

+133
-2
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
name: Regenerate models from OpenAPI spec
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
docs_pr_number:
7+
description: "PR number in apify/apify-docs that triggered this workflow"
8+
required: true
9+
type: string
10+
docs_pr_branch:
11+
description: "Branch name in apify/apify-docs PR"
12+
required: true
13+
type: string
14+
15+
concurrency:
16+
group: regenerate-models-${{ inputs.docs_pr_number }}
17+
cancel-in-progress: true
18+
19+
jobs:
20+
regenerate-models:
21+
name: Regenerate models
22+
runs-on: ubuntu-latest
23+
24+
steps:
25+
- name: Checkout apify-client-python
26+
uses: actions/checkout@v6
27+
28+
- name: Checkout apify-docs at PR branch
29+
uses: actions/checkout@v6
30+
with:
31+
repository: apify/apify-docs
32+
ref: ${{ inputs.docs_pr_branch }}
33+
path: apify-docs
34+
token: ${{ secrets.APIFY_SERVICE_ACCOUNT_GITHUB_TOKEN }}
35+
36+
- name: Set up Node.js
37+
uses: actions/setup-node@v6
38+
with:
39+
node-version: 24
40+
cache: npm
41+
cache-dependency-path: apify-docs/package-lock.json
42+
43+
- name: Build OpenAPI spec bundle
44+
run: |
45+
cd apify-docs
46+
corepack enable
47+
npm ci --force
48+
npm run openapi:build:json
49+
env:
50+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
51+
52+
- name: Set up uv
53+
uses: astral-sh/setup-uv@v6
54+
55+
- name: Generate models from local spec
56+
run: |
57+
# Temporarily point datamodel-codegen to the local spec instead of the remote URL
58+
sed -i 's|^url = ".*"|input = "apify-docs/static/api/openapi.json"|' pyproject.toml
59+
uv run datamodel-codegen
60+
git checkout -- pyproject.toml
61+
62+
- name: Check for changes
63+
id: changes
64+
run: |
65+
if git diff --exit-code src/apify_client/_models.py; then
66+
echo "No changes in generated models"
67+
echo "changed=false" >> "$GITHUB_OUTPUT"
68+
else
69+
echo "Models have changed"
70+
echo "changed=true" >> "$GITHUB_OUTPUT"
71+
fi
72+
73+
- name: Configure git
74+
if: steps.changes.outputs.changed == 'true'
75+
run: |
76+
git config user.name "apify-service-account"
77+
git config user.email "apify-service-account@users.noreply.github.com"
78+
79+
- name: Create or update PR
80+
if: steps.changes.outputs.changed == 'true'
81+
id: pr
82+
run: |
83+
BRANCH="chore/update-models-docs-pr-${{ inputs.docs_pr_number }}"
84+
DOCS_PR_URL="https://github.com/apify/apify-docs/pull/${{ inputs.docs_pr_number }}"
85+
86+
git checkout -b "$BRANCH"
87+
git add src/apify_client/_models.py
88+
git commit -m "chore: update generated models from apify-docs PR #${{ inputs.docs_pr_number }}"
89+
git push --force origin "$BRANCH"
90+
91+
# Check if PR already exists
92+
EXISTING_PR=$(gh pr list --head "$BRANCH" --json url --jq '.[0].url' 2>/dev/null || true)
93+
94+
if [ -n "$EXISTING_PR" ]; then
95+
echo "PR already exists: $EXISTING_PR"
96+
echo "pr_url=$EXISTING_PR" >> "$GITHUB_OUTPUT"
97+
echo "created=false" >> "$GITHUB_OUTPUT"
98+
else
99+
BODY="This PR updates the auto-generated Pydantic models based on OpenAPI specification changes in [apify-docs PR #${{ inputs.docs_pr_number }}]($DOCS_PR_URL).
100+
101+
## Changes
102+
103+
- Regenerated \`src/apify_client/_models.py\` using \`datamodel-codegen\`
104+
105+
## Source
106+
107+
- apify-docs PR: $DOCS_PR_URL"
108+
109+
PR_URL=$(gh pr create \
110+
--title "chore: update generated models from apify-docs PR #${{ inputs.docs_pr_number }}" \
111+
--body "$BODY" \
112+
--head "$BRANCH" \
113+
--base master)
114+
echo "Created PR: $PR_URL"
115+
echo "pr_url=$PR_URL" >> "$GITHUB_OUTPUT"
116+
echo "created=true" >> "$GITHUB_OUTPUT"
117+
fi
118+
env:
119+
GITHUB_TOKEN: ${{ secrets.APIFY_SERVICE_ACCOUNT_GITHUB_TOKEN }}
120+
121+
- name: Comment on apify-docs PR
122+
if: steps.changes.outputs.changed == 'true' && steps.pr.outputs.created == 'true'
123+
run: |
124+
gh pr comment "${{ inputs.docs_pr_number }}" \
125+
--repo apify/apify-docs \
126+
--body "A PR to update the Python client models has been created in apify-client-python: ${{ steps.pr.outputs.pr_url }}
127+
128+
This was automatically triggered by OpenAPI specification changes in this PR."
129+
env:
130+
GITHUB_TOKEN: ${{ secrets.APIFY_SERVICE_ACCOUNT_GITHUB_TOKEN }}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ __pycache__
2828

2929
# Virtual envs
3030
.direnv
31-
.env
3231
.envrc
3332
.python-version
3433
.venv
@@ -80,3 +79,5 @@ storage/
8079

8180
# Tmp dir
8281
tmp/
82+
config.bat
83+
node_modules

website/babel.config.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)