about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@fakecake.org>2026-06-22 16:56:31 +0300
committerAndrew Dolgov <fox@fakecake.org>2026-06-22 17:17:28 +0300
commit9fbcbede3f275801d97b5b2852750a72b277b7f3 (patch)
treef01c6ab6206c65a2a26d25e5ce8c1289f282492d
parent17296a2ef8fa2658dca471285bce40ee67469179 (diff)
downloadthe-epube-9fbcbede3f275801d97b5b2852750a72b277b7f3.tar.gz
the-epube-9fbcbede3f275801d97b5b2852750a72b277b7f3.tar.xz
test mr review job
-rw-r--r--.gitlab-ci.yml8
-rw-r--r--review_mr.sh39
2 files changed, 47 insertions, 0 deletions
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 868e54f..8965794 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -188,3 +188,11 @@ deploy-codeberg:
   rules:
     - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CODEBERG_PUSH_TOKEN != null
     - if: $CI_COMMIT_BRANCH == "dev" && $CODEBERG_PUSH_TOKEN != null
+
+mr-review:
+  stage: lint
+  image: ${INFRA_IMAGE}
+  rules:
+    - if: $CI_PIPELINE_SOURCE == "merge_request_event" && $MR_REVIEW_TOKEN != null
+  script:
+    - sh ./review_mr.sh
diff --git a/review_mr.sh b/review_mr.sh
new file mode 100644
index 0000000..4343dae
--- /dev/null
+++ b/review_mr.sh
@@ -0,0 +1,39 @@
+#!/usr/bin/env bash
+#set -e
+
+# 1. Fetch the diff for the current Merge Request
+API_URL="${CI_SERVER_URL}/api/v4/projects/${CI_PROJECT_ID}/merge_requests/${CI_MERGE_REQUEST_IID}/diffs"
+RESPONSE_FILE="diff.json"
+
+curl --header "PRIVATE-TOKEN: ${MR_REVIEW_TOKEN}" "${API_URL}" -o "${RESPONSE_FILE}"
+
+# 2. Extract and format the diff (truncating if it exceeds the model's context)
+# You may need to refine this to loop through files depending on LLM constraints
+DIFF_CONTENT=$(jq -r 'map(.diff) | join("\n")' "${RESPONSE_FILE}" | head -c 10000)
+
+# 3. Send to your OpenAI-compatible endpoint
+PAYLOAD=$(jq -n \
+  --arg prompt "Review the following code diff for potential bugs, security issues, or improvements. Provide concise feedback in markdown format:\n\n${DIFF_CONTENT}" \
+  '{messages: [{role: "user", content: $prompt}], max_tokens: 1024, chat_template_kwargs: {enable_thinking: false }}')
+
+echo $PAYLOAD
+
+LLM_RESPONSE=$(curl -s -X POST "${MR_REVIEW_ENDPOINT}/chat/completions" \
+  -H "Content-Type: application/json" \
+  -H "Authorization: Bearer ${MR_REVIEW_APIKEY}" \
+  -d "$PAYLOAD")
+
+echo ${LLM_RESPONSE}
+
+COMMENT_BODY=$(echo "$LLM_RESPONSE" | jq -r '.choices[0].message.content')
+
+# 4. Post the review comment back to the Merge Request
+COMMENT_URL="${CI_SERVER_URL}/api/v4/projects/${CI_PROJECT_ID}/merge_requests/${CI_MERGE_REQUEST_IID}/notes"
+
+COMMENT_JSON=$(jq -n --arg body "$COMMENT_BODY" '{body: ("### AI Code Review Summary\n\n" + $body)}')
+
+curl --header "PRIVATE-TOKEN: ${MR_REVIEW_TOKEN}" \
+     --header "Content-Type: application/json" \
+     --request POST \
+     --data "$COMMENT_JSON" \
+     "${COMMENT_URL}"