Skip to content

Commit a40fd94

Browse files
authored
[no-ci] Fix race condition in PR metadata check workflow (#1874)
* Fix race condition in PR metadata check workflow The workflow reads assignees, labels, and milestone from the event payload, which is a snapshot taken at trigger time. When a PR is opened, the `opened` event fires before labels/milestone/assignee are fully applied, causing the check to fail with stale data. Fix by fetching live PR data via `gh api` instead of relying on the event payload. The downstream validation logic is unchanged. * Use --jq to filter gh api response to only needed fields Address review feedback: use gh's built-in --jq flag to extract only assignees, labels, and milestone from the API response, avoiding unnecessary processing of the full PR payload. * Use gh pr view instead of gh api for cleaner PR data fetch Address review feedback: use the higher-level `gh pr view` command with --json and --jq instead of constructing the raw API URL.
1 parent 93f3880 commit a40fd94

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

.github/workflows/pr-metadata-check.yml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ jobs:
2525
steps:
2626
- name: Check for assignee, labels, and milestone
2727
env:
28-
ASSIGNEES: ${{ toJson(github.event.pull_request.assignees) }}
29-
LABELS: ${{ toJson(github.event.pull_request.labels) }}
30-
MILESTONE: ${{ github.event.pull_request.milestone && github.event.pull_request.milestone.title || '' }}
3128
PR_URL: ${{ github.event.pull_request.html_url }}
29+
PR_NUMBER: ${{ github.event.pull_request.number }}
30+
GH_REPO: ${{ github.repository }}
31+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3232
IS_BOT: ${{ github.actor == 'dependabot[bot]' || github.actor == 'pre-commit-ci[bot]' || github.actor == 'copy-pr-bot[bot]' }}
3333
IS_DRAFT: ${{ github.event.pull_request.draft }}
3434
run: |
@@ -37,6 +37,15 @@ jobs:
3737
exit 0
3838
fi
3939
40+
# Fetch live PR data to avoid stale event payload (race condition
41+
# when labels/milestone are added shortly after PR creation).
42+
PR_JSON=$(gh pr view "${PR_NUMBER}" --repo "${GH_REPO}" \
43+
--json assignees,labels,milestone \
44+
--jq '{assignees: .assignees, labels: .labels, milestone: (.milestone.title // empty)}')
45+
ASSIGNEES=$(echo "$PR_JSON" | jq '.assignees')
46+
LABELS=$(echo "$PR_JSON" | jq '.labels')
47+
MILESTONE=$(echo "$PR_JSON" | jq -r '.milestone')
48+
4049
ERRORS=""
4150
4251
ASSIGNEE_COUNT=$(echo "$ASSIGNEES" | jq 'length')

0 commit comments

Comments
 (0)