|
| 1 | +import { Injectable, Logger } from '@nestjs/common'; |
| 2 | +import { execSync } from 'child_process'; |
| 3 | +import { FileSpecialistMapper } from './file-specialist-mapper'; |
| 4 | +import { ChecklistService } from '../checklist/checklist.service'; |
| 5 | +import type { ReviewPrInput, ReviewPrResult, PrMeta } from './review-pr.types'; |
| 6 | + |
| 7 | +@Injectable() |
| 8 | +export class ReviewPrService { |
| 9 | + private readonly logger = new Logger(ReviewPrService.name); |
| 10 | + private readonly mapper = new FileSpecialistMapper(); |
| 11 | + |
| 12 | + constructor(private readonly checklistService: ChecklistService) {} |
| 13 | + |
| 14 | + async reviewPr(input: ReviewPrInput): Promise<ReviewPrResult> { |
| 15 | + const start = Date.now(); |
| 16 | + const { prNumber, issueNumber } = input; |
| 17 | + |
| 18 | + const prMeta = this.fetchPrMeta(prNumber); |
| 19 | + const diff = this.fetchPrDiff(prNumber); |
| 20 | + |
| 21 | + const checklists = await this.generateChecklists(prMeta.changedFiles); |
| 22 | + const recommendedSpecialists = this.mapSpecialists(prMeta.changedFiles); |
| 23 | + const acceptanceCriteria = issueNumber ? this.fetchIssueCriteria(issueNumber) : null; |
| 24 | + |
| 25 | + const reviewProtocol = this.buildReviewProtocol(prNumber, issueNumber); |
| 26 | + |
| 27 | + return { |
| 28 | + prMeta, |
| 29 | + diff, |
| 30 | + checklists, |
| 31 | + recommendedSpecialists, |
| 32 | + acceptanceCriteria, |
| 33 | + reviewProtocol, |
| 34 | + duration: Date.now() - start, |
| 35 | + }; |
| 36 | + } |
| 37 | + |
| 38 | + private fetchPrMeta(prNumber: number): PrMeta { |
| 39 | + try { |
| 40 | + const raw = execSync( |
| 41 | + `gh pr view ${prNumber} --json title,headRefName,files,additions,deletions`, |
| 42 | + { encoding: 'utf-8', timeout: 15000 }, |
| 43 | + ); |
| 44 | + const data = JSON.parse(raw); |
| 45 | + return { |
| 46 | + title: data.title ?? '', |
| 47 | + branch: data.headRefName ?? '', |
| 48 | + changedFiles: (data.files ?? []).map((f: { path: string }) => f.path), |
| 49 | + additions: data.additions ?? 0, |
| 50 | + deletions: data.deletions ?? 0, |
| 51 | + }; |
| 52 | + } catch (error) { |
| 53 | + throw new Error( |
| 54 | + `Failed to fetch PR #${prNumber}: ${error instanceof Error ? error.message : String(error)}`, |
| 55 | + ); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private fetchPrDiff(prNumber: number): string { |
| 60 | + try { |
| 61 | + return execSync(`gh pr diff ${prNumber}`, { |
| 62 | + encoding: 'utf-8', |
| 63 | + timeout: 15000, |
| 64 | + maxBuffer: 10 * 1024 * 1024, |
| 65 | + }); |
| 66 | + } catch (error) { |
| 67 | + this.logger.warn(`Failed to fetch diff for PR #${prNumber}: ${error}`); |
| 68 | + return ''; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private async generateChecklists(changedFiles: string[]): Promise<unknown[]> { |
| 73 | + if (changedFiles.length === 0) return []; |
| 74 | + try { |
| 75 | + const result = await this.checklistService.generateChecklist({ |
| 76 | + files: changedFiles, |
| 77 | + }); |
| 78 | + return result.checklists; |
| 79 | + } catch (error) { |
| 80 | + this.logger.warn(`Checklist generation failed: ${error}`); |
| 81 | + return []; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private mapSpecialists(changedFiles: string[]): string[] { |
| 86 | + const mapped = this.mapper.mapFiles(changedFiles); |
| 87 | + const specialists = [...new Set(mapped.map(m => m.specialist))]; |
| 88 | + return specialists; |
| 89 | + } |
| 90 | + |
| 91 | + private fetchIssueCriteria(issueNumber: number): string | null { |
| 92 | + try { |
| 93 | + const raw = execSync(`gh issue view ${issueNumber} --json body`, { |
| 94 | + encoding: 'utf-8', |
| 95 | + timeout: 10000, |
| 96 | + }); |
| 97 | + const data = JSON.parse(raw); |
| 98 | + const body: string = data.body ?? ''; |
| 99 | + |
| 100 | + const criteriaMatch = body.match(/## Acceptance criteria\s*\n([\s\S]*?)(?=\n## |\n---|$)/i); |
| 101 | + return criteriaMatch ? criteriaMatch[1].trim() : body.slice(0, 2000); |
| 102 | + } catch { |
| 103 | + return null; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private buildReviewProtocol(prNumber: number, issueNumber?: number): string { |
| 108 | + const issueCmd = issueNumber ? `\ngh issue view ${issueNumber}` : ''; |
| 109 | + return [ |
| 110 | + 'Follow the canonical PR review cycle (pr-review-cycle.md):', |
| 111 | + '', |
| 112 | + '1. CI GATE (BLOCKING): gh pr checks ' + prNumber, |
| 113 | + '2. LOCAL VERIFICATION: yarn lint && yarn type-check', |
| 114 | + '3. READ THE DIFF: gh pr diff ' + prNumber, |
| 115 | + '4. CODE QUALITY SCAN: unused imports, any types, missing error handling, dead code', |
| 116 | + '5. SPEC COMPLIANCE: ' + (issueNumber ? `gh issue view ${issueNumber}` : 'N/A'), |
| 117 | + '6. TEST COVERAGE: new logic tested? edge cases covered?', |
| 118 | + '7. WRITE REVIEW: gh pr review ' + prNumber + ' --comment --body "<structured review>"', |
| 119 | + '', |
| 120 | + 'Severity scale: critical / high / medium / low (see severity-classification.md)', |
| 121 | + 'Approval gate: Critical=0 AND High=0', |
| 122 | + '', |
| 123 | + 'Commands:', |
| 124 | + `gh pr checks ${prNumber}`, |
| 125 | + `gh pr diff ${prNumber}`, |
| 126 | + issueCmd, |
| 127 | + ] |
| 128 | + .filter(Boolean) |
| 129 | + .join('\n'); |
| 130 | + } |
| 131 | +} |
0 commit comments