-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add CodeRabbit review extraction command #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Commands; | ||
|
|
||
| use App\Services\CodeRabbitService; | ||
| use App\Services\QdrantService; | ||
| use Illuminate\Support\Str; | ||
| use LaravelZero\Framework\Commands\Command; | ||
|
|
||
| use function Laravel\Prompts\error; | ||
| use function Laravel\Prompts\info; | ||
| use function Laravel\Prompts\spin; | ||
| use function Laravel\Prompts\table; | ||
| use function Laravel\Prompts\warning; | ||
|
|
||
| class CoderabbitExtractCommand extends Command | ||
| { | ||
| protected $signature = 'coderabbit:extract | ||
| {--pr= : PR number to extract CodeRabbit reviews from} | ||
| {--min-severity=low : Minimum severity to include (critical, high, medium, low)} | ||
| {--dry-run : Show findings without adding to knowledge base}'; | ||
|
|
||
| protected $description = 'Extract CodeRabbit review findings from a PR and add to knowledge base'; | ||
|
|
||
| private const VALID_SEVERITIES = ['critical', 'high', 'medium', 'low']; | ||
|
|
||
| public function handle(CodeRabbitService $coderabbit, QdrantService $qdrant): int | ||
| { | ||
| /** @var string|null $prOption */ | ||
| $prOption = is_string($this->option('pr')) ? $this->option('pr') : null; | ||
| /** @var string $minSeverity */ | ||
| $minSeverity = is_string($this->option('min-severity')) ? $this->option('min-severity') : 'low'; | ||
| /** @var bool $dryRun */ | ||
| $dryRun = (bool) $this->option('dry-run'); | ||
|
|
||
| if ($prOption === null || $prOption === '') { | ||
| error('The --pr option is required.'); | ||
|
|
||
| return self::FAILURE; | ||
| } | ||
|
|
||
| if (! is_numeric($prOption) || (int) $prOption < 1) { | ||
| error('The --pr option must be a positive integer.'); | ||
|
|
||
| return self::FAILURE; | ||
| } | ||
|
|
||
| $prNumber = (int) $prOption; | ||
|
|
||
| if (! in_array($minSeverity, self::VALID_SEVERITIES, true)) { | ||
| error('Invalid --min-severity. Valid: '.implode(', ', self::VALID_SEVERITIES)); | ||
|
|
||
| return self::FAILURE; | ||
| } | ||
|
|
||
| // Fetch CodeRabbit review comments | ||
| /** @var array{comments: list<array{body: string, path: string|null, author: string}>, pr_title: string, pr_url: string}|null $reviewData */ | ||
| $reviewData = spin( | ||
| fn () => $coderabbit->fetchReviewComments($prNumber), | ||
| "Fetching CodeRabbit reviews for PR #{$prNumber}..." | ||
| ); | ||
|
|
||
| if ($reviewData === null) { | ||
| error("Failed to fetch PR #{$prNumber}. Ensure gh CLI is authenticated and the PR exists."); | ||
|
|
||
| return self::FAILURE; | ||
| } | ||
|
|
||
| if ($reviewData['comments'] === []) { | ||
| warning("No CodeRabbit review comments found on PR #{$prNumber}."); | ||
|
|
||
| return self::SUCCESS; | ||
| } | ||
|
|
||
| info('Found '.count($reviewData['comments'])." CodeRabbit comment(s) on PR #{$prNumber}: {$reviewData['pr_title']}"); | ||
|
|
||
| // Parse into structured findings | ||
| $findings = $coderabbit->parseFindings($reviewData['comments']); | ||
|
|
||
| if ($findings === []) { | ||
| warning('No actionable findings extracted from CodeRabbit reviews.'); | ||
|
|
||
| return self::SUCCESS; | ||
| } | ||
|
|
||
| // Filter by minimum severity | ||
| $findings = $coderabbit->filterBySeverity($findings, $minSeverity); | ||
|
|
||
| if ($findings === []) { | ||
| warning("No findings meet the minimum severity threshold: {$minSeverity}"); | ||
|
|
||
| return self::SUCCESS; | ||
| } | ||
|
|
||
| info(count($findings)." finding(s) extracted (min severity: {$minSeverity})"); | ||
|
|
||
| if ($dryRun) { | ||
| $this->displayFindings($findings, $prNumber); | ||
| info('Dry run complete. No entries added to knowledge base.'); | ||
|
|
||
| return self::SUCCESS; | ||
| } | ||
|
|
||
| // Add findings to knowledge base | ||
| $added = 0; | ||
| $failed = 0; | ||
|
|
||
| foreach ($findings as $index => $finding) { | ||
| $semanticId = "knowledge-pr-{$prNumber}-coderabbit-".($index + 1); | ||
|
|
||
| $data = [ | ||
| 'id' => $semanticId, | ||
| 'title' => "[PR #{$prNumber}] {$finding['title']}", | ||
| 'content' => $finding['content'], | ||
| 'tags' => array_filter(['coderabbit', 'code-review', "pr-{$prNumber}", $finding['file']]), | ||
| 'category' => 'architecture', | ||
| 'priority' => $finding['severity'], | ||
| 'confidence' => $finding['confidence'], | ||
| 'status' => 'draft', | ||
| 'source' => $reviewData['pr_url'], | ||
| 'ticket' => "PR-{$prNumber}", | ||
| 'evidence' => "Extracted from CodeRabbit review on PR #{$prNumber}", | ||
| 'last_verified' => now()->toIso8601String(), | ||
| ]; | ||
|
|
||
| try { | ||
| $success = $qdrant->upsert($data, 'default', false); | ||
| if ($success) { | ||
| $added++; | ||
| } else { | ||
| $failed++; | ||
| } | ||
| } catch (\Throwable) { | ||
| $failed++; | ||
| } | ||
| } | ||
|
|
||
| $this->displayFindings($findings, $prNumber); | ||
|
|
||
| info("{$added} finding(s) added to knowledge base."); | ||
| if ($failed > 0) { | ||
| warning("{$failed} finding(s) failed to store."); | ||
| } | ||
|
|
||
| return self::SUCCESS; | ||
| } | ||
|
|
||
| /** | ||
| * @param list<array{title: string, content: string, file: string|null, severity: string, confidence: int}> $findings | ||
| */ | ||
| private function displayFindings(array $findings, int $prNumber): void | ||
| { | ||
| $rows = []; | ||
| foreach ($findings as $index => $finding) { | ||
| $rows[] = [ | ||
| "knowledge-pr-{$prNumber}-coderabbit-".($index + 1), | ||
| Str::limit($finding['title'], 50), | ||
| strtoupper($finding['severity']), | ||
| "{$finding['confidence']}%", | ||
| $finding['file'] ?? 'N/A', | ||
| ]; | ||
| } | ||
|
|
||
| table( | ||
| ['Semantic ID', 'Title', 'Severity', 'Confidence', 'File'], | ||
| $rows | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Category is hardcoded as
'architecture'but the issue specifies'code-review'.The linked issue
#89states findings should be persisted withcategory: 'code-review'. The hardcoded'architecture'value on line 118 doesn't match this requirement.Proposed fix
🤖 Prompt for AI Agents