Skip to content

Commit cddb6ac

Browse files
Pigbibicodex
andcommitted
fix: retire stale strategy QPK pin proposals
Co-Authored-By: Codex <noreply@openai.com>
1 parent bf464da commit cddb6ac

2 files changed

Lines changed: 85 additions & 23 deletions

File tree

scripts/merge_verified_strategy_qpk_pin_prs.py

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,48 @@ def parse_args() -> argparse.Namespace:
286286
return parser.parse_args()
287287

288288

289+
def process_repo(
290+
repo: RepoSpec,
291+
*,
292+
qpk_sha: str,
293+
close_superseded: bool,
294+
env: dict[str, str],
295+
) -> None:
296+
"""Queue the current pin when eligible, then retire verified older pins.
297+
298+
A current generated PR may already have merged by the time the hourly
299+
workflow runs again. Cleanup must therefore be independent of finding an
300+
open current PR; otherwise historical proposal branches accumulate forever.
301+
"""
302+
303+
branch = expected_branch(repo, qpk_sha)
304+
pr = open_pr_payload(repo, branch, env=env)
305+
if pr is None:
306+
print(f"{repo.name}: no current generated PR")
307+
else:
308+
reason = candidate_reason(
309+
pr=pr,
310+
changed_files=changed_files(repo, int(pr["number"]), env=env),
311+
pyproject_text=pyproject_text(repo, pr["headRefOid"], env=env),
312+
qpk_sha=qpk_sha,
313+
)
314+
if reason is not None:
315+
print(f"{repo.name}: skipped:{reason}")
316+
else:
317+
merge_candidate(repo, pr, env=env)
318+
print(f"{repo.name}: queued:{pr['url']}")
319+
320+
if close_superseded:
321+
closed = close_superseded_candidates(
322+
repo,
323+
current_branch=branch,
324+
qpk_sha=qpk_sha,
325+
env=env,
326+
)
327+
if closed:
328+
print(f"{repo.name}: closed_superseded:{','.join(closed)}")
329+
330+
289331
def main() -> int:
290332
args = parse_args()
291333
qpk_sha = args.qpk_sha.strip()
@@ -299,31 +341,12 @@ def main() -> int:
299341
failures = 0
300342
for repo in STRATEGY_REPOS:
301343
try:
302-
branch = expected_branch(repo, qpk_sha)
303-
pr = open_pr_payload(repo, branch, env=env)
304-
if pr is None:
305-
print(f"{repo.name}: no current generated PR")
306-
continue
307-
reason = candidate_reason(
308-
pr=pr,
309-
changed_files=changed_files(repo, int(pr["number"]), env=env),
310-
pyproject_text=pyproject_text(repo, pr["headRefOid"], env=env),
344+
process_repo(
345+
repo,
311346
qpk_sha=qpk_sha,
347+
close_superseded=args.close_superseded,
348+
env=env,
312349
)
313-
if reason is not None:
314-
print(f"{repo.name}: skipped:{reason}")
315-
continue
316-
merge_candidate(repo, pr, env=env)
317-
print(f"{repo.name}: queued:{pr['url']}")
318-
if args.close_superseded:
319-
closed = close_superseded_candidates(
320-
repo,
321-
current_branch=branch,
322-
qpk_sha=qpk_sha,
323-
env=env,
324-
)
325-
if closed:
326-
print(f"{repo.name}: closed_superseded:{','.join(closed)}")
327350
except (RuntimeError, subprocess.CalledProcessError, ValueError, UnicodeDecodeError) as exc:
328351
failures += 1
329352
if isinstance(exc, subprocess.CalledProcessError):

tests/test_merge_verified_strategy_qpk_pin_prs.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
ALLOWED_CHANGED_FILES,
55
candidate_reason,
66
expected_branch,
7+
process_repo,
78
superseded_pr_reason,
89
)
910
from scripts.open_downstream_qpk_pin_prs import RepoSpec
@@ -79,3 +80,41 @@ def test_only_a_recognized_older_generated_pr_can_be_closed() -> None:
7980
manual = _pr()
8081
manual["headRefName"] = "codex/manual-dependency-change"
8182
assert superseded_pr_reason(pr=manual, current_branch=current_branch) == "unexpected_branch"
83+
84+
85+
def test_cleanup_runs_after_current_pin_has_already_merged(monkeypatch, capsys) -> None:
86+
repo = RepoSpec("UsEquityStrategies")
87+
monkeypatch.setattr(
88+
"scripts.merge_verified_strategy_qpk_pin_prs.open_pr_payload",
89+
lambda *_args, **_kwargs: None,
90+
)
91+
observed: dict[str, object] = {}
92+
93+
def close(repo_arg, *, current_branch, qpk_sha, env):
94+
observed.update(
95+
repo=repo_arg,
96+
current_branch=current_branch,
97+
qpk_sha=qpk_sha,
98+
env=env,
99+
)
100+
return ["123"]
101+
102+
monkeypatch.setattr(
103+
"scripts.merge_verified_strategy_qpk_pin_prs.close_superseded_candidates",
104+
close,
105+
)
106+
107+
process_repo(
108+
repo,
109+
qpk_sha=TARGET,
110+
close_superseded=True,
111+
env={"GH_TOKEN": "test"},
112+
)
113+
114+
assert observed["repo"] == repo
115+
assert observed["current_branch"] == expected_branch(repo, TARGET)
116+
assert observed["qpk_sha"] == TARGET
117+
assert capsys.readouterr().out.splitlines() == [
118+
"UsEquityStrategies: no current generated PR",
119+
"UsEquityStrategies: closed_superseded:123",
120+
]

0 commit comments

Comments
 (0)