diff --git a/src/gitea/comments-api.ts b/src/gitea/comments-api.ts index 437ff6d..ea31573 100644 --- a/src/gitea/comments-api.ts +++ b/src/gitea/comments-api.ts @@ -1,4 +1,5 @@ import { GiteaClient } from "./client.js"; +import { runBestEffort } from "./run-best-effort.js"; export const PROGRESS_COMMENT_MARKER = ""; @@ -26,8 +27,14 @@ export async function deleteProgressComment(input: { owner: string; repo: string; commentId: number; + correlationId?: string; }): Promise { - await input.gitea.deleteIssueComment(input.owner, input.repo, input.commentId); + await runBestEffort({ + operation: "Delete progress comment", + correlationId: input.correlationId, + context: { comment_id: input.commentId }, + fn: () => input.gitea.deleteIssueComment(input.owner, input.repo, input.commentId) + }); } export const TIMEOUT_FAILURE_MARKER = ""; diff --git a/src/gitea/review-api.ts b/src/gitea/review-api.ts index 81319fa..977cf43 100644 --- a/src/gitea/review-api.ts +++ b/src/gitea/review-api.ts @@ -1,8 +1,7 @@ -import { log } from "../logging/logger.js"; import { GiteaClient, PullFile } from "./client.js"; import { ReviewResult } from "../cursor/review-schema.js"; +import { runBestEffort } from "./run-best-effort.js"; -/** Best-effort cleanup; never throws (Gitea may return 500 for some review states). */ export async function deletePriorBotReviews(input: { gitea: GiteaClient; owner: string; @@ -11,29 +10,25 @@ export async function deletePriorBotReviews(input: { botLogin: string; correlationId?: string; }): Promise { - try { - const reviews = await input.gitea.getReviews(input.owner, input.repo, input.prNumber); - const botReviews = reviews.filter((review) => review.user?.login === input.botLogin); + await runBestEffort({ + operation: "List prior bot reviews", + correlationId: input.correlationId, + context: { pr_number: input.prNumber }, + fn: async () => { + const reviews = await input.gitea.getReviews(input.owner, input.repo, input.prNumber); + const botReviews = reviews.filter((review) => review.user?.login === input.botLogin); - for (const review of botReviews) { - try { - await input.gitea.deleteReview(input.owner, input.repo, input.prNumber, review.id); - } catch (error) { - log("warn", "Failed to delete prior bot review (continuing)", { - correlation_id: input.correlationId, - review_id: review.id, - pr_number: input.prNumber, - error: error instanceof Error ? error.message : String(error) + for (const review of botReviews) { + await runBestEffort({ + operation: "Delete prior bot review", + correlationId: input.correlationId, + context: { review_id: review.id, pr_number: input.prNumber }, + fn: () => + input.gitea.deleteReview(input.owner, input.repo, input.prNumber, review.id) }); } } - } catch (error) { - log("warn", "Failed to list prior bot reviews for cleanup (continuing)", { - correlation_id: input.correlationId, - pr_number: input.prNumber, - error: error instanceof Error ? error.message : String(error) - }); - } + }); } export async function postReview(input: { diff --git a/src/gitea/run-best-effort.ts b/src/gitea/run-best-effort.ts new file mode 100644 index 0000000..71b61b1 --- /dev/null +++ b/src/gitea/run-best-effort.ts @@ -0,0 +1,23 @@ +import { log } from "../logging/logger.js"; + +/** Run a cleanup step; log failures and continue the review flow. */ +export async function runBestEffort(input: { + operation: string; + correlationId?: string; + context?: Record; + fn: () => Promise; +}): Promise { + try { + await input.fn(); + log("info", `${input.operation} succeeded`, { + correlation_id: input.correlationId, + ...input.context + }); + } catch (error) { + log("warn", `${input.operation} failed (continuing)`, { + correlation_id: input.correlationId, + ...input.context, + error: error instanceof Error ? error.message : String(error) + }); + } +} diff --git a/src/run/review-runner.ts b/src/run/review-runner.ts index bc8ac72..e10f0b1 100644 --- a/src/run/review-runner.ts +++ b/src/run/review-runner.ts @@ -234,21 +234,13 @@ async function executeReview(params: { throw error; } finally { if (progressCommentId !== undefined) { - try { - await deleteProgressComment({ gitea, owner, repo, commentId: progressCommentId }); - log("info", "Removed progress comment from PR", { - correlation_id: input.correlationId, - pr_number: prNumber, - comment_id: progressCommentId - }); - } catch (error) { - log("warn", "Failed to remove progress comment from PR", { - correlation_id: input.correlationId, - pr_number: prNumber, - comment_id: progressCommentId, - error: error instanceof Error ? error.message : String(error) - }); - } + await deleteProgressComment({ + gitea, + owner, + repo, + commentId: progressCommentId, + correlationId: input.correlationId + }); } } }