Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions .github/workflows/copilot-review-cap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Copilot review cap

# Org ruleset reviews each PR once when it opens. This workflow requests a new Copilot review on
# each push until the PR has MAX_REVIEWS Copilot reviews, then asks the author to work locally.
on:
pull_request:
Comment thread
Copilot marked this conversation as resolved.
Resolved
types: [opened, synchronize, reopened, ready_for_review]
Comment on lines +5 to +7

permissions: {}

concurrency:
group: copilot-review-cap-${{ github.repository }}-${{ github.event.pull_request.number }}
# Never cancel: a cancelled run on a required workflow blocks the merge.
cancel-in-progress: false

jobs:
copilot-review:
runs-on: ubuntu-latest
timeout-minutes: 5
permissions:
pull-requests: write
env:
GH_TOKEN: ${{ github.token }}
ACTION: ${{ github.event.action }}
DRAFT: ${{ github.event.pull_request.draft }}
FORK: ${{ github.event.pull_request.head.repo.full_name != github.repository }}
REPO: ${{ github.repository }}
PR: ${{ github.event.pull_request.number }}
AUTHOR: ${{ github.event.pull_request.user.login }}
MAX_REVIEWS: "3"
COPILOT: copilot-pull-request-reviewer[bot]
MARKER: "<!-- copilot-review-cap -->"
steps:
- name: Request a Copilot review or ask the author to work locally
run: |
set -euo pipefail
# Fork PRs get a read-only token, so skip them instead of failing a required check.
if [[ "$ACTION" != "synchronize" || "$DRAFT" == "true" || "$FORK" == "true" ]]; then
echo "Nothing to do for action=$ACTION draft=$DRAFT fork=$FORK"
exit 0
fi

reviews=$(gh api --paginate --slurp "repos/$REPO/pulls/$PR/reviews" |
jq --arg bot "$COPILOT" '[.[][] | select(.user.login == $bot)] | length')
echo "Copilot reviews on #$PR: $reviews of $MAX_REVIEWS"

if (( reviews < MAX_REVIEWS )); then
if out=$(gh api -X POST "repos/$REPO/pulls/$PR/copilot-code-review" 2>&1); then
echo "Requested Copilot review"
elif grep -q "HTTP 409" <<<"$out"; then
echo "A Copilot review is already pending"
else
echo "::error::Copilot review request failed for $REPO#$PR: $out"
exit 1
fi
exit 0
fi

posted=$(gh api --paginate --slurp "repos/$REPO/issues/$PR/comments" |
jq --arg m "$MARKER" '[.[][] | select(.body | contains($m))] | length')
if (( posted > 0 )); then
echo "Cap comment already posted"
exit 0
fi
body="$MARKER
@$AUTHOR Copilot has reviewed this PR $MAX_REVIEWS times, which is our limit per PR. More rounds rarely find new problems, and each one costs AI units. Take a step back: pull the branch, run it locally and go through the full diff with Claude Code or a teammate. You can still request a Copilot review by hand if you really need one."
gh api -X POST "repos/$REPO/issues/$PR/comments" -f body="$body"
Loading