also looking at this
fix(review): give pull request reviews the analyzers and learnings they skipped
#1415
4 files
+316
-106
| @@ -1,3 +1,4 @@ | ||
| 1 | import type { AiClient } from '../ai/types' | |
| 1 | 2 | import type { GitProvider } from '../git/provider' |
| 2 | 3 | import { assertSupports } from '../git/provider' |
| 3 | 4 | import type { BuddyConfig } from '../types' |
| @@ -8,6 +9,7 @@ import { getDefaultLogger } from '../utils/logger' | ||
| 8 | 9 | import { parseUnifiedDiff } from './diff' |
| 9 | 10 | import { reviewDiff } from './engine' |
| 10 | 11 | import { composeInstructions, loadGuidelines } from './guidelines' |
| 12 | import type { ReviewProfile } from './engine' | |
| 11 | 13 | import type { ReviewTrigger } from './filters' |
| 12 | 14 | import { reviewSkipReason } from './filters' |
| 13 | 15 | import { needsReview, parseReviewState, upsertReviewState } from './marker' |
| @@ -25,6 +27,18 @@ export interface RunReviewOptions { | ||
| 25 | 27 | summaryOnly?: boolean |
| 26 | 28 | /** Skip when the head commit was already reviewed */ |
| 27 | 29 | skipIfReviewed?: boolean |
| 30 | /** Override the configured review profile */ | |
| 31 | profile?: ReviewProfile | |
| 32 | /** Report what would be posted without posting it */ | |
| 33 | dryRun?: boolean | |
| 34 | /** | |
| 35 | * Client to review with; omitted means build one from config. | |
| 36 | * | |
| 37 | * Injectable for the same reason `attemptFix` takes one: an orchestration | |
| 38 | * that constructs its own client cannot be tested without a key, and this | |
| 39 | * one drifted from its second caller for exactly that long. | |
| 40 | */ | |
| 41 | ai?: AiClient | null | |
| 28 | 42 | /** |
| 29 | 43 | * Whether Buddy chose to review or was asked to (default: `requested`). |
| 30 | 44 | * |
| @@ -80,7 +94,7 @@ export async function runReviewForPR(options: RunReviewOptions): Promise<string> | ||
| 80 | 94 | return 'Already reviewed at this commit.' |
| 81 | 95 | } |
| 82 | 96 | |
| 83 | const ai = createAiClient(config, logger) | |
| 97 | const ai = options.ai === undefined ? createAiClient(config, logger) : options.ai | |
| 84 | 98 | |
| 85 | 99 | // Analyzers run whether or not AI is configured, so a repository without a |
| 86 | 100 | // key still gets secret scanning and workflow auditing on its pull requests. |
| @@ -110,6 +124,11 @@ export async function runReviewForPR(options: RunReviewOptions): Promise<string> | ||
| 110 | 124 | { headSha, requestChangesOn: config.ai?.review?.requestChangesOn }, |
| 111 | 125 | ) |
| 112 | 126 | |
| 127 | if (options.dryRun) { | |
| 128 | reportDryRun(prepared, logger) | |
| 129 | return `Would post ${analysis.findings.length} static-analysis finding(s).` | |
| 130 | } | |
| 131 | ||
| 113 | 132 | assertSupports(provider, 'inlineReviewComments', 'createReview', 'posting a review') |
| 114 | 133 | await provider.createReview(prNumber, prepared) |
| 115 | 134 | await persistReviewState(provider, prNumber, pr.body, prepared.state, state?.paused, logger) |
| @@ -129,7 +148,7 @@ export async function runReviewForPR(options: RunReviewOptions): Promise<string> | ||
| 129 | 148 | |
| 130 | 149 | const result = await reviewDiff(ai, { |
| 131 | 150 | diff, |
| 132 | profile: config.ai?.review?.profile, | |
| 151 | profile: options.profile ?? config.ai?.review?.profile, | |
| 133 | 152 | summaryOnly: options.summaryOnly ?? config.ai?.review?.summaryOnly, |
| 134 | 153 | instructions: composeInstructions({ |
| 135 | 154 | global: config.ai?.review?.instructions, |
| @@ -151,6 +170,11 @@ export async function runReviewForPR(options: RunReviewOptions): Promise<string> | ||
| 151 | 170 | seenFingerprints: options.full ? [] : state?.fingerprints ?? [], |
| 152 | 171 | }) |
| 153 | 172 | |
| 173 | if (options.dryRun) { | |
| 174 | reportDryRun(prepared, logger) | |
| 175 | return `Would post ${result.findings.length} finding(s).` | |
| 176 | } | |
| 177 | ||
| 154 | 178 | assertSupports(provider, 'inlineReviewComments', 'createReview', 'posting a review') |
| 155 | 179 | await provider.createReview(prNumber, prepared) |
| 156 | 180 | await persistReviewState(provider, prNumber, pr.body, prepared.state, state?.paused, logger) |
| @@ -160,6 +184,19 @@ export async function runReviewForPR(options: RunReviewOptions): Promise<string> | ||
| 160 | 184 | : `Reviewed — ${result.findings.length} finding(s) posted.` |
| 161 | 185 | } |
| 162 | 186 | |
| 187 | /** | |
| 188 | * Print a review instead of posting it. | |
| 189 | * | |
| 190 | * @param prepared - The review that would have been posted | |
| 191 | * @param logger - Where to print | |
| 192 | */ | |
| 193 | function reportDryRun(prepared: PreparedReview, logger: Logger): void { | |
| 194 | logger.info(`\n${prepared.body}\n`) | |
| 195 | ||
| 196 | for (const comment of prepared.comments) | |
| 197 | logger.info(`${comment.path}:${comment.line}\n${comment.body}\n`) | |
| 198 | } | |
| 199 | ||
| 163 | 200 | /** |
| 164 | 201 | * Write the state a review established back onto the pull request. |
| 165 | 202 | * |