From 8b0aeac3e111db307474f10a44f978ec37adbd34 Mon Sep 17 00:00:00 2001 From: lezi-fun <177434121+lezi-fun@users.noreply.github.com> Date: Tue, 19 May 2026 16:55:45 +0800 Subject: [PATCH] Add scheduled branch cleanup --- README.md | 1 + src/actions/runReview.ts | 16 +++++++ src/review/processor.ts | 95 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+) diff --git a/README.md b/README.md index cb4441b..6c0137b 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ GitHub Actions bot that reviews pull requests by calling Codex CLI, leaves inlin - Supports a lenient pass when a repository administrator comments `/lenient-check`. - If no repository administrator has commented or reviewed within the last 24 hours, the lenient trigger automatically broadens to users with `write`, `maintain`, or `admin` permission. - A scheduled recheck runs hourly so the 24-hour fallback can take effect even if no new PR event arrives after the window expires. +- The hourly scheduled run also deletes merged branches immediately and deletes branches for closed PRs after 3 days. - In lenient mode, only blocks dangerous changes, runtime-impacting issues, errors, crashes, broken builds, data-loss risks, and security risks. - A PR that passes lenient mode is not auto-merged until an eligible reviewer approves the current head commit. - Eligible reviewers for lenient merge approval are: diff --git a/src/actions/runReview.ts b/src/actions/runReview.ts index 5eb2cc5..1d05e2f 100644 --- a/src/actions/runReview.ts +++ b/src/actions/runReview.ts @@ -7,6 +7,7 @@ import { processLenientCheckComment, processPullRequest, processScheduledLenientMerges, + processScheduledBranchCleanup, processPullRequestReviewApproval } from "../review/processor.js"; @@ -144,6 +145,21 @@ async function main(): Promise { if (eventName === "schedule") { const scheduledPayload = payload as ScheduledPayload; + try { + await processScheduledBranchCleanup(octokit, { + owner: scheduledPayload.repository.owner.login, + repo: scheduledPayload.repository.name + }); + } catch (error) { + logger.warn( + { + error, + owner: scheduledPayload.repository.owner.login, + repo: scheduledPayload.repository.name + }, + "Scheduled branch cleanup failed." + ); + } await processScheduledLenientMerges(octokit, { owner: scheduledPayload.repository.owner.login, repo: scheduledPayload.repository.name diff --git a/src/review/processor.ts b/src/review/processor.ts index 84a4c4c..7e44956 100644 --- a/src/review/processor.ts +++ b/src/review/processor.ts @@ -13,6 +13,7 @@ const reviewer = new CodexCliReviewer(); const CHECK_RUN_NAME = "ghbot review"; export const LENIENT_COMMENT_COMMAND = "/lenient-check"; const ADMIN_RESPONSE_WINDOW_MS = 24 * 60 * 60 * 1000; +const CLOSED_BRANCH_DELETE_AFTER_MS = 3 * 24 * 60 * 60 * 1000; export async function processPullRequest(octokit: Octokit, ref: PullRequestRef, mode: ReviewMode = "strict"): Promise { const { owner, repo, pullNumber } = ref; @@ -153,6 +154,55 @@ export async function processScheduledLenientMerges( } } +export async function processScheduledBranchCleanup( + octokit: Octokit, + params: { + owner: string; + repo: string; + } +): Promise { + const pullRequests = await octokit.paginate(octokit.rest.pulls.list, { + owner: params.owner, + repo: params.repo, + state: "all", + per_page: 100 + }); + + const now = Date.now(); + + for (const pullRequest of pullRequests) { + if (!pullRequest.head?.ref || pullRequest.head.repo?.full_name !== `${params.owner}/${params.repo}`) { + continue; + } + + if (pullRequest.merged_at) { + await deleteBranchIfPresent(octokit, { + owner: params.owner, + repo: params.repo, + branch: pullRequest.head.ref, + reason: `merged PR #${pullRequest.number}` + }); + continue; + } + + if (pullRequest.state !== "closed" || !pullRequest.closed_at) { + continue; + } + + const closedAt = Date.parse(pullRequest.closed_at); + if (Number.isNaN(closedAt) || now - closedAt < CLOSED_BRANCH_DELETE_AFTER_MS) { + continue; + } + + await deleteBranchIfPresent(octokit, { + owner: params.owner, + repo: params.repo, + branch: pullRequest.head.ref, + reason: `closed PR #${pullRequest.number} older than 3 days` + }); + } +} + export async function processPullRequestReviewApproval( octokit: Octokit, params: { @@ -818,6 +868,51 @@ async function dismissExistingBotReviews( } } +async function deleteBranchIfPresent( + octokit: Octokit, + params: { + owner: string; + repo: string; + branch: string; + reason: string; + } +): Promise { + try { + await withRetry("github.git.deleteRef", async () => { + return octokit.rest.git.deleteRef({ + owner: params.owner, + repo: params.repo, + ref: `heads/${params.branch}` + }); + }); + + logger.info( + { + owner: params.owner, + repo: params.repo, + branch: params.branch, + reason: params.reason + }, + "Deleted branch after PR cleanup." + ); + } catch (error) { + if (isNotFoundError(error)) { + return; + } + + logger.warn( + { + error, + owner: params.owner, + repo: params.repo, + branch: params.branch, + reason: params.reason + }, + "Failed to delete branch during PR cleanup." + ); + } +} + function shouldFallbackToCommentReview(error: unknown, event: "APPROVE" | "REQUEST_CHANGES"): boolean { if (event !== "APPROVE") { return false;