Retry failed review requests
This commit is contained in:
1 parent
50d0ae5be9
commit
fd9479590a
5 files changed
+84
-11
No files matched your search
@@ -18,6 +18,7 @@ GitHub Actions bot that reviews pull requests with an AI reviewer, leaves inline
|
||||
- If the reviewer detects clearly malicious code, the bot comments with the reason and closes the pull request.
|
||||
- Posts inline comments only on valid added diff lines.
|
||||
- Approves clean pull requests.
|
||||
- Retries transient OpenAI and GitHub API request failures up to 5 times before giving up.
|
||||
- If `AUTO_MERGE=true`, merges only after:
|
||||
- the AI reviewer says the PR is safe,
|
||||
- GitHub reports the PR as mergeable,
|
||||
|
||||
@@ -2,6 +2,7 @@ import fs from "node:fs";
|
||||
import { config } from "../config.js";
|
||||
import { createGitHubClient } from "../github/client.js";
|
||||
import { logger } from "../logger.js";
|
||||
import { withRetry } from "../retry.js";
|
||||
import {
|
||||
processLenientCheckComment,
|
||||
processPullRequest,
|
||||
@@ -73,12 +74,14 @@ async function main(): Promise<void> {
|
||||
if (eventName === "pull_request_target") {
|
||||
const prPayload = payload as PullRequestPayload;
|
||||
if (prPayload.action === "opened") {
|
||||
await octokit.rest.issues.createComment({
|
||||
await withRetry("github.issues.createComment.started", async () => {
|
||||
return octokit.rest.issues.createComment({
|
||||
owner: prPayload.repository.owner.login,
|
||||
repo: prPayload.repository.name,
|
||||
issue_number: prPayload.pull_request.number,
|
||||
body: "Automated review has started. I am checking this pull request now."
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
await processPullRequest(
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import { logger } from "./logger.js";
|
||||
|
||||
const MAX_ATTEMPTS = 5;
|
||||
const BASE_DELAY_MS = 1_000;
|
||||
|
||||
export async function withRetry<T>(
|
||||
label: string,
|
||||
operation: () => Promise<T>,
|
||||
options?: {
|
||||
maxAttempts?: number;
|
||||
baseDelayMs?: number;
|
||||
}
|
||||
): Promise<T> {
|
||||
const maxAttempts = options?.maxAttempts ?? MAX_ATTEMPTS;
|
||||
const baseDelayMs = options?.baseDelayMs ?? BASE_DELAY_MS;
|
||||
let lastError: unknown;
|
||||
|
||||
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
|
||||
try {
|
||||
return await operation();
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
|
||||
logger.warn(
|
||||
{
|
||||
error,
|
||||
label,
|
||||
attempt,
|
||||
maxAttempts
|
||||
},
|
||||
"Request failed."
|
||||
);
|
||||
|
||||
if (attempt === maxAttempts) {
|
||||
break;
|
||||
}
|
||||
|
||||
await delay(baseDelayMs * attempt);
|
||||
}
|
||||
}
|
||||
|
||||
throw lastError;
|
||||
}
|
||||
|
||||
function delay(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { OpenAI } from "openai";
|
||||
import { z } from "zod";
|
||||
import { config } from "../config.js";
|
||||
import { withRetry } from "../retry.js";
|
||||
import type { PullRequestFile, ReviewDecision, ReviewMode } from "../types.js";
|
||||
|
||||
const reviewDecisionSchema = z.object({
|
||||
@@ -31,7 +32,8 @@ export class OpenAiReviewer {
|
||||
files: PullRequestFile[];
|
||||
mode: ReviewMode;
|
||||
}): Promise<ReviewDecision> {
|
||||
const response = await this.client.responses.create({
|
||||
const response = await withRetry("openai.responses.create", async () => {
|
||||
return this.client.responses.create({
|
||||
model: config.openAiModel,
|
||||
temperature: 0.1,
|
||||
...(config.openAiReasoningEffort ? { reasoning: { effort: config.openAiReasoningEffort } } : {}),
|
||||
@@ -88,6 +90,7 @@ export class OpenAiReviewer {
|
||||
}
|
||||
]
|
||||
});
|
||||
});
|
||||
|
||||
const raw = response.output_text;
|
||||
if (!raw) {
|
||||
|
||||
+28
-9
@@ -3,6 +3,7 @@ import { config } from "../config.js";
|
||||
import { requiredChecksAreGreen } from "../github/checks.js";
|
||||
import { collectValidNewLines, toDiffPosition } from "../github/diff.js";
|
||||
import { logger } from "../logger.js";
|
||||
import { withRetry } from "../retry.js";
|
||||
import type { PullRequestFile, PullRequestRef, ReviewDecision, ReviewFinding, ReviewMode } from "../types.js";
|
||||
import { formatReviewBody } from "./format.js";
|
||||
import { OpenAiReviewer } from "./openaiReviewer.js";
|
||||
@@ -188,12 +189,14 @@ export async function processLenientCheckComment(
|
||||
"Processing lenient check comment command."
|
||||
);
|
||||
|
||||
await octokit.rest.issues.createComment({
|
||||
await withRetry("github.issues.createComment.lenientRequested", async () => {
|
||||
return octokit.rest.issues.createComment({
|
||||
owner: params.owner,
|
||||
repo: params.repo,
|
||||
issue_number: params.pullNumber,
|
||||
body: `Lenient check requested by @${params.commenterLogin}. Re-running the review with runtime and security focus only.`
|
||||
});
|
||||
});
|
||||
|
||||
await processPullRequest(
|
||||
octokit,
|
||||
@@ -234,12 +237,14 @@ async function maybeMergePullRequest(
|
||||
});
|
||||
|
||||
if (!approvedByOwner) {
|
||||
await octokit.rest.issues.createComment({
|
||||
await withRetry("github.issues.createComment.awaitLenientApproval", async () => {
|
||||
return 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;
|
||||
}
|
||||
@@ -247,12 +252,14 @@ async function maybeMergePullRequest(
|
||||
|
||||
const mergeablePullRequest = await waitForMergeable(octokit, owner, repo, pullNumber);
|
||||
if (mergeablePullRequest.mergeable !== true || mergeablePullRequest.mergeable_state === "dirty") {
|
||||
await octokit.rest.issues.createComment({
|
||||
await withRetry("github.issues.createComment.notMergeable", async () => {
|
||||
return octokit.rest.issues.createComment({
|
||||
owner,
|
||||
repo,
|
||||
issue_number: pullNumber,
|
||||
body: `Automated review approved this PR, but it was not merged because GitHub reports mergeable=${mergeablePullRequest.mergeable} and mergeable_state=${mergeablePullRequest.mergeable_state}.`
|
||||
});
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -263,23 +270,27 @@ async function maybeMergePullRequest(
|
||||
ref: params.headSha
|
||||
});
|
||||
if (!checks.ok) {
|
||||
await octokit.rest.issues.createComment({
|
||||
await withRetry("github.issues.createComment.requiredChecks", async () => {
|
||||
return octokit.rest.issues.createComment({
|
||||
owner,
|
||||
repo,
|
||||
issue_number: pullNumber,
|
||||
body: `Automated review approved this PR, but it was not merged because required checks are not green. ${checks.reason ?? ""}`.trim()
|
||||
});
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
await octokit.rest.pulls.merge({
|
||||
await withRetry("github.pulls.merge", async () => {
|
||||
return octokit.rest.pulls.merge({
|
||||
owner,
|
||||
repo,
|
||||
pull_number: pullNumber,
|
||||
merge_method: config.mergeMethod,
|
||||
commit_title: `${params.title} (#${pullNumber})`
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function hasCurrentHeadApprovalFrom(
|
||||
@@ -380,7 +391,8 @@ async function submitReview(
|
||||
const event: "APPROVE" | "REQUEST_CHANGES" =
|
||||
params.decision.safeToMerge && !hasBlockingFinding ? "APPROVE" : "REQUEST_CHANGES";
|
||||
|
||||
await octokit.rest.pulls.createReview({
|
||||
await withRetry("github.pulls.createReview", async () => {
|
||||
return octokit.rest.pulls.createReview({
|
||||
owner: params.owner,
|
||||
repo: params.repo,
|
||||
pull_number: params.pullNumber,
|
||||
@@ -389,6 +401,7 @@ async function submitReview(
|
||||
body: formatReviewBody(params.decision, unpostedFindings, params.mode),
|
||||
comments
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function upsertReviewCheckRun(
|
||||
@@ -406,7 +419,8 @@ async function upsertReviewCheckRun(
|
||||
const conclusion =
|
||||
params.decision.shouldClosePullRequest || hasBlockingFinding || !params.decision.safeToMerge ? "action_required" : "success";
|
||||
|
||||
await octokit.rest.checks.create({
|
||||
await withRetry("github.checks.create", async () => {
|
||||
return octokit.rest.checks.create({
|
||||
owner: params.owner,
|
||||
repo: params.repo,
|
||||
name: CHECK_RUN_NAME,
|
||||
@@ -425,6 +439,7 @@ async function upsertReviewCheckRun(
|
||||
},
|
||||
details_url: `https://github.com/${params.owner}/${params.repo}/pull/${params.pullNumber}`
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function isLenientCheckComment(body: string): boolean {
|
||||
@@ -440,19 +455,23 @@ async function closeMaliciousPullRequest(
|
||||
reason: string;
|
||||
}
|
||||
): Promise<void> {
|
||||
await octokit.rest.issues.createComment({
|
||||
await withRetry("github.issues.createComment.closeMalicious", async () => {
|
||||
return 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({
|
||||
await withRetry("github.pulls.update.closeMalicious", async () => {
|
||||
return octokit.rest.pulls.update({
|
||||
owner: params.owner,
|
||||
repo: params.repo,
|
||||
pull_number: params.pullNumber,
|
||||
state: "closed"
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function waitForMergeable(
|
||||
|
||||
Reference in new issue
Block a user