Add pr-reviewer module: Gitea webhooks, Spring AI structured output, quote-checked inline comments, de-duplication across pushes and live tests against Gitea 1.27.3 on Spring AI 2.1.0

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b4HkWoC7hLzk61vZa8
This commit is contained in:
Claude
2026-12-04 07:49:36 +05:30
parent 38c9ca8452
commit 8fa3c5d682
74 changed files with 4794 additions and 0 deletions
@@ -0,0 +1,101 @@
package com.ankurm.prreviewer;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
/**
* Packs hunks into model-sized chunks. A chunk never crosses a file boundary. Whole hunks are kept
* together when they fit; a hunk larger than the limit is split between lines, and every line keeps
* its own number, so a split cannot shift anything.
*/
public final class Chunker {
private static final Pattern IGNORED = Pattern.compile(
"(?:.*\\.lock|.*-lock\\.(?:json|yaml)|.*\\.min\\.(?:js|css)|(?:.*/)?(?:target|build|generated|vendor|node_modules)/.*)");
/** A line this chunk adds, in order of appearance. */
public record Added(int line, String text) {}
public record Chunk(String file, List<UnifiedDiff.Line> lines) {
/** Added lines by new-file number: the only lines a comment may land on. */
public Map<Integer, String> addedLines() {
Map<Integer, String> map = new LinkedHashMap<>();
lines.stream().filter(l -> l.type() == UnifiedDiff.Type.ADDED)
.forEach(l -> map.put(l.newLine(), l.text()));
return map;
}
/** What the model sees: new-file line number, marker, code. Removed lines get no number. */
public String render() {
StringBuilder sb = new StringBuilder();
for (var l : lines) {
switch (l.type()) {
case ADDED -> sb.append("%5d + %s%n".formatted(l.newLine(), l.text()));
case CONTEXT -> sb.append("%5d %s%n".formatted(l.newLine(), l.text()));
case REMOVED -> sb.append(" - %s%n".formatted(l.text()));
}
}
return sb.toString();
}
/** Identity of the code without line numbers, so a chunk that only moved is still the same chunk. */
String contentKey() {
StringBuilder sb = new StringBuilder(file);
lines.forEach(l -> sb.append('\n').append(l.type().name().charAt(0)).append(l.text()));
return Hashes.sha256(sb.toString());
}
}
public record Result(List<Chunk> chunks, List<String> skippedFiles) {}
private final int maxChars;
public Chunker(int maxChars) {
this.maxChars = maxChars;
}
public Result chunk(List<UnifiedDiff.FileDiff> files) {
List<Chunk> out = new ArrayList<>();
List<String> skipped = new ArrayList<>();
for (var file : files) {
if (file.deleted() || file.binary() || IGNORED.matcher(file.path()).matches()) {
skipped.add(file.path());
continue;
}
List<UnifiedDiff.Line> pending = new ArrayList<>();
for (var hunk : file.hunks()) {
if (!pending.isEmpty() && size(pending) + size(hunk.lines()) > maxChars) {
emit(out, file.path(), pending);
}
for (var line : hunk.lines()) {
if (!pending.isEmpty() && size(pending) + size(line) > maxChars) {
emit(out, file.path(), pending);
}
pending.add(line);
}
}
emit(out, file.path(), pending);
}
return new Result(out, skipped);
}
private static int size(UnifiedDiff.Line l) {
return l.text().length() + 9; // number, marker and newline
}
private static int size(List<UnifiedDiff.Line> lines) {
return lines.stream().mapToInt(Chunker::size).sum();
}
/** A chunk with no added line has nothing a comment could attach to, so it is never sent. */
private static void emit(List<Chunk> out, String file, List<UnifiedDiff.Line> pending) {
if (pending.stream().anyMatch(l -> l.type() == UnifiedDiff.Type.ADDED)) {
out.add(new Chunk(file, List.copyOf(pending)));
}
pending.clear();
}
}