Skip to content

Commit 9c266f2

Browse files
authored
ci: prevent regenerate-models from opening duplicate empty PRs (#895)
The `Regenerate models` workflow opened a new (and mostly empty) PR on every dispatch from a single apify-docs PR. For example, docs PR #2689 produced 7 client PRs (#888 to #894). Root cause: the "Point branch at current master" step force-pushed the shared `update-models-docs-pr-<N>` branch to master before committing. That intermediate "branch == master" state made the open PR's head identical to its base, which GitHub auto-closes. The next dispatch's `gh pr list --head` (open PRs only) then found nothing and created a brand-new PR, leaving the auto-closed one looking empty. Fix: drop the force-push-to-master step and append the regenerated models to the auto-update branch as signed ("Verified") commits via `apify/actions/signed-commit`. A fresh docs PR creates the branch and its first commit; each later commit on the docs PR appends another commit, so the client PR mirrors the docs PR and stays open throughout. Repeated dispatches that regenerate identical models add no commit (`committed=false`), and the docs-PR comment fires only when a PR is created or a commit is appended.
1 parent 86f0e0b commit 9c266f2

1 file changed

Lines changed: 63 additions & 24 deletions

File tree

‎.github/workflows/manual_regenerate_models.yaml‎

Lines changed: 63 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,45 @@ jobs:
6363
with:
6464
token: ${{ secrets.APIFY_SERVICE_ACCOUNT_GITHUB_TOKEN }}
6565

66+
# Record master's SHA up front: a later step checks out the auto-update branch, so the change check below
67+
# can't compare against HEAD. Resolve master from the remote rather than the checked-out ref: the run may be
68+
# dispatched from a non-master ref, but that check and the PR both target master, so master is the baseline.
69+
- name: Record master ref
70+
id: base
71+
run: |
72+
git fetch --depth=1 origin master
73+
echo "sha=$(git rev-parse FETCH_HEAD)" >> "$GITHUB_OUTPUT"
74+
75+
# Does the auto-update branch already exist on the remote? If so, a previous dispatch opened the PR
76+
# and we append to it. If not, we start it from master (below) so signed-commit creates it there.
77+
- name: Determine auto-update branch state
78+
id: branch
79+
run: |
80+
if git ls-remote --exit-code --heads origin "$BRANCH" >/dev/null 2>&1; then
81+
echo "exists=true" >> "$GITHUB_OUTPUT"
82+
echo "create=false" >> "$GITHUB_OUTPUT"
83+
else
84+
echo "exists=false" >> "$GITHUB_OUTPUT"
85+
echo "create=true" >> "$GITHUB_OUTPUT"
86+
fi
87+
88+
# Check out the existing branch before regenerating so the new models land as a NEW commit on top,
89+
# mirroring the commit just pushed to the docs PR. We must switch now, while the tree is clean:
90+
# signed-commit's own checkout is a plain `git checkout` that would refuse to overwrite the
91+
# regenerated files (which differ between master and the branch) once they're in the working tree.
92+
- name: Check out existing auto-update branch
93+
if: steps.branch.outputs.exists == 'true'
94+
run: |
95+
git fetch --depth=1 origin "$BRANCH"
96+
git checkout -B "$BRANCH" FETCH_HEAD
97+
98+
# The branch doesn't exist yet, so regenerate from the recorded master SHA rather than the dispatched
99+
# ref (a manual run may be dispatched from a non-master ref). Regenerating on master keeps the codegen
100+
# tooling current, and signed-commit then creates the branch (the PR head) on top of master, the PR base.
101+
- name: Start the auto-update branch from master
102+
if: steps.branch.outputs.exists == 'false'
103+
run: git checkout "${{ steps.base.outputs.sha }}"
104+
66105
# Download the pre-built OpenAPI spec artifact from the apify-docs workflow run.
67106
# Skipped for manual runs — datamodel-codegen will fetch from the published spec URL instead.
68107
- name: Download OpenAPI spec artifact
@@ -93,46 +132,44 @@ jobs:
93132
uv run poe generate-models
94133
fi
95134
96-
# Proceed only when regeneration actually changes the models relative to the current master.
97-
# The job runs on a fresh master checkout, so anything already merged into master (e.g. a
98-
# manually merged client PR carrying the same spec change, or a docs PR that merged master in
99-
# and re-emits an already-applied change) produces no diff here and we skip — instead of
100-
# opening a PR that just duplicates what master already has.
135+
# Proceed only when the regenerated models differ from master (compared against the recorded SHA,
136+
# since the tree may now sit on the branch), which skips empty-PR runs: a spec change that doesn't
137+
# affect the client models, or one already merged into master. Also avoids creating an empty branch.
101138
- name: Check for model changes
102139
id: changes
103140
run: |
104-
if git diff --quiet -- src/apify_client/_models.py src/apify_client/_typeddicts.py src/apify_client/_literals.py; then
141+
if git diff --quiet "${{ steps.base.outputs.sha }}" -- src/apify_client/_models.py src/apify_client/_typeddicts.py src/apify_client/_literals.py; then
105142
echo "No model changes relative to master — nothing to regenerate."
106143
echo "has_changes=false" >> "$GITHUB_OUTPUT"
107144
else
108145
echo "has_changes=true" >> "$GITHUB_OUTPUT"
109146
fi
110147
111-
# Point the auto-update branch at the current master so the signed-commit step below records
112-
# the regenerated models as a single commit on top of it. Resetting to master (instead of
113-
# building on a possibly stale existing branch) is what keeps the committed diff relative to
114-
# the current master: an already-merged change can never reappear. Any previous content on the
115-
# branch is intentionally replaced, so the PR always reflects "current master + freshly
116-
# regenerated models" — and it still updates whenever the source docs PR gets new commits.
117-
- name: Point branch at current master
118-
if: steps.changes.outputs.has_changes == 'true'
119-
run: |
120-
git checkout -B "$BRANCH"
121-
git push --force origin "HEAD:refs/heads/$BRANCH"
122-
123-
- name: Commit model changes
148+
# Append the regenerated models to the auto-update branch as a single signed ("Verified") commit
149+
# via apify/actions/signed-commit (GitHub's createCommitOnBranch GraphQL mutation). It's added on
150+
# top of the branch tip, never resetting to master: a fresh docs PR creates the branch
151+
# (create-branch) and its first commit, and each later docs-PR commit triggers a dispatch that
152+
# appends another, so the client PR mirrors the docs PR and stays open.
153+
#
154+
# Appending (rather than force-pushing the branch to master, as before) is what keeps the PR open:
155+
# a "branch == master" tip makes the PR head equal its base, which GitHub auto-closes, and each
156+
# dispatch then opens a duplicate PR. signed-commit also sets committed=false when the staged
157+
# files match the tip, so a repeated dispatch regenerating identical models adds no commit.
158+
- name: Commit regenerated models
124159
id: commit
125160
if: steps.changes.outputs.has_changes == 'true'
126161
uses: apify/actions/signed-commit@v1.3.0
127162
with:
128163
message: ${{ env.TITLE }}
129-
add: 'src/apify_client/_models.py src/apify_client/_typeddicts.py src/apify_client/_literals.py'
164+
add: "src/apify_client/_models.py src/apify_client/_typeddicts.py src/apify_client/_literals.py"
130165
github-token: ${{ secrets.APIFY_SERVICE_ACCOUNT_GITHUB_TOKEN }}
131166
branch: ${{ env.BRANCH }}
132-
create-branch: 'false'
167+
create-branch: "${{ steps.branch.outputs.create }}"
133168

169+
# Ensure exactly one PR exists for this branch. It's no longer auto-closed, so an existing PR is
170+
# reused. Only the first run (or one whose PR step a concurrent dispatch cancelled) creates it.
134171
- name: Create or update PR
135-
if: steps.commit.outputs.committed == 'true'
172+
if: steps.changes.outputs.has_changes == 'true'
136173
id: pr
137174
env:
138175
GH_TOKEN: ${{ secrets.APIFY_SERVICE_ACCOUNT_GITHUB_TOKEN }}
@@ -164,9 +201,11 @@ jobs:
164201
echo "created=true" >> "$GITHUB_OUTPUT"
165202
fi
166203
167-
# Post a cross-repo comment on the original docs PR so reviewers know about the corresponding client-python PR.
204+
# Post a cross-repo comment on the docs PR pointing reviewers to the companion client-python PR.
205+
# Only when something happened: the PR was just created, or a commit was appended. A repeated
206+
# dispatch that changes nothing (committed=false) posts no comment, avoiding noise on the docs PR.
168207
- name: Comment on apify-docs PR
169-
if: steps.commit.outputs.committed == 'true' && inputs.docs_pr_number
208+
if: inputs.docs_pr_number && (steps.pr.outputs.created == 'true' || steps.commit.outputs.committed == 'true')
170209
env:
171210
GH_TOKEN: ${{ secrets.APIFY_SERVICE_ACCOUNT_GITHUB_TOKEN }}
172211
PR_CREATED: ${{ steps.pr.outputs.created }}

0 commit comments

Comments
 (0)