Files

70 lines
3.5 KiB
Markdown

# pr-reviewer
Companion code for *Build an AI Pull Request Reviewer in Java: Spring AI, Gitea Webhooks and Inline Comments* on
[ankurm.com](https://ankurm.com). A Spring Boot service that receives Gitea pull request webhooks, reviews the diff
with a model through Spring AI structured output, checks every finding against the diff, and posts inline comments
once, however many times the branch is pushed.
It goes further than the smaller [`code-review-bot`](../code-review-bot) module and is tested against a real Gitea.
## Run the tests
```bash
export JAVA_HOME=/path/to/jdk-25
mvn test -pl pr-reviewer -am # offline: no Docker, no API key (live tests are skipped)
pr-reviewer/scripts/run-all.sh # also starts a throwaway Gitea 1.27.3 in Docker and rewrites output/
```
The live tests create repositories and webhooks. They refuse to run unless `GITEA_URL` is on localhost.
Real model (optional): `pr-reviewer/scripts/ollama-up.sh`, export what it prints, then run `run-all.sh`
and `scripts/live-demo.sh`.
## Run the service
```bash
mvn -pl pr-reviewer -am package -DskipTests
GITEA_URL=https://git.example.com GITEA_TOKEN=... GITEA_BOT_LOGIN=reviewbot GITEA_WEBHOOK_SECRET=... \
OPENAI_API_KEY=... java -jar pr-reviewer/target/pr-reviewer-1.0.0.jar
```
Webhook: URL `https://<host>/webhook/gitea`, content type `application/json`, the same secret, pull request events.
For Ollama set `OPENAI_BASE_URL=http://localhost:11434/v1` and `OPENAI_MODEL`. The OpenAI hosted API was not called
while building this module (no key).
## Source
| File | Role |
|---|---|
| `WebhookController`, `Signatures` | HMAC over the raw body, event filter, immediate `202` |
| `ReviewQueue` | one review at a time per pull request (single process only) |
| `UnifiedDiff`, `Chunker` | parse by hunk counts, pack hunks per file |
| `ModelReviewer`, `Finding`, `FenceStripper`, `UsageMeter` | Spring AI `entity()` with schema validation, token count including retries |
| `LineGuard` | a finding is posted only if its quoted code is on an added line |
| `Fingerprint`, `CommentFormat` | de-duplication marker; neutralises `@` mentions and forged markers |
| `ReviewService` | the pipeline, cache, limits, head-moved check, pending-review cleanup |
| `GiteaClient` | the Gitea REST calls |
## Output (written by tests and scripts)
| File | Shows |
|---|---|
| `01`, `02` | diff parsing traps; splitting a large hunk |
| `03`, `04` | the exact prompt and schema; fenced, invalid and prose replies |
| `05` | the line guard on invented and shifted line numbers |
| `06`, `07` | de-duplication across pushes; cache and identical lines |
| `08`, `09`, `10` | limits and failures; untrusted text; a real captured webhook |
| `11` to `13` | what Gitea 1.27.3 accepts, the PENDING review left by a failed call, an old `commit_id` |
| `14` to `16` | end to end through real webhooks; no redelivery; pipeline timings |
| `17` | cost, **computed estimates** (JTokkit token counts, list prices of 2026-12-03) |
| `18` | `qwen2.5-coder` 1.5b and 3b on a 2 vCPU machine, five runs each |
| `19`, `20`, `21` | Gitea's default webhook block; the packaged jar with a local model; the earlier module's chunker |
## Limits
Review state is in the pull request, but the queue and cache are in memory, so run one replica. A renamed file gets
new fingerprints. Only added lines can receive comments. GitHub is not supported. Timings come from one shared
2 vCPU cloud machine.
Verified with JDK 25 (Temurin 25.0.5.1), Spring Boot 4.2.0, Spring AI 2.1.0, JUnit 6.1.3, Gitea 1.27.3.