From 542dde7990db75e9cfa6176fb85961a354cd8f62 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Tue, 19 May 2026 07:32:26 +0000 Subject: [PATCH] fix(triage): apply --limit consistently in dry-run and close modes Previously --limit was only honored in real-close mode because closed was incremented inside an 'if not dry_run' guard. A dry-run preview with --limit N still iterated and printed every matching PR, defeating the purpose of using --limit to preview a small batch. Count both dry-run and real close actions toward the limit so the flag behaves identically in both modes. --- .github/scripts/close_low_quality_prs.py | 16 ++++--- .../test_github_close_low_quality_prs.py | 46 +++++++++++++++++++ 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/.github/scripts/close_low_quality_prs.py b/.github/scripts/close_low_quality_prs.py index 66c6d03a467..26bd6e44cb2 100644 --- a/.github/scripts/close_low_quality_prs.py +++ b/.github/scripts/close_low_quality_prs.py @@ -392,7 +392,11 @@ def main() -> int: "--limit", type=int, default=None, - help="Maximum number of PRs to close in one run (safety net).", + help=( + "Maximum number of PRs to close in one run (safety net). " + "Applied in dry-run too so `--limit N` previews exactly the " + "first N closures." + ), ) args = parser.parse_args() @@ -446,11 +450,11 @@ def main() -> int: label=args.close_label, ) - if not dry_run: - closed += 1 - if args.limit is not None and closed >= args.limit: - print(f"\nReached --limit={args.limit}; stopping.") - break + closed += 1 + if args.limit is not None and closed >= args.limit: + verb = "Would close" if dry_run else "Closed" + print(f"\nReached --limit={args.limit} ({verb} count); stopping.") + break print("\n=== Summary ===") for key, value in summary.items(): diff --git a/tests/test_litellm/test_github_close_low_quality_prs.py b/tests/test_litellm/test_github_close_low_quality_prs.py index 3b2918324b1..316ca696179 100644 --- a/tests/test_litellm/test_github_close_low_quality_prs.py +++ b/tests/test_litellm/test_github_close_low_quality_prs.py @@ -447,6 +447,52 @@ class TestMainOptoutLabelDefault: assert default not in captured["optout_labels"], default +class TestMainLimitFlag: + """`--limit N` must cap closures in both dry-run and real mode.""" + + def _patch_three_closeable_prs(self, closer_module, monkeypatch): + prs = [ + { + "number": i, + "title": f"p{i}", + "createdAt": "2026-05-10T00:00:00Z", + "isDraft": False, + "labels": [], + "author": {"login": f"ext{i}"}, + } + for i in (1, 2, 3) + ] + monkeypatch.setattr(closer_module, "fetch_open_prs", lambda repo: prs) + monkeypatch.setattr( + closer_module, + "evaluate_pr", + lambda pr, now, mad, ms, repo, ol: ("close", 2, 30), + ) + called: list[int] = [] + + def fake_close_pr(pr, **kwargs): + called.append(pr["number"]) + + monkeypatch.setattr(closer_module, "close_pr", fake_close_pr) + return called + + def test_should_stop_at_limit_in_dry_run(self, closer_module, monkeypatch): + called = self._patch_three_closeable_prs(closer_module, monkeypatch) + monkeypatch.setattr(sys, "argv", ["close_low_quality_prs.py", "--limit", "2"]) + rc = closer_module.main() + assert rc == 0 + assert len(called) == 2 + + def test_should_stop_at_limit_when_closing(self, closer_module, monkeypatch): + called = self._patch_three_closeable_prs(closer_module, monkeypatch) + monkeypatch.setattr( + sys, "argv", ["close_low_quality_prs.py", "--limit", "2", "--close"] + ) + rc = closer_module.main() + assert rc == 0 + assert len(called) == 2 + + class TestFetchOpenPrsLimitWarning: """`fetch_open_prs` must surface a warning when the gh CLI cap is hit."""