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
+26
View File
@@ -0,0 +1,26 @@
#!/usr/bin/env bash
# Feeds the chunker of the earlier code-review-bot module a diff whose added line starts with "++". Writes output/21.
set -euo pipefail
cd "$(dirname "$0")/.."
OUT=target/earlier-bot
mkdir -p $OUT
cat > $OUT/Probe.java <<'JAVA'
import com.ankurm.reviewbot.DiffChunker;
public class Probe {
public static void main(String[] args) {
String diff = "diff --git a/x.sql b/x.sql\n--- a/x.sql\n+++ b/x.sql\n@@ -1,2 +1,4 @@\n select 1;\n+++i;\n+select 2;\n select 3;\n";
for (var chunk : new DiffChunker(2000).chunk(diff).chunks()) {
System.out.print(chunk.text());
System.out.println("added lines: " + chunk.addedLines());
}
}
}
JAVA
javac -d $OUT ../code-review-bot/src/main/java/com/ankurm/reviewbot/DiffChunker.java $OUT/Probe.java
{
echo "new file content is: select 1; / ++i; / select 2; / select 3; (added: lines 2 and 3)"
echo "code-review-bot DiffChunker says:"
java -cp $OUT Probe
} > output/21-earlier-bot-chunker.txt
cat output/21-earlier-bot-chunker.txt
+26
View File
@@ -0,0 +1,26 @@
#!/usr/bin/env bash
# Starts a throwaway Gitea 1.27.3 for the live tests and writes scripts/.gitea.env. Never point the tests at a real server.
set -euo pipefail
cd "$(dirname "$0")"
IMAGE=${GITEA_IMAGE:-gitea/gitea:1.27.3}
docker rm -f pr-reviewer-gitea >/dev/null 2>&1 || true
docker run -d --name pr-reviewer-gitea -p 127.0.0.1:3118:3000 \
-e GITEA__security__INSTALL_LOCK=true \
-e GITEA__security__ALLOWED_HOST_LIST=private \
-e GITEA__database__DB_TYPE=sqlite3 \
-e GITEA__server__ROOT_URL=http://localhost:3118/ "$IMAGE" >/dev/null
until curl -sf --noproxy '*' http://127.0.0.1:3118/api/v1/version >/dev/null; do sleep 2; done
PASS="Local-$(head -c 12 /dev/urandom | od -An -tx1 | tr -d ' \n')"
for user in dev reviewbot; do
extra=""; [ "$user" = dev ] && extra="--admin"
docker exec -u git pr-reviewer-gitea gitea admin user create --username "$user" --password "$PASS" \
--email "$user@example.com" --must-change-password=false $extra >/dev/null
done
token() { docker exec -u git pr-reviewer-gitea gitea admin user generate-access-token --username "$1" --scopes all --token-name t --raw; }
{
echo "GITEA_DEV_TOKEN=$(token dev)"
echo "GITEA_BOT_TOKEN=$(token reviewbot)"
# the address under which the container reaches the machine running the tests
echo "WEBHOOK_HOST=$(docker network inspect bridge -f '{{(index .IPAM.Config 0).Gateway}}')"
} > .gitea.env
echo "Gitea is up on http://127.0.0.1:3118"
+41
View File
@@ -0,0 +1,41 @@
#!/usr/bin/env bash
# The packaged application, a local Gitea and a local model, wired the way a deployment is.
# Needs: scripts/gitea-up.sh and scripts/ollama-up.sh already run, and LLM_URL / LLM_MODEL exported. Writes output/20.
set -euo pipefail
cd "$(dirname "$0")/.."
. scripts/.gitea.env
(cd .. && mvn -B -q -pl pr-reviewer -am package -DskipTests)
API=http://127.0.0.1:3118/api/v1
as() { local t=$1; shift; curl -s --noproxy '*' -H "Authorization: token $t" -H 'Content-Type: application/json' "$@"; }
REPO=demo-$RANDOM
SECRET=local-$RANDOM$RANDOM
GITEA_URL=http://127.0.0.1:3118 GITEA_TOKEN=$GITEA_BOT_TOKEN GITEA_WEBHOOK_SECRET=$SECRET \
OPENAI_BASE_URL=$LLM_URL OPENAI_MODEL=$LLM_MODEL \
java -jar target/pr-reviewer-1.0.0.jar --server.port=8118 --spring.ai.openai.timeout=600s > target/app.log 2>&1 &
APP=$!
trap 'kill $APP 2>/dev/null || true' EXIT
until grep -q 'Started ReviewerApplication' target/app.log; do sleep 1; kill -0 $APP; done
as $GITEA_DEV_TOKEN -d "{\"name\":\"$REPO\",\"auto_init\":true,\"default_branch\":\"main\"}" $API/user/repos >/dev/null
as $GITEA_DEV_TOKEN -X PUT -d '{"permission":"write"}' $API/repos/dev/$REPO/collaborators/reviewbot
as $GITEA_DEV_TOKEN -d "{\"type\":\"gitea\",\"active\":true,\"events\":[\"pull_request\"],\"config\":{\"url\":\"http://$WEBHOOK_HOST:8118/webhook/gitea\",\"content_type\":\"json\",\"secret\":\"$SECRET\"}}" $API/repos/dev/$REPO/hooks >/dev/null
CODE=$(printf 'package shop;\n\npublic class Greeter {\n}\n' | base64 -w0)
as $GITEA_DEV_TOKEN -d "{\"content\":\"$CODE\",\"message\":\"base\",\"branch\":\"main\"}" $API/repos/dev/$REPO/contents/src/Greeter.java >/dev/null
SHA=$(as $GITEA_DEV_TOKEN $API/repos/dev/$REPO/contents/src/Greeter.java | grep -o '"sha":"[0-9a-f]*"' | head -1 | cut -d'"' -f4)
CODE=$(printf 'package shop;\n\npublic class Greeter {\n String password = "hunter2";\n\n boolean isAdmin(String role) {\n return role == "admin";\n }\n}\n' | base64 -w0)
START=$(date +%s)
as $GITEA_DEV_TOKEN -X PUT -d "{\"content\":\"$CODE\",\"sha\":\"$SHA\",\"message\":\"feature\",\"branch\":\"main\",\"new_branch\":\"feature\"}" $API/repos/dev/$REPO/contents/src/Greeter.java >/dev/null
as $GITEA_DEV_TOKEN -d '{"head":"feature","base":"main","title":"Greeter"}' $API/repos/dev/$REPO/pulls >/dev/null
for i in $(seq 1 600); do grep -qE 'reviewed dev/|review of .* failed' target/app.log && break; sleep 1; done
END=$(date +%s)
{
echo "packaged jar, local Gitea 1.27.3, model $LLM_MODEL on a 2 vCPU CPU, one pull request"
echo "seconds from opening the pull request to the finished report: $((END - START))"
grep -E 'reviewed dev/|review of .* failed' target/app.log | sed 's/^.* : //' | sed 's/, /,\n /g'
echo
echo "inline comments read back from Gitea:"
for id in $(as $GITEA_DEV_TOKEN $API/repos/dev/$REPO/pulls/1/reviews | python3 -I -c 'import json,sys; print(*[r["id"] for r in json.load(sys.stdin)])'); do
as $GITEA_DEV_TOKEN $API/repos/dev/$REPO/pulls/1/reviews/$id/comments | python3 -I -c 'import json,sys
for c in json.load(sys.stdin): print(" %s:%d\n%s" % (c["path"], c["position"], "\n".join(" "+l for l in c["body"].splitlines())))'
done
} > output/20-live-demo.txt
cat output/20-live-demo.txt
+8
View File
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
# A local model for the real-model test: no key, no cost, slow on CPU.
set -euo pipefail
docker rm -f pr-reviewer-ollama >/dev/null 2>&1 || true
docker run -d --name pr-reviewer-ollama -p 127.0.0.1:11434:11434 -v pr-reviewer-ollama:/root/.ollama ollama/ollama >/dev/null
sleep 5
docker exec pr-reviewer-ollama ollama pull "${LLM_MODEL:-qwen2.5-coder:1.5b}"
echo "export LLM_URL=http://127.0.0.1:11434/v1 LLM_MODEL=${LLM_MODEL:-qwen2.5-coder:1.5b}"
+12
View File
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
# Regenerates every file in output/. Needs JDK 25 and Docker. Set LLM_URL (an OpenAI-compatible /v1 URL)
# and LLM_MODEL to also run the real-model test; without them output/18 is left as committed.
# output/20 comes from scripts/live-demo.sh, which needs the same two variables.
set -euo pipefail
cd "$(dirname "$0")/../.."
pr-reviewer/scripts/gitea-up.sh
set -a; . pr-reviewer/scripts/.gitea.env; set +a
export GITEA_URL=http://127.0.0.1:3118
mvn -B -q test -pl pr-reviewer -am
pr-reviewer/scripts/webhook-defaults.sh
pr-reviewer/scripts/earlier-bot-check.sh
+25
View File
@@ -0,0 +1,25 @@
#!/usr/bin/env bash
# What a Gitea with default settings does with a webhook to a private address. Writes output/19.
set -euo pipefail
cd "$(dirname "$0")/.."
IMAGE=${GITEA_IMAGE:-gitea/gitea:1.27.3}
NAME=pr-reviewer-gitea-defaults
trap 'docker rm -f $NAME >/dev/null' EXIT
docker run -d --name $NAME -p 127.0.0.1:3121:3000 -e GITEA__security__INSTALL_LOCK=true -e GITEA__database__DB_TYPE=sqlite3 "$IMAGE" >/dev/null
API=http://127.0.0.1:3121/api/v1
until curl -sf --noproxy '*' $API/version >/dev/null; do sleep 2; done
docker exec -u git $NAME gitea admin user create --username dev --password "Local-$RANDOM$RANDOM$RANDOM" --email [email protected] --admin --must-change-password=false >/dev/null
TOKEN=$(docker exec -u git $NAME gitea admin user generate-access-token --username dev --scopes all --token-name t --raw)
HOST=$(docker network inspect bridge -f '{{(index .IPAM.Config 0).Gateway}}')
post() { curl -s -o /dev/null -w '%{http_code}' --noproxy '*' -H "Authorization: token $TOKEN" -H 'Content-Type: application/json' -d "$2" "$API$1"; }
{
echo "create repository : $(post /user/repos '{"name":"x","auto_init":true,"default_branch":"main"}')"
echo "create webhook : $(post /repos/dev/x/hooks "{\"type\":\"gitea\",\"active\":true,\"events\":[\"pull_request\"],\"config\":{\"url\":\"http://$HOST:8118/webhook/gitea\",\"content_type\":\"json\",\"secret\":\"s\"}}")"
echo "push a branch : $(post /repos/dev/x/contents/a.txt '{"content":"YQ==","message":"a","branch":"main","new_branch":"f"}')"
echo "open pull request : $(post /repos/dev/x/pulls '{"head":"f","base":"main","title":"t"}')"
sleep 8
echo
echo "Gitea log:"
docker logs $NAME 2>&1 | grep 'Unable to deliver' | sed 's/^.*handler() \[E\] //' | tail -1
} > output/19-webhook-blocked-by-default.txt
cat output/19-webhook-blocked-by-default.txt