diff --git a/.github/workflows/pr-build.yml b/.github/workflows/pr-build.yml index 6cebdbd7..c0795e9b 100644 --- a/.github/workflows/pr-build.yml +++ b/.github/workflows/pr-build.yml @@ -15,7 +15,36 @@ concurrency: cancel-in-progress: true jobs: + authorize: + runs-on: ubuntu-latest + outputs: + allowed: ${{ steps.auth.outputs.allowed }} + env: + ALLOWED_ACTORS: ${{ vars.ALLOWED_NON_DEV_PR_ACTORS }} + ACTOR: ${{ github.actor }} + BASE_REF: ${{ github.event.pull_request.base.ref }} + steps: + - name: Check PR authorization + id: auth + shell: bash + run: | + set -euo pipefail + if [ "$BASE_REF" = "dev" ]; then + echo "allowed=true" >> "$GITHUB_OUTPUT" + exit 0 + fi + + normalized=",${ALLOWED_ACTORS}," + if [[ "$normalized" == *",${ACTOR},"* ]]; then + echo "allowed=true" >> "$GITHUB_OUTPUT" + else + echo "allowed=false" >> "$GITHUB_OUTPUT" + echo "Skipping builds for unauthorized PR targeting $BASE_REF" >&2 + fi + build: + needs: authorize + if: ${{ needs.authorize.outputs.allowed == 'true' }} uses: ./.github/workflows/build-and-upload.yml with: ref: ${{ github.event.pull_request.head.sha }} diff --git a/.github/workflows/restrict-non-dev-prs.yml b/.github/workflows/restrict-non-dev-prs.yml new file mode 100644 index 00000000..11d43ba9 --- /dev/null +++ b/.github/workflows/restrict-non-dev-prs.yml @@ -0,0 +1,54 @@ +name: Restrict Non-Dev PRs + +on: + pull_request_target: + types: + - opened + - reopened + - synchronize + +permissions: + contents: read + pull-requests: write + +jobs: + restrict-non-dev-prs: + if: ${{ github.event.pull_request.base.ref != 'dev' }} + runs-on: ubuntu-latest + env: + ALLOWED_ACTORS: ${{ vars.ALLOWED_NON_DEV_PR_ACTORS }} + ACTOR: ${{ github.actor }} + PR_NUMBER: ${{ github.event.pull_request.number }} + BASE_REF: ${{ github.event.pull_request.base.ref }} + steps: + - name: Check allowed actor + id: auth + shell: bash + run: | + set -euo pipefail + normalized=",${ALLOWED_ACTORS}," + if [[ "$normalized" == *",${ACTOR},"* ]]; then + echo "authorized=true" >> "$GITHUB_OUTPUT" + else + echo "authorized=false" >> "$GITHUB_OUTPUT" + fi + + - name: Comment on unauthorized PR + if: ${{ steps.auth.outputs.authorized != 'true' }} + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + gh pr comment "$PR_NUMBER" --body "Thanks for the contribution. PRs need to target \`dev\` branch. Please retarget this PR to the dev branch" + + - name: Close unauthorized PR + if: ${{ steps.auth.outputs.authorized != 'true' }} + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + gh pr close "$PR_NUMBER" + + - name: Fail unauthorized PR + if: ${{ steps.auth.outputs.authorized != 'true' }} + run: | + echo "Actor $ACTOR is not allowed to open PRs targeting $BASE_REF" >&2 + exit 1