/ opencodehub / src / lib / review / change-grouper.ts
Swadhin Biswas Feature/ai code reviewer (#73) · 8/2/2026
105a287
202 lines · 6.5 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
 * Change Grouper
 * Groups related file changes into semantic units for easier review.
 */

import type { DiffChunk } from "./diff-chunker";
import type { DependencyGraph } from "./dep-graph";
import type { ComplexityResult } from "./complexity-engine";
import { analyzeAllComplexity } from "./complexity-engine";

export interface ChangeGroup {
  id: string;
  title: string;
  description: string;
  files: string[];
  layer: string;
  complexity: ComplexityResult;
  riskLevel: "low" | "medium" | "high" | "critical";
  totalAdditions: number;
  totalDeletions: number;
}

// Feature detection patterns
const FEATURE_PATTERNS: [RegExp, (match: RegExpMatchArray) => string][] = [
  [/(?:add|create|new|introduce)s?\s+(\w+)/i, (m) => `Add ${m[1]}`],
  [/(?:fix|patch|resolve|bug)s?\s+(\w+)/i, (m) => `Fix ${m[1]}`],
  [/(?:refactor|clean|reorganize)s?\s+(\w+)/i, (m) => `Refactor ${m[1]}`],
  [/(?:update|modify|change|improve)s?\s+(\w+)/i, (m) => `Update ${m[1]}`],
  [/(?:remove|delete|deprecate)s?\s+(\w+)/i, (m) => `Remove ${m[1]}`],
];

function guessFeatureName(files: string[]): string {
  // Try to extract feature name from file paths
  const allPaths = files.join("/");

  // Look for common patterns
  const featureMatch = allPaths.match(/(?:src\/lib\/|src\/components\/|src\/pages\/)([^/]+)/);
  if (featureMatch) {
    const name = featureMatch[1]
      .replace(/-/g, " ")
      .replace(/_/g, " ")
      .replace(/\b\w/g, c => c.toUpperCase());
    return name;
  }

  // Extract from file names
  const fileNames = files.map(f => f.split("/").pop()?.replace(/\.\w+$/, "") || "");
  const commonPrefix = findCommonPrefix(fileNames);
  if (commonPrefix.length > 2) {
    return commonPrefix
      .replace(/-/g, " ")
      .replace(/_/g, " ")
      .replace(/\b\w/g, c => c.toUpperCase());
  }

  return "Related Changes";
}

function findCommonPrefix(strings: string[]): string {
  if (strings.length === 0) return "";
  let prefix = strings[0];
  for (const s of strings.slice(1)) {
    while (!s.startsWith(prefix)) {
      prefix = prefix.slice(0, -1);
      if (prefix === "") return "";
    }
  }
  return prefix;
}

function groupByDirectory(chunks: DiffChunk[]): Map<string, DiffChunk[]> {
  const groups = new Map<string, DiffChunk[]>();

  for (const chunk of chunks) {
    const parts = chunk.filePath.split("/");
    // Use the deepest meaningful directory (up to 3 levels deep)
    const dirParts = parts.slice(0, Math.min(parts.length - 1, 3));
    const dir = dirParts.join("/") || "root";

    if (!groups.has(dir)) groups.set(dir, []);
    groups.get(dir)!.push(chunk);
  }

  return groups;
}

function groupByDependency(chunks: DiffChunk[], graph: DependencyGraph): DiffChunk[][] {
  const fileSet = new Set(chunks.map(c => c.filePath));
  const chunkMap = new Map(chunks.map(c => [c.filePath, c]));
  const groups: DiffChunk[][] = [];
  const visited = new Set<string>();

  for (const chunk of chunks) {
    if (visited.has(chunk.filePath)) continue;

    const groupFiles = new Set<string>();
    const queue = [chunk.filePath];

    while (queue.length > 0) {
      const current = queue.shift()!;
      if (visited.has(current)) continue;
      visited.add(current);
      groupFiles.add(current);

      for (const edge of graph.edges) {
        if ((edge.from === current || edge.to === current) && fileSet.has(edge.from) && fileSet.has(edge.to)) {
          if (!visited.has(edge.from)) queue.push(edge.from);
          if (!visited.has(edge.to)) queue.push(edge.to);
        }
      }
    }

    const groupChunks = Array.from(groupFiles)
      .map(f => chunkMap.get(f))
      .filter((c): c is DiffChunk => c !== undefined);
    groups.push(groupChunks);
  }

  return groups;
}

export function groupChanges(
  chunks: DiffChunk[],
  graph: DependencyGraph,
): ChangeGroup[] {
  if (chunks.length === 0) return [];

  const complexities = analyzeAllComplexity(chunks);

  // For small PRs (<= 5 files), create a single group
  if (chunks.length <= 5) {
    const title = guessFeatureName(chunks.map(c => c.filePath));
    const complexity = complexities.aggregate;
    return [{
      id: "group-1",
      title,
      description: `Changes in ${chunks.length} file(s)`,
      files: chunks.map(c => c.filePath),
      layer: determinePrimaryLayer(chunks),
      complexity,
      riskLevel: complexity.riskLevel,
      totalAdditions: chunks.reduce((s, c) => s + c.additions, 0),
      totalDeletions: chunks.reduce((s, c) => s + c.deletions, 0),
    }];
  }

  // For larger PRs, group by dependency graph first, then by directory
  const depGroups = groupByDependency(chunks, graph);
  const dirGroups = groupByDirectory(chunks);

  // Use whichever grouping produces fewer, more meaningful groups
  const useDepGroups = depGroups.length <= dirGroups.size && depGroups.length > 1;
  const rawGroups = useDepGroups ? depGroups : Array.from(dirGroups.values());

  return rawGroups.map((groupChunks, index) => {
    const files = groupChunks.map(c => c.filePath);
    const title = guessFeatureName(files);
    const groupComplexities = groupChunks.map(c => complexities.perFile.get(c.filePath)!).filter(Boolean);

    const worstComplexity = groupComplexities.reduce(
      (worst, c) => c.riskScore > (worst?.riskScore || 0) ? c : worst,
      groupComplexities[0],
    );

    return {
      id: `group-${index + 1}`,
      title,
      description: `Changes in ${groupChunks.length} file(s)`,
      files,
      layer: determinePrimaryLayer(groupChunks),
      complexity: worstComplexity || complexities.aggregate,
      riskLevel: worstComplexity?.riskLevel || "low",
      totalAdditions: groupChunks.reduce((s, c) => s + c.additions, 0),
      totalDeletions: groupChunks.reduce((s, c) => s + c.deletions, 0),
    };
  });
}

function determinePrimaryLayer(chunks: DiffChunk[]): string {
  const layerCounts = new Map<string, number>();
  for (const chunk of chunks) {
    const path = chunk.filePath.toLowerCase();
    let layer = "backend";
    if (/component|page|layout|\.tsx|\.vue|\.astro/i.test(path)) layer = "frontend";
    else if (/schema|migrat|model|entity/i.test(path)) layer = "database";
    else if (/test|spec|__tests__/i.test(path)) layer = "testing";
    else if (/auth|security|permission|token/i.test(path)) layer = "security";
    else if (/docker|deploy|ci|cd|infra/i.test(path)) layer = "infrastructure";
    layerCounts.set(layer, (layerCounts.get(layer) || 0) + 1);
  }

  let maxCount = 0;
  let primaryLayer = "backend";
  for (const [layer, count] of layerCounts) {
    if (count > maxCount) {
      maxCount = count;
      primaryLayer = layer;
    }
  }
  return primaryLayer;
}