diff --git a/bin/chunk_preflight/main.py b/bin/chunk_preflight/main.py index fcd47f15..d35b5cad 100644 --- a/bin/chunk_preflight/main.py +++ b/bin/chunk_preflight/main.py @@ -14,7 +14,9 @@ It then prints: - the item's worktree: a sibling of the checkout named -item-, on the branch item-. It continues a worktree already on that branch; else it adds one on the local branch, else on a new branch tracking origin's, else on a new branch off the default branch; -- the open pull requests, as gh lists them on a github.com origin, and else as tea does; +- the open pull requests whose head is the item's branch, each by its number and URL, as gh lists + them on a github.com origin, and else as tea does. No title reaches the output: on a public forge, + anyone may open a pull request, and its title would reach the worker's prompt; - the items of that release that a chunk taking the item may take too: none is a question, and no item outside them and the item blocks one; - each file or folder git tracks that the item's tagline or detail names in backticks, with a @@ -68,16 +70,17 @@ def preflight(root: pathlib.Path) -> list[str]: todo = file_at(default, "todo.md") planned = todo if isinstance(todo, Failure) else items(todo) if isinstance(planned, Failure): - return [*found, f"- No plan item: {default}'s todo.md: {planned.reason}", *pull_requests(root)] + return [*found, f"- No plan item: {default}'s todo.md: {planned.reason}"] first = next_item(planned or []) if first is None: why = f"every item of {default}'s next release waits" if planned else f"{default} holds no todo.md items table" - return [*found, f"- No plan item: {why}, so no worktree was made.", *pull_requests(root)] + return [*found, f"- No plan item: {why}, so no worktree was made."] assert isinstance(todo, str) and planned found.append(f"- Next release: {release_name(first)}. Its first item that does not wait: item {first.id}, L {first.likelihood or 'unscored'}, E + I {first.escape_and_impact or 'unscored'}.") - worktree = item_worktree(root, f"item-{first.id}", default) + branch = f"item-{first.id}" + worktree = item_worktree(root, branch, default) found.append(f"- Worktree: {worktree.reason}" if isinstance(worktree, Failure) else f"- Worktree: {worktree[0]}, {worktree[1]}") - found += pull_requests(root) + found.append(f"- Open pull requests from {branch}: {pull_requests(root, branch)}") found += bundled(planned, first) details = item_details(todo) detail = details.get(first.id, "") if isinstance(details, dict) else f"(todo.md's details cannot be read: {details.reason})" @@ -125,12 +128,19 @@ def item_worktree(root: pathlib.Path, branch: str, default: str) -> tuple[pathli return Failure(f"none made: {added.reason}") if isinstance(added, Failure) else (path, how) -def pull_requests(root: pathlib.Path) -> list[str]: +def pull_requests(root: pathlib.Path, branch: str) -> str: + """Each open pull request from branch, by its number and URL, or why none can be listed.""" url = git("remote", "get-url", "origin") if isinstance(url, Failure): - return ["- Open pull requests: none, as the checkout has no origin remote."] - command = ["gh", "pr", "list"] if "github.com" in url else ["tea", "pulls", "list", "--fields", "index,title,head,state", "--output", "tsv"] - return [f"- Open pull requests: {printed(tuple(command), command, root)}"] + return "none, as the checkout has no origin remote." + github = "github.com" in url + command = ["gh", "pr", "list", "--head", branch, "--json", "number,url", "--template", "{{range .}}{{.number}}\t{{.url}}\n{{end}}"] if github else ["tea", "pulls", "list", "--fields", "index,head,url", "--output", "tsv"] + done = ran(command, root) + if isinstance(done, Failure) or done.returncode: + return f"cannot be listed: `{' '.join(command[:3])}` {done.reason if isinstance(done, Failure) else f'exited {done.returncode}: {done.stderr.strip()}'}" + rows = [line.split("\t") for line in done.stdout.splitlines()] + opened = [f"#{row[0]} {row[1]}" for row in rows if len(row) == 2] if github else [f"#{row[0]} {row[2]}" for row in rows if len(row) == 3 and row[1] == branch] + return ", ".join(opened) if opened else "none." def bundled(planned: list[Item], first: Item) -> list[str]: @@ -160,13 +170,19 @@ def size(path: pathlib.Path, files: int) -> str: return f"cannot be read: {failure}" +def ran(command: list[str], cwd: pathlib.Path) -> subprocess.CompletedProcess[str] | Failure: + try: + return subprocess.run(command, capture_output=True, cwd=cwd, stdin=subprocess.DEVNULL, text=True, timeout=TIMEOUT) + except (OSError, subprocess.TimeoutExpired) as failure: + return Failure(f"could not finish: {failure}") + + def printed(shown: tuple[str, ...], command: list[str], cwd: pathlib.Path) -> str: """What command printed, run in cwd, and its exit code, under its name as shown.""" name = f"`{' '.join(shown)}`" - try: - done = subprocess.run(command, capture_output=True, cwd=cwd, stdin=subprocess.DEVNULL, text=True, timeout=TIMEOUT) - except (OSError, subprocess.TimeoutExpired) as failure: - return f"{name} could not finish: {failure}" + done = ran(command, cwd) + if isinstance(done, Failure): + return f"{name} {done.reason}" output = "\n".join(part.strip() for part in (done.stdout, done.stderr) if part.strip()) return f"{name} exited {done.returncode}, printing:\n\n{quoted(output)}" if output else f"{name} exited {done.returncode}, printing nothing." diff --git a/bin/chunk_preflight/test_chunk_preflight.py b/bin/chunk_preflight/test_chunk_preflight.py index 1f3831cb..94828f69 100644 --- a/bin/chunk_preflight/test_chunk_preflight.py +++ b/bin/chunk_preflight/test_chunk_preflight.py @@ -42,8 +42,8 @@ TODO = """# todo Change `src/app.py`'s `run` and the files under `src`, not `gone.py`. """ CHANGELOG = "# Changelog\n\n## Unreleased\n\n- One.\n - A nested line.\n- Two.\n\n## 0.1.0\n\n- Old.\n" -# Prints its arguments, then two open pull requests, as `tea pulls list --output tsv` does. -FAKE_TEA = "#!/bin/sh\necho \"tea $*\"\nprintf 'index\\ttitle\\thead\\tstate\\n12\\tWIP: the next thing\\titem-3\\topen\\n'\n" +# Two open pull requests, as `tea pulls list --fields index,head,url --output tsv` prints them; anything else exits 1. +FAKE_TEA = "#!/bin/sh\n[ \"$*\" = 'pulls list --fields index,head,url --output tsv' ] || exit 1\nprintf 'index\\thead\\turl\\n12\\titem-3\\thttps://forge/pulls/12\\n13\\tother\\thttps://forge/pulls/13\\n'\n" class Repo: @@ -104,8 +104,7 @@ class ChunkPreflightTool(unittest.TestCase): "- `src/app.py`: 3 lines", "- `src`: 1 file", "Change `src/app.py`'s `run` and the files under `src`, not `gone.py`.", - "tea pulls list --fields index,title,head,state --output tsv", - "12\tWIP: the next thing\titem-3\topen", + "- Open pull requests from item-3: #12 https://forge/pulls/12\n", "`rank_todo.py --check todo.md` exited 0", "`larv-review needs-scoring-run 3` exited 0", "`reading-cost` exited 0",