import dataclasses import decimal import re from rank_todo import cells, ITEMS_HEADER, table_rows from larv_review.seats import DIMENSIONS from larv_review.history import entries, Entry from failure import Failure CAP = 3 FLOOR = decimal.Decimal("7.0") PASSES = ("architecture", "full scan", "security", "stability", "product-owner", "prose") ROUND = re.compile(r"^Answer (.+) round ([0-9]+):") @dataclasses.dataclass(frozen=True) class HistoryChange: """A merged change on the default branch that touched the history, and the history before and after it.""" commit: str before: str after: str @dataclasses.dataclass(frozen=True) class PlanItem: id: int release: tuple[int, int, int] tagline: str def means(scores: dict[str, list[str]]) -> list[decimal.Decimal]: """The seats' mean of each score, in DIMENSIONS' order, rounded half up to two decimals.""" return [(sum((decimal.Decimal(seat[index]) for seat in scores.values()), decimal.Decimal(0)) / len(scores)).quantize(decimal.Decimal("0.01"), decimal.ROUND_HALF_UP) for index in range(len(DIMENSIONS))] def under_floor(found: list[decimal.Decimal]) -> list[str]: return [f"{name} {mean}" for name, mean in zip(DIMENSIONS, found) if mean < FLOOR] def round_counts(titles: list[str]) -> dict[str, int]: """Each pass's highest round among the branch's `Answer round :` commit titles; a pass with none has 0.""" counts = dict.fromkeys(PASSES, 0) for title in titles: if named := ROUND.match(title): counts[named.group(1)] = max(counts.get(named.group(1), 0), int(named.group(2))) return counts def measured_commit(changes: list[HistoryChange]) -> str | None: """The newest change, of changes given newest first, that adds a scoring run to the history, or whose last added paired ruling is better.""" for change in changes: held = {entry.heading for entry in entries(change.before)} added = [entry for entry in entries(change.after) if entry.heading not in held] rulings = sorted((entry for entry in added if entry.against), key=lambda entry: entry.time) if any(entry.against is None for entry in added) or rulings and rulings[-1].ruling == "better": return change.commit return None def first_ruling(history: str) -> Entry | None: rulings = sorted((entry for entry in entries(history) if entry.against), key=lambda entry: entry.time) return rulings[0] if rulings else None def plan_items(todo: str) -> list[PlanItem] | Failure: """The ID, release and tagline of each row in a todo.md's items table, in the shape `rank_todo.py --help` shows; a plan with no table has no items.""" lines = todo.splitlines() if ITEMS_HEADER not in lines: return [] table = table_rows(lines, ITEMS_HEADER) if isinstance(table, Failure): return table items = [] for row in table.rows: found = cells(row, 10) if isinstance(found, Failure): return found id_, release, _, tagline, *_ = found if not re.fullmatch(r"[0-9]+", id_) or not re.fullmatch(r"[0-9]+\.[0-9]+\.[0-9]+", release): return Failure(f"a todo.md row has no whole-number ID or no semver release: {row}") major, minor, patch = (int(part) for part in release.split(".")) items.append(PlanItem(int(id_), (major, minor, patch), tagline)) return items def scoring_run_due(before: list[PlanItem], after: list[PlanItem], ships: list[int], landed: bool) -> list[str]: """Why the chunk takes 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 the newest release tag. A release has work landed towards it when the chunk ships one of its items, or when it is the earliest release before names and landed holds.""" plan = {item.id: item for item in before + after} 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()] left = {item.release for item in after if item.id not in ships} named = {item.release for item in before} | {item.release for item in shipped} earliest = min((item.release for item in before), default=None) for release in sorted(named - left): if any(item.release == release for item in shipped) or release == earliest and landed: reasons.append(f"leaves release {'.'.join(map(str, release))} with no plan item") return reasons