diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 94de11a..c582acb 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -4,6 +4,9 @@ on: push: branches: [main] pull_request: + # Started by copilot-auto-merge.yml after it merges: a merge made with + # GITHUB_TOKEN starts no push run. + workflow_dispatch: permissions: contents: read diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8051353..851986c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,6 +4,9 @@ on: push: branches: [main] pull_request: + # Started by copilot-auto-merge.yml after it merges: a merge made with + # GITHUB_TOKEN starts no push run. + workflow_dispatch: permissions: contents: read diff --git a/.github/workflows/copilot-auto-merge.yml b/.github/workflows/copilot-auto-merge.yml new file mode 100644 index 0000000..70b700a --- /dev/null +++ b/.github/workflows/copilot-auto-merge.yml @@ -0,0 +1,140 @@ +# Pilot, this repo only: an owner PR that Copilot approved merges once every +# gate the ruleset requires passes (STANDARDS.md, Copilot auto-merge). +# +# It merges only the exact commit Copilot's latest review approved, and only +# while that commit is still the PR's head. Never a fork, Dependabot +# (auto-merge.yml handles those) or release-please: the release PR stays a +# human checkpoint before any caller picks a change up. +# +# It merges directly rather than switching GitHub's auto-merge on. Auto-merge +# stays on across pushes, so a push Copilot never saw could merge if its checks +# passed before anything switched it off. `gh pr merge --match-head-commit` +# makes GitHub refuse the merge once the head is not the approved commit, and +# the ruleset refuses it until that commit's gates pass, so there is no window. +# +# Triggers. Not `pull_request_review`: GitHub holds a run started by Copilot's +# review for manual approval before any job is read (action_required with no +# jobs, later failure), reportedly starts none for an approval without inline +# comments, and runs the PR's own copy of this file with a write token, so a +# PR could rewrite its own guards. Instead a scheduled sweep runs the copy on +# the default branch, so a PR never runs its own version. No +# `workflow_dispatch` either: a manual run can pick a PR branch's copy. +# +# A merge made with GITHUB_TOKEN starts no `push` workflow, so after each merge +# the sweep starts ci and checks on the default branch itself: a dispatch is one +# of the events that token may start. +# +# Nothing from a PR is checked out or run: the job reads PR metadata through +# the API and calls `gh pr merge`, and nothing else. +name: copilot-auto-merge + +on: + schedule: + # Every 10 minutes: GitHub's schedule is best effort, so a run can lag. + - cron: "*/10 * * * *" + +permissions: {} + +# One sweep at a time; a queued one does the same thing, so none is cancelled. +concurrency: + group: ${{ github.workflow }} + cancel-in-progress: false + +jobs: + sweep: + name: merge owner PRs that Copilot approved + timeout-minutes: 10 + runs-on: ubuntu-latest + permissions: + actions: write # starting ci and checks on the merge + contents: write # the squash merge + pull-requests: write # merging a pull request + steps: + - name: Merge what Copilot's latest review approved + env: + GH_TOKEN: ${{ github.token }} + REPO: ${{ github.repository }} + OWNER: ${{ github.repository_owner }} + run: | + copilot='copilot-pull-request-reviewer[bot]' + failed=0 + # Read from the API: a schedule event's payload has no repository. + default_branch=$(gh api "repos/$REPO" --jq .default_branch) + [ -n "$default_branch" ] || exit 1 + # Empty fields print as "-": a tab is whitespace to `read`, so an + # empty field would shift the ones after it. + # Into the default branch only: the ruleset that makes a merge wait + # for the gates covers that branch alone. + prs=$(gh api --paginate \ + "repos/$REPO/pulls?state=open&base=$default_branch&per_page=100" \ + --jq '.[] | [.number, .user.login, (.head.repo.full_name // "-"), + .head.ref, .head.sha, .draft, + (.auto_merge.enabled_by.login // "-")] | @tsv') + while IFS=$'\t' read -r number author head_repo head_ref head_sha draft enabled_by <&3; do + [ -n "$number" ] || continue + # The owner's PRs from a branch in this repo, never a fork. + if [ "$author" != "$OWNER" ] || [ "$head_repo" != "$REPO" ]; then + continue + fi + case "$author" in dependabot\[bot\] | github-actions\[bot\]) continue ;; esac + case "$head_ref" in dependabot/* | release-please--*) continue ;; esac + # A draft is how the owner holds a PR back from this pilot. + [ "$draft" = false ] || continue + # Auto-merge the owner switched on is the owner's call. + [ "$enabled_by" = - ] || continue + + # Per PR, so one failed call never stops the rest of the sweep. + # Reviews come oldest first, so the last line is Copilot's latest. + if ! reviews=$(gh api --paginate "repos/$REPO/pulls/$number/reviews?per_page=100" \ + --jq '.[] | select(.user.login == "copilot-pull-request-reviewer[bot]") + | "\(.state) \(.commit_id)"'); then + echo "::warning::#$number: could not read its reviews" + failed=1 + continue + fi + latest=$(printf '%s\n' "$reviews" | tail -n 1) + [ "$latest" = "APPROVED $head_sha" ] || continue + + # Merge only once GitHub reports the PR mergeable with its required + # checks passed; until then the next sweep looks again. + if ! state=$(gh pr view "$number" --repo "$REPO" --json mergeStateStatus \ + --jq .mergeStateStatus); then + echo "::warning::#$number: could not read its merge state" + failed=1 + continue + fi + case "$state" in + CLEAN | UNSTABLE | HAS_HOOKS) ;; + *) continue ;; + esac + # GitHub refuses unless the head is still the approved commit. + if gh pr merge --squash --match-head-commit "$head_sha" \ + --repo "$REPO" "$number"; then + echo "::notice::#$number: merged $head_sha, which $copilot approved" + for workflow in ci.yml checks.yml; do + if ! gh workflow run "$workflow" --ref "$default_branch" --repo "$REPO"; then + echo "::warning::#$number: merged, but could not start $workflow on $default_branch" + failed=1 + fi + done + # A dispatch runs on the branch tip at that moment, so a merge + # landing before either one leaves this commit without that run. + # Checked after both: the tip only moves forward, so a tip still + # at the merge commit means both ran on it. Said loudly rather + # than threading a revision through every reusable workflow; the + # tip's runs still cover the combined result. + merged=$(gh pr view "$number" --repo "$REPO" --json mergeCommit \ + --jq .mergeCommit.oid || true) + tip=$(gh api "repos/$REPO/commits/$default_branch" --jq .sha || true) + if [ -z "$merged" ] || [ "$merged" != "$tip" ]; then + echo "::warning::#$number: $default_branch moved past its merge commit ${merged:-(unknown)} before its ci and checks runs started, so they may test ${tip:-a later commit} instead" + failed=1 + fi + else + echo "::warning::#$number: could not merge $head_sha" + failed=1 + fi + done 3< [--apply|--diff] # dry run by default `conventions`, `dependabot-auto-merge`) run in callers' repos. **This repo's own** (`ci`, `checks`, `pull-request`, `auto-merge`, `release-please`) call the reusable ones locally (`uses: ./...`), so a change is exercised here - before any caller can pin it. + before any caller can pin it. `copilot-auto-merge` is a pilot for this repo + alone and calls nothing (STANDARDS.md, Copilot auto-merge). - `template/core` is copied into every repo, then `template/` on top; an overlay file replaces the core file at the same path. - `scripts/check-conventions.py` enforces the checkable rules in `STANDARDS.md`. diff --git a/STANDARDS.md b/STANDARDS.md index 42fd528..c0018a9 100644 --- a/STANDARDS.md +++ b/STANDARDS.md @@ -254,6 +254,51 @@ repository variable, because a job-level `if:` can read `vars` but not security group filtered a high advisory out of its own job and still reported success, so the alert sat open with nothing to show for it. +## Copilot auto-merge (pilot) + +In this repo only, and not yet a reusable workflow: an owner PR that Copilot +approved merges itself once every gate the ruleset requires passes. +`copilot-auto-merge.yml` merges it, and the ruleset refuses the merge until the +gates are green and every review thread is resolved, so a Copilot review with +comments blocks it. The ruleset asks Copilot for a review on each PR and on +every push to it. + +- **Whose PRs.** Only the owner's, from a branch in this repo: never a fork, + never Dependabot (its own auto-merge above covers it), and never a + release-please PR, so the release stays a human checkpoint before any caller + picks a change up. A draft is left alone, which is how to hold one back. +- **Only the commit Copilot approved.** The merge goes ahead only while + Copilot's latest review is an approval of the PR's current head, and once + GitHub reports the PR mergeable. It names that commit (`--match-head-commit`), + so GitHub refuses it if a push has moved the head. Not GitHub's auto-merge: + that stays on across pushes, so a push Copilot never saw could merge if its + checks passed before anything switched it off. +- **No `pull_request_review` trigger.** GitHub holds a run started by Copilot's + review for manual approval before any job is read, reportedly starts none for + an approval without inline comments, and would run the PR's own copy of the + workflow with a write token. So a sweep every ten minutes merges what is + ready. It runs the copy on the default branch, so a PR, including the one that + changes the workflow, never runs its own version, and it has no + `workflow_dispatch`, since a manual run can pick a PR branch's copy. Nothing + from a PR is checked out or run. +- **CI on `main` still runs.** A merge made with `GITHUB_TOKEN` starts no `push` + workflow, so after each merge the sweep dispatches `ci` and `checks` on the + default branch, which that token may do. A dispatch runs on the branch tip, so + if another merge lands before either dispatch the sweep fails with a warning + naming the commit left without runs of its own; the tip's runs still cover the + combined result. +- **Risk accepted.** A change can be written by an agent, approved by Copilot + and merged with no human reading it. The release PR is then the only + checkpoint before the change reaches caller repos. On its first five PRs here + Copilot approved every one with no comments, so it is a light filter. + +**Extend it** to other repos, as a reusable workflow, once it has merged at +least ten PRs here with no change reverted or fixed afterwards for something a +reviewer should have caught, and with no push after an approval ever seen to +merge. **End it** on the first change that reaches a release PR broken in a way +a reading would have caught, or if Copilot reviews stop arriving reliably: +delete the workflow and drop the ruleset's Copilot review rule. + ## Repository settings Tracked as data in `settings/` and applied by `scripts/configure.sh`; @@ -281,7 +326,12 @@ workflow can open or approve a PR it was not built to. The ruleset (`settings/ruleset-main.json`) blocks deletion and force pushes, requires linear history, a PR with resolved conversations and squash merge, and the gates. No bypass actors, so it binds admins too. Zero required approvals: -these are solo repos. +these are solo repos. Where the repo runs the Copilot auto-merge pilot, it also +asks Copilot to review each PR and every push to it, which reaches only authors +with Copilot code review, so not Dependabot or release-please. Elsewhere the +rule is dropped, since nothing acts on those reviews. A Copilot approval counts +toward required approvals only where a Copilot setting allows it, and even then +a count of one would leave those two waiting for an approval that never comes. The exception is a repo whose owner commits straight to the default branch on purpose, such as a link list where each change is one validated line and a PR diff --git a/scripts/configure.sh b/scripts/configure.sh index 10ebcba..25681c6 100755 --- a/scripts/configure.sh +++ b/scripts/configure.sh @@ -252,7 +252,18 @@ if [ "$visibility" = public ]; then run gh api -X PATCH "repos/$repo/code-scanning/default-setup" --silent \ -f state=configured + # The Copilot review rule only where the Copilot auto-merge pilot runs + # (scripts/ruleset.jq). + copilot_review=false + case " +$workflows +" in + *" +.github/workflows/copilot-auto-merge.yml +"*) copilot_review=true ;; + esac ruleset=$(jq -c --arg gates "$gates" --arg admin_bypass "$admin_bypass" \ + --arg copilot_review "$copilot_review" \ -f "$here/scripts/ruleset.jq" "$settings/ruleset-main.json") if [ -n "$gates" ]; then echo "requires: $(printf '%s' "$gates" | paste -sd, - | sed 's/,/, /g')" @@ -265,6 +276,9 @@ if [ "$visibility" = public ]; then if [ "$admin_bypass" = true ]; then echo "admins may bypass the ruleset: the repo commits .github/admin-bypass" fi + if [ "$copilot_review" = true ]; then + echo "requests a Copilot review on every push: the repo has copilot-auto-merge.yml" + fi existing=$(gh api "repos/$repo/rulesets" --jq '.[] | select(.name == "main") | .id') if [ -n "$existing" ]; then run gh api -X PUT "repos/$repo/rulesets/$existing" --silent --input - <<<"$ruleset" diff --git a/scripts/ruleset.jq b/scripts/ruleset.jq index 5c619bc..3e21f05 100644 --- a/scripts/ruleset.jq +++ b/scripts/ruleset.jq @@ -10,6 +10,11 @@ # $admin_bypass is "true" where the repo commits .github/admin-bypass: the # repository admin role may then push to the default branch directly, while # everyone else (Dependabot included) still goes through a gated PR. +# +# $copilot_review is "true" where the repo has copilot-auto-merge.yml. The +# Copilot review rule is kept only there: that workflow is what needs a fresh +# review on every push, and elsewhere the rule would spend Copilot credits on +# reviews nothing acts on. ($gates | split("\n") | map(select(. != ""))) as $present | .rules |= map( @@ -22,6 +27,7 @@ else . end) | .rules |= map(select(.type != "required_status_checks" or (.parameters.required_status_checks | length) > 0)) +| .rules |= map(select(.type != "copilot_code_review" or $copilot_review == "true")) | .bypass_actors = (if $admin_bypass == "true" then [{"actor_id": 5, "actor_type": "RepositoryRole", "bypass_mode": "always"}] else [] end) diff --git a/settings/ruleset-main.json b/settings/ruleset-main.json index 678c83f..b5584a8 100644 --- a/settings/ruleset-main.json +++ b/settings/ruleset-main.json @@ -34,6 +34,13 @@ { "context": "checks gate", "integration_id": 15368 } ] } + }, + { + "type": "copilot_code_review", + "parameters": { + "review_on_push": true, + "review_draft_pull_requests": false + } } ] } diff --git a/tests/test_boilerplate.py b/tests/test_boilerplate.py index ece460a..86892ea 100644 --- a/tests/test_boilerplate.py +++ b/tests/test_boilerplate.py @@ -307,10 +307,11 @@ def test_codecov_ships_only_with_the_overlays_that_report_coverage(): assert node.read_text() == python.read_text() -def ruleset(*gates: str, admin_bypass: bool = False) -> dict: +def ruleset(*gates: str, admin_bypass: bool = False, copilot_review: bool = False) -> dict: result = subprocess.run( ["jq", "--arg", "gates", "".join(f"{g}\n" for g in gates), "--arg", "admin_bypass", str(admin_bypass).lower(), + "--arg", "copilot_review", str(copilot_review).lower(), "-f", str(ROOT / "scripts/ruleset.jq"), str(ROOT / "settings/ruleset-main.json")], check=True, capture_output=True, text=True, ) @@ -349,6 +350,21 @@ def test_ruleset_binds_admins_unless_the_repo_opts_out(): assert required_checks(ruleset("checks gate", admin_bypass=True)) == ["checks gate"] +@pytest.mark.parametrize("gates", [("checks gate",), ()]) +def test_ruleset_requests_a_copilot_review_on_every_push(gates): + # A fresh review per push: copilot-auto-merge.yml acts only on an approval + # of the PR's current head. + rules = ruleset(*gates, copilot_review=True)["rules"] + (rule,) = [r for r in rules if r["type"] == "copilot_code_review"] + assert rule["parameters"] == {"review_on_push": True, "review_draft_pull_requests": False} + + +@pytest.mark.parametrize("gates", [("checks gate",), ()]) +def test_ruleset_has_no_copilot_review_without_the_pilot(gates): + # Elsewhere the rule would spend Copilot credits on reviews nothing uses. + assert not [r for r in ruleset(*gates)["rules"] if r["type"] == "copilot_code_review"] + + def test_template_claude_md_is_a_symlink(): assert os.readlink(ROOT / "template/core/CLAUDE.md") == "AGENTS.md" diff --git a/tests/test_copilot_auto_merge.py b/tests/test_copilot_auto_merge.py new file mode 100644 index 0000000..ee283e1 --- /dev/null +++ b/tests/test_copilot_auto_merge.py @@ -0,0 +1,258 @@ +"""The Copilot auto-merge pilot's guards (copilot-auto-merge.yml, issue #71): +checked statically, and by running the sweep against a fake `gh`.""" + +from __future__ import annotations + +import subprocess +from pathlib import Path + +import pytest +import yaml + +ROOT = Path(__file__).resolve().parent.parent +WORKFLOW = ROOT / ".github/workflows/copilot-auto-merge.yml" +REPO = "atdr/github-boilerplate" +OWNER = "atdr" +WRITE = {"actions": "write", "contents": "write", "pull-requests": "write"} + + +@pytest.fixture(scope="module") +def workflow() -> dict: + return yaml.safe_load(WORKFLOW.read_text()) + + +def steps(workflow: dict): + for job in workflow["jobs"].values(): + yield from job["steps"] + + +def test_triggers_run_only_the_default_branch_copy(workflow): + # PyYAML reads a bare `on:` as True. + on = workflow.get("on", workflow.get(True)) + # pull_request_review would run the PR's own copy with a write token, and + # a manual workflow_dispatch run can pick a PR branch's copy. + assert set(on) == {"schedule"} + + +def test_permissions_are_minimal(workflow): + assert workflow["permissions"] == {} + for job in workflow["jobs"].values(): + assert job["permissions"] == WRITE + + +def test_nothing_from_the_pr_is_checked_out_or_run(workflow): + for step in steps(workflow): + assert "uses" not in step, "no actions at all, actions/checkout above all" + assert "${{" not in step["run"], "values reach run through env" + assert "checkout" not in WORKFLOW.read_text() + + +def test_merges_directly_never_through_auto_merge(workflow): + # Auto-merge stays on across pushes, so a push Copilot never saw could + # merge; a direct merge bound to the approved commit cannot. + assert set(workflow["jobs"]) == {"sweep"} + assert "--auto" not in WORKFLOW.read_text() + + +def test_concurrency_never_cancels(workflow): + assert workflow["concurrency"]["cancel-in-progress"] is False + + +def test_sweep_guards_are_in_the_script(workflow): + job = workflow["jobs"]["sweep"] + (step,) = job["steps"] + script = step["run"] + assert step["env"]["OWNER"] == "${{ github.repository_owner }}" + assert '[ "$author" != "$OWNER" ] || [ "$head_repo" != "$REPO" ]' in script + assert "release-please--*" in script + assert "dependabot/*" in script + assert 'select(.user.login == "copilot-pull-request-reviewer[bot]")' in script + assert '[ "$latest" = "APPROVED $head_sha" ]' in script + assert '--match-head-commit "$head_sha"' in script + + +FAKE_GH = r"""#!/bin/bash +echo "$*" >>"$FAKE/calls" +if [ "$1" = pr ] && [ "$2" = view ]; then + n=$3 + [ ! -f "$FAKE/view-fail-$n" ] || exit 1 + # The squash commit; the tip is the same unless a test moves it. + case "$*" in *mergeCommit*) echo cccccccccccccccccccccccccccccccccccccccc; exit 0 ;; esac + if [ -f "$FAKE/state-$n" ]; then cat "$FAKE/state-$n"; else echo CLEAN; fi + exit 0 +fi +# Another merge landing between the two dispatches, when a test asks for one. +if [ "$1 $2 $3" = "workflow run ci.yml" ] && [ -f "$FAKE/advance-on-ci" ]; then + echo dddddddddddddddddddddddddddddddddddddddd >"$FAKE/tip" +fi +if [ "$1" = api ]; then + for arg in "$@"; do + case "$arg" in + repos/*/pulls\?*) + case "$arg" in *"base=main&"*) ;; *) echo "wrong base: $arg" >&2; exit 1 ;; esac + cat "$FAKE/prs" + ;; + repos/*/reviews*) + n=${arg#*/pulls/} + n=${n%%/*} + [ ! -f "$FAKE/reviews-fail-$n" ] || exit 1 + [ ! -f "$FAKE/reviews-$n" ] || cat "$FAKE/reviews-$n" + ;; + "repos/$REPO") echo main ;; + "repos/$REPO/commits/main") + if [ -f "$FAKE/tip" ]; then cat "$FAKE/tip"; else echo cccccccccccccccccccccccccccccccccccccccc; fi + ;; + esac + done + exit 0 +fi +[ ! -f "$FAKE/fail" ] +""" + +HEAD = "a" * 40 +OLD = "b" * 40 + + +def pr(number, author=OWNER, head_repo=REPO, ref="feat/x", sha=HEAD, draft=False, enabled_by="-"): + return "\t".join([str(number), author, head_repo, ref, sha, str(draft).lower(), enabled_by]) + + +def sweep(tmp_path: Path, prs: list[str], reviews: dict[int, list[str]], fail: bool = False, + states: dict[int, str] | None = None, broken: tuple[str, ...] = ()): + script = yaml.safe_load(WORKFLOW.read_text())["jobs"]["sweep"]["steps"][0]["run"] + bin_dir = tmp_path / "bin" + bin_dir.mkdir() + (bin_dir / "gh").write_text(FAKE_GH) + (bin_dir / "gh").chmod(0o755) + (tmp_path / "prs").write_text("".join(f"{line}\n" for line in prs)) + for number, lines in reviews.items(): + (tmp_path / f"reviews-{number}").write_text("".join(f"{line}\n" for line in lines)) + if fail: + (tmp_path / "fail").touch() + for number, state in (states or {}).items(): + (tmp_path / f"state-{number}").write_text(f"{state}\n") + for name in broken: + (tmp_path / name).touch() + (tmp_path / "calls").touch() + (tmp_path / "sweep.sh").write_text(script) + result = subprocess.run( + # How GitHub runs a `run:` step on Linux. + ["bash", "--noprofile", "--norc", "-eo", "pipefail", str(tmp_path / "sweep.sh")], + env={"PATH": f"{bin_dir}:/usr/bin:/bin", "FAKE": str(tmp_path), "REPO": REPO, + "OWNER": OWNER}, + capture_output=True, text=True, + ) + merges = [c for c in (tmp_path / "calls").read_text().splitlines() if c.startswith("pr merge")] + return result, merges + + +def merge(number: int, sha: str = HEAD) -> str: + return f"pr merge --squash --match-head-commit {sha} --repo {REPO} {number}" + + +def test_each_merge_starts_ci_and_checks_on_the_default_branch(tmp_path): + # A GITHUB_TOKEN merge starts no push run, so the sweep dispatches them. + result, _ = sweep(tmp_path, [pr(1)], {1: [f"APPROVED {HEAD}"]}) + assert result.returncode == 0, result.stderr + calls = (tmp_path / "calls").read_text().splitlines() + assert calls[-5:] == [ + merge(1), + f"workflow run ci.yml --ref main --repo {REPO}", + f"workflow run checks.yml --ref main --repo {REPO}", + f"pr view 1 --repo {REPO} --json mergeCommit --jq .mergeCommit.oid", + f"api repos/{REPO}/commits/main --jq .sha", + ] + assert "::warning" not in result.stdout + + +@pytest.mark.parametrize("when", ["before both dispatches", "between the dispatches"]) +def test_a_merge_overtaken_before_its_dispatches_is_reported(tmp_path, when): + # A dispatched run tests the tip at that moment, so this commit would lack + # one or both runs of its own. + if when == "before both dispatches": + (tmp_path / "tip").write_text("d" * 40 + "\n") + else: + (tmp_path / "advance-on-ci").touch() + result, merges = sweep(tmp_path, [pr(1)], {1: [f"APPROVED {HEAD}"]}) + assert result.returncode == 1 + assert merges == [merge(1)] + assert f"#1: main moved past its merge commit {'c' * 40}" in result.stdout + + +def test_ci_and_checks_accept_a_dispatch(): + for name in ("ci", "checks"): + on = yaml.safe_load((ROOT / f".github/workflows/{name}.yml").read_text()) + on = on.get("on", on.get(True)) + assert "workflow_dispatch" in on + + +def test_sweep_merges_only_an_approval_of_the_current_head(tmp_path): + approved = [f"APPROVED {HEAD}"] + result, merges = sweep( + tmp_path, + [ + pr(1), + pr(2), # approved an older head + pr(3), # approved, then commented on the head + pr(4, author="someone"), + pr(5, head_repo="someone/fork"), + pr(6, head_repo="-"), # fork since deleted + pr(7, author="dependabot[bot]", ref="dependabot/npm/x"), + pr(8, author="github-actions[bot]", ref="release-please--branches--main"), + pr(9, ref="release-please--branches--main"), + pr(10, draft=True), + pr(11, enabled_by=OWNER), # the owner switched auto-merge on + pr(12), # no Copilot review yet + ], + { + 1: approved, + 2: [f"APPROVED {OLD}"], + 3: [f"APPROVED {HEAD}", f"COMMENTED {HEAD}"], + **{n: approved for n in range(4, 12)}, + }, + ) + assert result.returncode == 0, result.stderr + assert merges == [merge(1)] + + +def test_sweep_waits_until_github_reports_the_pr_mergeable(tmp_path): + approved = [f"APPROVED {HEAD}"] + result, merges = sweep( + tmp_path, + [pr(1), pr(2), pr(3), pr(4)], + {n: approved for n in range(1, 5)}, + states={1: "BLOCKED", 2: "BEHIND", 3: "UNSTABLE", 4: "UNKNOWN"}, + ) + assert result.returncode == 0, result.stderr + assert merges == [merge(3)] + + +def test_one_failed_read_does_not_stop_the_sweep(tmp_path): + approved = [f"APPROVED {HEAD}"] + result, merges = sweep( + tmp_path, + [pr(1), pr(2), pr(3)], + {n: approved for n in range(1, 4)}, + broken=("reviews-fail-1", "view-fail-2"), + ) + assert result.returncode == 1 + assert "#1: could not read its reviews" in result.stdout + assert "#2: could not read its merge state" in result.stdout + assert merges == [merge(3)] + + +def test_sweep_fails_visibly_but_tries_every_pr(tmp_path): + result, merges = sweep( + tmp_path, + [pr(1), pr(2)], + {1: [f"APPROVED {HEAD}"], 2: [f"APPROVED {HEAD}"]}, + fail=True, + ) + assert result.returncode == 1 + assert merges == [merge(1), merge(2)] + + +def test_sweep_with_no_open_prs(tmp_path): + result, merges = sweep(tmp_path, [], {}) + assert result.returncode == 0, result.stderr + assert merges == []