Skip to content

Commit 5ef9be8

Browse files
authored
Workflow fix members (#2460)
* fix e2e authorize gate to repo permission levels.
1 parent 9ba1347 commit 5ef9be8

5 files changed

Lines changed: 35 additions & 47 deletions

File tree

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,70 @@
1-
# Separate job: GitHub sends job-level secrets to the runner as soon as that job starts.
1+
# Gate before e2e jobs that use repository secrets (separate job so secrets are not sent if skipped).
2+
#
3+
# Uses the same collaborator permission API GitHub uses for push/merge access on
4+
# this repository (org base permissions, teams, and direct grants).
25
name: Authorize E2E secrets access
36

47
on:
58
workflow_call:
69
outputs:
710
authorized:
8-
description: true if e2e may run with repository secrets
911
value: ${{ jobs.authorize.outputs.authorized }}
1012

1113
jobs:
1214
authorize:
13-
name: Check repository access
15+
name: Check repository owner or member
1416
runs-on: ubuntu-latest
15-
timeout-minutes: 2
17+
timeout-minutes: 1
1618
if: github.event.action != 'labeled' || github.event.label.name == 'ok-to-test'
17-
permissions:
18-
contents: read
19-
members: read
19+
# Do not set permissions: contents: read — it blocks reading collaborator permissions.
2020
outputs:
2121
authorized: ${{ steps.check.outputs.authorized }}
2222
steps:
23-
- name: Allow repo members, or ok-to-test from an owner
23+
- name: Allow repo owners and members, or ok-to-test from an owner
2424
id: check
2525
env:
2626
GH_TOKEN: ${{ github.token }}
2727
EVENT_NAME: ${{ github.event_name }}
2828
EVENT_ACTION: ${{ github.event.action }}
29+
IS_FORK: ${{ github.event.repository.fork }}
30+
# PR author for merge-rights check (not github.actor — see dependabot hardening).
2931
USER: ${{ github.event.pull_request.user.login || github.actor }}
3032
ACTOR: ${{ github.actor }}
33+
REPO_OWNER: ${{ github.repository_owner }}
3134
REPOSITORY: ${{ github.repository }}
3235
run: |
3336
set -euo pipefail
34-
3537
allow() { echo "$1"; echo "authorized=true" >> "$GITHUB_OUTPUT"; exit 0; }
3638
deny() { echo "::warning::$1"; echo "authorized=false" >> "$GITHUB_OUTPUT"; exit 0; }
3739
38-
encode() { jq -nr --arg u "$1" '$u|@uri'; }
39-
40-
# True if the user is on this repository or an org owner/member.
41-
is_repo_user_or_member() {
42-
local encoded org role
43-
encoded=$(encode "$1")
44-
if gh api --silent "repos/${REPOSITORY}/collaborators/${encoded}"; then
45-
return 0
46-
fi
47-
org="${REPOSITORY%%/*}"
48-
role=$(gh api "orgs/${org}/memberships/${encoded}" \
49-
--jq 'if .state == "active" then .role else "none" end' 2>/dev/null || echo none)
50-
[ "$role" = "admin" ] || [ "$role" = "member" ]
51-
}
52-
53-
repo_permission() {
54-
gh api "repos/${REPOSITORY}/collaborators/$(encode "$1")/permission" --jq .permission 2>/dev/null || echo none
40+
permission_of() {
41+
gh api "repos/${REPOSITORY}/collaborators/$(jq -nr --arg u "$1" '$u|@uri')/permission" \
42+
--jq .permission 2>/dev/null || echo none
5543
}
5644
5745
[ "$EVENT_NAME" = "schedule" ] && allow "Scheduled run."
5846
59-
echo "executor=$USER"
60-
if is_repo_user_or_member "$USER"; then
61-
allow "$USER is a user or member of ${REPOSITORY}."
62-
fi
47+
echo "user=${USER} actor=${ACTOR} event=${EVENT_NAME}/${EVENT_ACTION:-none} fork=${IS_FORK}"
48+
49+
# Push to upstream: only users with push (merge) rights can push here.
50+
[ "$EVENT_NAME" = "push" ] && [ "$IS_FORK" != "true" ] && allow "Push to upstream repository."
51+
52+
# Push to your own fork.
53+
[ "$EVENT_NAME" = "push" ] && [ "$ACTOR" = "$REPO_OWNER" ] && allow "Push by fork owner."
54+
55+
USER_PERM=$(permission_of "$USER")
56+
echo "user_permission=${USER_PERM}"
57+
case "$USER_PERM" in
58+
admin|maintain|write|triage|read)
59+
allow "${USER} has repository access (${USER_PERM}, same source as merge rights)."
60+
;;
61+
esac
6362
64-
[ "$EVENT_ACTION" = "labeled" ] || deny "$USER is not a user or member of ${REPOSITORY}."
63+
# Outsider PR: repository owner added ok-to-test on this run.
64+
[ "$EVENT_ACTION" = "labeled" ] || deny "${USER} has no repository access."
6565
66-
LABELER_PERM=$(repo_permission "$ACTOR")
67-
echo "ok-to-test labeler=$ACTOR permission=$LABELER_PERM"
68-
[ "$LABELER_PERM" = "admin" ] && allow "Owner $ACTOR added ok-to-test."
66+
LABELER_PERM=$(permission_of "$ACTOR")
67+
echo "ok-to-test labeler=${ACTOR} permission=${LABELER_PERM}"
68+
[ "$LABELER_PERM" = "admin" ] && allow "Repository owner ${ACTOR} added ok-to-test."
6969
70-
deny "ok-to-test must be added by a repository owner."
70+
deny "ok-to-test must be added by a repository owner (admin)."

.github/workflows/e2e_tests.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ jobs:
1010
# Own job so repository secrets are never sent to a runner unless this passes.
1111
authorize:
1212
uses: ./.github/workflows/e2e_authorize.yaml
13-
permissions:
14-
contents: read
15-
members: read
1613

1714
e2e_tests:
1815
needs: authorize

.github/workflows/e2e_tests_lightspeed_evaluation.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ jobs:
99
# Own job so repository secrets are never sent to a runner unless this passes.
1010
authorize:
1111
uses: ./.github/workflows/e2e_authorize.yaml
12-
permissions:
13-
contents: read
14-
members: read
1512

1613
e2e_tests:
1714
needs: authorize

.github/workflows/e2e_tests_providers.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ jobs:
1010
# Own job so repository secrets are never sent to a runner unless this passes.
1111
authorize:
1212
uses: ./.github/workflows/e2e_authorize.yaml
13-
permissions:
14-
contents: read
15-
members: read
1613

1714
e2e_tests:
1815
needs: authorize

.github/workflows/e2e_tests_rhaiis.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ jobs:
1111
# Own job so repository secrets are never sent to a runner unless this passes.
1212
authorize:
1313
uses: ./.github/workflows/e2e_authorize.yaml
14-
permissions:
15-
contents: read
16-
members: read
1714

1815
e2e_tests:
1916
needs: authorize

0 commit comments

Comments
 (0)