Require owner approval for lenient merges
This commit is contained in:
1 parent
5cca2e8564
commit
34191e4872
8 files changed
+211
-7
No files matched your search
@@ -8,6 +8,9 @@ export function formatReviewBody(decision: ReviewDecision, unpostedFindings: Rev
|
||||
"",
|
||||
decision.summary,
|
||||
"",
|
||||
...(decision.shouldClosePullRequest
|
||||
? [`Close PR: yes`, "", `Close reason: ${decision.closeReason}`, ""]
|
||||
: []),
|
||||
`Decision: ${decision.safeToMerge ? "safe to merge" : "changes requested"}`
|
||||
];
|
||||
|
||||
|
||||
@@ -5,6 +5,8 @@ import type { PullRequestFile, ReviewDecision, ReviewMode } from "../types.js";
|
||||
|
||||
const reviewDecisionSchema = z.object({
|
||||
safeToMerge: z.boolean(),
|
||||
shouldClosePullRequest: z.boolean(),
|
||||
closeReason: z.string(),
|
||||
summary: z.string(),
|
||||
findings: z.array(
|
||||
z.object({
|
||||
@@ -37,9 +39,11 @@ export class OpenAiReviewer {
|
||||
schema: {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
required: ["safeToMerge", "summary", "findings"],
|
||||
required: ["safeToMerge", "shouldClosePullRequest", "closeReason", "summary", "findings"],
|
||||
properties: {
|
||||
safeToMerge: { type: "boolean" },
|
||||
shouldClosePullRequest: { type: "boolean" },
|
||||
closeReason: { type: "string" },
|
||||
summary: { type: "string" },
|
||||
findings: {
|
||||
type: "array",
|
||||
@@ -97,6 +101,9 @@ function buildSystemPrompt(mode: ReviewMode): string {
|
||||
const commonRules = [
|
||||
"You are a senior software engineer reviewing a GitHub pull request.",
|
||||
"Only set safeToMerge=true when there are no blocking findings.",
|
||||
"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.",
|
||||
"For each finding, choose a line number that exists on an added line in the supplied patch whenever possible.",
|
||||
"Do not invent files, line numbers, test results, or runtime behavior."
|
||||
];
|
||||
|
||||
+162
-5
@@ -59,16 +59,116 @@ export async function processPullRequest(octokit: Octokit, ref: PullRequestRef,
|
||||
mode
|
||||
});
|
||||
|
||||
if (decision.shouldClosePullRequest) {
|
||||
await closeMaliciousPullRequest(octokit, {
|
||||
owner,
|
||||
repo,
|
||||
pullNumber,
|
||||
reason: decision.closeReason || "The automated review found clearly malicious code."
|
||||
});
|
||||
logger.warn({ owner, repo, pullNumber }, "Closed pull request because malicious code was detected.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!decision.safeToMerge) {
|
||||
logger.info({ owner, repo, pullNumber }, "Review requested changes; not merging.");
|
||||
return;
|
||||
}
|
||||
|
||||
await maybeMergePullRequest(octokit, {
|
||||
owner,
|
||||
repo,
|
||||
pullNumber,
|
||||
title: pullRequest.title,
|
||||
headSha: pullRequest.head.sha,
|
||||
mode
|
||||
});
|
||||
}
|
||||
|
||||
export async function processPullRequestReviewApproval(
|
||||
octokit: Octokit,
|
||||
params: {
|
||||
owner: string;
|
||||
repo: string;
|
||||
pullNumber: number;
|
||||
reviewerLogin: string;
|
||||
state: string;
|
||||
commitId: string;
|
||||
}
|
||||
): Promise<void> {
|
||||
if (params.state !== "approved" || params.reviewerLogin !== config.lenientApprovalUser) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { data: pullRequest } = await octokit.rest.pulls.get({
|
||||
owner: params.owner,
|
||||
repo: params.repo,
|
||||
pull_number: params.pullNumber
|
||||
});
|
||||
|
||||
if (pullRequest.head.sha !== params.commitId) {
|
||||
logger.info(
|
||||
{
|
||||
owner: params.owner,
|
||||
repo: params.repo,
|
||||
pullNumber: params.pullNumber,
|
||||
approvedCommit: params.commitId,
|
||||
currentHead: pullRequest.head.sha
|
||||
},
|
||||
"Ignoring approval because it does not match the current PR head."
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
await maybeMergePullRequest(octokit, {
|
||||
owner: params.owner,
|
||||
repo: params.repo,
|
||||
pullNumber: params.pullNumber,
|
||||
title: pullRequest.title,
|
||||
headSha: pullRequest.head.sha,
|
||||
mode: "lenient"
|
||||
});
|
||||
}
|
||||
|
||||
async function maybeMergePullRequest(
|
||||
octokit: Octokit,
|
||||
params: {
|
||||
owner: string;
|
||||
repo: string;
|
||||
pullNumber: number;
|
||||
title: string;
|
||||
headSha: string;
|
||||
mode: ReviewMode;
|
||||
}
|
||||
): Promise<void> {
|
||||
const { owner, repo, pullNumber } = params;
|
||||
|
||||
if (!config.autoMerge) {
|
||||
logger.info({ owner, repo, pullNumber }, "AUTO_MERGE is disabled; approved only.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (params.mode === "lenient") {
|
||||
const approvedByOwner = await hasCurrentHeadApprovalFrom(octokit, {
|
||||
owner,
|
||||
repo,
|
||||
pullNumber,
|
||||
headSha: params.headSha,
|
||||
reviewerLogin: config.lenientApprovalUser
|
||||
});
|
||||
|
||||
if (!approvedByOwner) {
|
||||
await octokit.rest.issues.createComment({
|
||||
owner,
|
||||
repo,
|
||||
issue_number: pullNumber,
|
||||
body: `Lenient check passed, but this PR will not be merged until @${config.lenientApprovalUser} approves the current head commit.`
|
||||
});
|
||||
logger.info({ owner, repo, pullNumber, reviewer: config.lenientApprovalUser }, "Waiting for lenient approval before merging.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const mergeablePullRequest = await waitForMergeable(octokit, owner, repo, pullNumber);
|
||||
if (mergeablePullRequest.mergeable !== true || mergeablePullRequest.mergeable_state === "dirty") {
|
||||
await octokit.rest.issues.createComment({
|
||||
@@ -84,7 +184,7 @@ export async function processPullRequest(octokit: Octokit, ref: PullRequestRef,
|
||||
const checks = await requiredChecksAreGreen(octokit, {
|
||||
owner,
|
||||
repo,
|
||||
ref: pullRequest.head.sha
|
||||
ref: params.headSha
|
||||
});
|
||||
if (!checks.ok) {
|
||||
await octokit.rest.issues.createComment({
|
||||
@@ -102,7 +202,33 @@ export async function processPullRequest(octokit: Octokit, ref: PullRequestRef,
|
||||
repo,
|
||||
pull_number: pullNumber,
|
||||
merge_method: config.mergeMethod,
|
||||
commit_title: `${pullRequest.title} (#${pullNumber})`
|
||||
commit_title: `${params.title} (#${pullNumber})`
|
||||
});
|
||||
}
|
||||
|
||||
async function hasCurrentHeadApprovalFrom(
|
||||
octokit: Octokit,
|
||||
params: {
|
||||
owner: string;
|
||||
repo: string;
|
||||
pullNumber: number;
|
||||
headSha: string;
|
||||
reviewerLogin: string;
|
||||
}
|
||||
): Promise<boolean> {
|
||||
const reviews = await octokit.paginate(octokit.rest.pulls.listReviews, {
|
||||
owner: params.owner,
|
||||
repo: params.repo,
|
||||
pull_number: params.pullNumber,
|
||||
per_page: 100
|
||||
});
|
||||
|
||||
return reviews.some((review) => {
|
||||
return (
|
||||
review.user?.login === params.reviewerLogin &&
|
||||
review.state === "APPROVED" &&
|
||||
review.commit_id === params.headSha
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -225,7 +351,8 @@ async function upsertReviewCheckRun(
|
||||
}
|
||||
): Promise<void> {
|
||||
const hasBlockingFinding = params.decision.findings.some((finding) => finding.severity === "blocking");
|
||||
const conclusion = params.decision.safeToMerge && !hasBlockingFinding ? "success" : "action_required";
|
||||
const conclusion =
|
||||
params.decision.shouldClosePullRequest || hasBlockingFinding || !params.decision.safeToMerge ? "action_required" : "success";
|
||||
|
||||
await octokit.rest.checks.create({
|
||||
owner: params.owner,
|
||||
@@ -235,8 +362,14 @@ async function upsertReviewCheckRun(
|
||||
status: "completed",
|
||||
conclusion,
|
||||
output: {
|
||||
title: params.mode === "lenient" ? "Lenient review completed" : "Strict review completed",
|
||||
summary: params.decision.summary
|
||||
title: params.decision.shouldClosePullRequest
|
||||
? "Malicious code detected"
|
||||
: params.mode === "lenient"
|
||||
? "Lenient review completed"
|
||||
: "Strict review completed",
|
||||
summary: params.decision.shouldClosePullRequest
|
||||
? `${params.decision.summary}\n\nClose reason: ${params.decision.closeReason}`
|
||||
: params.decision.summary
|
||||
},
|
||||
actions:
|
||||
params.mode === "strict" && conclusion === "action_required"
|
||||
@@ -255,6 +388,30 @@ export function isLenientCheckAction(identifier: string): boolean {
|
||||
return identifier === LENIENT_ACTION_IDENTIFIER;
|
||||
}
|
||||
|
||||
async function closeMaliciousPullRequest(
|
||||
octokit: Octokit,
|
||||
params: {
|
||||
owner: string;
|
||||
repo: string;
|
||||
pullNumber: number;
|
||||
reason: string;
|
||||
}
|
||||
): Promise<void> {
|
||||
await octokit.rest.issues.createComment({
|
||||
owner: params.owner,
|
||||
repo: params.repo,
|
||||
issue_number: params.pullNumber,
|
||||
body: `This PR was automatically closed because the review detected clearly malicious code.\n\nReason: ${params.reason}`
|
||||
});
|
||||
|
||||
await octokit.rest.pulls.update({
|
||||
owner: params.owner,
|
||||
repo: params.repo,
|
||||
pull_number: params.pullNumber,
|
||||
state: "closed"
|
||||
});
|
||||
}
|
||||
|
||||
async function waitForMergeable(
|
||||
octokit: Octokit,
|
||||
owner: string,
|
||||
|
||||
Reference in new issue
Block a user