name: Orch Review Gate
on:
push:
permissions:
contents: read
pull-requests: read
issues: read
jobs:
review-gate:
runs-on: ubuntu-latest
if: github.ref != format('refs/heads/{0}', github.event.repository.default_branch)
steps:
- name: Check automated review status
env:
GH_TOKEN: ${{ github.token }}
ORCH_REVIEWER: ${{ vars.ORCH_REVIEWER }}
run: |
set -euo pipefail
BRANCH="${GITHUB_REF#refs/heads/}"
PR_NUMBER=$(gh pr list --repo "$GITHUB_REPOSITORY" --head "$BRANCH" \
--state open --json number -q '.[0].number')
if [ -z "$PR_NUMBER" ]; then
echo "No open PR for branch $BRANCH - skipping review gate."
exit 0
fi
if [ -z "${ORCH_REVIEWER:-}" ]; then
echo "Repository variable ORCH_REVIEWER is not set. Run 'orch init' again."
exit 1
fi
echo "Checking review status for PR #${PR_NUMBER} (branch: $BRANCH)"
# Only comments from ORCH_REVIEWER count, anyone can comment on a PR
LATEST_BODY=$(gh api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" --paginate \
| jq -rs --arg user "$ORCH_REVIEWER" \
'[.[][] | select(.user.login == $user and (.body | startswith("## Automated Review")))]
| sort_by(.created_at) | last | .body // empty')
if [ -z "$LATEST_BODY" ]; then
echo "No automated review from $ORCH_REVIEWER - review pending."
exit 1
fi
FIRST_LINE=$(printf '%s\n' "$LATEST_BODY" | head -1 | tr -d '\r')
case "$FIRST_LINE" in
"## Automated Review — Approve" | "## Automated Review - Approve")
echo "Automated review: approved"
;;
"## Automated Review — Changes Requested" | "## Automated Review - Changes Requested")
echo "Automated review: changes requested"
echo ""
printf '%s\n' "$LATEST_BODY" | tail -n +3
exit 1
;;
*)
echo "Unrecognized review comment format: $FIRST_LINE"
exit 1
;;
esac