Move the forge helpers into bin/forge/, which chunk_preflight, chunk_pr, deploy_release and larv_review import
CI / tests (pull_request) Successful in 3m9s

This commit is contained in:
2026-11-16 23:42:01 +01:00
parent 4b5086153a
commit 73600d7288
18 changed files with 179 additions and 145 deletions
+2
View File
@@ -51,6 +51,8 @@
!bin/deploy_release/ !bin/deploy_release/
!bin/deploy_release/*.py !bin/deploy_release/*.py
!bin/failure.py !bin/failure.py
!bin/forge/
!bin/forge/*.py
!bin/git_cli/ !bin/git_cli/
!bin/git_cli/*.py !bin/git_cli/*.py
!bin/judge-replay !bin/judge-replay
+3 -3
View File
@@ -194,14 +194,14 @@ Install puts only `loop-claude` on `PATH`; call the others by their path, such a
- `touch-predictions record <ID>...` 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. - `touch-predictions record <ID>...` 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 <file> [<pattern>] [--timeout <seconds>]` waits until a file a background job writes holds a line matching the pattern, and prints it. - `wait-for <file> [<pattern>] [--timeout <seconds>]` 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. - `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. - `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. - `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`. - `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 its 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`). - `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. - `judge_replay` imports `loop_claude`'s judge, verdicts, premises and records.
- `loop_claude` runs `chunk-preflight` as a process of its own. - `loop_claude` runs `chunk-preflight` as a process of its own.
+1 -2
View File
@@ -5,8 +5,7 @@ import time
import typing import typing
from failure import Failure from failure import Failure
from chunk_preflight.gathered import Forge from forge.commands import Forge, Run
from deploy_release.forge import 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. # 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 POLL_SECONDS = 60
+2 -3
View File
@@ -85,10 +85,9 @@ import time
import typing import typing
from chunk_pr.approval import approval, approved, awaited, unanswered 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 failure import Failure
from chunk_preflight.gathered import Forge from forge.commands import Forge, forge_of, gitea, Run, runner
from chunk_preflight.choices import forge_of
from project_config.main import located from project_config.main import located
from project_config.settings import ProjectConfig from project_config.settings import ProjectConfig
from plan_store.plans import says_no_item from plan_store.plans import says_no_item
+1 -1
View File
@@ -15,7 +15,7 @@ from chunk_pr.main import cleaned, merged, QUEUE, step, turn, USAGE
from failure import Failure from failure import Failure
from test_env import isolated_env from test_env import isolated_env
from project_config.settings import ProjectConfig from project_config.settings import ProjectConfig
from deploy_release.forge import Run from forge.commands import Run
from chunk_pr.approval import THREADS from chunk_pr.approval import THREADS
TOOL = pathlib.Path(__file__).resolve().parents[1] / "chunk-pr" TOOL = pathlib.Path(__file__).resolve().parents[1] / "chunk-pr"
+1 -5
View File
@@ -1,7 +1,7 @@
"""The pure choices the preflight's steps act on.""" """The pure choices the preflight's steps act on."""
import pathlib 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. # 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")} 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}." 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]: 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.""" """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": if forge == "gh":
+2 -1
View File
@@ -4,7 +4,8 @@ from touch_predictions.beside import chunks_beside
from release_plan.next_chunk import cut_name, NextChunk, NoItem from release_plan.next_chunk import cut_name, NextChunk, NoItem
from release_plan.release_standing import dated_cut, LastCut, unreleased_entries, UNSEEN_RELEASES from release_plan.release_standing import dated_cut, LastCut, unreleased_entries, UNSEEN_RELEASES
from failure import Failure 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 text_files.markdown_text import listed, quoted
from chunk_preflight.choices import PULLS_LIST from chunk_preflight.choices import PULLS_LIST
from plan.release import version from plan.release import version
+1 -4
View File
@@ -1,15 +1,12 @@
"""What the preflight gathers, which facts.render states.""" """What the preflight gathers, which facts.render states."""
import dataclasses import dataclasses
import pathlib import pathlib
import typing
from failure import Failure from failure import Failure
from forge.commands import Forge
from release_plan.release_standing import LastCut from release_plan.release_standing import LastCut
from release_plan.next_chunk import NextChunk, NoItem 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) @dataclasses.dataclass(frozen=True)
class Ran: class Ran:
+3 -2
View File
@@ -78,13 +78,14 @@ import typing
from git_cli.git_repo import commit_exists, default_branch, file_at, git, top from git_cli.git_repo import commit_exists, default_branch, file_at, git, top
from failure import Failure from failure import Failure
from chunk_preflight.gathered import Forge, Gathered, ItemFacts, NamedPath, Ran, Worktree from forge.commands import Forge, forge_of
from chunk_preflight.choices import forge_of, pulls_command, worktree_add, worktree_on from chunk_preflight.gathered import Gathered, ItemFacts, NamedPath, Ran, Worktree
from chunk_preflight.facts import JOINING_DETAILS, render from chunk_preflight.facts import JOINING_DETAILS, render
from project_config.main import located from project_config.main import located
from plan.named_paths import named_paths from plan.named_paths import named_paths
from release_plan.next_chunk import NextChunk, NoItem, plan_name, planned from release_plan.next_chunk import NextChunk, NoItem, plan_name, planned
from release_plan.release_standing import project_plan 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] BIN = pathlib.Path(__file__).resolve().parents[1]
# Each tool run in the item's worktree, {item} standing for the item's ID. # Each tool run in the item's worktree, {item} standing for the item's ID.
+1 -5
View File
@@ -13,7 +13,6 @@ from project_config.templates import BRANCH
from release_plan.next_chunk import CarriedOver, NoItem from release_plan.next_chunk import CarriedOver, NoItem
from chunk_preflight import choices from chunk_preflight import choices
from failure import Failure from failure import Failure
from chunk_preflight.choices import forge_of, worktree_add
from chunk_preflight.gathered import Gathered, ItemFacts, NamedPath, Ran, Worktree from chunk_preflight.gathered import Gathered, ItemFacts, NamedPath, Ran, Worktree
from test_env import isolated_env from test_env import isolated_env
from release_plan.release_standing import LastCut, UNSEEN_RELEASES 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 release_plan import next_chunk
from chunk_preflight.facts import open_pulls, render from chunk_preflight.facts import open_pulls, render
from plan_store.todo_md import plan_of from plan_store.todo_md import plan_of
from chunk_preflight.choices import worktree_add
from project_config.test_project_config import write_config from project_config.test_project_config import write_config
BIN = pathlib.Path(__file__).resolve().parents[1] 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, 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"]) 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): def test_reads_the_timeout_setting_as_seconds_above_0_and_else_takes_the_default(self):
self.assertEqual(seconds(None), TIMEOUT) self.assertEqual(seconds(None), TIMEOUT)
self.assertEqual(seconds("2.5"), 2.5) self.assertEqual(seconds("2.5"), 2.5)
+10 -9
View File
@@ -14,9 +14,10 @@ from deploy_release.deploy import deploy
from failure import Failure from failure import Failure
from plan_store.todo_md import FilePlan from plan_store.todo_md import FilePlan
from record_store.stores import FileStore from record_store.stores import FileStore
from deploy_release import forge from forge.commands import forge_of, runner
from chunk_preflight.choices import forge_of
from plan.release import newest_tag, Release, UNRELEASED_HEADING, version 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 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) url = git("config", "--get", "remote.origin.url", cwd=clone)
if isinstance(url, Failure): if isinstance(url, Failure):
return url 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) head = branch_head(branch, clone)
if isinstance(head, Failure): if isinstance(head, Failure):
return head 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") head = branch_head(branch, clone) or Failure(f"pushed {branch}, but origin holds no such branch")
if isinstance(head, Failure): if isinstance(head, Failure):
return head return head
number = forge.open_pull(on, branch, head, run) number = pulls.open_pull(on, branch, head, run)
if number is None: 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 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): if isinstance(number, str):
said(f"opened pull request {number}") said(f"opened pull request {number}")
if isinstance(number, Failure): if isinstance(number, Failure):
return number return number
required = forge.required_checks(on, base, run) required = pulls.required_checks(on, base, run)
if isinstance(required, Failure): if isinstance(required, Failure):
return required return required
said(f"waiting for the checks of pull request {number}" + (f"; {base} requires {', '.join(sorted(required))}" if required else "")) 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 return passed
said(f"the checks of pull request {number} 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 return merged
said(f"merged pull request {number}") said(f"merged pull request {number}")
return None return None
+23
View File
@@ -0,0 +1,23 @@
"""The pull request steps of a cut. They run gh where origin is on GitHub and tea elsewhere, as `forge.commands` 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.
"""
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
+4 -55
View File
@@ -1,16 +1,13 @@
import itertools
import json import json
import os import os
import pathlib import pathlib
import subprocess import subprocess
import sys import sys
import tempfile import tempfile
import typing
import unittest import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) 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 failure import Failure
from test_env import isolated_env from test_env import isolated_env
@@ -229,6 +226,10 @@ save()
""".replace("PYTHON", sys.executable) """.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): 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.""" """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") 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__": if __name__ == "__main__":
unittest.main() unittest.main()
+1
View File
@@ -0,0 +1 @@
"""The forge a checkout's origin is on, GitHub or Gitea, and the calls to it that several tools make: `commands.py` tells the two apart and runs their CLIs, `pulls.py` finds a pull request and waits for its checks."""
+42
View File
@@ -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 that talks to a forge: gh for a github.com origin, tea for any other.
Forge = typing.Literal["gh", "tea"]
# Runs a command in the clone: 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
@@ -1,50 +1,19 @@
"""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. """Finding a pull request from origin's own branch, and waiting for its checks.
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. Each call is pinned to head, a commit on that branch: it takes only a pull request from origin itself whose head is that commit. So a fork's pull request from a branch of the same name is never taken.
""" """
import fnmatch import fnmatch
import json
import subprocess
import time import time
import typing import typing
from failure import Failure 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.
POLL_SECONDS = 10 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 for the checks before it gives up; CI took about 3 minutes in 2026-11.
WAIT_SECONDS = 30 * 60 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: 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.""" """The number of the open pull request from origin's own branch whose head is head; None where there is none."""
@@ -75,13 +44,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]}") 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: 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. """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 +94,3 @@ def checks_passed(forge: Forge, number: str, head: str, required: set[str], run:
sleep(POLL_SECONDS) sleep(POLL_SECONDS)
waited_on = f"{', '.join(missing)}, which the protection requires, never passed" if missing else "a check is still pending" 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}") 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
+75
View File
@@ -0,0 +1,75 @@
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, POLL_SECONDS, required_checks, WAIT_SECONDS
from failure import Failure
from forge.commands import forge_of
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")
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()
+2 -2
View File
@@ -5,12 +5,12 @@ import pathlib
from git_cli.git_repo import branch_titles, git, top from git_cli.git_repo import branch_titles, git, top
from review_rules.round_titles import cap, round_counts from review_rules.round_titles import cap, round_counts
from failure import Failure 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 record_store.stores import goals_store, local_file, store
from larv_review.history_commands import history_file, places_report from larv_review.history_commands import history_file, places_report
from project_config.main import located, title_templates from project_config.main import located, title_templates
from review_rules.review_folder import made_review_folder 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 larv_review.plan_listing import plan_listing
from plan_store.stores import store as plan_store from plan_store.stores import store as plan_store
from review_rules.reviewer_context import principles_line, starts_bare from review_rules.reviewer_context import principles_line, starts_bare