Quick-AIReview.ps1
|
<#
.SYNOPSIS Quick AI Code Review - Direct test script .DESCRIPTION Simplified script that directly calls AI to review changed AL files. Bypasses some of the module's internal complexity for testing. .PARAMETER BaseBranch Base branch to compare against .EXAMPLE .\Quick-AIReview.ps1 -BaseBranch "main" #> param( [string]$BaseBranch = "origin/master" ) Write-Host "`n╔════════════════════════════════════════════════════════════════╗" -ForegroundColor Cyan Write-Host "║ Quick AI Code Review Test ║" -ForegroundColor Cyan Write-Host "╚════════════════════════════════════════════════════════════════╝" -ForegroundColor Cyan Write-Host "" # Check module if (-not (Get-Module -Name iFacto.AICodeReview -ListAvailable)) { Write-Host "❌ Module not installed" -ForegroundColor Red Write-Host " Install-Module iFacto.AICodeReview -Scope CurrentUser" -ForegroundColor Yellow exit 1 } Import-Module iFacto.AICodeReview -Force Write-Host "✓ Module loaded" -ForegroundColor Green # Check API key if (-not $env:GITHUB_TOKEN) { Write-Host "❌ GitHub token not set" -ForegroundColor Red Write-Host " `$env:GITHUB_TOKEN = 'your-token'" -ForegroundColor Yellow exit 1 } Write-Host "✓ GitHub token configured" -ForegroundColor Green # Get changed files Write-Host "" Write-Host "🔍 Finding changed AL files..." -ForegroundColor Cyan $changedFiles = Get-ChangedALFiles -BaseBranch $BaseBranch if (-not $changedFiles -or $changedFiles.Count -eq 0) { Write-Host "⚠️ No AL files changed" -ForegroundColor Yellow Write-Host "" Write-Host "Tips:" -ForegroundColor Gray Write-Host "- Make sure you have commits on current branch" -ForegroundColor Gray Write-Host "- Check base branch: $BaseBranch" -ForegroundColor Gray Write-Host "- Try: git diff $BaseBranch..HEAD --name-only '*.al'" -ForegroundColor Gray exit 0 } Write-Host "✓ Found $($changedFiles.Count) file(s):" -ForegroundColor Green $changedFiles | ForEach-Object { Write-Host " - $_" -ForegroundColor White } # Get file content for each changed file Write-Host "" Write-Host "📄 Reading file content..." -ForegroundColor Cyan $filesToReview = @() foreach ($file in $changedFiles) { if (Test-Path $file) { $content = Get-Content $file -Raw Write-Host "✓ Read: $file ($($content.Length) chars)" -ForegroundColor Green $filesToReview += @{ Path = $file Content = $content } } else { Write-Host "⚠️ File not found: $file" -ForegroundColor Yellow } } if ($filesToReview.Count -eq 0) { Write-Host "❌ No files to review" -ForegroundColor Red exit 1 } # Prepare review prompt Write-Host "" Write-Host "🤖 Calling AI for code review..." -ForegroundColor Cyan Write-Host " Provider: GitHub Models" -ForegroundColor Gray Write-Host " Model: gpt-4o" -ForegroundColor Gray Write-Host "" # Create combined code context $codeContext = @() foreach ($file in $filesToReview) { $codeContext += "File: $($file.Path)" $codeContext += "``````al" $codeContext += $file.Content $codeContext += "``````" $codeContext += "" } $prompt = @" You are reviewing Business Central AL code for quality and best practices. Review the following AL code files and identify violations: $($codeContext -join "`n") For each violation found, provide: 1. Severity (error or warning) 2. Line number (approximate) 3. Clear explanation of the issue 4. Suggestion for fix Focus on: - Naming conventions (field names should use spaces, not underscores) - Performance issues (database access in loops) - Missing error handling - Hardcoded strings (should use labels) - AL best practices Return your findings in this JSON format: { "violations": [ { "file": "path/to/file.al", "line": 15, "severity": "warning", "message": "Explanation of issue" } ] } If no violations found, return: {"violations": []} "@ # Call GitHub Models API try { $headers = @{ "Authorization" = "Bearer $env:GITHUB_TOKEN" "Content-Type" = "application/json" } $body = @{ model = "gpt-4o" messages = @( @{ role = "system" content = "You are an expert Business Central AL code reviewer." } @{ role = "user" content = $prompt } ) temperature = 0.3 max_tokens = 2000 } | ConvertTo-Json -Depth 10 Write-Host " Sending request..." -ForegroundColor Gray $response = Invoke-RestMethod -Uri "https://models.inference.ai.azure.com/chat/completions" ` -Method Post ` -Headers $headers ` -Body $body Write-Host "✓ AI response received" -ForegroundColor Green # Parse response $aiResponse = $response.choices[0].message.content Write-Host "" Write-Host "📋 AI Analysis:" -ForegroundColor Yellow Write-Host $aiResponse -ForegroundColor White # Try to parse JSON try { # Extract JSON from markdown if present if ($aiResponse -match '(?s)```json\s*(\{.*?\})\s*```') { $jsonText = $matches[1] } elseif ($aiResponse -match '(?s)\{.*?"violations".*?\}') { $jsonText = $matches[0] } else { $jsonText = $aiResponse } $result = $jsonText | ConvertFrom-Json Write-Host "" Write-Host "═══════════════════════════════════════════════════════════════" -ForegroundColor Cyan Write-Host "" if ($result.violations.Count -eq 0) { Write-Host "✅ No violations found!" -ForegroundColor Green Write-Host " Code looks good!" -ForegroundColor Gray } else { Write-Host "🔍 Found $($result.violations.Count) violation(s):" -ForegroundColor Yellow Write-Host "" foreach ($violation in $result.violations) { $icon = if ($violation.severity -eq "error") { "❌" } else { "⚠️" } $color = if ($violation.severity -eq "error") { "Red" } else { "Yellow" } Write-Host "$icon $($violation.severity.ToUpper())" -ForegroundColor $color -NoNewline Write-Host " - Line $($violation.line)" -ForegroundColor Gray Write-Host " File: $($violation.file)" -ForegroundColor Cyan Write-Host " $($violation.message)" -ForegroundColor White Write-Host "" } # Show in ADO format Write-Host "Azure DevOps Format:" -ForegroundColor Gray Write-Host "───────────────────────────────────────" -ForegroundColor Gray foreach ($violation in $result.violations) { $adoLine = "##vso[task.logissue type=$($violation.severity);sourcepath=$($violation.file);linenumber=$($violation.line);]$($violation.message)" Write-Host $adoLine -ForegroundColor Gray } } Write-Host "" Write-Host "═══════════════════════════════════════════════════════════════" -ForegroundColor Cyan Write-Host "" } catch { Write-Host "⚠️ Could not parse JSON response" -ForegroundColor Yellow Write-Host " Raw response shown above" -ForegroundColor Gray } } catch { Write-Host "❌ API call failed: $_" -ForegroundColor Red Write-Host $_.Exception.Message -ForegroundColor Red exit 1 } |