Skip to content

Enforce 2 collaborator reviews for fork PRs #500

Enforce 2 collaborator reviews for fork PRs

Enforce 2 collaborator reviews for fork PRs #500

# Copyright (C) 2025 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This workflow requires that pull requests coming from a forked repo (hence
# non-googlers) have 2 reviews from repo collaborators.
name: Enforce 2 collaborator reviews for fork PRs
on:
pull_request_review:
types: [submitted]
pull_request_target:
types: [synchronize]
permissions:
pull-requests: read
jobs:
enforce-reviews:
runs-on: self-hosted
steps:
- name: Check if PR is from a fork
id: fork_check
run: |
echo "is_fork=${{ github.event.pull_request.head.repo.full_name != github.repository }}" >> $GITHUB_ENV
- name: Fetch approved reviews
if: env.is_fork == 'true'
run: |
PR_NUMBER=${{ github.event.pull_request.number }}
REPO="${{ github.repository }}"
LATEST_SHA="${{ github.event.pull_request.head.sha }}"
REVIEWS=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/$REPO/pulls/$PR_NUMBER/reviews")
# Filter to latest commit and valid collaborator approvals
COUNT=$(echo "$REVIEWS" | jq --arg sha "$LATEST_SHA" '
[group_by(.user.login)[] | last] |
map(select(
.state == "APPROVED" and
(.author_association == "COLLABORATOR" or .author_association == "MEMBER") and
.commit_id == $sha
)) |
length')
echo "Collaborator/member approvals for latest commit ($LATEST_SHA): $COUNT"
if [ "$COUNT" -lt 2 ]; then
echo "❌ PR from fork requires 2 collaborator approvals on the latest commit."
exit 1
fi