Skip to content

Commit ad1a3ba

Browse files
radofuchsRadovan Fuchs
andauthored
fix auth gate for e2e tests (lightspeed-core#2458)
Co-authored-by: Radovan Fuchs <rfuchs@rfuchs-thinkpadp1gen7.tpb.csb>
1 parent 972c52e commit ad1a3ba

5 files changed

Lines changed: 30 additions & 21 deletions

File tree

.github/workflows/e2e_authorize.yaml

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,56 +10,61 @@ on:
1010

1111
jobs:
1212
authorize:
13-
name: Check owner or developer access
13+
name: Check repository access
1414
runs-on: ubuntu-latest
1515
timeout-minutes: 2
16-
# Adding some other label must not start e2e.
1716
if: github.event.action != 'labeled' || github.event.label.name == 'ok-to-test'
1817
permissions:
1918
contents: read
19+
members: read
2020
outputs:
2121
authorized: ${{ steps.check.outputs.authorized }}
2222
steps:
23-
- name: Verify executor is owner or developer
23+
- name: Allow repo 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 }}
2929
USER: ${{ github.event.pull_request.user.login || github.actor }}
3030
ACTOR: ${{ github.actor }}
31-
ASSOCIATION: ${{ github.event.pull_request.author_association }}
3231
REPOSITORY: ${{ github.repository }}
3332
run: |
3433
set -euo pipefail
3534
3635
allow() { echo "$1"; echo "authorized=true" >> "$GITHUB_OUTPUT"; exit 0; }
3736
deny() { echo "::warning::$1"; echo "authorized=false" >> "$GITHUB_OUTPUT"; exit 0; }
3837
39-
perm() {
40-
local encoded
41-
encoded=$(jq -nr --arg u "$1" '$u|@uri')
42-
gh api "repos/${REPOSITORY}/collaborators/${encoded}/permission" --jq .permission 2>/dev/null || echo none
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
4355
}
4456
4557
[ "$EVENT_NAME" = "schedule" ] && allow "Scheduled run."
4658
47-
USER_PERM=$(perm "$USER")
48-
echo "executor=$USER permission=$USER_PERM association=${ASSOCIATION:-none}"
49-
if [ "$USER_PERM" = "admin" ] || [ "$USER_PERM" = "write" ]; then
50-
allow "$USER is an owner or developer."
51-
fi
52-
# Org members often only have read on this repo, so perm() is not enough.
53-
if [ "$ASSOCIATION" = "OWNER" ] || [ "$ASSOCIATION" = "MEMBER" ]; then
54-
allow "$USER is an organization $ASSOCIATION."
59+
echo "executor=$USER"
60+
if is_repo_user_or_member "$USER"; then
61+
allow "$USER is a user or member of ${REPOSITORY}."
5562
fi
5663
57-
# ok-to-test only counts on the labeled event, so a later push cannot
58-
# reuse an old owner approval against a new PR head.
59-
[ "$EVENT_ACTION" = "labeled" ] || deny "$USER is not an owner, member, or developer."
64+
[ "$EVENT_ACTION" = "labeled" ] || deny "$USER is not a user or member of ${REPOSITORY}."
6065
61-
LABELER_PERM=$(perm "$ACTOR")
66+
LABELER_PERM=$(repo_permission "$ACTOR")
6267
echo "ok-to-test labeler=$ACTOR permission=$LABELER_PERM"
6368
[ "$LABELER_PERM" = "admin" ] && allow "Owner $ACTOR added ok-to-test."
6469
65-
deny "ok-to-test was not added by an owner."
70+
deny "ok-to-test must be added by a repository owner."

.github/workflows/e2e_tests.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ jobs:
1212
uses: ./.github/workflows/e2e_authorize.yaml
1313
permissions:
1414
contents: read
15+
members: read
1516

1617
e2e_tests:
1718
needs: authorize

.github/workflows/e2e_tests_lightspeed_evaluation.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ jobs:
1111
uses: ./.github/workflows/e2e_authorize.yaml
1212
permissions:
1313
contents: read
14+
members: read
1415

1516
e2e_tests:
1617
needs: authorize

.github/workflows/e2e_tests_providers.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ jobs:
1212
uses: ./.github/workflows/e2e_authorize.yaml
1313
permissions:
1414
contents: read
15+
members: read
1516

1617
e2e_tests:
1718
needs: authorize

.github/workflows/e2e_tests_rhaiis.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jobs:
1313
uses: ./.github/workflows/e2e_authorize.yaml
1414
permissions:
1515
contents: read
16+
members: read
1617

1718
e2e_tests:
1819
needs: authorize

0 commit comments

Comments
 (0)