import pathlib from failure import Failure from larv_review.git import file_at, landed_since_release, top from todo_table import Item, items, ITEMS_HEADER def scoring_run_due(before: list[Item], after: list[Item], ships: list[int], landed: bool) -> 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, and landed whether the default branch holds a commit after its newest release tag. 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 towards which work has landed, so no release ends unmeasured: it ships one of the release's items, or the release is the next one, the earliest before names, and landed holds. That covers a release whose last item moved to another release. """ 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): if any(item.release == release for item in shipped) or (release == next_release and landed): reasons.append(f"leaves release {'.'.join(map(str, release))} with no plan item") return reasons def emptied(before: list[Item], after: list[Item], shipped: list[Item]) -> 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.""" named = {item.release for item in before + shipped} left = {item.release for item in after if item.id not in {ship.id for ship in shipped}} return sorted(named - left) 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) if ITEMS_HEADER in before.splitlines() else [] if isinstance(planned, Failure): return Failure(f"{default}'s todo.md: {planned.reason}") chunk = items(after) if isinstance(chunk, Failure): return Failure(f"the checkout's todo.md: {chunk.reason}") landed = landed_since_release(default) if isinstance(landed, Failure): return landed return scoring_run_due(planned, chunk, ids, landed)