|
| 1 | +# Separate job: GitHub sends job-level secrets to the runner as soon as that job starts. |
| 2 | +name: Authorize E2E secrets access |
| 3 | + |
| 4 | +on: |
| 5 | + workflow_call: |
| 6 | + outputs: |
| 7 | + authorized: |
| 8 | + description: true if e2e may run with repository secrets |
| 9 | + value: ${{ jobs.authorize.outputs.authorized }} |
| 10 | + |
| 11 | +jobs: |
| 12 | + authorize: |
| 13 | + name: Check owner or developer access |
| 14 | + runs-on: ubuntu-latest |
| 15 | + timeout-minutes: 2 |
| 16 | + # Adding some other label must not start e2e. |
| 17 | + if: github.event.action != 'labeled' || github.event.label.name == 'ok-to-test' |
| 18 | + permissions: |
| 19 | + contents: read |
| 20 | + outputs: |
| 21 | + authorized: ${{ steps.check.outputs.authorized }} |
| 22 | + steps: |
| 23 | + - name: Verify executor is owner or developer |
| 24 | + id: check |
| 25 | + env: |
| 26 | + GH_TOKEN: ${{ github.token }} |
| 27 | + EVENT_NAME: ${{ github.event_name }} |
| 28 | + EVENT_ACTION: ${{ github.event.action }} |
| 29 | + USER: ${{ github.event.pull_request.user.login || github.actor }} |
| 30 | + ACTOR: ${{ github.actor }} |
| 31 | + REPOSITORY: ${{ github.repository }} |
| 32 | + run: | |
| 33 | + set -euo pipefail |
| 34 | +
|
| 35 | + allow() { echo "$1"; echo "authorized=true" >> "$GITHUB_OUTPUT"; exit 0; } |
| 36 | + deny() { echo "::warning::$1"; echo "authorized=false" >> "$GITHUB_OUTPUT"; exit 0; } |
| 37 | +
|
| 38 | + perm() { |
| 39 | + local encoded |
| 40 | + encoded=$(jq -nr --arg u "$1" '$u|@uri') |
| 41 | + gh api "repos/${REPOSITORY}/collaborators/${encoded}/permission" --jq .permission 2>/dev/null || echo none |
| 42 | + } |
| 43 | +
|
| 44 | + [ "$EVENT_NAME" = "schedule" ] && allow "Scheduled run." |
| 45 | +
|
| 46 | + USER_PERM=$(perm "$USER") |
| 47 | + echo "executor=$USER permission=$USER_PERM" |
| 48 | + if [ "$USER_PERM" = "admin" ] || [ "$USER_PERM" = "write" ]; then |
| 49 | + allow "$USER is an owner or developer." |
| 50 | + fi |
| 51 | +
|
| 52 | + # ok-to-test only counts on the labeled event, so a later push cannot |
| 53 | + # reuse an old owner approval against a new PR head. |
| 54 | + [ "$EVENT_ACTION" = "labeled" ] || deny "$USER is not an owner or developer." |
| 55 | +
|
| 56 | + LABELER_PERM=$(perm "$ACTOR") |
| 57 | + echo "ok-to-test labeler=$ACTOR permission=$LABELER_PERM" |
| 58 | + [ "$LABELER_PERM" = "admin" ] && allow "Owner $ACTOR added ok-to-test." |
| 59 | +
|
| 60 | + deny "ok-to-test was not added by an owner." |
0 commit comments