From 56774d3b7814caa2263543014beb9db1cc585fdb Mon Sep 17 00:00:00 2001 From: Anuradha Karuppiah <26330987+AnuradhaKaruppiah@users.noreply.github.com> Date: Mon, 9 Nov 2026 18:59:04 -0800 Subject: [PATCH 1/5] chore: add dry-run PR cleanup automation Signed-off-by: Anuradha Karuppiah <26330987+AnuradhaKaruppiah@users.noreply.github.com> --- .github/pr-cleanup.json | 6 + .github/workflows/pr-cleanup.yml | 75 ++++++ CONTRIBUTING.md | 66 +++++ scripts/ci/pr_cleanup.py | 411 +++++++++++++++++++++++++++++ tests/scripts/test_pr_cleanup.py | 426 +++++++++++++++++++++++++++++++ 5 files changed, 984 insertions(+) create mode 100644 .github/pr-cleanup.json create mode 100644 .github/workflows/pr-cleanup.yml create mode 100644 scripts/ci/pr_cleanup.py create mode 100644 tests/scripts/test_pr_cleanup.py diff --git a/.github/pr-cleanup.json b/.github/pr-cleanup.json new file mode 100644 index 000000000..b0d3c6ced --- /dev/null +++ b/.github/pr-cleanup.json @@ -0,0 +1,6 @@ +{ + "max_open_prs": 3, + "inactive_days": 28, + "exemption_org": "NVIDIA", + "exemption_team": "nemo-fabric-developers" +} diff --git a/.github/workflows/pr-cleanup.yml b/.github/workflows/pr-cleanup.yml new file mode 100644 index 000000000..517b02c92 --- /dev/null +++ b/.github/workflows/pr-cleanup.yml @@ -0,0 +1,75 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +name: PR Cleanup + +on: + pull_request_target: + types: [opened, reopened, synchronize] + schedule: + - cron: '23 9 * * *' + workflow_dispatch: + inputs: + apply: + description: 'Post comments and close PRs (otherwise report only)' + type: boolean + default: false + +concurrency: + group: pr-cleanup + cancel-in-progress: false + +jobs: + cleanup: + if: >- + github.repository == 'NVIDIA/NeMo-Fabric' && + (github.event_name != 'workflow_dispatch' || + github.ref == format('refs/heads/{0}', github.event.repository.default_branch)) + runs-on: ubuntu-24.04 + timeout-minutes: 20 + permissions: + contents: read + actions: read + pull-requests: write + steps: + - name: Checkout trusted default branch + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 + with: + ref: ${{ github.event.repository.default_branch }} + persist-credentials: false + + - name: Create organization membership read token + if: vars.PR_CLEANUP_APP_ID != '' + id: members + uses: actions/create-github-app-token@fee1f7d63c2ff003460e3d139729b119787bc349 # v2 + with: + app-id: ${{ vars.PR_CLEANUP_APP_ID }} + private-key: ${{ secrets.PR_CLEANUP_APP_PRIVATE_KEY }} + owner: NVIDIA + repositories: NeMo-Fabric + permission-members: read + + - name: Test cleanup policy + run: python3 -m unittest discover -s tests/scripts -p test_pr_cleanup.py -v + + - name: Reconcile pull requests + env: + GH_TOKEN: ${{ github.token }} + PR_CLEANUP_MEMBERS_TOKEN: ${{ steps.members.outputs.token }} + DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} + APPLY: ${{ (github.event_name == 'workflow_dispatch' && inputs.apply) || (github.event_name != 'workflow_dispatch' && vars.PR_CLEANUP_APPLY == 'true') }} + run: | + args=() + if [[ "$APPLY" == "true" ]]; then + args+=(--apply) + fi + python3 scripts/ci/pr_cleanup.py --repo "$GITHUB_REPOSITORY" "${args[@]}" + + - name: Save push observation history + if: always() && hashFiles('.tmp/pr-cleanup/state.json') != '' + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: pr-cleanup-state + path: .tmp/pr-cleanup/state.json + retention-days: 90 + if-no-files-found: error diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d22093743..417fa39aa 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -231,6 +231,72 @@ maintenance updates. This section describes how to prepare and submit changes for review. +### PR Cleanup Policy + +The PR Cleanup workflow initially runs in **dry-run mode**: it reports proposed +closures without posting comments or closing PRs. Once enforcement is enabled: + +- Each author may have up to **3 open PRs, including drafts**, across all target + branches. Cleanup keeps the three oldest by original creation time and closes + newer excess PRs. Maintainers of the + [`nemo-fabric-developers` team](https://github.com/orgs/NVIDIA/teams/nemo-fabric-developers) + are exempt from this cap; repository Maintain or Admin access alone does not + grant an exemption. Team roles are checked on every run. +- PRs with **more than 28 days without activity**, including drafts, are closed + regardless of author or team role. Human comments and edits, reviews and review + comments, reopening, and pushes count as activity. Bot comments, CI results, + labels, and draft/ready transitions alone do not count. + +Each closure explains its reason. To resume an inactive PR, reopen it when ready +and tag maintainers for review. To reopen a PR closed for exceeding the cap, +first merge or close another PR. Reopening preserves original creation order +and triggers policy evaluation again. Branches and discussions are preserved. + +Cleanup runs when PRs open, reopen, or receive commits, and daily. It acts after +PR creation, so an excess PR may briefly appear and trigger CI. + +#### Maintainer Setup and Rollout + +The configuration is in `.github/pr-cleanup.json`; the implementation and tests +are in `scripts/ci/pr_cleanup.py` and `tests/scripts/test_pr_cleanup.py`. + +1. Install an organization-approved GitHub App with organization **Members: + read** permission and access to this repository. Set repository variable + `PR_CLEANUP_APP_ID` and secret `PR_CLEANUP_APP_PRIVATE_KEY`. The workflow + obtains a short-lived membership-read token; repository writes use the + separate `GITHUB_TOKEN`. Missing or failed membership reads skip cap + enforcement and report an error, rather than assuming nobody is exempt. +2. Confirm the organization/repository Actions event policy permits + `pull_request_target`. The workflow executes only trusted default-branch + code, never PR-head code. Allow the pinned actions in the organization's + action policy. Manual dispatch is restricted to the default branch. +3. Merge with `PR_CLEANUP_APPLY` unset. Inspect dry-run summaries, announce the + policy, and then set that repository variable to `true` to enable automatic + comments and closures. Manual dispatch defaults to reporting only; selecting + its `apply` input explicitly requests enforcement for that run, regardless + of the automatic setting. Existing excess PRs are included in enforcement. +4. To pause closures, unset `PR_CLEANUP_APPLY` and avoid manual apply runs, or + disable the workflow. Previously closed PRs are not automatically reopened. + +Push timing is observed and saved in the workflow's `pr-cleanup-state` artifact; +commit author dates are not used as push timestamps. New PRs, missing/expired +state, or an observation gap of at least 28 days start a fresh 28-day observation +period. Consequently, the first deployment does not immediately close old PRs +for inactivity. Dry-run observation history carries forward into enforcement. +Keep artifact retention at least 28 days and do not delete the latest history +unless you intend to restart observation. Comment and review history is read +with pagination before an inactivity decision. Failed lookups skip affected +closures and appear in the workflow summary. Rechecks reduce races with human +activity, but GitHub does not offer atomic read-and-close operations. + +Local read-only inspection, with an authenticated `gh` CLI and an organization +membership-read token available in `PR_CLEANUP_MEMBERS_TOKEN`: + +```bash +GH_TOKEN="$(gh auth token)" python3 scripts/ci/pr_cleanup.py --repo NVIDIA/NeMo-Fabric +python3 -m unittest discover -s tests/scripts -p test_pr_cleanup.py -v +``` + ### Before Submitting Complete these checks before opening or updating a pull request: diff --git a/scripts/ci/pr_cleanup.py b/scripts/ci/pr_cleanup.py new file mode 100644 index 000000000..729a20370 --- /dev/null +++ b/scripts/ci/pr_cleanup.py @@ -0,0 +1,411 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +"""Reconcile PR limits. Default to read-only; use only trusted default-branch code.""" + +from __future__ import annotations + +import argparse +import io +import json +import os +import re +import subprocess +import sys +import zipfile +from collections import defaultdict +from datetime import datetime, timedelta, timezone +from pathlib import Path + + +ARTIFACT = "pr-cleanup-state" +MARKER = "" + if not any( + (comment.get("body") or "").startswith(marker) + and (comment.get("user") or {}).get("login") == "github-actions[bot]" + for comment in comments + ): + self.api.request( + f"{base}/issues/{number}/comments", + "POST", + {"body": f"{marker}\n{explanation}"}, + ) + # If closure fails, the existing marker allows a retry without another comment. + self.api.request(f"{base}/pulls/{number}", "PATCH", {"state": "closed"}) + self.log(number, "closed", explanation) + + def reconcile(self, pulls): + cutoff = self.now - timedelta(days=self.policy["inactive_days"]) + simulated_closed = set() + for pr in pulls: + number = pr["number"] + try: + activity, _, _ = self.activity(pr) + if activity >= cutoff: + continue + explanation = ( + f"No qualifying activity since {iso(activity)} (more than {self.policy['inactive_days']} days). " + "Reopen when ready to resume work and tag the maintainers for review." + ) + if self.apply: + fresh = self.api.request(f"repos/{self.api.repo}/pulls/{number}") + if fresh["state"] != "open" or self.activity(fresh)[0] >= cutoff: + self.log( + number, + "skipped", + "State or activity changed before closure", + ) + continue + self.close(fresh, "inactive", explanation) + else: + self.log(number, "would close", explanation) + simulated_closed.add(number) + except (CleanupError, KeyError, ValueError) as error: + self.errors.append(f"PR #{number}: inactivity skipped: {error}") + + # Failed inactivity closures still count toward the real cap. + remaining = ( + self.api.pulls() + if self.apply + else [pr for pr in pulls if pr["number"] not in simulated_closed] + ) + try: + maintainers = self.members_api.maintainers(self.policy) + except (CleanupError, KeyError, ValueError) as error: + self.errors.append( + f"Cap enforcement skipped: team maintainer lookup failed: {error}" + ) + return + limit = self.policy["max_open_prs"] + for pr in excess_prs(remaining, maintainers, limit): + number = pr["number"] + try: + current = remaining + if self.apply: + maintainers = self.members_api.maintainers(self.policy) + current = self.api.pulls() + if number not in { + item["number"] + for item in excess_prs(current, maintainers, limit) + }: + self.log( + number, + "skipped", + "Capacity, team role, or PR state changed", + ) + continue + author = pr["user"]["login"].casefold() + retained = sorted( + [ + item + for item in current + if item["user"]["login"].casefold() == author + ], + key=lambda item: (timestamp(item["created_at"]), item["number"]), + )[:limit] + links = ", ".join(f"#{item['number']}" for item in retained) + explanation = ( + f"The limit is {limit} open PRs per author, including drafts. " + f"Keeping the oldest: {links}. Merge or close one before reopening this PR." + ) + if self.apply: + self.close(pr, "cap", explanation) + else: + self.log(number, "would close", explanation) + except (CleanupError, KeyError, ValueError) as error: + self.errors.append( + f"PR #{number}: cap closure skipped or failed: {error}" + ) + + +def main(argv=None): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--repo", required=True) + parser.add_argument("--policy", default=".github/pr-cleanup.json") + parser.add_argument("--state", default=".tmp/pr-cleanup/state.json") + parser.add_argument( + "--apply", + action="store_true", + help="Explicitly enable PR comments and closures", + ) + args = parser.parse_args(argv) + if not re.fullmatch(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+", args.repo): + parser.error("Invalid repository") + policy = load_policy(args.policy) + api = GitHub(args.repo, os.environ["GH_TOKEN"]) + members_api = GitHub( + args.repo, os.environ.get("PR_CLEANUP_MEMBERS_TOKEN") or os.environ["GH_TOKEN"] + ) + now = datetime.now(timezone.utc) + pulls = api.pulls() + errors = [] + try: + previous = api.previous_state() + except (CleanupError, KeyError, ValueError, zipfile.BadZipFile) as error: + previous = None + errors.append( + f"Push observation history unavailable; starting a fresh observation period: {error}" + ) + event = None + if os.environ.get("GITHUB_EVENT_NAME") == "pull_request_target": + event = json.loads(Path(os.environ["GITHUB_EVENT_PATH"]).read_text()) + state = observe_heads(pulls, previous, now, policy["inactive_days"], event) + state["repository"] = args.repo + cleanup = Cleanup(api, members_api, policy, state, now, args.apply) + cleanup.reconcile(pulls) + # Persist even on partial failures: missing history never makes a PR look older. + path = Path(args.state) + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(json.dumps(state, indent=2) + "\n") + report = { + "mode": "apply" if args.apply else "dry-run", + "decisions": cleanup.rows, + "errors": errors + cleanup.errors, + } + rendered = json.dumps(report, indent=2) + print(rendered) + summary = os.environ.get("GITHUB_STEP_SUMMARY") + if summary: + # JSON is indented Markdown, so untrusted API text cannot inject headings/HTML. + with open(summary, "a") as stream: + stream.write( + "## PR cleanup\n\n" + + "\n".join(" " + line for line in rendered.splitlines()) + + "\n" + ) + return int(bool(report["errors"])) + + +if __name__ == "__main__": + try: + sys.exit(main()) + except ( + CleanupError, + KeyError, + ValueError, + OSError, + subprocess.TimeoutExpired, + ) as error: + print(f"Cleanup stopped without further mutations: {error}", file=sys.stderr) + sys.exit(1) diff --git a/tests/scripts/test_pr_cleanup.py b/tests/scripts/test_pr_cleanup.py new file mode 100644 index 000000000..fa5fdd04f --- /dev/null +++ b/tests/scripts/test_pr_cleanup.py @@ -0,0 +1,426 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +"""Policy and API integration tests; run with unittest without project dependencies.""" + +from __future__ import annotations + +import copy +import io +import json +import sys +import tempfile +import unittest +import zipfile +from datetime import datetime, timedelta, timezone +from pathlib import Path +from unittest.mock import patch + + +sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "scripts" / "ci")) +import pr_cleanup as cleanup # noqa: E402 + + +NOW = datetime(2026, 11, 9, 12, tzinfo=timezone.utc) +POLICY = { + "max_open_prs": 3, + "inactive_days": 28, + "exemption_org": "NVIDIA", + "exemption_team": "nemo-fabric-developers", +} + + +def pr(number, author="developer", days=40, draft=False): + return { + "number": number, + "user": {"login": author}, + "created_at": cleanup.iso(NOW - timedelta(days=days)), + "head": {"sha": f"head-{number}"}, + "state": "open", + "draft": draft, + } + + +def comment(days=1, bot=False, body="review feedback"): + return { + "user": { + "login": "github-actions[bot]" if bot else "reviewer", + "type": "Bot" if bot else "User", + }, + "updated_at": cleanup.iso(NOW - timedelta(days=days)), + "body": body, + } + + +def state(pulls, days=40): + return { + "version": 1, + "repository": "NVIDIA/NeMo-Fabric", + "observed_at": cleanup.iso(NOW - timedelta(hours=1)), + "pulls": { + str(item["number"]): { + "head": item["head"]["sha"], + "last_push_observed_at": cleanup.iso(NOW - timedelta(days=days)), + } + for item in pulls + }, + } + + +class FakeGitHub: + repo = "NVIDIA/NeMo-Fabric" + + def __init__(self, pulls, maintainers=()): + self.items = copy.deepcopy(pulls) + self.exempt = set(maintainers) + self.comments = {} + self.timeline = {} + self.reviews = {} + self.review_comments = {} + self.writes = [] + self.fail = set() + self.promote = None + self.members_calls = 0 + self.fresh_hook = None + + def pulls(self): + return copy.deepcopy([item for item in self.items if item["state"] == "open"]) + + def maintainers(self, policy): + self.members_calls += 1 + if "members" in self.fail: + raise cleanup.CleanupError("membership read failed") + if self.promote and self.members_calls > 1: + self.exempt.add(self.promote) + return self.exempt + + def pages(self, path): + if any(fragment in path for fragment in self.fail): + raise cleanup.CleanupError("lookup failed") + number = int(path.split("/")[-2]) + if path.endswith("/timeline"): + return copy.deepcopy(self.timeline.get(number, [])) + if path.endswith("/reviews"): + return copy.deepcopy(self.reviews.get(number, [])) + if "/pulls/" in path: + return copy.deepcopy(self.review_comments.get(number, [])) + return copy.deepcopy(self.comments.get(number, [])) + + def request(self, path, method="GET", payload=None): + number = int( + path.split("/")[-2] if path.endswith("/comments") else path.split("/")[-1] + ) + item = next(item for item in self.items if item["number"] == number) + if method == "GET": + if self.fresh_hook: + self.fresh_hook(self, number) + return copy.deepcopy(item) + if method == "PATCH" and "close" in self.fail: + raise cleanup.CleanupError("closure failed") + self.writes.append((method, number, payload)) + if method == "POST": + self.comments.setdefault(number, []).append( + comment(bot=True, body=payload["body"]) + ) + else: + item["state"] = payload["state"] + self.timeline.setdefault(number, []).append( + {"event": "closed", "id": 100 + len(self.writes)} + ) + return copy.deepcopy(item) + + +class CleanupTests(unittest.TestCase): + def run_policy(self, api, *, apply=False, days=0): + worker = cleanup.Cleanup(api, api, POLICY, state(api.items, days), NOW, apply) + worker.reconcile(api.pulls()) + return worker + + def test_cap_drafts_all_branches_and_newest_first(self): + items = [pr(i, days=50 - i, draft=i % 2 == 0) for i in range(1, 7)] + items[3]["base"] = {"ref": "release/1"} + self.assertEqual( + [item["number"] for item in cleanup.excess_prs(items, set(), 3)], [6, 5, 4] + ) + self.assertEqual(cleanup.excess_prs(items[:3], set(), 3), []) + + def test_ties_reopening_and_case_insensitive_authors(self): + items = [ + pr(i, author="Developer" if i % 2 else "developer") for i in range(1, 5) + ] + items[0]["updated_at"] = cleanup.iso( + NOW + ) # reopening does not change original order + self.assertEqual( + [item["number"] for item in cleanup.excess_prs(items, set(), 3)], [4] + ) + self.assertEqual(cleanup.excess_prs(items, {"developer"}, 3), []) + + def test_cap_exemption_is_team_role_not_repository_role(self): + api = FakeGitHub([pr(i, author="repo-admin") for i in range(1, 5)]) + api.items[-1]["permissions"] = {"admin": True} + worker = self.run_policy(api) + self.assertEqual([row["number"] for row in worker.rows], [4]) + api.exempt.add("repo-admin") + self.assertEqual(self.run_policy(api).rows, []) + + def test_dry_run_has_no_writes_and_apply_closes_newest(self): + api = FakeGitHub([pr(i) for i in range(1, 6)]) + self.run_policy(api) + self.assertEqual(api.writes, []) + self.run_policy(api, apply=True) + self.assertEqual( + [number for method, number, _ in api.writes if method == "PATCH"], [5, 4] + ) + self.assertEqual(len(api.pulls()), 3) + + def test_missing_team_permission_blocks_only_cap(self): + api = FakeGitHub([pr(i) for i in range(1, 5)]) + api.fail.add("members") + worker = self.run_policy(api, apply=True) + self.assertTrue(worker.errors) + self.assertEqual(api.writes, []) + worker = self.run_policy(api, apply=True, days=40) + self.assertEqual(len(api.pulls()), 0) # inactivity is independent of exemptions + self.assertTrue(worker.errors) + + def test_promotion_before_cap_closure(self): + api = FakeGitHub([pr(i) for i in range(1, 5)]) + api.promote = "developer" + self.run_policy(api, apply=True) + self.assertEqual(api.writes, []) + + def test_inactivity_boundary_and_team_maintainers(self): + api = FakeGitHub([pr(1, draft=True)], {"developer"}) + self.assertEqual(self.run_policy(api, days=28).rows, []) + self.assertEqual(self.run_policy(api, days=29).rows[0]["number"], 1) + + def test_human_comments_edits_reviews_and_review_comments_reset(self): + for surface in ("comments", "reviews", "review_comments"): + with self.subTest(surface=surface): + api = FakeGitHub([pr(1)]) + getattr(api, surface)[1] = [comment()] + self.assertEqual(self.run_policy(api, days=40).rows, []) + + def test_bot_ci_label_and_draft_changes_do_not_reset(self): + api = FakeGitHub([pr(1, draft=True)]) + api.items[0]["updated_at"] = cleanup.iso(NOW) + api.comments[1] = [comment(bot=True)] + api.timeline[1] = [{"event": "labeled", "created_at": cleanup.iso(NOW)}] + self.assertEqual(self.run_policy(api, days=40).rows[0]["number"], 1) + + def test_reopening_and_force_push_reset(self): + for kind in ("reopened", "head_ref_force_pushed"): + api = FakeGitHub([pr(1)]) + api.timeline[1] = [{"event": kind, "created_at": cleanup.iso(NOW)}] + self.assertEqual(self.run_policy(api, days=40).rows, []) + + def test_inactivity_before_cap_and_dry_run_parity(self): + items = [pr(i, days=50 - i) for i in range(1, 5)] + dry_api, real_api = FakeGitHub(items), FakeGitHub(items) + for api in (dry_api, real_api): + for number in (2, 3, 4): + api.comments[number] = [comment()] + dry = self.run_policy(dry_api, days=40) + real = self.run_policy(real_api, apply=True, days=40) + self.assertEqual([row["number"] for row in dry.rows], [1]) + self.assertEqual([row["number"] for row in real.rows], [1]) + self.assertEqual(len(real_api.pulls()), 3) + + def test_activity_lookup_failure_skips_inactivity(self): + api = FakeGitHub([pr(1)]) + api.fail.add("timeline") + worker = self.run_policy(api, apply=True, days=40) + self.assertTrue(worker.errors) + self.assertEqual(api.writes, []) + + def test_last_minute_comment_merge_and_head_change(self): + for change in ("comment", "merge", "push"): + api = FakeGitHub([pr(1)]) + + def update(api, number): + if change == "comment": + api.comments[number] = [comment()] + elif change == "merge": + api.items[0]["state"] = "closed" + else: + api.items[0]["head"]["sha"] = "new-head" + + api.fresh_hook = update + self.run_policy(api, apply=True, days=40) + self.assertEqual(api.writes, []) + + def test_partial_failure_retry_deduplicates_and_reopen_comments_again(self): + api = FakeGitHub([pr(1)]) + worker = cleanup.Cleanup(api, api, POLICY, state(api.items), NOW, True) + api.fail.add("close") + with self.assertRaises(cleanup.CleanupError): + worker.close(api.items[0], "cap", "reason") + api.fail.clear() + worker.close(api.items[0], "cap", "reason") + self.assertEqual(sum(method == "POST" for method, _, _ in api.writes), 1) + api.items[0]["state"] = "open" + worker.close(api.items[0], "cap", "reason") + self.assertEqual(sum(method == "POST" for method, _, _ in api.writes), 2) + + def test_bootstrap_changed_heads_gap_and_synchronize(self): + pulls = [pr(1)] + first = cleanup.observe_heads(pulls, None, NOW, 28) + self.assertEqual(first["pulls"]["1"]["last_push_observed_at"], cleanup.iso(NOW)) + previous = state(pulls) + continued = cleanup.observe_heads(pulls, previous, NOW, 28) + self.assertEqual( + continued["pulls"]["1"]["last_push_observed_at"], + previous["pulls"]["1"]["last_push_observed_at"], + ) + for event in (None, {"action": "synchronize", "number": 1}): + changed = copy.deepcopy(pulls) + if event is None: + changed[0]["head"]["sha"] = "new" + updated = cleanup.observe_heads(changed, previous, NOW, 28, event) + self.assertEqual( + updated["pulls"]["1"]["last_push_observed_at"], cleanup.iso(NOW) + ) + previous["observed_at"] = cleanup.iso(NOW - timedelta(days=28)) + reset = cleanup.observe_heads(pulls, previous, NOW, 28) + self.assertEqual(reset["pulls"]["1"]["last_push_observed_at"], cleanup.iso(NOW)) + + def test_policy_validation(self): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "policy.json" + for key, value in ( + ("max_open_prs", 0), + ("inactive_days", True), + ("exemption_team", "../../other"), + ): + path.write_text(json.dumps({**POLICY, key: value})) + with self.assertRaises(cleanup.CleanupError): + cleanup.load_policy(path) + + def test_pagination_and_team_role_query(self): + api = cleanup.GitHub("NVIDIA/NeMo-Fabric", "not-a-real-token") + with patch.object( + api, "request", side_effect=[[{"login": "A"}] * 100, [{"login": "B"}]] + ) as request: + self.assertEqual(api.maintainers(POLICY), {"a", "b"}) + self.assertIn( + "role=maintainer&per_page=100&page=2", request.call_args.args[0] + ) + with patch.object( + api, "request", side_effect=cleanup.CleanupError("page failed") + ): + with self.assertRaises(cleanup.CleanupError): + api.pages("repos/example/pulls") + + def test_state_artifacts_ignore_untrusted_run_events_and_expired_state(self): + api = cleanup.GitHub("NVIDIA/NeMo-Fabric", "not-a-real-token") + runs = [ + {"id": 1, "event": "pull_request", "head_branch": "main"}, + {"id": 2, "event": "workflow_dispatch", "head_branch": "attacker"}, + {"id": 3, "event": "schedule", "head_branch": "main"}, + ] + with patch.object( + api, + "pages", + side_effect=[runs, [{"id": 1, "name": cleanup.ARTIFACT, "expired": True}]], + ): + self.assertIsNone(api.previous_state()) + archive = io.BytesIO() + with zipfile.ZipFile(archive, "w") as zipped: + zipped.writestr("state.json", json.dumps(state([pr(1)]))) + with ( + patch.object( + api, + "pages", + side_effect=[ + runs, + [{"id": 4, "name": cleanup.ARTIFACT, "expired": False}], + ], + ), + patch.object(api, "raw", return_value=archive.getvalue()), + ): + self.assertEqual(api.previous_state()["version"], 1) + + def test_missing_or_expired_history_starts_fresh(self): + pulls = [pr(1)] + for previous in ( + None, + {**state(pulls), "observed_at": cleanup.iso(NOW - timedelta(days=90))}, + ): + observed = cleanup.observe_heads(pulls, previous, NOW, 28) + self.assertEqual( + observed["pulls"]["1"]["last_push_observed_at"], cleanup.iso(NOW) + ) + + def test_spoofed_cleanup_marker_is_still_human_activity(self): + api = FakeGitHub([pr(1)]) + api.comments[1] = [comment(body="")] + self.assertEqual(self.run_policy(api, days=40).rows, []) + + def test_spoofed_comment_does_not_suppress_closure_explanation(self): + api = FakeGitHub([pr(1)]) + api.comments[1] = [comment(body="")] + worker = cleanup.Cleanup(api, api, POLICY, state(api.items), NOW, True) + worker.close(api.items[0], "cap", "real reason") + self.assertEqual(sum(method == "POST" for method, _, _ in api.writes), 1) + + def test_main_defaults_to_no_mutations_and_saves_observations(self): + api = FakeGitHub([pr(i) for i in range(1, 5)]) + api.previous_state = lambda: None + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + policy = root / "policy.json" + policy.write_text(json.dumps(POLICY)) + with ( + patch.object(cleanup, "GitHub", return_value=api), + patch.dict( + cleanup.os.environ, + { + "GH_TOKEN": "test-token", + "GITHUB_EVENT_NAME": "workflow_dispatch", + "GITHUB_STEP_SUMMARY": str(root / "summary.md"), + }, + ), + patch("sys.stdout", new_callable=io.StringIO) as output, + ): + code = cleanup.main( + [ + "--repo", + api.repo, + "--policy", + str(policy), + "--state", + str(root / "state.json"), + ] + ) + self.assertEqual(code, 0) + self.assertEqual(api.writes, []) + self.assertEqual(json.loads(output.getvalue())["mode"], "dry-run") + self.assertEqual( + json.loads((root / "state.json").read_text())["repository"], api.repo + ) + self.assertTrue((root / "summary.md").exists()) + + def test_api_timeout_is_a_reportable_lookup_failure(self): + api = cleanup.GitHub("NVIDIA/NeMo-Fabric", "not-a-real-token") + with patch.object( + cleanup.subprocess, + "run", + side_effect=cleanup.subprocess.TimeoutExpired("gh", 90), + ): + with self.assertRaises(cleanup.CleanupError): + api.pulls() + + def test_gh_errors_do_not_echo_token_or_response_body(self): + api = cleanup.GitHub("NVIDIA/NeMo-Fabric", "secret-token") + response = type( + "Result", (), {"returncode": 1, "stdout": b"", "stderr": b"secret-token"} + )() + with patch.object(cleanup.subprocess, "run", return_value=response): + with self.assertRaises(cleanup.CleanupError) as error: + api.raw("repos/NVIDIA/NeMo-Fabric/pulls") + self.assertNotIn("secret-token", str(error.exception)) + + +if __name__ == "__main__": + unittest.main() From 5591af575ba5031763e852fe8b88116281b3a60f Mon Sep 17 00:00:00 2001 From: Anuradha Karuppiah <26330987+AnuradhaKaruppiah@users.noreply.github.com> Date: Mon, 9 Nov 2026 20:11:51 -0800 Subject: [PATCH 2/5] chore: configure fixed maintainer exemptions for PR cleanup Signed-off-by: Anuradha Karuppiah <26330987+AnuradhaKaruppiah@users.noreply.github.com> --- .github/pr-cleanup.json | 8 ++- .github/workflows/pr-cleanup.yml | 12 ----- CONTRIBUTING.md | 42 +++++++-------- scripts/ci/pr_cleanup.py | 32 ++++------- tests/scripts/test_pr_cleanup.py | 92 +++++++++++++++++--------------- 5 files changed, 85 insertions(+), 101 deletions(-) diff --git a/.github/pr-cleanup.json b/.github/pr-cleanup.json index b0d3c6ced..4111ee2e8 100644 --- a/.github/pr-cleanup.json +++ b/.github/pr-cleanup.json @@ -1,6 +1,10 @@ { "max_open_prs": 3, "inactive_days": 28, - "exemption_org": "NVIDIA", - "exemption_team": "nemo-fabric-developers" + "exempt_authors": [ + "sara-tadayon-nv", + "AjayThorve", + "dagardner-nv", + "AnuradhaKaruppiah" + ] } diff --git a/.github/workflows/pr-cleanup.yml b/.github/workflows/pr-cleanup.yml index 517b02c92..0ee784d76 100644 --- a/.github/workflows/pr-cleanup.yml +++ b/.github/workflows/pr-cleanup.yml @@ -38,24 +38,12 @@ jobs: ref: ${{ github.event.repository.default_branch }} persist-credentials: false - - name: Create organization membership read token - if: vars.PR_CLEANUP_APP_ID != '' - id: members - uses: actions/create-github-app-token@fee1f7d63c2ff003460e3d139729b119787bc349 # v2 - with: - app-id: ${{ vars.PR_CLEANUP_APP_ID }} - private-key: ${{ secrets.PR_CLEANUP_APP_PRIVATE_KEY }} - owner: NVIDIA - repositories: NeMo-Fabric - permission-members: read - - name: Test cleanup policy run: python3 -m unittest discover -s tests/scripts -p test_pr_cleanup.py -v - name: Reconcile pull requests env: GH_TOKEN: ${{ github.token }} - PR_CLEANUP_MEMBERS_TOKEN: ${{ steps.members.outputs.token }} DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} APPLY: ${{ (github.event_name == 'workflow_dispatch' && inputs.apply) || (github.event_name != 'workflow_dispatch' && vars.PR_CLEANUP_APPLY == 'true') }} run: | diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 417fa39aa..4a55024f5 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -233,18 +233,17 @@ This section describes how to prepare and submit changes for review. ### PR Cleanup Policy -The PR Cleanup workflow initially runs in **dry-run mode**: it reports proposed -closures without posting comments or closing PRs. Once enforcement is enabled: +The PR Cleanup workflow enforces the following rules when automatic enforcement +is enabled. Manual runs default to **dry-run mode**, reporting proposed closures +without posting comments or closing PRs. - Each author may have up to **3 open PRs, including drafts**, across all target branches. Cleanup keeps the three oldest by original creation time and closes - newer excess PRs. Maintainers of the - [`nemo-fabric-developers` team](https://github.com/orgs/NVIDIA/teams/nemo-fabric-developers) - are exempt from this cap; repository Maintain or Admin access alone does not - grant an exemption. Team roles are checked on every run. + newer excess PRs. Maintainers are exempt from this cap. - PRs with **more than 28 days without activity**, including drafts, are closed - regardless of author or team role. Human comments and edits, reviews and review - comments, reopening, and pushes count as activity. Bot comments, CI results, + regardless of author, including maintainers. Human comments and edits, + reviews and review comments, reopening, and pushes count as activity. Bot + comments, CI results, labels, and draft/ready transitions alone do not count. Each closure explains its reason. To resume an inactive PR, reopen it when ready @@ -260,22 +259,20 @@ PR creation, so an excess PR may briefly appear and trigger CI. The configuration is in `.github/pr-cleanup.json`; the implementation and tests are in `scripts/ci/pr_cleanup.py` and `tests/scripts/test_pr_cleanup.py`. -1. Install an organization-approved GitHub App with organization **Members: - read** permission and access to this repository. Set repository variable - `PR_CLEANUP_APP_ID` and secret `PR_CLEANUP_APP_PRIVATE_KEY`. The workflow - obtains a short-lived membership-read token; repository writes use the - separate `GITHUB_TOKEN`. Missing or failed membership reads skip cap - enforcement and report an error, rather than assuming nobody is exempt. -2. Confirm the organization/repository Actions event policy permits +The workflow uses the built-in repository `GITHUB_TOKEN`; no additional App or +credentials are required. The maintainer exemption list is maintained in the +policy configuration and should be updated when maintainers change. + +1. Confirm the organization/repository Actions event policy permits `pull_request_target`. The workflow executes only trusted default-branch code, never PR-head code. Allow the pinned actions in the organization's action policy. Manual dispatch is restricted to the default branch. -3. Merge with `PR_CLEANUP_APPLY` unset. Inspect dry-run summaries, announce the - policy, and then set that repository variable to `true` to enable automatic - comments and closures. Manual dispatch defaults to reporting only; selecting - its `apply` input explicitly requests enforcement for that run, regardless - of the automatic setting. Existing excess PRs are included in enforcement. -4. To pause closures, unset `PR_CLEANUP_APPLY` and avoid manual apply runs, or +2. Set repository variable `PR_CLEANUP_APPLY` to `true` to enable automatic + comments and closures after merge. Leave it unset for automatic dry-run + reports. Manual dispatch defaults to reporting only; selecting its `apply` + input explicitly requests enforcement for that run, regardless of the + automatic setting. Existing excess PRs are included in enforcement. +3. To pause closures, unset `PR_CLEANUP_APPLY` and avoid manual apply runs, or disable the workflow. Previously closed PRs are not automatically reopened. Push timing is observed and saved in the workflow's `pr-cleanup-state` artifact; @@ -289,8 +286,7 @@ with pagination before an inactivity decision. Failed lookups skip affected closures and appear in the workflow summary. Rechecks reduce races with human activity, but GitHub does not offer atomic read-and-close operations. -Local read-only inspection, with an authenticated `gh` CLI and an organization -membership-read token available in `PR_CLEANUP_MEMBERS_TOKEN`: +Local read-only inspection with an authenticated `gh` CLI: ```bash GH_TOKEN="$(gh auth token)" python3 scripts/ci/pr_cleanup.py --repo NVIDIA/NeMo-Fabric diff --git a/scripts/ci/pr_cleanup.py b/scripts/ci/pr_cleanup.py index 729a20370..4b2c54b52 100644 --- a/scripts/ci/pr_cleanup.py +++ b/scripts/ci/pr_cleanup.py @@ -42,9 +42,12 @@ def load_policy(path): for key in ("max_open_prs", "inactive_days"): if type(policy.get(key)) is not int or policy[key] < 1: raise CleanupError(f"Invalid {key}") - for key in ("exemption_org", "exemption_team"): - if not re.fullmatch(r"[A-Za-z0-9-]+", policy.get(key, "")): - raise CleanupError(f"Invalid {key}") + authors = policy.get("exempt_authors") + if not isinstance(authors, list) or any( + not isinstance(author, str) or not re.fullmatch(r"[A-Za-z0-9-]{1,39}", author) + for author in authors + ): + raise CleanupError("Invalid exempt_authors") return policy @@ -95,10 +98,6 @@ def pages(self, path, key=None): def pulls(self): return self.pages(f"repos/{self.repo}/pulls?state=open") - def maintainers(self, policy): - path = f"orgs/{policy['exemption_org']}/teams/{policy['exemption_team']}/members?role=maintainer" - return {member["login"].casefold() for member in self.pages(path)} - def previous_state(self): """Accept artifacts only from this trusted workflow's completed runs.""" path = ( @@ -201,9 +200,8 @@ def last_activity(pr, record, comments, reviews, review_comments, timeline): class Cleanup: - def __init__(self, api, members_api, policy, state, now, apply=False): + def __init__(self, api, policy, state, now, apply=False): self.api = api - self.members_api = members_api self.policy = policy self.state = state self.now = now @@ -290,20 +288,13 @@ def reconcile(self, pulls): if self.apply else [pr for pr in pulls if pr["number"] not in simulated_closed] ) - try: - maintainers = self.members_api.maintainers(self.policy) - except (CleanupError, KeyError, ValueError) as error: - self.errors.append( - f"Cap enforcement skipped: team maintainer lookup failed: {error}" - ) - return + maintainers = {author.casefold() for author in self.policy["exempt_authors"]} limit = self.policy["max_open_prs"] for pr in excess_prs(remaining, maintainers, limit): number = pr["number"] try: current = remaining if self.apply: - maintainers = self.members_api.maintainers(self.policy) current = self.api.pulls() if number not in { item["number"] @@ -312,7 +303,7 @@ def reconcile(self, pulls): self.log( number, "skipped", - "Capacity, team role, or PR state changed", + "Capacity or PR state changed", ) continue author = pr["user"]["login"].casefold() @@ -354,9 +345,6 @@ def main(argv=None): parser.error("Invalid repository") policy = load_policy(args.policy) api = GitHub(args.repo, os.environ["GH_TOKEN"]) - members_api = GitHub( - args.repo, os.environ.get("PR_CLEANUP_MEMBERS_TOKEN") or os.environ["GH_TOKEN"] - ) now = datetime.now(timezone.utc) pulls = api.pulls() errors = [] @@ -372,7 +360,7 @@ def main(argv=None): event = json.loads(Path(os.environ["GITHUB_EVENT_PATH"]).read_text()) state = observe_heads(pulls, previous, now, policy["inactive_days"], event) state["repository"] = args.repo - cleanup = Cleanup(api, members_api, policy, state, now, args.apply) + cleanup = Cleanup(api, policy, state, now, args.apply) cleanup.reconcile(pulls) # Persist even on partial failures: missing history never makes a PR look older. path = Path(args.state) diff --git a/tests/scripts/test_pr_cleanup.py b/tests/scripts/test_pr_cleanup.py index fa5fdd04f..6489bdf1d 100644 --- a/tests/scripts/test_pr_cleanup.py +++ b/tests/scripts/test_pr_cleanup.py @@ -25,8 +25,7 @@ POLICY = { "max_open_prs": 3, "inactive_days": 28, - "exemption_org": "NVIDIA", - "exemption_team": "nemo-fabric-developers", + "exempt_authors": ["maintainer"], } @@ -70,30 +69,19 @@ def state(pulls, days=40): class FakeGitHub: repo = "NVIDIA/NeMo-Fabric" - def __init__(self, pulls, maintainers=()): + def __init__(self, pulls): self.items = copy.deepcopy(pulls) - self.exempt = set(maintainers) self.comments = {} self.timeline = {} self.reviews = {} self.review_comments = {} self.writes = [] self.fail = set() - self.promote = None - self.members_calls = 0 self.fresh_hook = None def pulls(self): return copy.deepcopy([item for item in self.items if item["state"] == "open"]) - def maintainers(self, policy): - self.members_calls += 1 - if "members" in self.fail: - raise cleanup.CleanupError("membership read failed") - if self.promote and self.members_calls > 1: - self.exempt.add(self.promote) - return self.exempt - def pages(self, path): if any(fragment in path for fragment in self.fail): raise cleanup.CleanupError("lookup failed") @@ -131,8 +119,10 @@ def request(self, path, method="GET", payload=None): class CleanupTests(unittest.TestCase): - def run_policy(self, api, *, apply=False, days=0): - worker = cleanup.Cleanup(api, api, POLICY, state(api.items, days), NOW, apply) + def run_policy(self, api, *, apply=False, days=0, policy=None): + worker = cleanup.Cleanup( + api, policy or POLICY, state(api.items, days), NOW, apply + ) worker.reconcile(api.pulls()) return worker @@ -156,13 +146,13 @@ def test_ties_reopening_and_case_insensitive_authors(self): ) self.assertEqual(cleanup.excess_prs(items, {"developer"}, 3), []) - def test_cap_exemption_is_team_role_not_repository_role(self): + def test_cap_exemption_is_configured_not_repository_role(self): api = FakeGitHub([pr(i, author="repo-admin") for i in range(1, 5)]) api.items[-1]["permissions"] = {"admin": True} worker = self.run_policy(api) self.assertEqual([row["number"] for row in worker.rows], [4]) - api.exempt.add("repo-admin") - self.assertEqual(self.run_policy(api).rows, []) + policy = {**POLICY, "exempt_authors": ["REPO-ADMIN"]} + self.assertEqual(self.run_policy(api, policy=policy).rows, []) def test_dry_run_has_no_writes_and_apply_closes_newest(self): api = FakeGitHub([pr(i) for i in range(1, 6)]) @@ -174,24 +164,25 @@ def test_dry_run_has_no_writes_and_apply_closes_newest(self): ) self.assertEqual(len(api.pulls()), 3) - def test_missing_team_permission_blocks_only_cap(self): + def test_cap_uses_no_organization_api(self): api = FakeGitHub([pr(i) for i in range(1, 5)]) - api.fail.add("members") - worker = self.run_policy(api, apply=True) - self.assertTrue(worker.errors) - self.assertEqual(api.writes, []) - worker = self.run_policy(api, apply=True, days=40) - self.assertEqual(len(api.pulls()), 0) # inactivity is independent of exemptions - self.assertTrue(worker.errors) + with patch.object(api, "pages", wraps=api.pages) as pages: + self.run_policy(api, apply=True) + self.assertTrue( + all(call.args[0].startswith("repos/") for call in pages.call_args_list) + ) + self.assertEqual(len(api.pulls()), 3) - def test_promotion_before_cap_closure(self): + def test_capacity_rechecked_before_cap_closure(self): api = FakeGitHub([pr(i) for i in range(1, 5)]) - api.promote = "developer" - self.run_policy(api, apply=True) + initial = api.pulls() + with patch.object(api, "pulls", side_effect=[initial, initial, initial[:-1]]): + worker = self.run_policy(api, apply=True) self.assertEqual(api.writes, []) + self.assertEqual(worker.rows[0]["status"], "skipped") - def test_inactivity_boundary_and_team_maintainers(self): - api = FakeGitHub([pr(1, draft=True)], {"developer"}) + def test_inactivity_boundary_and_exempt_maintainers(self): + api = FakeGitHub([pr(1, author="maintainer", draft=True)]) self.assertEqual(self.run_policy(api, days=28).rows, []) self.assertEqual(self.run_policy(api, days=29).rows[0]["number"], 1) @@ -252,7 +243,7 @@ def update(api, number): def test_partial_failure_retry_deduplicates_and_reopen_comments_again(self): api = FakeGitHub([pr(1)]) - worker = cleanup.Cleanup(api, api, POLICY, state(api.items), NOW, True) + worker = cleanup.Cleanup(api, POLICY, state(api.items), NOW, True) api.fail.add("close") with self.assertRaises(cleanup.CleanupError): worker.close(api.items[0], "cap", "reason") @@ -291,26 +282,43 @@ def test_policy_validation(self): for key, value in ( ("max_open_prs", 0), ("inactive_days", True), - ("exemption_team", "../../other"), + ("exempt_authors", "maintainer"), + ("exempt_authors", [None]), + ("exempt_authors", ["bad/name"]), + ("exempt_authors", [""]), + ("exempt_authors", None), ): path.write_text(json.dumps({**POLICY, key: value})) with self.assertRaises(cleanup.CleanupError): cleanup.load_policy(path) - def test_pagination_and_team_role_query(self): + def test_pagination_and_failed_pages(self): api = cleanup.GitHub("NVIDIA/NeMo-Fabric", "not-a-real-token") with patch.object( - api, "request", side_effect=[[{"login": "A"}] * 100, [{"login": "B"}]] + api, "request", side_effect=[[pr(1)] * 100, [pr(2)]] ) as request: - self.assertEqual(api.maintainers(POLICY), {"a", "b"}) - self.assertIn( - "role=maintainer&per_page=100&page=2", request.call_args.args[0] - ) + self.assertEqual(len(api.pulls()), 101) + self.assertIn("per_page=100&page=2", request.call_args.args[0]) with patch.object( api, "request", side_effect=cleanup.CleanupError("page failed") ): with self.assertRaises(cleanup.CleanupError): - api.pages("repos/example/pulls") + api.pulls() + + def test_each_configured_maintainer_bypasses_only_cap(self): + policy = cleanup.load_policy( + Path(__file__).resolve().parents[2] / ".github/pr-cleanup.json" + ) + self.assertEqual(len(policy["exempt_authors"]), 4) + for author in policy["exempt_authors"]: + with self.subTest(author=author): + api = FakeGitHub( + [pr(i, author=author.upper(), draft=True) for i in range(1, 5)] + ) + self.assertEqual(self.run_policy(api, policy=policy).rows, []) + self.assertEqual( + len(self.run_policy(api, policy=policy, days=40).rows), 4 + ) def test_state_artifacts_ignore_untrusted_run_events_and_expired_state(self): api = cleanup.GitHub("NVIDIA/NeMo-Fabric", "not-a-real-token") @@ -360,7 +368,7 @@ def test_spoofed_cleanup_marker_is_still_human_activity(self): def test_spoofed_comment_does_not_suppress_closure_explanation(self): api = FakeGitHub([pr(1)]) api.comments[1] = [comment(body="")] - worker = cleanup.Cleanup(api, api, POLICY, state(api.items), NOW, True) + worker = cleanup.Cleanup(api, POLICY, state(api.items), NOW, True) worker.close(api.items[0], "cap", "real reason") self.assertEqual(sum(method == "POST" for method, _, _ in api.writes), 1) From 649cf60366ab76d4232c1c59a0f9cc58727bb8fe Mon Sep 17 00:00:00 2001 From: Anuradha Karuppiah <26330987+AnuradhaKaruppiah@users.noreply.github.com> Date: Mon, 9 Nov 2026 20:15:34 -0800 Subject: [PATCH 3/5] chore: address PR cleanup review documentation and permissions Signed-off-by: Anuradha Karuppiah <26330987+AnuradhaKaruppiah@users.noreply.github.com> --- .github/workflows/pr-cleanup.yml | 12 +++++++--- scripts/ci/pr_cleanup.py | 32 +++++++++++++++++++++++++++ tests/scripts/test_pr_cleanup.py | 38 ++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 3 deletions(-) diff --git a/.github/workflows/pr-cleanup.yml b/.github/workflows/pr-cleanup.yml index 0ee784d76..687f29faa 100644 --- a/.github/workflows/pr-cleanup.yml +++ b/.github/workflows/pr-cleanup.yml @@ -4,6 +4,8 @@ name: PR Cleanup on: + # Authenticated PR maintenance requires this trigger. Execute only the + # trusted default branch below; never check out or run code from the PR head. pull_request_target: types: [opened, reopened, synchronize] schedule: @@ -15,12 +17,16 @@ on: type: boolean default: false +# Deny inherited token access; the cleanup job grants only its required scopes. +permissions: {} + concurrency: group: pr-cleanup cancel-in-progress: false jobs: cleanup: + name: Reconcile PR cleanup policy if: >- github.repository == 'NVIDIA/NeMo-Fabric' && (github.event_name != 'workflow_dispatch' || @@ -28,9 +34,9 @@ jobs: runs-on: ubuntu-24.04 timeout-minutes: 20 permissions: - contents: read - actions: read - pull-requests: write + contents: read # Check out trusted default-branch code. + actions: read # Restore observation history from this workflow's artifacts. + pull-requests: write # Read PR activity, explain decisions, and close PRs. steps: - name: Checkout trusted default branch uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 diff --git a/scripts/ci/pr_cleanup.py b/scripts/ci/pr_cleanup.py index 4b2c54b52..658c7b3c0 100644 --- a/scripts/ci/pr_cleanup.py +++ b/scripts/ci/pr_cleanup.py @@ -23,10 +23,13 @@ class CleanupError(RuntimeError): + """Report incomplete API data or invalid policy without exposing credentials.""" + pass def timestamp(value): + """Parse a timezone-aware API timestamp; reject ambiguous local times.""" parsed = datetime.fromisoformat(value.replace("Z", "+00:00")) if parsed.tzinfo is None: raise CleanupError("Activity timestamps must include a timezone") @@ -34,10 +37,12 @@ def timestamp(value): def iso(value): + """Render an observation time as an explicit UTC ISO timestamp.""" return value.astimezone(timezone.utc).isoformat() def load_policy(path): + """Validate limits and the maintainer allowlist before permitting PR mutations.""" policy = json.loads(Path(path).read_text()) for key in ("max_open_prs", "inactive_days"): if type(policy.get(key)) is not int or policy[key] < 1: @@ -55,10 +60,16 @@ class GitHub: """Use the runner's gh CLI for authenticated API calls and artifact redirects.""" def __init__(self, repo, token): + """Bind API requests to one repository and its authentication token.""" self.repo = repo self.token = token def raw(self, path, method="GET", payload=None): + """Call gh with a fixed executable and argument list, never a shell. + + Endpoints come from trusted code and API identifiers; PR text is sent as + JSON on stdin. Let gh handle authenticated artifact-download redirects. + """ command = ["gh", "api", "--hostname", "github.com", "--method", method, path] if payload is not None: command += ["--input", "-"] @@ -67,6 +78,7 @@ def raw(self, path, method="GET", payload=None): command, input=json.dumps(payload).encode() if payload is not None else None, capture_output=True, + shell=False, timeout=90, env={**os.environ, "GH_TOKEN": self.token}, ) @@ -80,9 +92,11 @@ def raw(self, path, method="GET", payload=None): return result.stdout def request(self, path, method="GET", payload=None): + """Decode one API response as JSON and propagate lookup failures.""" return json.loads(self.raw(path, method, payload)) def pages(self, path, key=None): + """Collect every API page or fail rather than return a partial PR list.""" items = [] separator = "&" if "?" in path else "?" for page in range(1, 1001): @@ -96,6 +110,7 @@ def pages(self, path, key=None): raise CleanupError("Pagination limit reached; refusing incomplete data") def pulls(self): + """List all open PRs, including drafts and every target branch.""" return self.pages(f"repos/{self.repo}/pulls?state=open") def previous_state(self): @@ -135,6 +150,7 @@ def previous_state(self): def excess_prs(pulls, maintainers, limit): + """Select newest excess PRs per non-exempt author, retaining the oldest limit.""" authors = defaultdict(list) for pr in pulls: if pr["user"]["login"].casefold() not in maintainers: @@ -177,11 +193,13 @@ def observe_heads(pulls, previous, now, days, event=None): def human(item): + """Distinguish human comment authors from bot-generated activity.""" user = item.get("user") or {} return user.get("type") == "User" and not user.get("login", "").endswith("[bot]") def last_activity(pr, record, comments, reviews, review_comments, timeline): + """Find the latest qualifying activity, ignoring bot comments and CI events.""" dates = [timestamp(pr["created_at"]), timestamp(record["last_push_observed_at"])] for event in timeline: if event.get("event") in {"reopened", "head_ref_force_pushed"}: @@ -200,7 +218,10 @@ def last_activity(pr, record, comments, reviews, review_comments, timeline): class Cleanup: + """Reconcile inactivity first, then per-author limits, with optional writes.""" + def __init__(self, api, policy, state, now, apply=False): + """Capture the validated policy, observation state, and explicit apply mode.""" self.api = api self.policy = policy self.state = state @@ -210,9 +231,11 @@ def __init__(self, api, policy, state, now, apply=False): self.errors = [] def log(self, number, status, reason): + """Record a reportable decision without writing to GitHub.""" self.rows.append({"number": number, "status": status, "reason": reason}) def activity(self, pr): + """Fetch complete discussion history and reject a changed observed PR head.""" number = pr["number"] base = f"repos/{self.api.repo}" comments = self.api.pages(f"{base}/issues/{number}/comments") @@ -254,6 +277,13 @@ def close(self, pr, reason, explanation): self.log(number, "closed", explanation) def reconcile(self, pulls): + """Evaluate both rules and recheck candidates before apply-mode closures. + + Dry-run simulates inactivity removals before calculating excess PRs. + Failed activity reads skip only affected inactivity decisions; failed + closures remain open and count toward the cap. GitHub offers no atomic + read-and-close operation, so a race with human activity remains. + """ cutoff = self.now - timedelta(days=self.policy["inactive_days"]) simulated_closed = set() for pr in pulls: @@ -331,6 +361,7 @@ def reconcile(self, pulls): def main(argv=None): + """Run cleanup in dry-run mode unless explicitly enabled, then save observations.""" parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--repo", required=True) parser.add_argument("--policy", default=".github/pr-cleanup.json") @@ -373,6 +404,7 @@ def main(argv=None): } rendered = json.dumps(report, indent=2) print(rendered) + # This path is supplied by the Actions runner, never by PR content. summary = os.environ.get("GITHUB_STEP_SUMMARY") if summary: # JSON is indented Markdown, so untrusted API text cannot inject headings/HTML. diff --git a/tests/scripts/test_pr_cleanup.py b/tests/scripts/test_pr_cleanup.py index 6489bdf1d..33d289ab5 100644 --- a/tests/scripts/test_pr_cleanup.py +++ b/tests/scripts/test_pr_cleanup.py @@ -30,6 +30,7 @@ def pr(number, author="developer", days=40, draft=False): + """Build an open PR fixture with a deterministic creation time and head.""" return { "number": number, "user": {"login": author}, @@ -41,6 +42,7 @@ def pr(number, author="developer", days=40, draft=False): def comment(days=1, bot=False, body="review feedback"): + """Build human or bot activity with a server-provided update timestamp.""" return { "user": { "login": "github-actions[bot]" if bot else "reviewer", @@ -52,6 +54,7 @@ def comment(days=1, bot=False, body="review feedback"): def state(pulls, days=40): + """Build continuous push observations independently of commit author dates.""" return { "version": 1, "repository": "NVIDIA/NeMo-Fabric", @@ -67,9 +70,12 @@ def state(pulls, days=40): class FakeGitHub: + """Model PR state and partial API failures while recording all mutations.""" + repo = "NVIDIA/NeMo-Fabric" def __init__(self, pulls): + """Initialize isolated PRs, activity surfaces, and failure injection hooks.""" self.items = copy.deepcopy(pulls) self.comments = {} self.timeline = {} @@ -80,9 +86,11 @@ def __init__(self, pulls): self.fresh_hook = None def pulls(self): + """Return fresh copies of currently open PRs for preflight rechecks.""" return copy.deepcopy([item for item in self.items if item["state"] == "open"]) def pages(self, path): + """Serve discussion surfaces or inject an incomplete lookup failure.""" if any(fragment in path for fragment in self.fail): raise cleanup.CleanupError("lookup failed") number = int(path.split("/")[-2]) @@ -95,6 +103,7 @@ def pages(self, path): return copy.deepcopy(self.comments.get(number, [])) def request(self, path, method="GET", payload=None): + """Apply simulated comments and closures, including a failed final PATCH.""" number = int( path.split("/")[-2] if path.endswith("/comments") else path.split("/")[-1] ) @@ -119,7 +128,10 @@ def request(self, path, method="GET", payload=None): class CleanupTests(unittest.TestCase): + """Exercise cleanup policy, activity, retries, and API boundaries.""" + def run_policy(self, api, *, apply=False, days=0, policy=None): + """Run a reconciliation with controlled observations and optional enforcement.""" worker = cleanup.Cleanup( api, policy or POLICY, state(api.items, days), NOW, apply ) @@ -127,6 +139,7 @@ def run_policy(self, api, *, apply=False, days=0, policy=None): return worker def test_cap_drafts_all_branches_and_newest_first(self): + """Count drafts across branches and close the newest PRs above the cap.""" items = [pr(i, days=50 - i, draft=i % 2 == 0) for i in range(1, 7)] items[3]["base"] = {"ref": "release/1"} self.assertEqual( @@ -135,6 +148,7 @@ def test_cap_drafts_all_branches_and_newest_first(self): self.assertEqual(cleanup.excess_prs(items[:3], set(), 3), []) def test_ties_reopening_and_case_insensitive_authors(self): + """Keep original creation order with deterministic ties and normalized logins.""" items = [ pr(i, author="Developer" if i % 2 else "developer") for i in range(1, 5) ] @@ -147,6 +161,7 @@ def test_ties_reopening_and_case_insensitive_authors(self): self.assertEqual(cleanup.excess_prs(items, {"developer"}, 3), []) def test_cap_exemption_is_configured_not_repository_role(self): + """Require an explicit maintainer exemption even for repository admins.""" api = FakeGitHub([pr(i, author="repo-admin") for i in range(1, 5)]) api.items[-1]["permissions"] = {"admin": True} worker = self.run_policy(api) @@ -155,6 +170,7 @@ def test_cap_exemption_is_configured_not_repository_role(self): self.assertEqual(self.run_policy(api, policy=policy).rows, []) def test_dry_run_has_no_writes_and_apply_closes_newest(self): + """Compare read-only decisions with apply-mode newest-first closures.""" api = FakeGitHub([pr(i) for i in range(1, 6)]) self.run_policy(api) self.assertEqual(api.writes, []) @@ -165,6 +181,7 @@ def test_dry_run_has_no_writes_and_apply_closes_newest(self): self.assertEqual(len(api.pulls()), 3) def test_cap_uses_no_organization_api(self): + """Enforce the cap using repository APIs without team-membership credentials.""" api = FakeGitHub([pr(i) for i in range(1, 5)]) with patch.object(api, "pages", wraps=api.pages) as pages: self.run_policy(api, apply=True) @@ -174,6 +191,7 @@ def test_cap_uses_no_organization_api(self): self.assertEqual(len(api.pulls()), 3) def test_capacity_rechecked_before_cap_closure(self): + """Skip a cap candidate when a concurrent closure has freed capacity.""" api = FakeGitHub([pr(i) for i in range(1, 5)]) initial = api.pulls() with patch.object(api, "pulls", side_effect=[initial, initial, initial[:-1]]): @@ -182,11 +200,13 @@ def test_capacity_rechecked_before_cap_closure(self): self.assertEqual(worker.rows[0]["status"], "skipped") def test_inactivity_boundary_and_exempt_maintainers(self): + """Apply the strict inactivity boundary to draft PRs from exempt maintainers.""" api = FakeGitHub([pr(1, author="maintainer", draft=True)]) self.assertEqual(self.run_policy(api, days=28).rows, []) self.assertEqual(self.run_policy(api, days=29).rows[0]["number"], 1) def test_human_comments_edits_reviews_and_review_comments_reset(self): + """Reset inactivity for qualifying human feedback on every discussion surface.""" for surface in ("comments", "reviews", "review_comments"): with self.subTest(surface=surface): api = FakeGitHub([pr(1)]) @@ -194,6 +214,7 @@ def test_human_comments_edits_reviews_and_review_comments_reset(self): self.assertEqual(self.run_policy(api, days=40).rows, []) def test_bot_ci_label_and_draft_changes_do_not_reset(self): + """Exclude automation and metadata-only changes from the inactivity clock.""" api = FakeGitHub([pr(1, draft=True)]) api.items[0]["updated_at"] = cleanup.iso(NOW) api.comments[1] = [comment(bot=True)] @@ -201,12 +222,14 @@ def test_bot_ci_label_and_draft_changes_do_not_reset(self): self.assertEqual(self.run_policy(api, days=40).rows[0]["number"], 1) def test_reopening_and_force_push_reset(self): + """Treat reopening and force-push timeline events as fresh activity.""" for kind in ("reopened", "head_ref_force_pushed"): api = FakeGitHub([pr(1)]) api.timeline[1] = [{"event": kind, "created_at": cleanup.iso(NOW)}] self.assertEqual(self.run_policy(api, days=40).rows, []) def test_inactivity_before_cap_and_dry_run_parity(self): + """Free inactive capacity first and preserve dry-run/apply decision parity.""" items = [pr(i, days=50 - i) for i in range(1, 5)] dry_api, real_api = FakeGitHub(items), FakeGitHub(items) for api in (dry_api, real_api): @@ -219,6 +242,7 @@ def test_inactivity_before_cap_and_dry_run_parity(self): self.assertEqual(len(real_api.pulls()), 3) def test_activity_lookup_failure_skips_inactivity(self): + """Avoid inactivity closures when discussion history cannot be read completely.""" api = FakeGitHub([pr(1)]) api.fail.add("timeline") worker = self.run_policy(api, apply=True, days=40) @@ -226,10 +250,12 @@ def test_activity_lookup_failure_skips_inactivity(self): self.assertEqual(api.writes, []) def test_last_minute_comment_merge_and_head_change(self): + """Reject stale inactivity decisions after new feedback, merging, or pushes.""" for change in ("comment", "merge", "push"): api = FakeGitHub([pr(1)]) def update(api, number): + """Inject concurrent human activity or a PR state change during preflight.""" if change == "comment": api.comments[number] = [comment()] elif change == "merge": @@ -242,6 +268,7 @@ def update(api, number): self.assertEqual(api.writes, []) def test_partial_failure_retry_deduplicates_and_reopen_comments_again(self): + """Reuse an explanation after PATCH failure but explain a new reopen cycle.""" api = FakeGitHub([pr(1)]) worker = cleanup.Cleanup(api, POLICY, state(api.items), NOW, True) api.fail.add("close") @@ -255,6 +282,7 @@ def test_partial_failure_retry_deduplicates_and_reopen_comments_again(self): self.assertEqual(sum(method == "POST" for method, _, _ in api.writes), 2) def test_bootstrap_changed_heads_gap_and_synchronize(self): + """Reset observed push timing conservatively on new heads or uncertain history.""" pulls = [pr(1)] first = cleanup.observe_heads(pulls, None, NOW, 28) self.assertEqual(first["pulls"]["1"]["last_push_observed_at"], cleanup.iso(NOW)) @@ -277,6 +305,7 @@ def test_bootstrap_changed_heads_gap_and_synchronize(self): self.assertEqual(reset["pulls"]["1"]["last_push_observed_at"], cleanup.iso(NOW)) def test_policy_validation(self): + """Reject invalid limits and malformed maintainer exemption configuration.""" with tempfile.TemporaryDirectory() as directory: path = Path(directory) / "policy.json" for key, value in ( @@ -293,6 +322,7 @@ def test_policy_validation(self): cleanup.load_policy(path) def test_pagination_and_failed_pages(self): + """Read subsequent PR pages and fail on an incomplete listing.""" api = cleanup.GitHub("NVIDIA/NeMo-Fabric", "not-a-real-token") with patch.object( api, "request", side_effect=[[pr(1)] * 100, [pr(2)]] @@ -306,6 +336,7 @@ def test_pagination_and_failed_pages(self): api.pulls() def test_each_configured_maintainer_bypasses_only_cap(self): + """Verify every configured maintainer bypasses the cap but remains subject to inactivity.""" policy = cleanup.load_policy( Path(__file__).resolve().parents[2] / ".github/pr-cleanup.json" ) @@ -321,6 +352,7 @@ def test_each_configured_maintainer_bypasses_only_cap(self): ) def test_state_artifacts_ignore_untrusted_run_events_and_expired_state(self): + """Accept only usable observation artifacts from trusted workflow executions.""" api = cleanup.GitHub("NVIDIA/NeMo-Fabric", "not-a-real-token") runs = [ {"id": 1, "event": "pull_request", "head_branch": "main"}, @@ -350,6 +382,7 @@ def test_state_artifacts_ignore_untrusted_run_events_and_expired_state(self): self.assertEqual(api.previous_state()["version"], 1) def test_missing_or_expired_history_starts_fresh(self): + """Give PRs a full observation period when reliable push history is unavailable.""" pulls = [pr(1)] for previous in ( None, @@ -361,11 +394,13 @@ def test_missing_or_expired_history_starts_fresh(self): ) def test_spoofed_cleanup_marker_is_still_human_activity(self): + """Count a human comment even when its body imitates an automation marker.""" api = FakeGitHub([pr(1)]) api.comments[1] = [comment(body="")] self.assertEqual(self.run_policy(api, days=40).rows, []) def test_spoofed_comment_does_not_suppress_closure_explanation(self): + """Prevent a human-authored marker from suppressing the bot explanation.""" api = FakeGitHub([pr(1)]) api.comments[1] = [comment(body="")] worker = cleanup.Cleanup(api, POLICY, state(api.items), NOW, True) @@ -373,6 +408,7 @@ def test_spoofed_comment_does_not_suppress_closure_explanation(self): self.assertEqual(sum(method == "POST" for method, _, _ in api.writes), 1) def test_main_defaults_to_no_mutations_and_saves_observations(self): + """Keep CLI defaults read-only while persisting state and a run summary.""" api = FakeGitHub([pr(i) for i in range(1, 5)]) api.previous_state = lambda: None with tempfile.TemporaryDirectory() as directory: @@ -410,6 +446,7 @@ def test_main_defaults_to_no_mutations_and_saves_observations(self): self.assertTrue((root / "summary.md").exists()) def test_api_timeout_is_a_reportable_lookup_failure(self): + """Convert API timeouts into safe, reportable lookup errors.""" api = cleanup.GitHub("NVIDIA/NeMo-Fabric", "not-a-real-token") with patch.object( cleanup.subprocess, @@ -420,6 +457,7 @@ def test_api_timeout_is_a_reportable_lookup_failure(self): api.pulls() def test_gh_errors_do_not_echo_token_or_response_body(self): + """Keep credentials and untrusted response bodies out of API error messages.""" api = cleanup.GitHub("NVIDIA/NeMo-Fabric", "secret-token") response = type( "Result", (), {"returncode": 1, "stdout": b"", "stderr": b"secret-token"} From a116d82b1d6470e7a16eb03d33585be6cb50a37b Mon Sep 17 00:00:00 2001 From: Anuradha Karuppiah <26330987+AnuradhaKaruppiah@users.noreply.github.com> Date: Mon, 9 Nov 2026 20:20:18 -0800 Subject: [PATCH 4/5] chore: extend PR cleanup maintainer exemptions Signed-off-by: Anuradha Karuppiah <26330987+AnuradhaKaruppiah@users.noreply.github.com> --- .github/pr-cleanup.json | 4 +++- tests/scripts/test_pr_cleanup.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/pr-cleanup.json b/.github/pr-cleanup.json index 4111ee2e8..b62143b64 100644 --- a/.github/pr-cleanup.json +++ b/.github/pr-cleanup.json @@ -5,6 +5,8 @@ "sara-tadayon-nv", "AjayThorve", "dagardner-nv", - "AnuradhaKaruppiah" + "AnuradhaKaruppiah", + "yczhang-nv", + "abhisawa-Nvidia" ] } diff --git a/tests/scripts/test_pr_cleanup.py b/tests/scripts/test_pr_cleanup.py index 33d289ab5..f66f1446d 100644 --- a/tests/scripts/test_pr_cleanup.py +++ b/tests/scripts/test_pr_cleanup.py @@ -340,7 +340,7 @@ def test_each_configured_maintainer_bypasses_only_cap(self): policy = cleanup.load_policy( Path(__file__).resolve().parents[2] / ".github/pr-cleanup.json" ) - self.assertEqual(len(policy["exempt_authors"]), 4) + self.assertEqual(len(policy["exempt_authors"]), 6) for author in policy["exempt_authors"]: with self.subTest(author=author): api = FakeGitHub( From 3d7fd3be9aa0646a1ed37a8fa6a3e0eebac731f3 Mon Sep 17 00:00:00 2001 From: Anuradha Karuppiah <26330987+AnuradhaKaruppiah@users.noreply.github.com> Date: Tue, 10 Nov 2026 09:56:23 -0800 Subject: [PATCH 5/5] chore: count edited human reviews in PR inactivity checks Signed-off-by: Anuradha Karuppiah <26330987+AnuradhaKaruppiah@users.noreply.github.com> --- scripts/ci/pr_cleanup.py | 5 ++- tests/scripts/test_pr_cleanup.py | 63 +++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/scripts/ci/pr_cleanup.py b/scripts/ci/pr_cleanup.py index 658c7b3c0..803393ac1 100644 --- a/scripts/ci/pr_cleanup.py +++ b/scripts/ci/pr_cleanup.py @@ -204,7 +204,10 @@ def last_activity(pr, record, comments, reviews, review_comments, timeline): for event in timeline: if event.get("event") in {"reopened", "head_ref_force_pushed"}: dates.append(timestamp(event["created_at"])) - for item in [*comments, *reviews, *review_comments]: + # The reviews endpoint omits updated_at; reviewed timeline events expose + # edits to top-level reviews. Apply the same human-author filter to both. + review_events = [event for event in timeline if event.get("event") == "reviewed"] + for item in [*comments, *reviews, *review_comments, *review_events]: if human(item): value = ( item.get("updated_at") diff --git a/tests/scripts/test_pr_cleanup.py b/tests/scripts/test_pr_cleanup.py index f66f1446d..cb25d8b70 100644 --- a/tests/scripts/test_pr_cleanup.py +++ b/tests/scripts/test_pr_cleanup.py @@ -213,6 +213,58 @@ def test_human_comments_edits_reviews_and_review_comments_reset(self): getattr(api, surface)[1] = [comment()] self.assertEqual(self.run_policy(api, days=40).rows, []) + def test_edited_top_level_human_review_prevents_inactivity_closure(self): + """Use timeline review edits when the reviews endpoint has only submitted_at.""" + api = FakeGitHub([pr(1)]) + review = { + "id": 10, + "user": {"login": "reviewer", "type": "User"}, + "state": "approved", + "submitted_at": cleanup.iso(NOW - timedelta(days=40)), + } + api.reviews[1] = [review] + api.timeline[1] = [ + {**review, "event": "reviewed", "updated_at": cleanup.iso(NOW)} + ] + worker = self.run_policy(api, apply=True, days=40) + self.assertEqual(worker.errors, []) + self.assertEqual(worker.rows, []) + self.assertEqual(api.writes, []) + self.assertEqual(len(api.pulls()), 1) + + def test_edited_top_level_bot_review_does_not_reset_inactivity(self): + """Continue to ignore recent bot-review edits exposed by the timeline.""" + api = FakeGitHub([pr(1)]) + review = { + "id": 10, + "user": {"login": "review-bot[bot]", "type": "Bot"}, + "state": "commented", + "submitted_at": cleanup.iso(NOW - timedelta(days=40)), + } + api.reviews[1] = [review] + api.timeline[1] = [ + {**review, "event": "reviewed", "updated_at": cleanup.iso(NOW)} + ] + worker = self.run_policy(api, apply=True, days=40) + self.assertEqual(worker.errors, []) + self.assertEqual([row["status"] for row in worker.rows], ["closed"]) + self.assertEqual(len(api.pulls()), 0) + + def test_review_timeline_without_updated_at_uses_submission_time(self): + """Do not invent recent activity when timeline review edit time is absent.""" + api = FakeGitHub([pr(1)]) + review = { + "id": 10, + "user": {"login": "reviewer", "type": "User"}, + "state": "approved", + "submitted_at": cleanup.iso(NOW - timedelta(days=40)), + } + api.reviews[1] = [review] + api.timeline[1] = [{**review, "event": "reviewed"}] + worker = self.run_policy(api, apply=True, days=40) + self.assertEqual(worker.errors, []) + self.assertEqual([row["status"] for row in worker.rows], ["closed"]) + def test_bot_ci_label_and_draft_changes_do_not_reset(self): """Exclude automation and metadata-only changes from the inactivity clock.""" api = FakeGitHub([pr(1, draft=True)]) @@ -251,13 +303,22 @@ def test_activity_lookup_failure_skips_inactivity(self): def test_last_minute_comment_merge_and_head_change(self): """Reject stale inactivity decisions after new feedback, merging, or pushes.""" - for change in ("comment", "merge", "push"): + for change in ("comment", "review", "merge", "push"): api = FakeGitHub([pr(1)]) def update(api, number): """Inject concurrent human activity or a PR state change during preflight.""" if change == "comment": api.comments[number] = [comment()] + elif change == "review": + api.timeline[number] = [ + { + "event": "reviewed", + "user": {"login": "reviewer", "type": "User"}, + "submitted_at": cleanup.iso(NOW - timedelta(days=40)), + "updated_at": cleanup.iso(NOW), + } + ] elif change == "merge": api.items[0]["state"] = "closed" else: