Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions scripts/retro_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]
Expand Down
31 changes: 29 additions & 2 deletions tests/test_retro_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def issue(number, created, closed=None, reason=None):


@pytest.fixture
def sources(tmp_path, monkeypatch):
def sources(tmp_path, monkeypatch, request):
commit_rows, commit_total = getattr(request, "param", (1, 1))
merged = issue(1, "2026-05-26T06:00:00Z", SINCE)
at_end = issue(2, SINCE, END)
p0 = issue(4, "2026-05-23T12:00:00Z")
Expand All @@ -41,7 +42,8 @@ def sources(tmp_path, monkeypatch):
{"event": "commented", "created_at": END},
],
"issues/5/timeline": [pull_ref],
"pulls/3/commits": [{"commit": {"committer": {"date": "2026-05-28T21:00:00Z"}}}],
"pulls/3": {"commits": commit_total},

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: 'Unavailable count' case now only covers an explicit null, not a missing field or a failed detail read

With the fixture rewrite, the (250, None) case serves {"commits": null}. The old test served {}, which covered a missing key. Neither version covers the case where the gh api repos/.../pulls/3 detail read itself fails (CalledProcessError, so inputs.get returns None). That is the more realistic meaning of an unavailable count in the acceptance criteria. _complete_pr_commits (scripts/retro_metrics.py:303) handles all three through (detail or {}).get("commits") and the int type check, so the code has no bug. Only the regression coverage is thinner. Smallest fix: when commit_total is None, leave pulls/3 out of rows, or raise CalledProcessError for that path in collect. That exercises the failure path and still asserts one detail read and unknown activity.

"pulls/3/commits": [{"commit": {"committer": {"date": "2026-05-28T21:00:00Z"}}}] * commit_rows,
"issues/3/comments": [],
}
calls = []
Expand All @@ -68,6 +70,9 @@ def collect(argv, *, input, **kwargs):
raise subprocess.CalledProcessError(1, argv)
path = label
data = rows[path]
if path == "pulls/3/commits":
offset = (int(query.rsplit("=", 1)[1]) - 1) * 100
data = data[offset:offset + 100]
return subprocess.CompletedProcess(argv, 0, json.dumps(data), "")

monkeypatch.setattr(retro_metrics.subprocess, "run", collect)
Expand Down Expand Up @@ -196,3 +201,25 @@ 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(("sources", "detail_reads", "stalled"), [
((101, None), 0, True),
((250, 250), 1, True),
((250, 251), 1, False),
((250, None), 1, False),
], indirect=["sources"])
def test_stalled_issue_checks_linked_pr_commit_count_at_limit(sources, detail_reads, stalled, capsys):
calls, args = sources
options = ["--alerts", "--stall-hours", "1"]
assert retro_metrics.main([*args, *options, "--record"]) == 0
live = capsys.readouterr()
alerts = json.loads(live.out)["alerts"]
assert any(a["kind"] == "stalled_p0" and a["issue"] == 5 for a in alerts) == stalled
assert any(a.get("source") == "p0/#5/activity" for a in alerts) == (not stalled)
assert sum(call[-1] == f"repos/{REPO}/pulls/3" for call in calls) == detail_reads

count = len(calls)
assert retro_metrics.main([*args, *options]) == 0
assert capsys.readouterr() == live
assert len(calls) == count
Loading