Skip to content

Commit 66bd22e

Browse files
committed
CI: harden restricted paths guard label handling
Fetch live PR labels to avoid stale event data, use pull-request label edits, and keep the restricted-paths review label in place until a maintainer removes it manually. Made-with: Cursor
1 parent d452d73 commit 66bd22e

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

.github/workflows/restricted-paths-guard.yml

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
name: "CI: Restricted Paths Guard"
55

66
on:
7+
# Run on drafts too so maintainers get early awareness on WIP PRs.
78
# Label updates on fork PRs require pull_request_target permissions.
89
pull_request_target:
910
types:
@@ -18,14 +19,12 @@ jobs:
1819
if: github.repository_owner == 'NVIDIA'
1920
runs-on: ubuntu-latest
2021
permissions:
21-
issues: write
22-
pull-requests: read
22+
pull-requests: write
2323
steps:
2424
- name: Inspect PR author signals for restricted paths
2525
env:
2626
# PR metadata inputs
2727
AUTHOR_ASSOCIATION: ${{ github.event.pull_request.author_association || 'NONE' }}
28-
EXISTING_LABELS: ${{ toJson(github.event.pull_request.labels.*.name) }}
2928
PR_AUTHOR: ${{ github.event.pull_request.user.login }}
3029
PR_NUMBER: ${{ github.event.pull_request.number }}
3130
PR_URL: ${{ github.event.pull_request.html_url }}
@@ -71,6 +70,26 @@ jobs:
7170
exit 1
7271
fi
7372
73+
# Fetch live PR labels to avoid stale event payload (race condition
74+
# when labels are changed shortly before the workflow runs).
75+
if ! LIVE_LABELS=$(
76+
gh pr view "${PR_NUMBER}" --repo "${REPO}" \
77+
--json labels \
78+
--jq '[.labels[].name]'
79+
); then
80+
echo "::error::Failed to inspect the current PR labels."
81+
{
82+
echo "## Restricted Paths Guard Failed"
83+
echo ""
84+
echo "- **Error**: Failed to inspect the current PR labels."
85+
echo "- **Author**: $PR_AUTHOR"
86+
echo "- **Author association**: $AUTHOR_ASSOCIATION"
87+
echo ""
88+
echo "Please update the PR at: $PR_URL"
89+
} >> "$GITHUB_STEP_SUMMARY"
90+
exit 1
91+
fi
92+
7493
TOUCHES_RESTRICTED_PATHS=false
7594
if [ -n "$MATCHING_RESTRICTED_PATHS" ]; then
7695
TOUCHES_RESTRICTED_PATHS=true
@@ -97,15 +116,20 @@ jobs:
97116
esac
98117
fi
99118
119+
NEEDS_REVIEW_LABEL=false
120+
if [ "$TOUCHES_RESTRICTED_PATHS" = "true" ] && [ "$HAS_TRUSTED_SIGNAL" = "false" ]; then
121+
NEEDS_REVIEW_LABEL=true
122+
fi
123+
100124
LABEL_ALREADY_PRESENT=false
101-
if jq -e --arg label "$REVIEW_LABEL" '.[] == $label' <<<"$EXISTING_LABELS" >/dev/null; then
125+
if jq -e --arg label "$REVIEW_LABEL" '.[] == $label' <<<"$LIVE_LABELS" >/dev/null; then
102126
LABEL_ALREADY_PRESENT=true
103127
fi
104128
105-
if [ "$TOUCHES_RESTRICTED_PATHS" = "true" ] && [ "$HAS_TRUSTED_SIGNAL" = "false" ]; then
129+
if [ "$NEEDS_REVIEW_LABEL" = "true" ]; then
106130
if [ "$LABEL_ALREADY_PRESENT" = "true" ]; then
107131
LABEL_ACTION="already present"
108-
elif ! gh issue edit "$PR_NUMBER" --repo "$REPO" --add-label "$REVIEW_LABEL"; then
132+
elif ! gh pr edit "$PR_NUMBER" --repo "$REPO" --add-label "$REVIEW_LABEL"; then
109133
echo "::error::Failed to add the $REVIEW_LABEL label."
110134
{
111135
echo "## Restricted Paths Guard Failed"
@@ -122,6 +146,8 @@ jobs:
122146
else
123147
LABEL_ACTION="added"
124148
fi
149+
elif [ "$LABEL_ALREADY_PRESENT" = "true" ]; then
150+
LABEL_ACTION="left in place (manual removal required)"
125151
fi
126152
127153
{
@@ -137,8 +163,11 @@ jobs:
137163
echo ""
138164
write_matching_restricted_paths
139165
fi
140-
if [ "$TOUCHES_RESTRICTED_PATHS" = "true" ] && [ "$HAS_TRUSTED_SIGNAL" = "false" ]; then
166+
if [ "$NEEDS_REVIEW_LABEL" = "true" ]; then
141167
echo ""
142168
echo "- **Manual follow-up**: No trusted signal was found, so \`$REVIEW_LABEL\` is required."
169+
elif [ "$LABEL_ALREADY_PRESENT" = "true" ]; then
170+
echo ""
171+
echo "- **Manual follow-up**: Existing \`$REVIEW_LABEL\` was left in place intentionally because this workflow does not inspect every commit. Remove it manually after reviewing the PR for restricted-paths policy compliance."
143172
fi
144173
} >> "$GITHUB_STEP_SUMMARY"

0 commit comments

Comments
 (0)