Add lenient review check action

This commit is contained in:
lezi-fun committed 2026-05-12 21:32:04 +08:00
1 parent 63eba2db87
commit 5cca2e8564
6 files changed
+153 -21

No files matched your search

+4 -2
View File
@@ -1,9 +1,11 @@
import type { ReviewDecision, ReviewFinding } from "../types.js";
import type { ReviewDecision, ReviewFinding, ReviewMode } from "../types.js";
export function formatReviewBody(decision: ReviewDecision, unpostedFindings: ReviewFinding[]): string {
export function formatReviewBody(decision: ReviewDecision, unpostedFindings: ReviewFinding[], mode: ReviewMode): string {
const lines = [
`## Automated review`,
"",
`Mode: ${mode === "lenient" ? "lenient" : "strict"}`,
"",
decision.summary,
"",
`Decision: ${decision.safeToMerge ? "safe to merge" : "changes requested"}`
+29 -9
View File
@@ -1,7 +1,7 @@
import OpenAI from "openai";
import { z } from "zod";
import { config } from "../config.js";
import type { PullRequestFile, ReviewDecision } from "../types.js";
import type { PullRequestFile, ReviewDecision, ReviewMode } from "../types.js";
const reviewDecisionSchema = z.object({
safeToMerge: z.boolean(),
@@ -24,6 +24,7 @@ export class OpenAiReviewer {
title: string;
body: string | null;
files: PullRequestFile[];
mode: ReviewMode;
}): Promise<ReviewDecision> {
const response = await this.client.chat.completions.create({
model: config.openAiModel,
@@ -62,14 +63,7 @@ export class OpenAiReviewer {
messages: [
{
role: "system",
content: [
"You are a strict senior software engineer reviewing a GitHub pull request.",
"Find correctness bugs, security issues, data-loss risks, broken tests, bad error handling, and maintainability problems.",
"Only set safeToMerge=true when there are no blocking findings.",
"Use suggestion severity for non-blocking style or clarity improvements.",
"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."
].join(" ")
content: buildSystemPrompt(input.mode)
},
{
role: "user",
@@ -98,3 +92,29 @@ export class OpenAiReviewer {
return reviewDecisionSchema.parse(JSON.parse(raw));
}
}
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.",
"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."
];
if (mode === "lenient") {
return [
...commonRules,
"This is a lenient review requested by the pull request author.",
"Only report issues that are dangerous, affect runtime behavior, can cause errors, can crash, can break builds/tests, can lose data, or create clear security problems.",
"Do not block on style, naming, subjective maintainability, small refactors, missing comments, formatting, or non-dangerous best-practice preferences.",
"Use suggestion severity only when the issue is still runtime-relevant but not necessarily blocking."
].join(" ");
}
return [
...commonRules,
"This is a strict review.",
"Find correctness bugs, security issues, data-loss risks, broken tests, bad error handling, and maintainability problems.",
"Use suggestion severity for non-blocking style or clarity improvements."
].join(" ");
}
+92 -5
View File
@@ -3,14 +3,16 @@ import { config } from "../config.js";
import { requiredChecksAreGreen } from "../github/checks.js";
import { collectValidNewLines, toDiffPosition } from "../github/diff.js";
import { logger } from "../logger.js";
import type { PullRequestFile, PullRequestRef, ReviewDecision, ReviewFinding } from "../types.js";
import type { PullRequestFile, PullRequestRef, ReviewDecision, ReviewFinding, ReviewMode } from "../types.js";
import { formatReviewBody } from "./format.js";
import { OpenAiReviewer } from "./openaiReviewer.js";
import { compactFilesForReview } from "./prompt.js";
const reviewer = new OpenAiReviewer();
const LENIENT_ACTION_IDENTIFIER = "lenient_check";
const CHECK_RUN_NAME = "ghbot review";
export async function processPullRequest(octokit: Octokit, ref: PullRequestRef): Promise<void> {
export async function processPullRequest(octokit: Octokit, ref: PullRequestRef, mode: ReviewMode = "strict"): Promise<void> {
const { owner, repo, pullNumber } = ref;
const { data: pullRequest } = await octokit.rest.pulls.get({
@@ -34,7 +36,8 @@ export async function processPullRequest(octokit: Octokit, ref: PullRequestRef):
const decision = await reviewer.review({
title: pullRequest.title,
body: pullRequest.body,
files: compactFiles
files: compactFiles,
mode
});
await submitReview(octokit, {
@@ -43,7 +46,17 @@ export async function processPullRequest(octokit: Octokit, ref: PullRequestRef):
pullNumber,
commitId: pullRequest.head.sha,
files,
decision
decision,
mode
});
await upsertReviewCheckRun(octokit, {
owner,
repo,
headSha: pullRequest.head.sha,
pullNumber,
decision,
mode
});
if (!decision.safeToMerge) {
@@ -93,6 +106,37 @@ export async function processPullRequest(octokit: Octokit, ref: PullRequestRef):
});
}
export async function processLenientCheckRunAction(
octokit: Octokit,
params: {
owner: string;
repo: string;
headSha: string;
}
): Promise<void> {
const pulls = await octokit.rest.repos.listPullRequestsAssociatedWithCommit({
owner: params.owner,
repo: params.repo,
commit_sha: params.headSha
});
const pullRequest = pulls.data.find((pull) => pull.state === "open");
if (!pullRequest) {
logger.warn(params, "No open pull request found for lenient check action.");
return;
}
await processPullRequest(
octokit,
{
owner: params.owner,
repo: params.repo,
pullNumber: pullRequest.number
},
"lenient"
);
}
async function listPullRequestFiles(
octokit: Octokit,
owner: string,
@@ -124,6 +168,7 @@ async function submitReview(
commitId: string;
files: PullRequestFile[];
decision: ReviewDecision;
mode: ReviewMode;
}
): Promise<void> {
const validLines = collectValidNewLines(params.files);
@@ -163,11 +208,53 @@ async function submitReview(
pull_number: params.pullNumber,
commit_id: params.commitId,
event,
body: formatReviewBody(params.decision, unpostedFindings),
body: formatReviewBody(params.decision, unpostedFindings, params.mode),
comments
});
}
async function upsertReviewCheckRun(
octokit: Octokit,
params: {
owner: string;
repo: string;
headSha: string;
pullNumber: number;
decision: ReviewDecision;
mode: ReviewMode;
}
): Promise<void> {
const hasBlockingFinding = params.decision.findings.some((finding) => finding.severity === "blocking");
const conclusion = params.decision.safeToMerge && !hasBlockingFinding ? "success" : "action_required";
await octokit.rest.checks.create({
owner: params.owner,
repo: params.repo,
name: CHECK_RUN_NAME,
head_sha: params.headSha,
status: "completed",
conclusion,
output: {
title: params.mode === "lenient" ? "Lenient review completed" : "Strict review completed",
summary: params.decision.summary
},
actions:
params.mode === "strict" && conclusion === "action_required"
? [
{
label: "Lenient check",
description: "Run only critical runtime checks.",
identifier: LENIENT_ACTION_IDENTIFIER
}
]
: undefined
});
}
export function isLenientCheckAction(identifier: string): boolean {
return identifier === LENIENT_ACTION_IDENTIFIER;
}
async function waitForMergeable(
octokit: Octokit,
owner: string,