Skip configured cleanup branches

This commit is contained in:
lezi-fun committed 2026-05-19 17:20:44 +08:00
1 parent af332a5d1c
commit c400b78989
7 files changed
+113

No files matched your search

+13
View File
@@ -5,6 +5,17 @@ const optionalString = z.preprocess((value) => {
return value === "" ? undefined : value;
}, z.string().optional());
const csvList = z.preprocess((value) => {
if (typeof value !== "string") {
return [];
}
return value
.split(",")
.map((part) => part.trim())
.filter(Boolean);
}, z.array(z.string()));
const envBoolean = z.preprocess((value) => {
if (typeof value !== "string") {
return value;
@@ -49,6 +60,7 @@ const configSchema = z.object({
mergeMethod: z.enum(["merge", "squash", "rebase"]).default("squash"),
requireChecks: envBoolean.default(true),
maxPatchChars: z.coerce.number().int().positive().default(120_000),
branchCleanupSkipBranches: csvList,
logLevel: z.string().min(1).default("info")
});
@@ -67,5 +79,6 @@ export const config = configSchema.parse({
mergeMethod: process.env.MERGE_METHOD,
requireChecks: process.env.REQUIRE_CHECKS,
maxPatchChars: process.env.MAX_PATCH_CHARS,
branchCleanupSkipBranches: process.env.BRANCH_CLEANUP_SKIP_BRANCHES,
logLevel: process.env.LOG_LEVEL
});
+2
View File
@@ -339,6 +339,8 @@ function buildSystemPrompt(mode: ReviewMode): string {
"When you identify a bug, also check nearby code paths and obvious related regressions, but stay close to what the diff actually makes plausible.",
"Prefer false negatives over false positives. Do not report a finding unless you can explain a concrete failure mode, misuse case, or broken behavior from the diff itself.",
"Do not report hypothetical, speculative, low-probability, style-only, architecture-preference, or vague maintainability concerns.",
"When reviewing branch cleanup logic, judge the ordinary feature-branch case first. Do not block the change by bringing in extreme repository-specific examples such as shared long-lived branches or unusual protected branch workflows unless the diff is explicitly about those cases.",
"If a concern is really about a repository-specific exception, prefer a configurable skip list or existing branch protection instead of rejecting the feature for the common case.",
"Set shouldClosePullRequest=true only for clearly malicious code: backdoors, credential theft, token exfiltration, destructive commands, malware, hidden persistence, privilege escalation, supply-chain compromise, or intentionally abusive behavior.",
"Do not set shouldClosePullRequest=true for ordinary bugs, crashes, failing tests, incomplete code, suspicious-but-unproven code, or low-quality changes.",
"When shouldClosePullRequest=true, explain the evidence in closeReason. Otherwise closeReason must be an empty string.",
+84
View File
@@ -161,6 +161,11 @@ export async function processScheduledBranchCleanup(
repo: string;
}
): Promise<void> {
const { data: repository } = await octokit.rest.repos.get({
owner: params.owner,
repo: params.repo
});
const pullRequests = await octokit.paginate(octokit.rest.pulls.list, {
owner: params.owner,
repo: params.repo,
@@ -175,6 +180,20 @@ export async function processScheduledBranchCleanup(
continue;
}
if (shouldSkipBranchCleanup(pullRequest.head.ref, repository.default_branch)) {
logger.info(
{
owner: params.owner,
repo: params.repo,
branch: pullRequest.head.ref,
pullNumber: pullRequest.number,
defaultBranch: repository.default_branch
},
"Skipping branch cleanup because the branch is configured or considered a common shared branch."
);
continue;
}
const hasOtherOpenPullRequestWithSameRef = pullRequests.some((candidate) => {
return (
candidate.number !== pullRequest.number &&
@@ -197,6 +216,25 @@ export async function processScheduledBranchCleanup(
continue;
}
const branchProtected = await isProtectedBranch(octokit, {
owner: params.owner,
repo: params.repo,
branch: pullRequest.head.ref
});
if (branchProtected) {
logger.info(
{
owner: params.owner,
repo: params.repo,
branch: pullRequest.head.ref,
pullNumber: pullRequest.number
},
"Skipping branch cleanup because the branch is protected."
);
continue;
}
if (pullRequest.merged_at) {
await deleteBranchIfPresent(octokit, {
owner: params.owner,
@@ -935,6 +973,52 @@ async function deleteBranchIfPresent(
}
}
function shouldSkipBranchCleanup(branch: string, defaultBranch: string): boolean {
if (branch === defaultBranch) {
return true;
}
return config.branchCleanupSkipBranches.some((pattern) => branchMatchesPattern(branch, pattern));
}
function branchMatchesPattern(branch: string, pattern: string): boolean {
if (!pattern) {
return false;
}
const escaped = pattern
.replace(/[.+^${}()|[\]\\]/g, "\\$&")
.replace(/\*/g, ".*")
.replace(/\?/g, ".");
return new RegExp(`^${escaped}$`).test(branch);
}
async function isProtectedBranch(
octokit: Octokit,
params: {
owner: string;
repo: string;
branch: string;
}
): Promise<boolean> {
try {
const { data } = await octokit.rest.repos.getBranch({
owner: params.owner,
repo: params.repo,
branch: params.branch
});
return Boolean(data.protected);
} catch (error) {
if (isNotFoundError(error)) {
return false;
}
throw error;
}
}
function shouldFallbackToCommentReview(error: unknown, event: "APPROVE" | "REQUEST_CHANGES"): boolean {
if (event !== "APPROVE") {
return false;