85 lines
4.7 KiB
Python
85 lines
4.7 KiB
Python
import pathlib
|
|
|
|
from chunk_pick import Cut, default_plan, LastCut, next_release, Unknown
|
|
from failure import Failure
|
|
from todo_table import Item, items, NoItemsTable, renames, version
|
|
from git_repo import top
|
|
|
|
|
|
def scoring_run_due(before: list[Item], after: list[Item], ships: list[int], landed: tuple[int, int, int] | None, renamed: set[tuple[int, int, int]]) -> list[str] | Failure:
|
|
"""Why the plan calls a scoring run, empty when it does not.
|
|
|
|
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. A release the chunk's plan marks renamed is due none.
|
|
|
|
before is the default branch's plan and after the chunk's. ships holds the IDs of the items the chunk ships. landed is the next release where work has landed towards it, else None (`landed_towards`). renamed holds the releases the chunk's plan marks renamed.
|
|
"""
|
|
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()]
|
|
for release in emptied(before, after, shipped, renamed):
|
|
if work_landed(release, shipped, landed):
|
|
# SKILL.md → "Second run" keys on this wording.
|
|
reasons.append(f"leaves release {version(release)} with no plan item")
|
|
return reasons
|
|
|
|
|
|
# larv-review --help points here rather than restating the rule, so the rule is written once.
|
|
def work_landed(release: tuple[int, int, int], shipped: list[Item], landed: tuple[int, int, int] | None) -> bool:
|
|
"""Whether work has landed towards release, for the scoring-run trigger. It has when the chunk ships one of the release's items. It has too when release equals landed: the default branch's next release, once work has landed towards it."""
|
|
return any(item.release == release for item in shipped) or release == landed
|
|
|
|
|
|
def landed_towards(found: list[Item], cut: LastCut) -> tuple[int, int, int] | None | Failure:
|
|
"""The next release on the default branch, once work has landed towards it: once one or more changes are unreleased, as `chunk_pick.unreleased_changes` counts them. None where there is no next release, or no change is unreleased. found is the default branch's plan."""
|
|
upcoming = next_release(found, cut)
|
|
if isinstance(upcoming, Unknown):
|
|
return Failure("could not tell the next release: the default branch's todo.md history cannot be read")
|
|
# Cut's release is the last cut's version or earlier, so no work lands towards it.
|
|
if isinstance(upcoming, Cut):
|
|
return None
|
|
if isinstance(upcoming.landed, Failure):
|
|
return Failure(f"could not tell whether work landed towards the next release: {upcoming.landed.reason}")
|
|
return upcoming.release if upcoming.landed else None
|
|
|
|
|
|
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
|
|
held = default_plan(checkout, default)
|
|
before = held.todo
|
|
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)
|
|
if isinstance(planned, NoItemsTable):
|
|
planned = []
|
|
if isinstance(planned, Failure):
|
|
return Failure(f"{default}'s todo.md: {planned.reason}")
|
|
chunk = items(after)
|
|
if isinstance(chunk, NoItemsTable):
|
|
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 = landed_towards(planned, held.cut)
|
|
if isinstance(landed, Failure):
|
|
return landed
|
|
return scoring_run_due(planned, chunk, ids, landed, renamed)
|