62 lines
3.4 KiB
Python
62 lines
3.4 KiB
Python
import pathlib
|
|
|
|
from larv_review.git import committed_since_release
|
|
from failure import Failure
|
|
from git_repo import file_at, top
|
|
from todo_table import Item, items, renames, version
|
|
|
|
|
|
def scoring_run_due(before: list[Item], after: list[Item], ships: list[int], landed: bool, renamed: set[tuple[int, int, int]]) -> list[str] | Failure:
|
|
"""Why the plan calls a scoring run, empty when it does not. before is the default branch's plan, after the chunk's, ships the IDs it ships, landed whether the default branch holds a commit after its newest release tag, and renamed the releases the chunk's plan marks renamed.
|
|
|
|
A chunk that ships an item naming the floor is due one, so it can prove the floor met. So is a chunk that empties a release after work has landed towards it, so no release ends unmeasured, short of one renamed as below. Work has landed when the chunk ships one of the release's items, or when the release is the earliest one `before` names and `landed` holds. A release the chunk's plan marks renamed is due none.
|
|
"""
|
|
plan = {item.id: item for item in before + after}
|
|
if unknown := sorted(set(ships) - set(plan)):
|
|
return Failure(f"neither plan holds item {', '.join(map(str, unknown))}")
|
|
shipped = [plan[id_] for id_ in ships]
|
|
reasons = [f"ships item {item.id}, which names the comprehension floor" for item in shipped if "comprehension floor" in item.tagline.lower()]
|
|
next_release = min((item.release for item in before), default=None)
|
|
for release in emptied(before, after, shipped, renamed):
|
|
if any(item.release == release for item in shipped) or (release == next_release and landed):
|
|
# SKILL.md → "Second run" keys on this wording.
|
|
reasons.append(f"leaves release {version(release)} with no plan item")
|
|
return reasons
|
|
|
|
|
|
def emptied(before: list[Item], after: list[Item], shipped: list[Item], renamed: set[tuple[int, int, int]]) -> list[tuple[int, int, int]]:
|
|
"""Each release the default branch's plan or a shipped item names that the chunk's plan, less the shipped items, names no longer, unless the chunk's plan marks it renamed."""
|
|
named = {item.release for item in before + shipped}
|
|
kept = [item for item in after if item.id not in {ship.id for ship in shipped}]
|
|
return sorted(named - {item.release for item in kept} - renamed)
|
|
|
|
|
|
def plan_reasons(default: str, ids: list[int]) -> list[str] | Failure:
|
|
"""Why the plan calls a scoring run, from the default branch's todo.md and the checkout's."""
|
|
checkout = top()
|
|
if isinstance(checkout, Failure):
|
|
return checkout
|
|
before = file_at(default, "todo.md")
|
|
if isinstance(before, Failure):
|
|
return before
|
|
try:
|
|
after = pathlib.Path(checkout, "todo.md").read_text(encoding="utf-8")
|
|
except (OSError, UnicodeDecodeError) as failure:
|
|
return Failure(f"could not read todo.md: {failure}")
|
|
# While a chunk reshapes todo.md into one table, the default branch's todo.md has no items table.
|
|
planned = items(before) or []
|
|
if isinstance(planned, Failure):
|
|
return Failure(f"{default}'s todo.md: {planned.reason}")
|
|
chunk = items(after)
|
|
if chunk is None:
|
|
return Failure("the checkout's todo.md has no items table; rank_todo.py --help shows one")
|
|
if isinstance(chunk, Failure):
|
|
return Failure(f"the checkout's todo.md: {chunk.reason}")
|
|
renamed = renames(after)
|
|
if isinstance(renamed, Failure):
|
|
return Failure(f"the checkout's todo.md: {renamed.reason}")
|
|
landed = committed_since_release(default)
|
|
if isinstance(landed, Failure):
|
|
return landed
|
|
return scoring_run_due(planned, chunk, ids, landed, renamed)
|