Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.
Python agent-orchestrationaiai-agentschatopsdiscordlangchainmcpmicrosoft-teamsmulti-agentopen-sourceopenaiself-hostedslack
fix(ci): keep one unlistable PR from stopping the migration heads sweep
lbangalosbt37511bd07618
diff --git a/.github/workflows/pr-ci.yml b/.github/workflows/pr-ci.ymlindex f28a8ed17..66330e248 100644--- a/.github/workflows/pr-ci.yml+++ b/.github/workflows/pr-ci.yml@@ -90,6 +90,8 @@ jobs:backend:- *shared- 'core/**'+ # Tested from core/tests, so a change to it alone still runs them.+ - 'scripts/check_migration_heads_for_open_prs.py'console:- *shared- 'console/**'diff --git a/core/tests/test_check_migration_heads_for_open_prs.py b/core/tests/test_check_migration_heads_for_open_prs.pynew file mode 100644index 000000000..26d6ed5b0--- /dev/null+++ b/core/tests/test_check_migration_heads_for_open_prs.py@@ -0,0 +1,184 @@+"""Tests for scripts/check_migration_heads_for_open_prs.py, the sweep that+re-checks open PRs' migrations each time a new head lands on main.++`gh` and `git` are replaced by a fake that answers from fixed data, so these+run without a network, a token or a checkout of any PR.+"""++from __future__ import annotations++import importlib.util+import json+import subprocess+from pathlib import Path+from types import ModuleType+from typing import Any++import pytest++REPO_ROOT = Path(__file__).resolve().parents[2]+++def _load_script() -> ModuleType:+ """Import the script, which is not an installed package."""+ path = REPO_ROOT / "scripts" / "check_migration_heads_for_open_prs.py"+ spec = importlib.util.spec_from_file_location(+ "check_migration_heads_for_open_prs", path+ )+ assert spec is not None and spec.loader is not None+ module = importlib.util.module_from_spec(spec)+ spec.loader.exec_module(module)+ return module+++sweep = _load_script()+++def _migration(revision: str, down_revision: str | None) -> str:+ return f'revision: str = "{revision}"\ndown_revision = {down_revision!r}\n'+++def _in_versions(filename: str) -> str:+ return f"{sweep.VERSIONS_PATH}/{filename}"+++class FakeGitHub:+ """Stands in for `run`: answers the `gh` and `git` calls the sweep makes.++ `files` maps a PR number to the paths its files endpoint lists, or to the+ error that endpoint fails with. `heads` maps a PR number to the versions+ directory on its head, as path to file text. Posted statuses are kept in+ `statuses`, by commit.+ """++ def __init__(+ self,+ open_prs: list[dict[str, Any]],+ files: dict[int, list[str] | subprocess.CalledProcessError],+ heads: dict[int, dict[str, str]],+ ) -> None:+ self.open_prs = open_prs+ self.files = files+ self.heads = heads+ self.statuses: dict[str, str] = {}+ self.fetched: int | None = None++ def __call__(+ self,+ *args: str,+ cwd: Path | None = None,+ check: bool = True,+ stdin: str | None = None,+ ) -> subprocess.CompletedProcess[str]:+ if args[:3] == ("gh", "pr", "list"):+ return self._answer(args, json.dumps(self.open_prs))+ if args[:3] == ("gh", "api", "--paginate"):+ number = int(args[3].split("/")[-2])+ listed = self.files[number]+ if isinstance(listed, subprocess.CalledProcessError):+ raise listed+ return self._answer(args, "\n".join(listed))+ if args[:2] == ("git", "fetch"):+ self.fetched = int(args[-1].split("/")[2])+ return self._answer(args, "")+ if args[:2] == ("git", "ls-tree"):+ assert self.fetched is not None+ return self._answer(args, "\n".join(self.heads[self.fetched]))+ if args[:2] == ("git", "show"):+ assert self.fetched is not None+ path = args[2].removeprefix("FETCH_HEAD:")+ return self._answer(args, self.heads[self.fetched][path])+ if args[:2] == ("gh", "api") and "/statuses/" in args[2]:+ assert stdin is not None+ self.statuses[args[2].rsplit("/", 1)[1]] = json.loads(stdin)["state"]+ return self._answer(args, "{}")+ raise AssertionError(f"unexpected command: {args}")++ @staticmethod+ def _answer(args: tuple[str, ...], stdout: str) -> subprocess.CompletedProcess[str]:+ return subprocess.CompletedProcess(list(args), 0, stdout, "")+++def _diff_too_large(number: int) -> subprocess.CalledProcessError:+ return subprocess.CalledProcessError(+ 1,+ ["gh", "api", f"repos/{{owner}}/{{repo}}/pulls/{number}/files"],+ output="",+ stderr="HTTP 406: Sorry, the diff exceeded the maximum number of lines (20000)",+ )+++def test_one_unlistable_pr_does_not_stop_the_sweep(+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path+) -> None:+ """The incident this guards against: a PR whose files GitHub would not+ list ended the sweep before a PR racing for the same parent was flagged,+ and that PR then merged a second head.++ Both PRs must get a status: the racing one red, the unlistable one checked+ anyway and green.+ """+ base = {"a_first.py": _migration("a", None), "b_second.py": _migration("b", "a")}+ for name, text in base.items():+ (tmp_path / name).write_text(text)+ monkeypatch.setattr(sweep, "VERSIONS_DIR", tmp_path)+ monkeypatch.setenv("RUN_URL", "https://example.invalid/run")++ unlistable, racing = "1" * 40, "2" * 40+ github = FakeGitHub(+ open_prs=[+ {"number": 672, "headRefOid": unlistable},+ {"number": 593, "headRefOid": racing},+ ],+ files={672: _diff_too_large(672), 593: [_in_versions("c_racing.py")]},+ heads={+ 672: {_in_versions(name): text for name, text in base.items()},+ 593: {+ _in_versions("a_first.py"): base["a_first.py"],+ _in_versions("c_racing.py"): _migration("c", "a"),+ },+ },+ )+ monkeypatch.setattr(sweep, "run", github)++ assert sweep.main() == 1+ assert github.statuses == {unlistable: "success", racing: "failure"}+++def test_a_pr_that_touches_no_migration_is_not_checked(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ github = FakeGitHub(+ open_prs=[{"number": 7, "headRefOid": "3" * 40}],+ files={7: ["core/switch_core/config.py", "README.md"]},+ heads={},+ )+ monkeypatch.setattr(sweep, "run", github)++ assert sweep.open_prs_touching_migrations() == []+++def test_a_file_list_cut_off_at_the_limit_is_checked_anyway(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """The files endpoint stops at its limit, so a PR with that many files+ and no migration among them may still have one past the cut."""+ pr = {"number": 8, "headRefOid": "4" * 40}+ github = FakeGitHub(+ open_prs=[pr],+ files={8: [f"console/file_{i}.ts" for i in range(sweep.MAX_LISTED_FILES)]},+ heads={},+ )+ monkeypatch.setattr(sweep, "run", github)++ assert sweep.open_prs_touching_migrations() == [pr]+++def test_a_pr_whose_files_cannot_be_listed_is_a_candidate(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ pr = {"number": 9, "headRefOid": "5" * 40}+ github = FakeGitHub(open_prs=[pr], files={9: _diff_too_large(9)}, heads={})+ monkeypatch.setattr(sweep, "run", github)++ assert sweep.open_prs_touching_migrations() == [pr]diff --git a/scripts/check_migration_heads_for_open_prs.py b/scripts/check_migration_heads_for_open_prs.pyindex 802103c94..e218455d7 100644--- a/scripts/check_migration_heads_for_open_prs.py+++ b/scripts/check_migration_heads_for_open_prs.py@@ -70,6 +70,9 @@ VERSIONS_DIR = REPO_ROOT / "core" / "switch_core" / "migrations" / "versions"VERSIONS_PATH = "core/switch_core/migrations/versions"STATUS_CONTEXT = "migration-heads-on-merge"PR_LIST_LIMIT = 1000+# GitHub's pull request files endpoint lists at most this many files, however+# many pages are asked for.+MAX_LISTED_FILES = 3000# The repository is never named in an argument list: `gh` takes it from the# checkout, and its `{owner}/{repo}` placeholder fills it in for an API path.@@ -205,7 +208,10 @@ def run(def open_prs_touching_migrations() -> list[dict[str, Any]]:- """Open pull requests that add or change a migration.+ """Open pull requests that add or change a migration, or might.++ A PR whose files cannot be listed is included rather than skipped; see+ `pr_touches_migrations`.No `--repo`: `gh` resolves it from the checkout this runs in, which keepsthe repository out of the argument list entirely.@@ -232,12 +238,53 @@ def open_prs_touching_migrations() -> list[dict[str, Any]]:f"{len(open_prs)} open PRs reached the --limit of {PR_LIST_LIMIT}; ""some were not listed and would be skipped without a word")- candidates = []- for pr in open_prs:- diff = run("gh", "pr", "diff", str(pr["number"]), "--name-only")- if any(line.startswith(VERSIONS_PATH) for line in diff.stdout.splitlines()):- candidates.append(pr)- return candidates+ return [+ pr for pr in open_prs if pr_touches_migrations(int(pr["number"])) is not False+ ]+++def pr_touches_migrations(number: int) -> bool | None:+ """Whether the PR changes a file under `versions/`, from its file list.++ The files endpoint rather than `gh pr diff`: GitHub refuses a diff of more+ than 20,000 lines (HTTP 406), and one such PR used to abort this whole+ sweep before the PRs after it were looked at, which let a second head+ reach main unflagged (#593, merged while #672 was open).++ None when the answer is unknown: the list could not be read, or it may+ have been cut off at `MAX_LISTED_FILES`. The caller then checks the PR+ anyway. Checking a PR that turns out not to touch migrations costs one+ fetch; skipping one that does is how a second head lands.+ """+ try:+ listing = run(+ "gh",+ "api",+ "--paginate",+ # 100 is the largest page GitHub serves; the default is 30.+ f"repos/{{owner}}/{{repo}}/pulls/{number}/files?per_page=100",+ "--jq",+ ".[].filename",+ )+ except (subprocess.CalledProcessError, OSError) as exc:+ detail = (+ exc.stderr.strip()+ if isinstance(exc, subprocess.CalledProcessError) and exc.stderr+ else str(exc)+ )+ print(+ f"::warning::PR #{number}: could not list its files, so it is checked anyway: {detail}"+ )+ return None+ files = listing.stdout.splitlines()+ if any(path.startswith(f"{VERSIONS_PATH}/") for path in files):+ return True+ if len(files) >= MAX_LISTED_FILES:+ print(+ f"::warning::PR #{number}: its file list may be cut off at {MAX_LISTED_FILES}, so it is checked anyway"+ )+ return None+ return Falsedef fetch_pr_revisions(pr_number: int) -> list[RevisionFile]: