Skip to content

Commit 5500d84

Browse files
committed
Use collaborator permission API instead of event payload author_association
The webhook event payload's author_association field is unreliable for PRs originating from forks: even if the author is an org member or explicit collaborator with maintain/write permissions, fork PRs receive CONTRIBUTOR. This change queries the collaborator permission API directly to get the author's actual permission level (admin/maintain/write/triage/read/none), which is authoritative regardless of whether the PR comes from a fork or a branch in the main repo. Requires contents:write permission to access the collaborator API endpoint. Made-with: Cursor
1 parent d818a75 commit 5500d84

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ jobs:
1919
if: github.repository_owner == 'NVIDIA'
2020
runs-on: ubuntu-latest
2121
permissions:
22+
contents: write # needed for collaborator permission check
2223
pull-requests: write
2324
steps:
2425
- name: Inspect PR author signals for restricted paths
2526
env:
26-
# PR metadata inputs
27-
AUTHOR_ASSOCIATION: ${{ github.event.pull_request.author_association || 'NONE' }}
27+
# PR metadata inputs (author_association from event payload is
28+
# unreliable for fork PRs, so we query the collaborator API directly)
2829
PR_AUTHOR: ${{ github.event.pull_request.user.login }}
2930
PR_NUMBER: ${{ github.event.pull_request.number }}
3031
PR_URL: ${{ github.event.pull_request.html_url }}
@@ -38,6 +39,15 @@ jobs:
3839
run: |
3940
set -euo pipefail
4041
42+
# Query the collaborator permission API to get the author's actual
43+
# permission level. This is authoritative regardless of whether the
44+
# PR originates from a fork or a branch in the main repo.
45+
# Returns: admin, maintain, write, triage, read, or none.
46+
COLLABORATOR_PERMISSION=$(
47+
gh api "repos/$REPO/collaborators/$PR_AUTHOR/permission" \
48+
--jq '.permission' 2>/dev/null || echo "none"
49+
)
50+
4151
if ! MATCHING_RESTRICTED_PATHS=$(
4252
gh api \
4353
--paginate \
@@ -63,7 +73,7 @@ jobs:
6373
echo ""
6474
echo "- **Error**: Failed to inspect the PR file list."
6575
echo "- **Author**: $PR_AUTHOR"
66-
echo "- **Author association**: $AUTHOR_ASSOCIATION"
76+
echo "- **Collaborator permission**: $COLLABORATOR_PERMISSION"
6777
echo ""
6878
echo "Please update the PR at: $PR_URL"
6979
} >> "$GITHUB_STEP_SUMMARY"
@@ -83,7 +93,7 @@ jobs:
8393
echo ""
8494
echo "- **Error**: Failed to inspect the current PR labels."
8595
echo "- **Author**: $PR_AUTHOR"
86-
echo "- **Author association**: $AUTHOR_ASSOCIATION"
96+
echo "- **Collaborator permission**: $COLLABORATOR_PERMISSION"
8797
echo ""
8898
echo "Please update the PR at: $PR_URL"
8999
} >> "$GITHUB_STEP_SUMMARY"
@@ -107,11 +117,11 @@ jobs:
107117
TRUSTED_SIGNALS="(none)"
108118
109119
if [ "$TOUCHES_RESTRICTED_PATHS" = "true" ]; then
110-
case "$AUTHOR_ASSOCIATION" in
111-
COLLABORATOR|MEMBER|OWNER)
120+
case "$COLLABORATOR_PERMISSION" in
121+
admin|maintain|write)
112122
HAS_TRUSTED_SIGNAL=true
113-
LABEL_ACTION="not needed (author association is a trusted signal)"
114-
TRUSTED_SIGNALS="author_association:$AUTHOR_ASSOCIATION"
123+
LABEL_ACTION="not needed (collaborator permission is a trusted signal)"
124+
TRUSTED_SIGNALS="collaborator_permission:$COLLABORATOR_PERMISSION"
115125
;;
116126
esac
117127
fi
@@ -136,7 +146,7 @@ jobs:
136146
echo ""
137147
echo "- **Error**: Failed to add the \`$REVIEW_LABEL\` label."
138148
echo "- **Author**: $PR_AUTHOR"
139-
echo "- **Author association**: $AUTHOR_ASSOCIATION"
149+
echo "- **Collaborator permission**: $COLLABORATOR_PERMISSION"
140150
echo ""
141151
write_matching_restricted_paths
142152
echo ""
@@ -154,7 +164,7 @@ jobs:
154164
echo "## Restricted Paths Guard Completed"
155165
echo ""
156166
echo "- **Author**: $PR_AUTHOR"
157-
echo "- **Author association**: $AUTHOR_ASSOCIATION"
167+
echo "- **Collaborator permission**: $COLLABORATOR_PERMISSION"
158168
echo "- **Touches restricted paths**: $TOUCHES_RESTRICTED_PATHS"
159169
echo "- **Restricted paths**: \`cuda_bindings/\`, \`cuda_python/\`"
160170
echo "- **Trusted signals**: $TRUSTED_SIGNALS"

0 commit comments

Comments
 (0)