Skip to content

Commit 6e6a123

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

File tree

1 file changed

+52
-9
lines changed

1 file changed

+52
-9
lines changed

.github/workflows/e2e.yaml

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,57 @@ on:
2525
- "test/testdata/**"
2626

2727
jobs:
28+
# New Job: Checks permissions for PRs and outputs the result.
29+
check-permissions:
30+
name: Check PR author permissions
31+
# This job only runs for pull_request_target events.
32+
if: github.event_name == 'pull_request_target'
33+
runs-on: ubuntu-latest
34+
outputs:
35+
granted: ${{ steps.permission_check.outputs.result }}
36+
steps:
37+
- name: Check user permissions
38+
id: permission_check
39+
uses: actions/github-script@v7
40+
with:
41+
result-encoding: string # Capture the script's return value as an output
42+
script: |
43+
const actor = context.payload.pull_request.user.login;
44+
45+
// Allow dependabot and other bots unconditionally.
46+
if (actor.endsWith('[bot]')) {
47+
core.info(`User @${actor} is a bot, allowing.`);
48+
return 'true';
49+
}
50+
51+
try {
52+
const response = await github.rest.repos.getCollaboratorPermissionLevel({
53+
owner: context.repo.owner,
54+
repo: context.repo.repo,
55+
username: actor,
56+
});
57+
58+
const permission = response.data.permission;
59+
if (permission === 'admin' || permission === 'write') {
60+
core.info(`✅ User @${actor} has '${permission}' permission. Proceeding.`);
61+
return 'true';
62+
} else {
63+
core.warning(`User @${actor} has '${permission}' permission. 'write' or 'admin' is required. Skipping E2E tests.`);
64+
return 'false';
65+
}
66+
} catch (error) {
67+
core.warning(`Could not verify permission for @${actor}. They might not be a collaborator. Error: ${error.message}`);
68+
return 'false';
69+
}
70+
71+
# Modified Job: Now depends on the check-permissions job.
2872
e2e-tests:
29-
# Run on schedule, unconditional workflow_dispatch,
30-
# or pull_request_target if the actor has write/admin permissions.
73+
needs: [check-permissions] # It depends on the result of the check.
74+
# The job runs on schedule/dispatch, or on PRs if the check-permissions job granted access.
3175
if: >
3276
github.event_name == 'schedule' ||
3377
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))
78+
(github.event_name == 'pull_request_target' && needs.check-permissions.outputs.granted == 'true')
3579
concurrency:
3680
group: ${{ github.workflow }}-${{ matrix.provider }}-${{ github.event.pull_request.number || github.ref_name }}
3781
cancel-in-progress: true
@@ -72,6 +116,8 @@ jobs:
72116
with:
73117
ref: ${{ inputs.target_ref || github.event.pull_request.head.sha || github.sha }}
74118

119+
# The permission check step has been moved to the `check-permissions` job.
120+
75121
- uses: actions/setup-go@v5
76122
with:
77123
go-version-file: "go.mod"
@@ -93,8 +139,8 @@ jobs:
93139
nohup gosmee client --saveDir /tmp/gosmee-replay ${{ secrets.PYSMEE_URL }} "http://${CONTROLLER_DOMAIN_URL}" &
94140
95141
- name: Setup tmate session
96-
uses: mxschmitt/action-tmate@v3
97142
if: ${{ github.event_name == 'workflow_dispatch' && inputs.debug_enabled }}
143+
uses: mxschmitt/action-tmate@v3
98144
with:
99145
detached: true
100146
limit-access-to-actor: true
@@ -121,11 +167,8 @@ jobs:
121167
run: |
122168
./hack/gh-workflow-ci.sh create_second_github_app_controller_on_ghe
123169
124-
# Adjusted step-level conditions based on the new job-level logic
125170
- name: Run E2E Tests
126-
# This step runs for schedule, PR target (if job started), or workflow_dispatch (if job started)
127-
# Remove the old label check which is no longer relevant for triggering.
128-
if: ${{ github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' || github.event_name == 'pull_request_target' }}
171+
# The job-level `if` condition already handles this, so the step can run unconditionally
129172
env:
130173
TEST_PROVIDER: ${{ matrix.provider }}
131174
TEST_BITBUCKET_CLOUD_TOKEN: ${{ secrets.BITBUCKET_CLOUD_TOKEN }}
@@ -142,7 +185,6 @@ jobs:
142185
./hack/gh-workflow-ci.sh run_e2e_tests
143186
144187
- name: Run E2E Tests on nightly
145-
# This step still runs specifically for schedule or workflow_dispatch
146188
if: ${{ github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' }}
147189
env:
148190
NIGHTLY_E2E_TEST: "true"
@@ -188,3 +230,4 @@ jobs:
188230
notify_when: "failure"
189231
env:
190232
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
233+

0 commit comments

Comments
 (0)