|
| 1 | +package evolution |
| 2 | + |
| 3 | +import ( |
| 4 | + "os/exec" |
| 5 | + "path/filepath" |
| 6 | + "sort" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +// CodeAnalysis holds analysis results for the codebase. |
| 11 | +type CodeAnalysis struct { |
| 12 | + TODOs []string // file:line: content |
| 13 | + Hotspots []string // most changed files |
| 14 | + NoTestPkgs []string // packages without test files |
| 15 | + BuildOK bool |
| 16 | + TestOK bool |
| 17 | +} |
| 18 | + |
| 19 | +// AnalyzeCodebase scans the repo for improvement opportunities. |
| 20 | +func AnalyzeCodebase(repoPath string) CodeAnalysis { |
| 21 | + analysis := CodeAnalysis{BuildOK: true, TestOK: true} |
| 22 | + |
| 23 | + // Check build |
| 24 | + if err := exec.Command("go", "build", "./...").Run(); err != nil { |
| 25 | + analysis.BuildOK = false |
| 26 | + } |
| 27 | + |
| 28 | + // Check tests |
| 29 | + if err := exec.Command("go", "test", "./...").Run(); err != nil { |
| 30 | + analysis.TestOK = false |
| 31 | + } |
| 32 | + |
| 33 | + // Find TODOs/FIXMEs |
| 34 | + analysis.TODOs = findTODOs(repoPath) |
| 35 | + |
| 36 | + // Find hotspots (most changed files) |
| 37 | + analysis.Hotspots = findHotspots(repoPath) |
| 38 | + |
| 39 | + // Find packages without tests |
| 40 | + analysis.NoTestPkgs = findNoTestPackages(repoPath) |
| 41 | + |
| 42 | + return analysis |
| 43 | +} |
| 44 | + |
| 45 | +// FormatAnalysis returns a human-readable summary for the agent. |
| 46 | +func (a CodeAnalysis) FormatAnalysis() string { |
| 47 | + var sb strings.Builder |
| 48 | + |
| 49 | + if !a.BuildOK { |
| 50 | + sb.WriteString("🔴 BUILD BROKEN — fix this first!\n\n") |
| 51 | + } |
| 52 | + if !a.TestOK { |
| 53 | + sb.WriteString("🔴 TESTS FAILING — fix this first!\n\n") |
| 54 | + } |
| 55 | + |
| 56 | + if len(a.TODOs) > 0 { |
| 57 | + sb.WriteString("## TODOs found in code\n\n") |
| 58 | + limit := 10 |
| 59 | + if len(a.TODOs) < limit { |
| 60 | + limit = len(a.TODOs) |
| 61 | + } |
| 62 | + for _, todo := range a.TODOs[:limit] { |
| 63 | + sb.WriteString("- " + todo + "\n") |
| 64 | + } |
| 65 | + sb.WriteString("\n") |
| 66 | + } |
| 67 | + |
| 68 | + if len(a.Hotspots) > 0 { |
| 69 | + sb.WriteString("## Most changed files (hotspots)\n\n") |
| 70 | + limit := 5 |
| 71 | + if len(a.Hotspots) < limit { |
| 72 | + limit = len(a.Hotspots) |
| 73 | + } |
| 74 | + for _, h := range a.Hotspots[:limit] { |
| 75 | + sb.WriteString("- " + h + "\n") |
| 76 | + } |
| 77 | + sb.WriteString("\n") |
| 78 | + } |
| 79 | + |
| 80 | + if len(a.NoTestPkgs) > 0 { |
| 81 | + sb.WriteString("## Packages without tests\n\n") |
| 82 | + for _, pkg := range a.NoTestPkgs { |
| 83 | + sb.WriteString("- " + pkg + "\n") |
| 84 | + } |
| 85 | + sb.WriteString("\n") |
| 86 | + } |
| 87 | + |
| 88 | + return sb.String() |
| 89 | +} |
| 90 | + |
| 91 | +func findTODOs(repoPath string) []string { |
| 92 | + out, err := exec.Command("grep", "-rn", "--include=*.go", |
| 93 | + "TODO\\|FIXME\\|HACK\\|XXX", |
| 94 | + filepath.Join(repoPath, "cmd"), |
| 95 | + filepath.Join(repoPath, "internal"), |
| 96 | + ).CombinedOutput() |
| 97 | + if err != nil { |
| 98 | + return nil |
| 99 | + } |
| 100 | + |
| 101 | + var results []string |
| 102 | + for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") { |
| 103 | + if line == "" { |
| 104 | + continue |
| 105 | + } |
| 106 | + // Strip repo path prefix |
| 107 | + line = strings.TrimPrefix(line, repoPath+"/") |
| 108 | + results = append(results, line) |
| 109 | + } |
| 110 | + return results |
| 111 | +} |
| 112 | + |
| 113 | +func findHotspots(repoPath string) []string { |
| 114 | + out, err := exec.Command("git", "-C", repoPath, "log", "--pretty=format:", |
| 115 | + "--name-only", "-50").CombinedOutput() |
| 116 | + if err != nil { |
| 117 | + return nil |
| 118 | + } |
| 119 | + |
| 120 | + counts := map[string]int{} |
| 121 | + for _, line := range strings.Split(string(out), "\n") { |
| 122 | + line = strings.TrimSpace(line) |
| 123 | + if line == "" || strings.HasSuffix(line, "_test.go") { |
| 124 | + continue |
| 125 | + } |
| 126 | + counts[line]++ |
| 127 | + } |
| 128 | + |
| 129 | + type entry struct { |
| 130 | + name string |
| 131 | + count int |
| 132 | + } |
| 133 | + var entries []entry |
| 134 | + for name, count := range counts { |
| 135 | + entries = append(entries, entry{name, count}) |
| 136 | + } |
| 137 | + sort.Slice(entries, func(i, j int) bool { |
| 138 | + return entries[i].count > entries[j].count |
| 139 | + }) |
| 140 | + |
| 141 | + var results []string |
| 142 | + limit := 10 |
| 143 | + if len(entries) < limit { |
| 144 | + limit = len(entries) |
| 145 | + } |
| 146 | + for _, e := range entries[:limit] { |
| 147 | + results = append(results, e.name+" (changed "+string(rune('0'+e.count))+"x)") |
| 148 | + } |
| 149 | + return results |
| 150 | +} |
| 151 | + |
| 152 | +func findNoTestPackages(repoPath string) []string { |
| 153 | + out, err := exec.Command("go", "list", "./...").Output() |
| 154 | + if err != nil { |
| 155 | + return nil |
| 156 | + } |
| 157 | + |
| 158 | + var noTest []string |
| 159 | + for _, pkg := range strings.Split(strings.TrimSpace(string(out)), "\n") { |
| 160 | + if pkg == "" || strings.Contains(pkg, "vendor") { |
| 161 | + continue |
| 162 | + } |
| 163 | + // Check if package has test files |
| 164 | + testOut, _ := exec.Command("go", "list", "-f", "{{.TestGoFiles}}", pkg).Output() |
| 165 | + if strings.TrimSpace(string(testOut)) == "[]" { |
| 166 | + noTest = append(noTest, pkg) |
| 167 | + } |
| 168 | + } |
| 169 | + return noTest |
| 170 | +} |
0 commit comments