Skip to content

Commit f5f7388

Browse files
committed
Add 'automated code review' label after PR creation, using GITHUB_TOKEN
When a PR is created with `gh pr create --label ...` (via the otelbot app token in code-review-sweep.yml), GitHub fires `opened` and `labeled` pull_request events within the same second. Both runs land in the build-pull-request.yml concurrency group with cancel-in-progress, so the (skipped, label-not-build-relevant) labeled run cancels the real opened run, leaving the PR with no build and a failing required-status-check (see e.g. PR #18441). Split into two steps: create the PR (otelbot app token, so the PR is authored by otelbot), then add the label as a separate step using the default GITHUB_TOKEN. GitHub's anti-recursion rule means actions taken with GITHUB_TOKEN don't trigger workflow events, so adding the label no longer fires a labeled pull_request event and the build for the PR is no longer cancelled.
1 parent f0833f0 commit f5f7388

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

.github/workflows/code-review-sweep.yml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ jobs:
6363
environment: protected
6464
permissions:
6565
contents: write # for git push
66+
pull-requests: write # for adding the "automated code review" label via GITHUB_TOKEN
6667
env:
6768
MODULES_JSON: ${{ needs.dispatch.outputs.modules }}
6869
MODEL: ${{ needs.dispatch.outputs.model }}
@@ -179,17 +180,36 @@ jobs:
179180

180181
- name: Create PR
181182
if: steps.commit.outputs.pushed == 'true'
183+
id: create-pr
182184
env:
183185
GH_TOKEN: ${{ steps.otelbot-token.outputs.token }}
184186
run: |
185187
branch="${{ steps.branch.outputs.name }}"
186188
title="Code review sweep (run ${GITHUB_RUN_ID})"
189+
# Create the PR without the label. Adding `--label` here uses the
190+
# otelbot app token, which fires a `labeled` pull_request event in
191+
# the same second as the `opened` event. Both runs land in the same
192+
# concurrency group on build-pull-request.yml with cancel-in-progress,
193+
# so the (skipped) labeled run cancels the real opened run and the
194+
# PR ends up with no build (see PR #18441).
187195
gh pr create \
188196
--title "$title" \
189197
--body-file "$PR_BODY" \
190198
--base main \
191-
--head "$branch" \
192-
--label "automated code review"
199+
--head "$branch"
200+
201+
- name: Add label to PR
202+
if: steps.commit.outputs.pushed == 'true'
203+
env:
204+
# Use the default GITHUB_TOKEN (not the otelbot app token) so that
205+
# adding the label does NOT trigger downstream workflows. GitHub's
206+
# anti-recursion rule: actions taken with GITHUB_TOKEN don't fire
207+
# workflow events. This avoids the labeled-vs-opened race that
208+
# would otherwise cancel the build for this PR.
209+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
210+
run: |
211+
branch="${{ steps.branch.outputs.name }}"
212+
gh pr edit "$branch" --add-label "automated code review"
193213
194214
- name: Ensure progress branch exists
195215
if: steps.review-loop.outputs.processed_count != '0'

0 commit comments

Comments
 (0)