"""The titles of larv-review's round commits, `Answer round : 🔴, 🟡 ( misread); `: each pass's round count against its cap, and the findings the rounds reported. counts the 🔴 and 🟡 findings a reviewer marked `[misread]`.""" import dataclasses import re from review_passes import PASSES # The most rounds a pass takes per chunk, per `docs/decisions.md` → `review: a pass gets at most two rounds per chunk, …`. rule-hook's `round-cap` refuses a round commit that `past_cap` finds takes its pass past it, and `larv-review round-count` marks a pass that reached it. CAP = 2 ROUND = re.compile(r"^Answer (.+?) round ([0-9]+):", re.IGNORECASE) # What follows a round title's colon: the round's 🔴 and 🟡 counts, then how many of them a reviewer marked `[misread]`, a count that titles from before it lack, then `;` and its summary, if any. FINDINGS = re.compile(r" ([0-9]+) 🔴, ([0-9]+) 🟡(?: \(([0-9]+) misread\))?(?:;|$)") @dataclasses.dataclass(frozen=True) class Findings: """A round title's findings: its 🔴 and 🟡 counts, and how many of them a reviewer marked `[misread]`, None for a title from before that count.""" red: int yellow: int misread: int | None def round_counts(titles: list[str]) -> dict[str, int]: """Each pass's round count among the branch's `Answer round :` commit titles: the highest , or the number of such commits where that is more, so a repeated number still counts. A pass with none has 0, and a title naming no known pass adds its name. Case is ignored.""" highest: dict[str, int] = dict.fromkeys(PASSES, 0) commits: dict[str, int] = dict.fromkeys(PASSES, 0) for title in titles: if named := ROUND.match(title): name = named.group(1).lower() highest[name] = max(highest.get(name, 0), int(named.group(2))) commits[name] = commits.get(name, 0) + 1 return {name: max(highest[name], commits[name]) for name in highest} def rounds_past_first(titles: list[str]) -> int: """Each pass's rounds past its first among the commit titles, as round_counts counts them, summed.""" return sum(max(0, count - 1) for count in round_counts(titles).values()) def lacks_counts(title: str) -> bool: """Whether title is a round title without all three counts after its colon, or with more misread findings than 🔴 and 🟡 findings.""" return ROUND.match(title) is not None and ((counts := title_counts(title)) is None or counts.misread is None) def past_cap(title: str, earlier: list[str]) -> bool: """Whether title is a round title that takes its pass past CAP, as round_counts counts title among the branch's earlier commit titles.""" named = ROUND.match(title) return named is not None and round_counts([*earlier, title])[named.group(1).lower()] > CAP def title_counts(title: str) -> Findings | None: """The counts after a round title's colon; None where title is no round title, lacks the 🔴 and 🟡 counts, or counts more misread findings than both.""" named = ROUND.match(title) found = FINDINGS.match(title, named.end()) if named else None if not found: return None counts = Findings(int(found.group(1)), int(found.group(2)), None if found.group(3) is None else int(found.group(3))) return None if counts.misread is not None and counts.misread > counts.red + counts.yellow else counts def round_findings(titles: list[str]) -> Findings | None: """The counts summed over the round titles among titles; None where a round title lacks the 🔴 and 🟡 counts, or no title is a round. Its misread is None where a round title lacks that count.""" counts = [] for title in titles: if ROUND.match(title): if (found := title_counts(title)) is None: return None counts.append(found) if not counts: return None misreads = [found.misread for found in counts if found.misread is not None] return Findings(sum(found.red for found in counts), sum(found.yellow for found in counts), sum(misreads) if len(misreads) == len(counts) else None)