How to Implement AI-Powered Code Review for GitLab Merge Request Changes
This tutorial shows how to modify a GitLab CI pipeline to fetch changed files via the Merge Request API, extract the diff with jq, build a JSON request for the glm‑4‑flash model, and use AI to review code changes, including prompt engineering and result interpretation.
In the previous tutorial we built a basic AI code‑review pipeline that sent the whole file content to an LLM. This part upgrades the pipeline to send only the changed portions of a Merge Request, allowing the AI to focus on the new code.
The GitLab Merge Request API provides a /changes endpoint, e.g. http://ip:port/api/v4/projects/1/merge_requests/1/changes, where the first 1 is the project ID and the second is the MR ID. The response contains a changes array, each element holding new_path, old_path and a diff string.
The diff follows the standard Git hunk format. For example, the header @@ -81,4 +81,12 @@ means the original file had four lines starting at line 81, which were replaced by twelve lines after the change. The diff can be handed to an LLM for explanation.
We modify the CI script to retrieve the change list, iterate over each file, and construct a JSON payload for the glm‑4‑flash model. The core snippet is:
# Get the array of changed files
MR_CHANGES=`curl -H "PRIVATE-TOKEN:$GITLAB_TOKEN" -X GET $GITLAB_API/projects/$CI_PROJECT_ID/merge_requests/$MR_ID/changes | jq -r '.changes[]'`
while read -r CHANGE; do
FILE_PATH=$(echo "$CHANGE" | jq -r '.new_path')
DIFF=$(echo "$CHANGE" | jq -r '.diff')
FILE_CONTENT=`curl -H "PRIVATE-TOKEN:$GITLAB_TOKEN" -X GET "$GITLAB_API/projects/$CI_PROJECT_ID/repository/files/$(echo $FILE_PATH | sed 's/\//%2F/g' | sed 's/\./%2E/g')/raw?ref=$COMMIT_SHA"`
JSON_PAYLOAD=$(jq -n \
--arg file_path "$FILE_PATH" \
--arg file_content "$FILE_CONTENT" \
--arg diff "$DIFF" \
'{ model:"glm-4-flash", messages: [ { role: "system", content: "You are a senior code reviewer, able to spot logical errors and potential issues. The user will provide file name, full content, and a Git Diff string." }, { role: "user", content: ("Please review the following file:
File name: " + $file_path + "
File content:
" + $file_content + "
Diff:
" + $diff) } ] }')
# send $JSON_PAYLOAD to the model (omitted for brevity)
doneAfter committing the updated CI file, the pipeline logs show the AI request being sent successfully, and the MR receives a comment containing the model’s review. Changing the system prompt from a generic reviewer to a “senior code reviewer” shifts the AI’s focus toward the diff, as demonstrated by the different comment contents.
Because the script relies heavily on jq, the tutorial also includes a brief jq reference: reading a JSON file, extracting a field ( jq .name data.json), filtering an array ( jq '.[] | select(.age > 30)' data.json), formatting output with -r, and modifying values ( jq '.city = "Narnia"' data.json). These examples illustrate how jq can parse the API responses and prepare the data for the AI request.
In summary, the article demonstrates a complete end‑to‑end workflow: fetch MR changes via the GitLab API, parse them with jq, build a context‑rich prompt for an LLM, and obtain AI‑driven code‑review feedback that concentrates on the actual modifications.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ubiquitous Tech
A ubiquitous public account for pirate enthusiasts, regularly sharing curated experiences, tech learning, and growth insights. Currently publishing articles on AI RAG customer service, AI MCP technology, and open-source design. Personal free Knowledge Planet: Awakening New World Programmer.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
