|
1 | 1 | name: Contribution Quality |
2 | 2 |
|
3 | | -# Use pull_request_target to get write permissions for fork PRs. |
4 | | -# IMPORTANT: This workflow runs in the context of the BASE branch, not the PR branch. |
5 | | -# Do NOT checkout or run code from the PR itself, only inspect PR metadata via the API. |
| 3 | +# Use pull_request_target so the workflow can safely comment/label on PRs. |
| 4 | +# IMPORTANT: This runs in the context of the BASE branch, not the PR branch. |
| 5 | +# Do NOT checkout or run code from the PR itself; only inspect PR metadata via the API. |
6 | 6 | on: |
7 | 7 | pull_request_target: |
8 | 8 | types: [opened, reopened, edited, synchronize, ready_for_review] |
|
28 | 28 | gate: |
29 | 29 | if: > |
30 | 30 | github.event_name == 'workflow_dispatch' || |
31 | | - (github.event_name == 'pull_request' && |
32 | | - github.event.pull_request.head.repo.fork == true) |
| 31 | + github.event_name == 'pull_request_target' |
33 | 32 | runs-on: ubuntu-latest |
34 | 33 | env: |
35 | 34 | DISPATCH_PR_NUMBER: ${{ inputs.pr_number }} |
@@ -64,24 +63,46 @@ jobs: |
64 | 63 | repo: context.repo.repo, |
65 | 64 | pull_number: prNumber |
66 | 65 | }); |
67 | | - core.setOutput('author_association', pr.author_association || 'NONE'); |
| 66 | + core.setOutput('author_login', pr.user?.login || ''); |
68 | 67 | core.setOutput('draft', pr.draft ? 'true' : 'false'); |
69 | 68 | core.setOutput('body', (pr.body || '').replace(/\r/g,'')); |
70 | 69 | core.setOutput('number', String(pr.number)); |
71 | 70 |
|
72 | | - - name: Skip trusted or drafts (unless forced) |
| 71 | + - name: Resolve author permission |
| 72 | + id: perm |
| 73 | + uses: actions/github-script@v7 |
| 74 | + with: |
| 75 | + script: | |
| 76 | + const login = "${{ steps.pr.outputs.author_login }}".toLowerCase(); |
| 77 | + let permission = 'none'; |
| 78 | + try { |
| 79 | + const { data } = await github.rest.repos.getCollaboratorPermissionLevel({ |
| 80 | + owner: context.repo.owner, |
| 81 | + repo: context.repo.repo, |
| 82 | + username: login, |
| 83 | + }); |
| 84 | + permission = (data.permission || 'none').toLowerCase(); |
| 85 | + } catch (error) { |
| 86 | + if (error.status !== 404) { |
| 87 | + core.warning(`Failed to resolve collaborator permission for ${login}: ${error.message}`); |
| 88 | + } |
| 89 | + } |
| 90 | +
|
| 91 | + const skip = ['admin', 'maintain', 'write'].includes(permission); |
| 92 | + core.info(`author=${login} permission=${permission} skip=${skip}`); |
| 93 | + core.setOutput('permission', permission); |
| 94 | + core.setOutput('skip', skip ? 'true' : 'false'); |
| 95 | +
|
| 96 | + - name: Skip trusted authors or drafts (unless forced) |
73 | 97 | id: gate |
74 | 98 | run: | |
75 | | - assoc="${{ steps.pr.outputs.author_association }}" |
76 | 99 | draft="${{ steps.pr.outputs.draft }}" |
77 | 100 | force="${{ steps.ctx.outputs.force_all }}" |
| 101 | + skip_by_permission="${{ steps.perm.outputs.skip }}" |
78 | 102 | if [ "$force" = "true" ]; then |
79 | 103 | echo "skip=false" >> "$GITHUB_OUTPUT" |
80 | 104 | else |
81 | | - case "$assoc" in |
82 | | - OWNER|COLLABORATOR|MEMBER) echo "skip=true" >> "$GITHUB_OUTPUT" ;; |
83 | | - *) echo "skip=false" >> "$GITHUB_OUTPUT" ;; |
84 | | - esac |
| 105 | + [ "$skip_by_permission" = "true" ] && echo "skip=true" >> "$GITHUB_OUTPUT" || echo "skip=false" >> "$GITHUB_OUTPUT" |
85 | 106 | [ "$draft" = "true" ] && echo "skip=true" >> "$GITHUB_OUTPUT" || true |
86 | 107 | fi |
87 | 108 |
|
|
0 commit comments