Skip to content

Commit d37b5bd

Browse files
committed
add check user premission step
Signed-off-by: Zaki Shaikh <zashaikh@redhat.com>
1 parent 86a29ea commit d37b5bd

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

.github/workflows/e2e.yaml

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
if: >
3232
github.event_name == 'schedule' ||
3333
github.event_name == 'workflow_dispatch' ||
34-
(github.event_name == 'pull_request_target' && contains(fromJSON('["zakisk", "infernus01", "savitaashture", "chmouel", "vdemeester", "enarha", "aThorp96", "waveywaves", "mathur07", "dependabot[bot]"]'), github.event.pull_request.user.login))
34+
github.event_name == 'pull_request_target'
3535
concurrency:
3636
group: ${{ github.workflow }}-${{ matrix.provider }}-${{ github.event.pull_request.number || github.ref_name }}
3737
cancel-in-progress: true
@@ -72,6 +72,41 @@ jobs:
7272
with:
7373
ref: ${{ inputs.target_ref || github.event.pull_request.head.sha || github.sha }}
7474

75+
# Step to check PR author's org membership and repo permissions.
76+
# This step will fail the job if checks do not pass, skipping subsequent steps.
77+
- name: Check user permissions on PRs
78+
if: github.event_name == 'pull_request_target'
79+
uses: actions/github-script@v7
80+
with:
81+
script: |
82+
const actor = context.payload.pull_request.user.login;
83+
const org = context.repo.owner;
84+
85+
// Allow dependabot and other bots unconditionally.
86+
if (actor.endsWith('[bot]')) {
87+
core.info(`User @${actor} is a bot, allowing.`);
88+
return;
89+
}
90+
91+
try {
92+
// Directly check the user's permission level on the repository.
93+
// This covers both org members and external collaborators with sufficient access.
94+
const response = await github.rest.repos.getCollaboratorPermissionLevel({
95+
owner: org,
96+
repo: context.repo.repo,
97+
username: actor,
98+
});
99+
100+
const permission = response.data.permission;
101+
if (permission !== 'admin' && permission !== 'write') {
102+
core.setFailed(`❌ User @${actor} has only '${permission}' repository permission. 'write' or 'admin' is required.`);
103+
} else {
104+
core.info(`✅ User @${actor} has '${permission}' repository permission. Proceeding.`);
105+
}
106+
} catch (error) {
107+
core.setFailed(`Permission check failed for @${actor}. They are likely not a collaborator on the repository. Error: ${error.message}`);
108+
}
109+
75110
- uses: actions/setup-go@v5
76111
with:
77112
go-version-file: "go.mod"

0 commit comments

Comments
 (0)