From dc4264aa7ea727e3fecb8a7ded7001544e26d295 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20B=C3=A4lter?= Date: Wed, 11 Nov 2026 09:43:50 +0100 Subject: [PATCH 1/3] Add workflow that limits Copilot reviews to 3 per PR The org ruleset requests a Copilot review on each push, and the reviews use AI units. This workflow requests a new review on each push until the PR has 3 Copilot reviews. Then it posts 1 comment that tells the author to test the change locally. --- .github/workflows/copilot-review-cap.yml | 64 ++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 .github/workflows/copilot-review-cap.yml diff --git a/.github/workflows/copilot-review-cap.yml b/.github/workflows/copilot-review-cap.yml new file mode 100644 index 0000000..ae9089e --- /dev/null +++ b/.github/workflows/copilot-review-cap.yml @@ -0,0 +1,64 @@ +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: + types: [opened, synchronize, reopened, ready_for_review] + +permissions: {} + +concurrency: + group: copilot-review-cap-${{ github.repository }}-${{ github.event.pull_request.number }} + cancel-in-progress: true + +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 }} + 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: "" + steps: + - name: Request a Copilot review or ask the author to work locally + run: | + set -euo pipefail + if [[ "$ACTION" != "synchronize" || "$DRAFT" == "true" ]]; then + echo "Nothing to do for action=$ACTION draft=$DRAFT" + 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" From 0d7fc1b24abfd423627ce5bcad34eab9a5fc43f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20B=C3=A4lter?= Date: Wed, 11 Nov 2026 09:57:41 +0100 Subject: [PATCH 2/3] Test push 1 From d453e8b7aea0019960b7d8fc633a1e82759ac03f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20B=C3=A4lter?= Date: Wed, 11 Nov 2026 10:17:28 +0100 Subject: [PATCH 3/3] Skip fork PRs and do not cancel runs --- .github/workflows/copilot-review-cap.yml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/.github/workflows/copilot-review-cap.yml b/.github/workflows/copilot-review-cap.yml index ae9089e..2ada6aa 100644 --- a/.github/workflows/copilot-review-cap.yml +++ b/.github/workflows/copilot-review-cap.yml @@ -10,7 +10,8 @@ permissions: {} concurrency: group: copilot-review-cap-${{ github.repository }}-${{ github.event.pull_request.number }} - cancel-in-progress: true + # Never cancel: a cancelled run on a required workflow blocks the merge. + cancel-in-progress: false jobs: copilot-review: @@ -22,6 +23,7 @@ jobs: 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 }} @@ -32,8 +34,9 @@ jobs: - name: Request a Copilot review or ask the author to work locally run: | set -euo pipefail - if [[ "$ACTION" != "synchronize" || "$DRAFT" == "true" ]]; then - echo "Nothing to do for action=$ACTION draft=$DRAFT" + # 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