diff --git a/scripts/retro_metrics.py b/scripts/retro_metrics.py index 4945b5ac..a8cee3ed 100755 --- a/scripts/retro_metrics.py +++ b/scripts/retro_metrics.py @@ -294,6 +294,16 @@ def count(value): } +def _complete_pr_commits(inputs, path, commits): + if commits is None: + return False + if len(commits) < 250: + return True + detail = inputs.get(path, lambda: run_json(["gh", "api", "--method", "GET", path]), dict) + count = (detail or {}).get("commits") + return type(count) is int and count == len(commits) + + def issue_progress(inputs, repo, issue, end): timeline = pages(inputs, f"repos/{repo}/issues/{issue['number']}/timeline") if timeline is None: @@ -313,9 +323,10 @@ def issue_progress(inputs, repo, issue, end): if not match or timestamp(event["created_at"]) >= end: continue dates += [timestamp(source["created_at"]), timestamp(event["created_at"])] - commits = pages(inputs, f"repos/{match[1]}/pulls/{match[2]}/commits") + pull = f"repos/{match[1]}/pulls/{match[2]}" + commits = pages(inputs, f"{pull}/commits") comments = pages(inputs, f"repos/{match[1]}/issues/{match[2]}/comments") - if commits is None or len(commits) >= 250 or comments is None: + if comments is None or not _complete_pr_commits(inputs, pull, commits): raise ValueError("linked PR activity unavailable or truncated") dates += [timestamp(item["commit"]["committer"]["date"]) for item in commits] dates += [timestamp(item["created_at"]) for item in comments] diff --git a/tests/test_retro_metrics.py b/tests/test_retro_metrics.py index 9f1309a5..f66cb609 100644 --- a/tests/test_retro_metrics.py +++ b/tests/test_retro_metrics.py @@ -1,5 +1,6 @@ import json import subprocess +from types import SimpleNamespace import pytest @@ -196,3 +197,53 @@ def test_invalid_band_is_usage_error(capsys): retro_metrics.main(["--repo", REPO, "--until", END, "--band", "merges", "3", "2"]) assert exc.value.code == 2 assert "ordered limits" in capsys.readouterr().err + + +@pytest.mark.parametrize(("rows", "total", "stalled"), [ + (101, None, True), + (250, 250, True), + (250, 251, False), + (250, None, False), +]) +def test_stalled_issue_checks_linked_pr_commit_count_at_limit( + tmp_path, monkeypatch, rows, total, stalled, +): + old = "2026-05-26T00:00:00Z" + pull = f"repos/{REPO}/pulls/3" + calls = [] + + def collect(argv): + endpoint = argv[-1] + calls.append(endpoint) + if endpoint == pull: + return {} if total is None else {"commits": total} + if endpoint.startswith(f"{pull}/commits?"): + page = int(endpoint.rsplit("=", 1)[1]) + size = min(100, rows - (page - 1) * 100) + return [{"commit": {"committer": {"date": old}}}] * size + if endpoint == f"repos/{REPO}/issues/3/comments?per_page=100&page=1": + return [] + assert endpoint == f"repos/{REPO}/issues/4/timeline?per_page=100&page=1" + return [{ + "event": "cross-referenced", "created_at": old, + "source": {"issue": {"created_at": old, "pull_request": { + "url": f"https://api.github.com/{pull}", + }}}, + }] + + monkeypatch.setattr(retro_metrics, "run_json", collect) + args = SimpleNamespace(repo=REPO, stall_hours=12) + priority_issues = [issue(4, old)] + end = retro_metrics.timestamp(END) + live = retro_metrics.Inputs(tmp_path, record=True) + alerts = retro_metrics.stalled_p0s(live, args, priority_issues, end) + assert [alert["kind"] for alert in alerts] == (["stalled_p0"] if stalled else []) + assert any(a["source"] == "p0/#4/activity" for a in live.alerts) == (not stalled) + assert calls.count(pull) == (1 if rows == 250 else 0) + + live.save() + before = calls.copy() + replay = retro_metrics.Inputs(tmp_path, record=False) + assert retro_metrics.stalled_p0s(replay, args, priority_issues, end) == alerts + assert replay.alerts == live.alerts + assert calls == before