Implementing Line‑Level GitLab Code Review with AI: A Step‑by‑Step Guide

This article walks through using GitLab's Discussions API to write AI‑generated code review comments at specific lines or blocks, covering the required API endpoint, parameter extraction from merge requests, diff parsing with regular expressions, TypeScript implementation, and the end‑to‑end workflow that integrates OpenAI suggestions back into GitLab.

Ubiquitous Tech
Ubiquitous Tech
Ubiquitous Tech
Implementing Line‑Level GitLab Code Review with AI: A Step‑by‑Step Guide

Code review is a critical quality practice in enterprises; the previous chapters used GitLab Commit and Merge APIs to write review results to the overview area. This section introduces the Discussions API, which allows posting comments directly on specific lines or code blocks.

First, locate a Merge Request in GitLab. In the diff view, hovering over a line number reveals a comment icon where manual reviewers can add inline comments. The article shows screenshots of this UI.

The required endpoint is

POST /projects/:id/merge_requests/:merge_request_iid/discussions

. The request body must include the comment text and a position object that specifies the file path, old and new SHA values, and the exact line numbers. Both the Chinese and English GitLab documentation are referenced (images of the docs are included).

To populate the position fields, three SHA values are obtained from the Merge Request changes API, the file path from the same API, and the line numbers are derived from the diff string returned by the API. The article shows the relevant API responses and explains how the diff provides the necessary context.

Two TypeScript interfaces, ILineReviewResult and ILineCodePosition, are defined to model the review result and the code position (image of the interface definitions). A new method is added to gitlab.ts that assembles the request payload and calls the Discussions API.

Parsing line numbers from the diff is the core challenge. A diff consists of one or more @@ -oldStart,oldLen +newStart,newLen @@ blocks. The article explains the meaning of the -, +, and context lines, and how the @@ header indicates the range of changed lines.

The helper function getDiffBlocks(diff: string) splits the full diff into separate blocks using the regular expression (?=@@\s-\d+(?:,\d+)?\s\+\d+(?:,\d+)?\s@@). The regex uses a positive look‑ahead to keep the @@ header in each block. The article includes the full source of this function inside a

<code> block.</p>
<p>Another helper, <code>getLineObj(matches: RegExpMatchArray, item: string)</code>, calculates the concrete line numbers. It extracts the start and end line numbers for both old and new versions, examines the last line of the diff chunk to see whether it begins with <code>+</code> (added), <code>-</code> (deleted), or neither, and then computes <code>new_line</code> and/or <code>old_line</code> accordingly. The complete implementation is shown in a code block.

The main workflow in index.ts iterates over the merge request data and the list of changed files. It skips renamed or deleted files and those without a diff block. For each diff block, it extracts the line range, calls OpenAI.executeCodeReview to obtain AI‑generated suggestions, builds an ILineReviewResult object with the comment text, file path, and line numbers, and finally posts the comment via the Discussions API.

Result screenshots demonstrate that the AI suggestions appear as inline comments at the correct lines (e.g., line 60 in DoubleRange and another block later in the same file). The article discusses the advantages—fine‑grained feedback and clear location of issues—and the drawbacks, such as increased review time for files with many small changes.

In summary, the article provides a complete, reproducible implementation for line‑level AI code review in GitLab, from extracting diff information to posting contextual comments, and highlights practical considerations for real‑world adoption.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

TypeScriptAICode ReviewGitLabAPIdiff parsing
Ubiquitous Tech
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.