import decimal import re from larv_review.seats import DIMENSIONS, FIRST_SEATS, Seat, SPLIT_SEATS from larv_review.history import entries, Entry, HistoryChange, PLACE from failure import Failure from rank_todo import Item, ITEMS_HEADER, parse_row, table_rows CAP = 3 FLOOR = decimal.Decimal("7.0") PASSES = ("architecture", "full scan", "security", "stability", "product-owner", "prose") ROUND = re.compile(r"^Answer (.+?) round ([0-9]+):") VOTES = ("better", "same", "worse") def votes(seats: list[Seat]) -> dict[str, str] | Failure: """Each seat's vote as it counts: a better or worse vote naming no `path:line` place counts as same.""" names = [seat.name for seat in seats] if names[:2] != list(FIRST_SEATS) or names[2:] not in ([], list(SPLIT_SEATS)): return Failure(f"a paired ruling's seats are {' and '.join(FIRST_SEATS)}, then, when their votes differ, {' and '.join(SPLIT_SEATS)}, in that order; the file has {', '.join(names) or 'none'}") if unknown := [seat for seat in seats if seat.said not in VOTES]: return Failure(f"{unknown[0].name}'s vote {unknown[0].said!r} is not better, same or worse") return {seat.name: seat.said if names_a_place(seat) else "same" for seat in seats} def names_a_place(seat: Seat) -> bool: return any(PLACE.search(place) for place in seat.places) def ruling(counted: list[str]) -> str | None: """The vote that leads each other vote by two or more, else same; None when only the first two seats voted and they differ, so two more must vote.""" if len(counted) == 2 and counted[0] != counted[1]: return None for vote in VOTES: if all(counted.count(vote) >= counted.count(other) + 2 for other in VOTES if other != vote): return vote return "same" def means(scores: dict[str, list[str]]) -> list[decimal.Decimal]: """The seats' mean of each score, in the order each seat's scores are, rounded half up to two decimals.""" return [mean([decimal.Decimal(score) for score in column]) for column in zip(*scores.values())] def mean(values: list[decimal.Decimal]) -> decimal.Decimal: return (sum(values, decimal.Decimal(0)) / len(values)).quantize(decimal.Decimal("0.01"), decimal.ROUND_HALF_UP) def under_floor(found: list[decimal.Decimal]) -> list[str]: return [f"{name} {value}" for name, value in zip(DIMENSIONS, found) if value < 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, and a title naming no known pass adds its name.""" 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 base(changes: list[HistoryChange], history: str, default: str) -> tuple[str, str] | None: """The commit a paired ruling reads against, and how it was found: the last measured commit, else the base the history's first paired ruling names; None when neither exists, so the base is the merge base of HEAD and the default branch. history is the default branch's.""" if found := measured_commit(changes): return found, f"The newest merged change on {default} that adds a scoring run to the history, or whose last added paired ruling is better." if (first := first_ruling(history)) and first.against: return first.against, f"No merged change on {default} adds a scoring run or a better ruling; the base its first paired ruling names." return None def plan_items(todo: str) -> list[Item] | Failure: """Each row of a todo.md's items table, read as `rank_todo.py` reads it.""" table = table_rows(todo.splitlines(), ITEMS_HEADER) if isinstance(table, Failure): return table items = [] for row in table.rows: item = parse_row(row) if isinstance(item, Failure): return item items.append(item) return items def scoring_run_due(before: list[Item], after: list[Item], ships: list[int], landed: bool) -> list[str] | Failure: """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} 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()] 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