-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreview.go
More file actions
121 lines (111 loc) · 3.74 KB
/
review.go
File metadata and controls
121 lines (111 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package cli
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/randomcodespace/codeiq/internal/graph"
"github.com/randomcodespace/codeiq/internal/review"
"github.com/spf13/cobra"
)
func init() {
registerSubcommand(func() *cobra.Command {
var (
base string
head string
model string
outFile string
format string
focus []string
)
cmd := &cobra.Command{
Use: "review [path]",
Short: "LLM-driven review of a PR diff against the indexed graph.",
Long: `Run an LLM review of git diff base..head, using the codeiq graph
as evidence context. Defaults: base=HEAD~1, head=HEAD, model=gpt-oss:20b
via local Ollama (set OLLAMA_API_KEY for Ollama Cloud).
Output formats:
--format=markdown (default) human-readable review
--format=json structured Report for piping into other tools
Plan §3 — Phase 3 of the optimization plan.`,
Example: ` codeiq review --base origin/main --head HEAD
OLLAMA_API_KEY=... codeiq review --model gpt-oss:120b
codeiq review --base v1.0 --head v1.1 --out review.md`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
path := "."
if len(args) == 1 {
path = args[0]
}
abs, err := filepath.Abs(path)
if err != nil {
return err
}
cfg := review.DefaultConfig()
if model != "" {
cfg.Model = model
}
client := review.NewClient(cfg)
// Best-effort: open the enriched Kuzu store read-only so the
// review prompt carries graph evidence per changed file. If
// the store isn't present (no enrich yet) we fall back to
// diff-only review with a stderr warning.
var gctx review.GraphContext
gdir := filepath.Join(abs, ".codeiq", "graph", "codeiq.kuzu")
if store, err := graph.OpenReadOnly(gdir, 30*time.Second); err == nil {
defer store.Close()
gctx = review.NewKuzuGraphContext(store)
} else {
fmt.Fprintf(os.Stderr, "review: graph store not available (%v); falling back to diff-only review. Run 'codeiq enrich' first to include graph evidence.\n", err)
}
svc := review.NewService(client, gctx)
ctx, cancel := context.WithTimeout(cmd.Context(), cfg.Timeout+30*time.Second)
defer cancel()
rep, err := svc.Review(ctx, abs, base, head, focus)
if err != nil {
return fmt.Errorf("review: %w", err)
}
var rendered string
if format == "json" {
b, _ := json.MarshalIndent(rep, "", " ")
rendered = string(b) + "\n"
} else {
rendered = renderMarkdown(rep)
}
if outFile == "" {
fmt.Fprint(cmd.OutOrStdout(), rendered)
return nil
}
return os.WriteFile(outFile, []byte(rendered), 0644)
},
}
cmd.Flags().StringVar(&base, "base", "", "Base git ref (default: HEAD~1)")
cmd.Flags().StringVar(&head, "head", "", "Head git ref (default: HEAD)")
cmd.Flags().StringVar(&model, "model", "", "Override LLM model (default: from config)")
cmd.Flags().StringVarP(&outFile, "out", "o", "", "Write output to file instead of stdout")
cmd.Flags().StringVar(&format, "format", "markdown", "Output format: markdown | json")
cmd.Flags().StringSliceVar(&focus, "focus", nil, "Limit review to these file paths")
return cmd
})
}
func renderMarkdown(rep *review.Report) string {
var b strings.Builder
fmt.Fprintf(&b, "# Code Review (model: %s)\n\n", rep.Model)
fmt.Fprintf(&b, "## Summary\n\n%s\n\n", rep.Summary)
if len(rep.Findings) == 0 {
b.WriteString("## Findings\n\nNo findings.\n")
return b.String()
}
b.WriteString("## Findings\n\n")
for _, f := range rep.Findings {
loc := f.File
if f.Line > 0 {
loc = fmt.Sprintf("%s:%d", f.File, f.Line)
}
fmt.Fprintf(&b, "- **[%s] %s** — %s\n", strings.ToUpper(f.Severity), loc, f.Comment)
}
return b.String()
}