Skip to content

Commit 3823319

Browse files
committed
feat: Add check user permission step in e2e workflow
This commit adds a new step to the end-to-end (e2e) workflow that checks the triggering user's repository permissions. The step runs a script that queries the GitHub REST API to determine whether the user has sufficient access (e.g., write, admin, etc.). This verification helps enforce permission-based controls within the CI process. Signed-off-by: Zaki Shaikh <[email protected]>
1 parent b78ba64 commit 3823319

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

.github/workflows/e2e.yaml

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ jobs:
3131
if: >
3232
github.event_name == 'schedule' ||
3333
github.event_name == 'workflow_dispatch' ||
34-
(github.event_name == 'pull_request_target' &&
35-
contains(fromJSON('["OWNER", "MEMBER", "COLLABORATOR", "CONTRIBUTOR"]'), github.event.pull_request.author_association))
34+
github.event_name == 'pull_request_target'
3635
concurrency:
3736
group: ${{ github.workflow }}-${{ matrix.provider }}-${{ github.event.pull_request.number || github.ref_name }}
3837
cancel-in-progress: true
@@ -73,6 +72,42 @@ jobs:
7372
with:
7473
ref: ${{ inputs.target_ref || github.event.pull_request.head.sha || github.sha }}
7574

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 a specific list of trusted bots to bypass the permission check.
86+
const trustedBots = ['dependabot[bot]']; // Add any other trusted bot accounts here
87+
if (trustedBots.includes(actor)) {
88+
core.info(`User @${actor} is a trusted bot, allowing.`);
89+
return;
90+
}
91+
92+
try {
93+
// Directly check the user's permission level on the repository.
94+
// This covers both org members and external collaborators with sufficient access.
95+
const response = await github.rest.repos.getCollaboratorPermissionLevel({
96+
owner: org,
97+
repo: context.repo.repo,
98+
username: actor,
99+
});
100+
101+
const permission = response.data.permission;
102+
if (permission !== 'admin' && permission !== 'write') {
103+
core.setFailed(`❌ User @${actor} has only '${permission}' repository permission. 'write' or 'admin' is required.`);
104+
} else {
105+
core.info(`✅ User @${actor} has '${permission}' repository permission. Proceeding.`);
106+
}
107+
} catch (error) {
108+
core.setFailed(`Permission check failed for @${actor}. They are likely not a collaborator on the repository. Error: ${error.message}`);
109+
}
110+
76111
- uses: actions/setup-go@v5
77112
with:
78113
go-version-file: "go.mod"

0 commit comments

Comments
 (0)