Add scheduled branch cleanup
This commit is contained in:
1 parent
27db22cd78
commit
8b0aeac3e1
3 files changed
+112
No files matched your search
@@ -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`.
|
- 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.
|
- 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.
|
- 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.
|
- 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.
|
- 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:
|
- Eligible reviewers for lenient merge approval are:
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
processLenientCheckComment,
|
processLenientCheckComment,
|
||||||
processPullRequest,
|
processPullRequest,
|
||||||
processScheduledLenientMerges,
|
processScheduledLenientMerges,
|
||||||
|
processScheduledBranchCleanup,
|
||||||
processPullRequestReviewApproval
|
processPullRequestReviewApproval
|
||||||
} from "../review/processor.js";
|
} from "../review/processor.js";
|
||||||
|
|
||||||
@@ -144,6 +145,21 @@ async function main(): Promise<void> {
|
|||||||
|
|
||||||
if (eventName === "schedule") {
|
if (eventName === "schedule") {
|
||||||
const scheduledPayload = payload as ScheduledPayload;
|
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, {
|
await processScheduledLenientMerges(octokit, {
|
||||||
owner: scheduledPayload.repository.owner.login,
|
owner: scheduledPayload.repository.owner.login,
|
||||||
repo: scheduledPayload.repository.name
|
repo: scheduledPayload.repository.name
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ const reviewer = new CodexCliReviewer();
|
|||||||
const CHECK_RUN_NAME = "ghbot review";
|
const CHECK_RUN_NAME = "ghbot review";
|
||||||
export const LENIENT_COMMENT_COMMAND = "/lenient-check";
|
export const LENIENT_COMMENT_COMMAND = "/lenient-check";
|
||||||
const ADMIN_RESPONSE_WINDOW_MS = 24 * 60 * 60 * 1000;
|
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<void> {
|
export async function processPullRequest(octokit: Octokit, ref: PullRequestRef, mode: ReviewMode = "strict"): Promise<void> {
|
||||||
const { owner, repo, pullNumber } = ref;
|
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<void> {
|
||||||
|
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(
|
export async function processPullRequestReviewApproval(
|
||||||
octokit: Octokit,
|
octokit: Octokit,
|
||||||
params: {
|
params: {
|
||||||
@@ -818,6 +868,51 @@ async function dismissExistingBotReviews(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function deleteBranchIfPresent(
|
||||||
|
octokit: Octokit,
|
||||||
|
params: {
|
||||||
|
owner: string;
|
||||||
|
repo: string;
|
||||||
|
branch: string;
|
||||||
|
reason: string;
|
||||||
|
}
|
||||||
|
): Promise<void> {
|
||||||
|
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 {
|
function shouldFallbackToCommentReview(error: unknown, event: "APPROVE" | "REQUEST_CHANGES"): boolean {
|
||||||
if (event !== "APPROVE") {
|
if (event !== "APPROVE") {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in new issue
Block a user