diff --git a/.gitignore b/.gitignore index 1e647053..94c1be88 100644 --- a/.gitignore +++ b/.gitignore @@ -51,6 +51,8 @@ !bin/deploy_release/ !bin/deploy_release/*.py !bin/failure.py +!bin/forge/ +!bin/forge/*.py !bin/git_cli/ !bin/git_cli/*.py !bin/judge-replay diff --git a/README.md b/README.md index a9b5d21e..e1fdcc17 100644 --- a/README.md +++ b/README.md @@ -194,14 +194,14 @@ Install puts only `loop-claude` on `PATH`; call the others by their path, such a - `touch-predictions record ...` appends what a chunk's items were predicted to touch, under `Touches:` in their details, beside the files and parts its branch touched, to the record the config names as `touch-predictions`; `touch-predictions --help` shows the spelling and how a part is named. `chunk-preflight` reads the same predictions to name the items that may start chunks at once. - `wait-for [] [--timeout ]` waits until a file a background job writes holds a line matching the pattern, and prints it. -`bin/` is the import root: each launcher puts it on `sys.path`, so a module imports another by its path under `bin/`, such as `from claude_code.entries import parse_entry`. Each tool's package holds its tests. Beside the packages, `bin/`'s root holds only the launchers, the tools of one file with their tests, `test_env.py`, `failure.py` and `setup.cfg`. `failure.py`'s `Failure`, which modules across `bin/` return for a failed step, belongs to no single package. `atlassian/`, `branch_chunks/`, `claude_code/`, `command_lines/`, `git_cli/`, `loop_effects/`, `loop_state/`, `machine_load/`, `model_routing/`, `notifier/`, `plan/`, `plan_store/`, `record_store/`, `release_plan/`, `review_rules/`, `rung_replay/`, `text_files/` and `transcript_archive/` serve more than one tool. No package imports one that imports it back, which `repo_checks/test_package_cycles.py` checks. +`bin/` is the import root: each launcher puts it on `sys.path`, so a module imports another by its path under `bin/`, such as `from claude_code.entries import parse_entry`. Each tool's package holds its tests. Beside the packages, `bin/`'s root holds only the launchers, the tools of one file with their tests, `test_env.py`, `failure.py` and `setup.cfg`. `failure.py`'s `Failure`, which modules across `bin/` return for a failed step, belongs to no single package. `atlassian/`, `branch_chunks/`, `claude_code/`, `command_lines/`, `forge/`, `git_cli/`, `loop_effects/`, `loop_state/`, `machine_load/`, `model_routing/`, `notifier/`, `plan/`, `plan_store/`, `record_store/`, `release_plan/`, `review_rules/`, `rung_replay/`, `text_files/` and `transcript_archive/` serve more than one tool. No package imports one that imports it back, which `repo_checks/test_package_cycles.py` checks. - `adf_codec/` converts markdown to ADF, the rich text of Jira Cloud and Confluence, and back, in a throwaway container running a pinned `@larvit/adf-codec`. It builds the container's image inside a heavy slot. - `atlassian/` holds what the Jira and Confluence backends share (`client.py`): one Atlassian account's credentials, the request both backends send, and each service's error messages. - `branch_chunks/` cuts a default branch into chunks and finds, for each, the items it shipped and the later chunks that fixed its code, for `score_outcomes` and `routing_figures`. It imports `reading_cost`'s record path. -- `chunk_pr` imports `deploy_release`'s forge calls, and `forge_of` as `deploy_release` does. - `command_lines/` reads a shell command: its simple commands, and a docker call's subcommand and memory limits, for `command_patterns`, `reading_cost`, `routing_figures`, `rule_hook` and `run_heavy`. -- `deploy_release` imports `chunk_preflight`'s `forge_of`, which tells a GitHub origin from a Gitea one, and `release_plan`'s `release_standing`. +- `deploy_release` imports `release_plan`'s `release_standing`. +- `forge/` tells a GitHub origin from a Gitea one and runs that forge's CLI (`commands.py`), and finds a pull request and waits for its checks (`pulls.py`), for `chunk_preflight`, `chunk_pr`, `deploy_release` and `larv_review`. - `git_cli/` runs git for every tool (`git_repo.py`), and reads the git calls in a shell command (`git_command.py`). - `judge_replay` imports `loop_claude`'s judge, verdicts, premises and records. - `loop_claude` runs `chunk-preflight` as a process of its own. diff --git a/bin/chunk_pr/approval.py b/bin/chunk_pr/approval.py index a10118c8..a33b08e5 100644 --- a/bin/chunk_pr/approval.py +++ b/bin/chunk_pr/approval.py @@ -5,8 +5,7 @@ import time import typing from failure import Failure -from chunk_preflight.gathered import Forge -from deploy_release.forge import Run +from forge.commands import Forge, Run # A human answers in hours, so merge polls once a minute and gives up after 90 minutes. That leaves half an hour for the queue and the checks inside the 2 hours a background command may run; a run that needs more is killed before the give-up message prints, and its next run goes on from what it pushed. POLL_SECONDS = 60 diff --git a/bin/chunk_pr/main.py b/bin/chunk_pr/main.py index 3515857b..0e91ea6a 100644 --- a/bin/chunk_pr/main.py +++ b/bin/chunk_pr/main.py @@ -86,10 +86,9 @@ import time import typing from chunk_pr.approval import approval, approved, awaited, unanswered -from deploy_release.forge import checks_passed, gitea, merged_pull, open_pull, required_checks, Run, runner +from forge.pulls import checks_passed, merged_pull, open_pull, required_checks from failure import Failure -from chunk_preflight.gathered import Forge -from chunk_preflight.choices import forge_of +from forge.commands import Forge, forge_of, gitea, Run, runner from project_config.main import located from project_config.settings import ProjectConfig from plan_store.plans import says_no_item diff --git a/bin/chunk_pr/test_chunk_pr.py b/bin/chunk_pr/test_chunk_pr.py index 79b886ea..a8ceafa1 100644 --- a/bin/chunk_pr/test_chunk_pr.py +++ b/bin/chunk_pr/test_chunk_pr.py @@ -15,7 +15,7 @@ from chunk_pr.main import cleaned, merged, QUEUE, step, turn, USAGE from failure import Failure from test_env import isolated_env from project_config.settings import ProjectConfig -from deploy_release.forge import Run +from forge.commands import Run from chunk_pr.approval import THREADS TOOL = pathlib.Path(__file__).resolve().parents[1] / "chunk-pr" diff --git a/bin/chunk_preflight/choices.py b/bin/chunk_preflight/choices.py index b8a48c57..c3468b8a 100644 --- a/bin/chunk_preflight/choices.py +++ b/bin/chunk_preflight/choices.py @@ -1,7 +1,7 @@ """The pure choices the preflight's steps act on.""" import pathlib -from chunk_preflight.gathered import Forge +from forge.commands import Forge # The command each forge's CLI lists pull requests with; pulls_command adds the flags that pick their rows and fields. PULLS_LIST: dict[Forge, tuple[str, ...]] = {"gh": ("gh", "pr", "list"), "tea": ("tea", "pulls", "list")} @@ -26,10 +26,6 @@ def worktree_add(branch: str, path: pathlib.Path, default: str, local: bool, pus return ["worktree", "add", "--quiet", "--no-track", "-b", branch, str(path), default], f"added on the new branch {branch}, made off {default}." -def forge_of(origin_url: str) -> Forge: - return "gh" if "github.com" in origin_url else "tea" - - def pulls_command(forge: Forge, branch: str) -> list[str]: """The command that lists open pull requests: gh only branch's, by number and URL; tea every one, by index, head and URL, for facts.open_pulls to pick branch's.""" if forge == "gh": diff --git a/bin/chunk_preflight/facts.py b/bin/chunk_preflight/facts.py index 33f8c3ba..f99ea8cf 100644 --- a/bin/chunk_preflight/facts.py +++ b/bin/chunk_preflight/facts.py @@ -4,7 +4,8 @@ from touch_predictions.beside import chunks_beside from release_plan.next_chunk import cut_name, NextChunk, NoItem from release_plan.release_standing import dated_cut, LastCut, unreleased_entries, UNSEEN_RELEASES from failure import Failure -from chunk_preflight.gathered import Forge, Gathered, ItemFacts, NamedPath, Ran +from forge.commands import Forge +from chunk_preflight.gathered import Gathered, ItemFacts, NamedPath, Ran from text_files.markdown_text import listed, quoted from chunk_preflight.choices import PULLS_LIST from plan.release import version diff --git a/bin/chunk_preflight/gathered.py b/bin/chunk_preflight/gathered.py index 0656e15e..ba81bdbe 100644 --- a/bin/chunk_preflight/gathered.py +++ b/bin/chunk_preflight/gathered.py @@ -1,15 +1,12 @@ """What the preflight gathers, which facts.render states.""" import dataclasses import pathlib -import typing from failure import Failure +from forge.commands import Forge from release_plan.release_standing import LastCut from release_plan.next_chunk import NextChunk, NoItem -# The CLI that lists a forge's pull requests: gh for a github.com origin, tea for any other. -Forge = typing.Literal["gh", "tea"] - @dataclasses.dataclass(frozen=True) class Ran: diff --git a/bin/chunk_preflight/main.py b/bin/chunk_preflight/main.py index c1490a77..daeb8a96 100644 --- a/bin/chunk_preflight/main.py +++ b/bin/chunk_preflight/main.py @@ -78,13 +78,14 @@ import typing from git_cli.git_repo import commit_exists, default_branch, file_at, git, top from failure import Failure -from chunk_preflight.gathered import Forge, Gathered, ItemFacts, NamedPath, Ran, Worktree -from chunk_preflight.choices import forge_of, pulls_command, worktree_add, worktree_on +from forge.commands import Forge, forge_of +from chunk_preflight.gathered import Gathered, ItemFacts, NamedPath, Ran, Worktree from chunk_preflight.facts import JOINING_DETAILS, render from project_config.main import located from plan.named_paths import named_paths from release_plan.next_chunk import NextChunk, NoItem, plan_name, planned from release_plan.release_standing import project_plan +from chunk_preflight.choices import pulls_command, worktree_add, worktree_on BIN = pathlib.Path(__file__).resolve().parents[1] # Each tool run in the item's worktree, {item} standing for the item's ID. diff --git a/bin/chunk_preflight/test_chunk_preflight.py b/bin/chunk_preflight/test_chunk_preflight.py index 5157f150..545cede4 100644 --- a/bin/chunk_preflight/test_chunk_preflight.py +++ b/bin/chunk_preflight/test_chunk_preflight.py @@ -13,7 +13,6 @@ from project_config.templates import BRANCH from release_plan.next_chunk import CarriedOver, NoItem from chunk_preflight import choices from failure import Failure -from chunk_preflight.choices import forge_of, worktree_add from chunk_preflight.gathered import Gathered, ItemFacts, NamedPath, Ran, Worktree from test_env import isolated_env from release_plan.release_standing import LastCut, UNSEEN_RELEASES @@ -21,6 +20,7 @@ from chunk_preflight.main import line_count, ran, seconds, TIMEOUT from release_plan import next_chunk from chunk_preflight.facts import open_pulls, render from plan_store.todo_md import plan_of +from chunk_preflight.choices import worktree_add from project_config.test_project_config import write_config BIN = pathlib.Path(__file__).resolve().parents[1] @@ -653,10 +653,6 @@ class Gathering(unittest.TestCase): self.assertEqual(worktree_add("item-3", path, "origin/main", False, True)[0], ["worktree", "add", "--quiet", "--track", "-b", "item-3", "/c/proj-item-3", "origin/item-3"]) self.assertEqual(worktree_add("item-3", path, "origin/main", False, False)[0], ["worktree", "add", "--quiet", "--no-track", "-b", "item-3", "/c/proj-item-3", "origin/main"]) - def test_lists_pull_requests_with_gh_on_a_github_origin_and_with_tea_elsewhere(self): - self.assertEqual(forge_of("git@github.com:o/r.git"), "gh") - self.assertEqual(forge_of("https://gitea.larvit.se/o/r.git"), "tea") - def test_reads_the_timeout_setting_as_seconds_above_0_and_else_takes_the_default(self): self.assertEqual(seconds(None), TIMEOUT) self.assertEqual(seconds("2.5"), 2.5) diff --git a/bin/deploy_release/cut.py b/bin/deploy_release/cut.py index c39a8fd6..b764bfbd 100644 --- a/bin/deploy_release/cut.py +++ b/bin/deploy_release/cut.py @@ -14,9 +14,10 @@ from deploy_release.deploy import deploy from failure import Failure from plan_store.todo_md import FilePlan from record_store.stores import FileStore -from deploy_release import forge -from chunk_preflight.choices import forge_of +from forge.commands import forge_of, runner from plan.release import newest_tag, Release, UNRELEASED_HEADING, version +from forge import pulls +from deploy_release import release_pull from project_config import templates @@ -153,7 +154,7 @@ def land(clone: str, ref: str, named: str, title: str) -> Failure | None: url = git("config", "--get", "remote.origin.url", cwd=clone) if isinstance(url, Failure): return url - on, run, branch, base = forge_of(url), forge.runner(clone), f"release-{named}", ref.removeprefix("origin/") + on, run, branch, base = forge_of(url), runner(clone), f"release-{named}", ref.removeprefix("origin/") head = branch_head(branch, clone) if isinstance(head, Failure): return head @@ -168,23 +169,23 @@ def land(clone: str, ref: str, named: str, title: str) -> Failure | None: head = branch_head(branch, clone) or Failure(f"pushed {branch}, but origin holds no such branch") if isinstance(head, Failure): return head - number = forge.open_pull(on, branch, head, run) + number = pulls.open_pull(on, branch, head, run) if number is None: - if isinstance(created := forge.create_pull(on, branch, base, title, run), Failure): + if isinstance(created := release_pull.create_pull(on, branch, base, title, run), Failure): return created - number = forge.open_pull(on, branch, head, run) or Failure(f"opened a pull request for {branch}, but the forge lists none from origin at {head[:12]}") + number = pulls.open_pull(on, branch, head, run) or Failure(f"opened a pull request for {branch}, but the forge lists none from origin at {head[:12]}") if isinstance(number, str): said(f"opened pull request {number}") if isinstance(number, Failure): return number - required = forge.required_checks(on, base, run) + required = pulls.required_checks(on, base, run) if isinstance(required, Failure): return required said(f"waiting for the checks of pull request {number}" + (f"; {base} requires {', '.join(sorted(required))}" if required else "")) - if isinstance(passed := forge.checks_passed(on, number, head, required, run), Failure): + if isinstance(passed := pulls.checks_passed(on, number, head, required, run), Failure): return passed said(f"the checks of pull request {number} passed") - if isinstance(merged := forge.merge(on, number, head, run), Failure): + if isinstance(merged := release_pull.merge(on, number, head, run), Failure): return merged said(f"merged pull request {number}") return None diff --git a/bin/deploy_release/release_pull.py b/bin/deploy_release/release_pull.py new file mode 100644 index 00000000..5d919e01 --- /dev/null +++ b/bin/deploy_release/release_pull.py @@ -0,0 +1,23 @@ +"""Opening and merging a cut's pull request, with gh where origin is on GitHub and with tea elsewhere. + +merge is pinned to head, the commit origin's release branch points at: the forge merges the pull request only while its head is still that commit, so a push to the branch after its checks passed is never merged. +""" +from failure import Failure +from forge.commands import Forge, gitea, Run + + +def create_pull(forge: Forge, branch: str, base: str, title: str, run: Run) -> Failure | None: + body = ["--body" if forge == "gh" else "--description", "Rename ## Unreleased in CHANGELOG.md to the version."] + command = ["gh", "pr"] if forge == "gh" else ["tea", "pulls"] + created = run([*command, "create", "--head", branch, "--base", base, "--title", title, *body]) + return created if isinstance(created, Failure) else None + + +def merge(forge: Forge, number: str, head: str, run: Run) -> Failure | None: + """Merge pull request number, fast-forward only on Gitea and by rebase on GitHub. The forge refuses where the pull request's head is no longer head.""" + if forge == "gh": + # GitHub has no fast-forward-only merge. A rebase merge of a branch already on top of main is the nearest, though it may give the commit a new hash, so the cut finds it on main by its title. + merged = run(["gh", "pr", "merge", number, "--rebase", "--match-head-commit", head]) + else: + merged = gitea(run, f"repos/{{owner}}/{{repo}}/pulls/{number}/merge", "Do=fast-forward-only", f"head_commit_id={head}") + return merged if isinstance(merged, Failure) else None diff --git a/bin/deploy_release/test_deploy_release.py b/bin/deploy_release/test_deploy_release.py index e91321c3..851a1487 100644 --- a/bin/deploy_release/test_deploy_release.py +++ b/bin/deploy_release/test_deploy_release.py @@ -1,16 +1,13 @@ -import itertools import json import os import pathlib import subprocess import sys import tempfile -import typing import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) -from deploy_release.forge import checks_passed, POLL_SECONDS, required_checks, WAIT_SECONDS from failure import Failure from test_env import isolated_env @@ -229,6 +226,10 @@ save() """.replace("PYTHON", sys.executable) +# The protection of main that requires CI's check. +REQUIRED = '{"name": "main", "enable_status_check": true, "status_check_contexts": ["CI / tests (pull_request)"]}' + + class Cut(unittest.TestCase): """A development clone whose next release, 0.2.0, holds no item, has its release review landed and one change unreleased; and a ~/.claude on v0.1.0.""" @@ -486,58 +487,6 @@ class Cut(unittest.TestCase): self.assertEqual(self.origin_git("log", "-1", "--format=%s", "main").strip(), "Release review of 0.2.0") -def tea(statuses: typing.Iterable[str], branch: str = '{"name": "main"}') -> typing.Callable[[list[str]], str]: - """A tea answering each read of the head's status with the next of statuses, and of branch main with branch.""" - answers = iter(statuses) - return lambda command: branch if command[-1] == "repos/{owner}/{repo}/branches/main" else next(answers) - - -REQUIRED = '{"name": "main", "enable_status_check": true, "status_check_contexts": ["CI / tests (pull_request)"]}' - - -class RequiredChecks(unittest.TestCase): - def test_reads_on_tea_the_checks_the_protection_of_the_base_requires(self): - self.assertEqual(required_checks("tea", "main", tea([], REQUIRED)), {"CI / tests (pull_request)"}) - - def test_reads_none_on_tea_where_the_protection_turns_status_checks_off(self): - self.assertEqual(required_checks("tea", "main", tea([], '{"name": "main", "enable_status_check": false, "status_check_contexts": ["CI / tests (pull_request)"]}')), set()) - - def test_fails_on_tea_where_the_branch_reads_in_another_shape(self): - self.assertIsInstance(required_checks("tea", "main", tea([], "[]")), Failure) - - def test_reads_none_on_github_whose_watch_covers_them(self): - self.assertEqual(required_checks("gh", "main", self.fail), set()) - - -class ChecksPassed(unittest.TestCase): - def test_waits_on_tea_while_a_check_is_pending(self): - slept = [] - self.assertIsNone(checks_passed("tea", "1", "abc", set(), tea(['{"state": "pending", "total_count": 1}', '{"state": "success", "total_count": 1}']), slept.append)) - self.assertEqual(slept, [POLL_SECONDS]) - - def test_takes_a_commit_with_no_status_on_tea_as_passed_where_the_base_requires_none(self): - self.assertIsNone(checks_passed("tea", "1", "abc", set(), tea(['{"state": "pending", "total_count": 0}']), self.fail)) - - def test_waits_on_tea_for_a_check_the_base_requires_until_it_reports_and_passes(self): - slept = [] - statuses = ['{"state": "pending", "total_count": 0, "statuses": []}', '{"state": "success", "total_count": 1, "statuses": [{"context": "lint", "status": "success"}]}', '{"state": "success", "total_count": 2, "statuses": [{"context": "lint", "status": "success"}, {"context": "CI / tests (pull_request)", "status": "success"}]}'] - self.assertIsNone(checks_passed("tea", "1", "abc", {"CI / tests (pull_request)"}, tea(statuses), slept.append)) - self.assertEqual(slept, [POLL_SECONDS, POLL_SECONDS]) - - def test_takes_on_tea_a_required_glob_as_met_by_a_check_it_matches(self): - passed = '{"state": "success", "total_count": 1, "statuses": [{"context": "CI / tests (pull_request)", "status": "success"}]}' - self.assertIsNone(checks_passed("tea", "1", "abc", {"CI / *"}, tea([passed]), self.fail)) - - def test_gives_up_on_tea_after_the_wait_naming_a_required_check_that_never_reported(self): - slept = [] - waited = checks_passed("tea", "1", "abc", {"CI / tests (pull_request)"}, tea(itertools.repeat('{"state": "pending", "total_count": 0, "statuses": []}')), slept.append) - self.assertIsInstance(waited, Failure) - self.assertIn("CI / tests (pull_request)", waited.reason) - self.assertEqual(sum(slept), WAIT_SECONDS) - - def test_fails_on_tea_where_a_check_failed(self): - self.assertIsInstance(checks_passed("tea", "1", "abc", set(), tea(['{"state": "failure", "total_count": 1}']), self.fail), Failure) - if __name__ == "__main__": unittest.main() diff --git a/bin/forge/__init__.py b/bin/forge/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/bin/forge/commands.py b/bin/forge/commands.py new file mode 100644 index 00000000..576c5aad --- /dev/null +++ b/bin/forge/commands.py @@ -0,0 +1,42 @@ +"""Which forge a checkout's origin is on, and running that forge's CLI in the checkout.""" +import json +import subprocess +import typing + +from failure import Failure + +# The CLI of the forge that origin is on, as forge_of picks it. +Forge = typing.Literal["gh", "tea"] + +# Runs a command in the checkout: what it printed, or a Failure with its reason. +Run = typing.Callable[[list[str]], str | Failure] + + +def forge_of(origin_url: str) -> Forge: + return "gh" if "github.com" in origin_url else "tea" + + +def runner(cwd: str) -> Run: + def run(command: list[str]) -> str | Failure: + try: + done = subprocess.run(command, capture_output=True, cwd=cwd, encoding="utf-8", errors="replace", stdin=subprocess.DEVNULL) + except OSError as failure: + return Failure(f"{command[0]} could not start: {failure}") + if done.returncode: + return Failure(f"{' '.join(command[:3])}: {(done.stderr or done.stdout).strip()}") + return done.stdout + return run + + +def gitea(run: Run, endpoint: str, *fields: str) -> typing.Any | Failure: + """What Gitea's API answered at endpoint, parsed; with fields, `key=value` strings, a POST of them. tea api exits 0 on an HTTP error too, so an answer with Gitea's `message` key is a Failure.""" + answered = run(["tea", "api", *(["--method", "POST"] if fields else []), *[word for field in fields for word in ("--field", field)], endpoint]) + if isinstance(answered, Failure) or not answered.strip(): + return answered + try: + parsed = json.loads(answered) + except ValueError: + return Failure(f"tea api {endpoint} printed no JSON: {answered.strip()[:200]}") + if isinstance(parsed, dict) and "message" in parsed: + return Failure(f"tea api {endpoint}: {parsed['message']}") + return parsed diff --git a/bin/deploy_release/forge.py b/bin/forge/pulls.py similarity index 60% rename from bin/deploy_release/forge.py rename to bin/forge/pulls.py index 047a5ae1..5644a66f 100644 --- a/bin/deploy_release/forge.py +++ b/bin/forge/pulls.py @@ -1,50 +1,16 @@ -"""The pull request steps of a cut. They run gh where origin is on GitHub and tea elsewhere, as chunk_preflight tells the two apart. - -Each step is pinned to head, the commit origin's release branch points at: the cut takes only a pull request from origin itself whose head is that commit, and the forge merges it only while its head is still that commit. So a fork's pull request from a branch of the same name, or a push to the branch after its checks passed, is never merged. -""" +"""Finding a pull request from origin's own branch, and waiting for the checks on its head commit.""" import fnmatch -import json -import subprocess import time import typing from failure import Failure -from chunk_preflight.choices import Forge +from forge.commands import Forge, gitea, Run -# How long the cut waits between two reads of the head commit's status on Gitea. +# How long checks_passed waits between two reads: of the head commit's status on Gitea, or of gh's watch while it reports no checks. POLL_SECONDS = 10 -# How long the cut waits for the checks before it gives up; CI took about 3 minutes in 2026-11. +# How long checks_passed waits on Gitea for the checks before it gives up; CI took about 3 minutes in 2026-11. gh's watch waits as long as the checks run. WAIT_SECONDS = 30 * 60 -# Runs a command in the clone: what it printed, or a Failure with its reason. -Run = typing.Callable[[list[str]], str | Failure] - - -def runner(cwd: str) -> Run: - def run(command: list[str]) -> str | Failure: - try: - done = subprocess.run(command, capture_output=True, cwd=cwd, encoding="utf-8", errors="replace", stdin=subprocess.DEVNULL) - except OSError as failure: - return Failure(f"{command[0]} could not start: {failure}") - if done.returncode: - return Failure(f"{' '.join(command[:3])}: {(done.stderr or done.stdout).strip()}") - return done.stdout - return run - - -def gitea(run: Run, endpoint: str, *fields: str) -> typing.Any | Failure: - """What Gitea's API answered at endpoint, parsed; with fields, `key=value` strings, a POST of them. tea api exits 0 on an HTTP error too, so an answer with Gitea's `message` key is a Failure.""" - answered = run(["tea", "api", *(["--method", "POST"] if fields else []), *[word for field in fields for word in ("--field", field)], endpoint]) - if isinstance(answered, Failure) or not answered.strip(): - return answered - try: - parsed = json.loads(answered) - except ValueError: - return Failure(f"tea api {endpoint} printed no JSON: {answered.strip()[:200]}") - if isinstance(parsed, dict) and "message" in parsed: - return Failure(f"tea api {endpoint}: {parsed['message']}") - return parsed - def open_pull(forge: Forge, branch: str, head: str, run: Run) -> str | None | Failure: """The number of the open pull request from origin's own branch whose head is head; None where there is none.""" @@ -75,13 +41,6 @@ def pull_at(forge: Forge, branch: str, head: str, state: typing.Literal["open", return Failure(f"tea api listed pull requests in a shape that can't be read: {str(pulls)[:200]}") -def create_pull(forge: Forge, branch: str, base: str, title: str, run: Run) -> Failure | None: - body = ["--body" if forge == "gh" else "--description", "Rename ## Unreleased in CHANGELOG.md to the version."] - command = ["gh", "pr"] if forge == "gh" else ["tea", "pulls"] - created = run([*command, "create", "--head", branch, "--base", base, "--title", title, *body]) - return created if isinstance(created, Failure) else None - - def required_checks(forge: Forge, base: str, run: Run) -> set[str] | Failure: """The checks, or glob patterns of checks, that the forge's protection of the branch base requires before a merge. None on GitHub, where gh's watch covers them. @@ -132,13 +91,3 @@ def checks_passed(forge: Forge, number: str, head: str, required: set[str], run: sleep(POLL_SECONDS) waited_on = f"{', '.join(missing)}, which the protection requires, never passed" if missing else "a check is still pending" return Failure(f"gave up on pull request {number} after {WAIT_SECONDS // 60} minutes: {waited_on}") - - -def merge(forge: Forge, number: str, head: str, run: Run) -> Failure | None: - """Merge pull request number, fast-forward only on Gitea and by rebase on GitHub. The forge refuses where the pull request's head is no longer head.""" - if forge == "gh": - # GitHub has no fast-forward-only merge. A rebase merge of a branch already on top of main is the nearest, though it may give the commit a new hash, so the cut finds it on main by its title. - merged = run(["gh", "pr", "merge", number, "--rebase", "--match-head-commit", head]) - else: - merged = gitea(run, f"repos/{{owner}}/{{repo}}/pulls/{number}/merge", "Do=fast-forward-only", f"head_commit_id={head}") - return merged if isinstance(merged, Failure) else None diff --git a/bin/forge/test_forge.py b/bin/forge/test_forge.py new file mode 100644 index 00000000..ee0675a4 --- /dev/null +++ b/bin/forge/test_forge.py @@ -0,0 +1,99 @@ +import itertools +import pathlib +import sys +import typing +import unittest + +sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) + +from forge.pulls import checks_passed, open_pull, POLL_SECONDS, required_checks, WAIT_SECONDS +from forge.commands import forge_of, gitea + + +class ForgeOf(unittest.TestCase): + def test_takes_a_github_origin_as_gh_and_any_other_as_tea(self): + self.assertEqual(forge_of("git@github.com:o/r.git"), "gh") + self.assertEqual(forge_of("https://gitea.larvit.se/o/r.git"), "tea") + + +# An answer no call can read, longer than the 200 characters of it that a failure quotes. +LONG = "[" + ", ".join(["1"] * 100) + "]" + + +class Gitea(unittest.TestCase): + def test_fails_with_giteas_message_where_it_answers_an_error(self): + self.assertEqual(gitea(lambda command: '{"message": "not found"}', "repos/o/r").reason, "tea api repos/o/r: not found") + + def test_fails_quoting_the_first_200_characters_where_tea_prints_no_json(self): + self.assertEqual(gitea(lambda command: "x" * 300, "repos/o/r").reason, f"tea api repos/o/r printed no JSON: {'x' * 200}") + + +class OpenPull(unittest.TestCase): + def test_fails_quoting_the_first_200_characters_where_tea_lists_pull_requests_in_another_shape(self): + self.assertEqual(open_pull("tea", "item-3", "abc", lambda command: LONG).reason, f"tea api listed pull requests in a shape that can't be read: {LONG[:200]}") + + +def tea(statuses: typing.Iterable[str], branch: str = '{"name": "main"}') -> typing.Callable[[list[str]], str]: + """A tea answering each read of the head's status with the next of statuses, and of branch main with branch.""" + answers = iter(statuses) + return lambda command: branch if command[-1] == "repos/{owner}/{repo}/branches/main" else next(answers) + + +REQUIRED = '{"name": "main", "enable_status_check": true, "status_check_contexts": ["CI / tests (pull_request)"]}' + + +class RequiredChecks(unittest.TestCase): + def test_reads_on_tea_the_checks_the_protection_of_the_base_requires(self): + self.assertEqual(required_checks("tea", "main", tea([], REQUIRED)), {"CI / tests (pull_request)"}) + + def test_reads_none_on_tea_where_the_protection_turns_status_checks_off(self): + self.assertEqual(required_checks("tea", "main", tea([], '{"name": "main", "enable_status_check": false, "status_check_contexts": ["CI / tests (pull_request)"]}')), set()) + + def test_fails_on_tea_quoting_the_first_200_characters_where_the_branch_reads_in_another_shape(self): + self.assertEqual(required_checks("tea", "main", tea([], LONG)).reason, f"tea api answered branch main in a shape that can't be read: {LONG[:200]}") + + def test_reads_none_on_github_whose_watch_covers_them(self): + self.assertEqual(required_checks("gh", "main", self.fail), set()) + + +class ChecksPassed(unittest.TestCase): + def test_waits_on_tea_while_a_check_is_pending(self): + slept = [] + self.assertIsNone(checks_passed("tea", "1", "abc", set(), tea(['{"state": "pending", "total_count": 1}', '{"state": "success", "total_count": 1}']), slept.append)) + self.assertEqual(slept, [POLL_SECONDS]) + + def test_takes_a_commit_with_no_status_on_tea_as_passed_where_the_base_requires_none(self): + self.assertIsNone(checks_passed("tea", "1", "abc", set(), tea(['{"state": "pending", "total_count": 0}']), self.fail)) + + def test_waits_on_tea_for_a_check_the_base_requires_until_it_reports_and_passes(self): + slept = [] + statuses = ['{"state": "pending", "total_count": 0, "statuses": []}', '{"state": "success", "total_count": 1, "statuses": [{"context": "lint", "status": "success"}]}', '{"state": "success", "total_count": 2, "statuses": [{"context": "lint", "status": "success"}, {"context": "CI / tests (pull_request)", "status": "success"}]}'] + self.assertIsNone(checks_passed("tea", "1", "abc", {"CI / tests (pull_request)"}, tea(statuses), slept.append)) + self.assertEqual(slept, [POLL_SECONDS, POLL_SECONDS]) + + def test_takes_on_tea_a_required_glob_as_met_by_a_check_it_matches(self): + passed = '{"state": "success", "total_count": 1, "statuses": [{"context": "CI / tests (pull_request)", "status": "success"}]}' + self.assertIsNone(checks_passed("tea", "1", "abc", {"CI / *"}, tea([passed]), self.fail)) + + def test_gives_up_on_tea_after_the_wait_naming_each_required_check_that_never_reported(self): + slept, read = [], [] + pending = tea(itertools.repeat('{"state": "pending", "total_count": 0, "statuses": []}')) + waited = checks_passed("tea", "1", "abc", {"lint", "CI / tests (pull_request)"}, lambda command: read.append(command) or pending(command), slept.append) + self.assertEqual(waited.reason, "gave up on pull request 1 after 30 minutes: CI / tests (pull_request), lint, which the protection requires, never passed") + self.assertEqual(sum(slept), WAIT_SECONDS) + self.assertEqual(len(read), WAIT_SECONDS // POLL_SECONDS + 1) + + def test_gives_up_on_tea_after_the_wait_saying_a_check_is_still_pending(self): + waited = checks_passed("tea", "1", "abc", set(), tea(itertools.repeat('{"state": "pending", "total_count": 1, "statuses": []}')), lambda seconds: None) + self.assertEqual(waited.reason, "gave up on pull request 1 after 30 minutes: a check is still pending") + + def test_fails_on_tea_where_a_check_failed(self): + self.assertEqual(checks_passed("tea", "1", "abc", set(), tea(['{"state": "failure", "total_count": 1}']), self.fail).reason, "a check on pull request 1 ended in failure") + + def test_fails_on_tea_quoting_the_first_200_characters_where_the_status_reads_in_another_shape(self): + self.assertEqual(checks_passed("tea", "1", "abc", set(), tea([LONG]), self.fail).reason, f"tea api printed no commit status for abc: {LONG[:200]}") + + + +if __name__ == "__main__": + unittest.main() diff --git a/bin/larv_review/reviewer_header.py b/bin/larv_review/reviewer_header.py index c4f06fde..f9be7e64 100644 --- a/bin/larv_review/reviewer_header.py +++ b/bin/larv_review/reviewer_header.py @@ -5,12 +5,12 @@ import pathlib from git_cli.git_repo import branch_titles, git, top from review_rules.round_titles import cap, round_counts from failure import Failure -from chunk_preflight.choices import forge_of +from forge.commands import forge_of, runner from record_store.stores import goals_store, local_file, store from larv_review.history_commands import history_file, places_report from project_config.main import located, title_templates from review_rules.review_folder import made_review_folder -from deploy_release.forge import open_pull, runner +from forge.pulls import open_pull from larv_review.plan_listing import plan_listing from plan_store.stores import store as plan_store from review_rules.reviewer_context import principles_line, starts_bare diff --git a/todo.md b/todo.md index af83bae4..1f887683 100644 --- a/todo.md +++ b/todo.md @@ -15,7 +15,6 @@ | 303 | 0.12.0 | | defect | **Archive each judge run's prompt and json reply beside the verdict it gave, so judge cost, replays and backups cover the judge as they cover the worker.** | 2 | 2 | 3 | 5 | 3 | 1.4 by 3, 3 by 2 | 4.2 | | 306 | 0.12.0 | | | **Set a strategy that keeps loop-claude's archive and larv-review's review folders backed up and small over the years, compressing older entries without losing any.** | 3 | 2 | 4 | 7 | 5 | 3 by 5 | 3.9 | | 304 | 0.12.0 | 303 | defect | **Archive each review-replay and judge-replay run's transcript beside its result, so a replay that decided a rung can be checked and re-priced later.** | 2 | 2 | 2 | 4 | 3 | 1.4 by 2, 3 by 2 | 3.8 | -| 287 | 0.12.0 | | | **Move the forge helpers `Forge`, `forge_of`, `runner`, `open_pull`, `merged_pull`, `pull_at`, `gitea`, `checks_passed` and `required_checks` into one module that `chunk_preflight`, `chunk_pr`, `deploy_release` and `larv_review` all import from, so no tool imports another tool's internals for them.** | 3 | 1 | 3 | 5 | 5 | code health by 3 | 2.9 | | 387 | 0.12.0 | | | **Bind a project's rung to its ladder in one value, so the routing functions and `Cycle` take one argument for both.** | 3 | 1 | 3 | 5 | 5 | code health by 3 | 2.9 | | 338 | 0.12.0 | | owner | **Script each step that 0.9.0's chunks repeated by hand and a program can compute, filing one item per step, and file this audit again for the next release.** | 1 | 2 | 1 | 2 | 2 | 1.4 by 3, 1.5 by 3 | 2.3 | | 288 | 0.12.0 | | | **Point the two reading-cost decisions at code health and goal 1.4's counter-signal, in place of the goal 1.1 they cite.** | 1 | 2 | 1 | 2 | 1 | code health by 1, 1.4 by 1 | 2.2 | @@ -133,29 +132,6 @@ Touches: - `bin/judge_replay/test_judge_replay.py` - `bin/loop_claude/cycle/judge.py`: `judge_args`, `run_judge` (small) -### 287. Move the forge helpers `Forge`, `forge_of`, `runner`, `open_pull`, `merged_pull`, `pull_at`, `gitea`, `checks_passed` and `required_checks` into one module that `chunk_preflight`, `chunk_pr`, `deploy_release` and `larv_review` all import from, so no tool imports another tool's internals for them. - -Today the helpers live in two tools. `Forge` is defined in `bin/chunk_preflight/gathered.py` and imported by `bin/chunk_preflight/choices.py`, `bin/chunk_preflight/facts.py`, `bin/chunk_preflight/main.py` and `bin/chunk_pr/main.py`, and again through `choices` by `bin/deploy_release/forge.py`. `forge_of` lives in `choices.py`, which `bin/chunk_pr/main.py`, `bin/deploy_release/cut.py` and `bin/larv_review/reviewer_header.py` import. `open_pull`, `runner` and `gitea` live in `bin/deploy_release/forge.py`, which `bin/chunk_pr/main.py` and `bin/larv_review/reviewer_header.py` import. So `deploy_release` imports `chunk_preflight`, and `chunk_pr` and `larv_review` import both. - -Both comprehension seats of PR #453's paired ruling (2026-11-09) named this web as a place that cost them time, and neither saw a reason for it: a second forge would need edits in `chunk_preflight`, `deploy_release`, `chunk_pr` and `larv_review`. - -Put the seven helpers in a folder named by what it holds, such as `bin/forge/`, and import them from there. Keep each helper's behaviour and its tests. `chunk_preflight/choices.py` keeps `pulls_command`, which is preflight's own. Check `docs/decisions.md` for a mention of where a helper lives, and update it. - -Touches: - -- `bin/deploy_release/forge.py` -- `bin/chunk_preflight/gathered.py`: `Forge` -- `bin/chunk_preflight/choices.py`: `forge_of`, `(module)` (small) -- `bin/chunk_preflight/facts.py`: `(module)` (small) -- `bin/chunk_preflight/main.py`: `(module)` (small) -- `bin/chunk_pr/main.py`: `(module)` (small) -- `bin/chunk_pr/approval.py`: `(module)` (small) -- `bin/deploy_release/cut.py`: `(module)` (small) -- `bin/larv_review/reviewer_header.py`: `(module)` (small) -- `bin/chunk_pr/test_chunk_pr.py` -- `bin/chunk_preflight/test_chunk_preflight.py` -- `bin/deploy_release/test_deploy_release.py` - ### 387. Bind a project's rung to its ladder in one value, so the routing functions and `Cycle` take one argument for both. Filed by PR #553, which made the routing ladder a file a project may replace. Its stability round 1 found two places the comprehension seats name as hard, touched and no easier to read: `bin/loop_claude/routing.py` `unfinished_route` took `ladder` as a fourth parameter, and `Cycle` in `bin/loop_claude/cycle/run.py` gained the field `ladder`. The patch threaded the ladder as an argument, since a wider change did not fit a patch cut during an outage.