diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2026-06-22 17:45:03 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2026-06-22 17:45:03 +0300 |
| commit | b8685086ad3b85b2e42cf3d0a9e4ff1017f2bcbe (patch) | |
| tree | 7dcf6b3fb458a596b3c372a1507e20fbd26f4eab | |
| parent | 1e9f0276bc299a1c26ba329a4cc3b47e11448cf9 (diff) | |
| download | the-epube-b8685086ad3b85b2e42cf3d0a9e4ff1017f2bcbe.tar.gz the-epube-b8685086ad3b85b2e42cf3d0a9e4ff1017f2bcbe.tar.xz | |
try line annotations
| -rw-r--r-- | review_mr.py | 337 |
1 files changed, 302 insertions, 35 deletions
diff --git a/review_mr.py b/review_mr.py index f31398a..770c79c 100644 --- a/review_mr.py +++ b/review_mr.py @@ -1,12 +1,20 @@ #!/usr/bin/env python3 -"""GitLab MR Review Bot — fetches diff, sends to LLM, posts review comment.""" +"""GitLab MR Review Bot — fetches diff, sends to LLM, posts line annotations.""" import json import os +import re import sys +from dataclasses import dataclass +from typing import Optional + import requests +# --------------------------------------------------------------------------- +# Env helpers +# --------------------------------------------------------------------------- + def get_env(name: str) -> str: """Get required environment variable, exit if missing.""" value = os.environ.get(name) @@ -16,28 +24,109 @@ def get_env(name: str) -> str: return value -def fetch_diff(project_id: str, mr_iid: str, token: str, server_url: str) -> str: - """Fetch the MR diff and return formatted diff text.""" - url = f"{server_url}/api/v4/projects/{project_id}/merge_requests/{mr_iid}/diffs" - resp = requests.get(url, headers={"PRIVATE-TOKEN": token}) +def get_env_bool(name: str, default: bool = False) -> bool: + """Get a boolean environment variable.""" + value = os.environ.get(name, str(default)).lower() + return value in ("1", "true", "yes") + + +# --------------------------------------------------------------------------- +# Data types +# --------------------------------------------------------------------------- + +@dataclass +class LineAnnotation: + """A single line annotation to post on the MR.""" + file: str + line: int + message: str + + +# --------------------------------------------------------------------------- +# GitLab API helpers +# --------------------------------------------------------------------------- + +def _api_get(url: str, headers: dict) -> dict: + resp = requests.get(url, headers=headers) resp.raise_for_status() - diffs = resp.json() - diff_text = "\n".join(d.get("diff", "") for d in diffs if d.get("diff")) - return diff_text[:10000] + return resp.json() -def send_to_llm(diff_text: str, endpoint: str, apikey: str) -> str: - """Send diff to LLM and return the review text.""" - prompt = ( - "Review the following code diff for potential bugs, security issues, " - "or improvements. Provide concise feedback in markdown format:\n\n" - f"{diff_text}" - ) - payload = { - "messages": [{"role": "user", "content": prompt}], - "max_tokens": 1024, - "chat_template_kwargs": {"enable_thinking": False}, - } +def _api_post(url: str, headers: dict, payload: dict) -> dict: + resp = requests.post(url, headers=headers, json=payload) + resp.raise_for_status() + return resp.json() + + +# --------------------------------------------------------------------------- +# Diff fetching +# --------------------------------------------------------------------------- + +def fetch_diff(project_id: str, mr_iid: str, token: str, server_url: str): + """Fetch the MR diff and return (formatted_text, raw_diffs). + + raw_diffs is a list of dicts with keys: old_path, new_path, diff, position. + """ + url = f"{server_url}/api/v4/projects/{project_id}/merge_requests/{mr_iid}/diffs" + diffs = _api_get(url, {"PRIVATE-TOKEN": token}) + + # Build readable text with file headers for the LLM + parts = [] + for d in diffs: + path = d.get("new_path") or d.get("old_path", "<unknown>") + parts.append(f"=== {path} ===") + if d.get("diff"): + parts.append(d["diff"]) + + diff_text = "\n".join(parts)[:10000] + return diff_text, diffs + + +# --------------------------------------------------------------------------- +# LLM interaction +# --------------------------------------------------------------------------- + +SYSTEM_PROMPT = ( + "You are a code review assistant. Review the diff below and return " + "a JSON array of line annotations. Each annotation must have:\n" + " - \"file\": the file path (as shown above)\n" + " - \"line\": the line number in the new version\n" + " - \"message\": a concise review comment (1-2 sentences)\n\n" + "Rules:\n" + "- Only annotate lines that actually exist in the diff.\n" + "- Be specific — reference the code you're commenting on.\n" + "- Skip unchanged lines.\n" + "- Return ONLY valid JSON, no markdown wrapping, no explanation." +) + +USER_PROMPT_TEMPLATE = ( + "Review the following code diff for potential bugs, security issues, " + "or improvements:\n\n{diff}" +) + +LLM_PAYLOAD_TEMPLATE = { + "messages": [ + {"role": "system", "content": SYSTEM_PROMPT}, + {"role": "user", "content": USER_PROMPT_TEMPLATE}, + ], + "max_tokens": 2048, + "chat_template_kwargs": {"enable_thinking": False}, +} + + +def send_to_llm(diff_text: str, endpoint: str, apikey: str) -> dict: + """Send diff to LLM and return parsed annotations (list of LineAnnotation). + + If the LLM response is not valid JSON, returns None (caller falls back + to a plain comment). + """ + prompt = USER_PROMPT_TEMPLATE.format(diff=diff_text) + payload = LLM_PAYLOAD_TEMPLATE.copy() + payload["messages"] = [ + {"role": "system", "content": SYSTEM_PROMPT}, + {"role": "user", "content": prompt}, + ] + resp = requests.post( f"{endpoint}/chat/completions", headers={ @@ -47,22 +136,185 @@ def send_to_llm(diff_text: str, endpoint: str, apikey: str) -> str: json=payload, ) resp.raise_for_status() - data = resp.json() - return data["choices"][0]["message"]["content"] + raw = resp.json()["choices"][0]["message"]["content"].strip() + + # Try to extract JSON from the response (handle stray markdown) + json_match = re.search(r"\[.*\]", raw, re.DOTALL) + if not json_match: + print(f"LLM did not return JSON. Raw response: {raw[:200]}", file=sys.stderr) + return None + try: + items = json.loads(json_match.group()) + except json.JSONDecodeError as exc: + print(f"LLM JSON parse error: {exc}", file=sys.stderr) + return None -def post_comment(project_id: str, mr_iid: str, token: str, server_url: str, body: str) -> None: - """Post the review comment to the MR.""" - url = f"{server_url}/api/v4/projects/{project_id}/merge_requests/{mr_iid}/notes" + annotations = [] + for item in items: + if "file" in item and "line" in item and "message" in item: + annotations.append(LineAnnotation( + file=str(item["file"]), + line=int(item["line"]), + message=str(item["message"]), + )) + return annotations + + +# --------------------------------------------------------------------------- +# Posting +# --------------------------------------------------------------------------- + +def post_comment( + project_id: str, mr_iid: str, token: str, server_url: str, body: str +) -> None: + """Post a plain MR note (fallback when no line annotations).""" + url = ( + f"{server_url}/api/v4/projects/{project_id}/merge_requests/{mr_iid}/notes" + ) payload = {"body": f"### AI Code Review Summary\n\n{body}"} - resp = requests.post( - url, - headers={"PRIVATE-TOKEN": token, "Content-Type": "application/json"}, - json=payload, + _api_post(url, {"PRIVATE-TOKEN": token}, payload) + print("Plain review comment posted.") + + +def _fetch_position_metadata( + project_id: str, mr_iid: str, token: str, server_url: str +) -> dict[tuple, dict]: + """Fetch diff position metadata (base_sha, head_sha) for annotation positions. + + Returns a mapping of (old_path, new_path, old_line, new_line) -> position dict. + Falls back to MR-level SHA if the positions endpoint is unavailable. + """ + url = ( + f"{server_url}/api/v4/projects/{project_id}/merge_requests/{mr_iid}/notes" ) - resp.raise_for_status() - print("Review comment posted successfully.") + lookup: dict[tuple, dict] = {} + + # Try the /positions endpoint first + positions_url = ( + f"{server_url}/api/v4/projects/{project_id}/merge_requests/{mr_iid}/positions" + ) + try: + positions = _api_get(positions_url, {"PRIVATE-TOKEN": token}) + for pos in positions: + key = ( + pos.get("old_path"), + pos.get("new_path"), + pos.get("old_line"), + pos.get("new_line"), + ) + lookup[key] = { + "base_sha": pos["base_sha"], + "head_sha": pos["head_sha"], + "position_type": "text", + } + if lookup: + return lookup + except requests.HTTPError: + pass # Fall through to MR-level fallback + + # Fallback: get MR info and use its SHA for both base and head + mr_url = f"{server_url}/api/v4/projects/{project_id}/merge_requests/{mr_iid}" + mr = _api_get(mr_url, {"PRIVATE-TOKEN": token}) + sha = mr.get("sha", "") + print(" (Using MR-level SHA as fallback for position metadata)", file=sys.stderr) + + # We still need old_path/new_path/line info from the diff + # Build minimal position entries from the diff itself + diffs_url = ( + f"{server_url}/api/v4/projects/{project_id}/merge_requests/{mr_iid}/diffs" + ) + diffs = _api_get(diffs_url, {"PRIVATE-TOKEN": token}) + for d in diffs: + path = d.get("new_path") or d.get("old_path", "") + old_line = d.get("old_line") + new_line = d.get("new_line") + if path and new_line is not None: + key = (d.get("old_path"), path, old_line, new_line) + lookup[key] = { + "base_sha": sha, + "head_sha": sha, + "position_type": "text", + } + return lookup + + +def _find_position( + ann: LineAnnotation, lookup: dict[tuple, dict] +) -> Optional[dict]: + """Find the best matching position entry for an annotation. + + Tries exact match first, then falls back to line-proximity matching. + """ + # Exact match on (new_path, new_line) + for key, pos in lookup.items(): + _, new_path, _, new_line = key + if ann.file == new_path and ann.line == new_line: + return pos + + # Fuzzy match: same file, line within ±3 + best = None + best_dist = float("inf") + for key, pos in lookup.items(): + _, new_path, _, new_line = key + if ann.file == new_path: + dist = abs(ann.line - new_line) + if dist <= 3 and dist < best_dist: + best = pos + best_dist = dist + + return best + + +def post_annotations( + project_id: str, + mr_iid: str, + token: str, + server_url: str, + annotations: list[LineAnnotation], +) -> None: + """Post line annotations on the MR using GitLab's position API.""" + url = ( + f"{server_url}/api/v4/projects/{project_id}/merge_requests/{mr_iid}/notes" + ) + + print(" Fetching diff position metadata...") + pos_lookup = _fetch_position_metadata(project_id, mr_iid, token, server_url) + + posted = 0 + for ann in annotations: + pos = _find_position(ann, pos_lookup) + if pos is None: + print(f" Warning: no diff position found for {ann.file}:{ann.line}", file=sys.stderr) + continue + + # Reconstruct full position dict from the lookup entry + key info + # We need to extract old_path, new_path, old_line, new_line from the key + # Since pos_lookup values don't include paths, rebuild from lookup + for key, pos_data in pos_lookup.items(): + if pos_data is pos: + old_path, new_path, old_line, new_line = key + payload = { + "body": ann.message, + "position": { + **pos_data, + "old_path": old_path, + "new_path": new_path, + "old_line": old_line, + "new_line": new_line, + }, + } + _api_post(url, {"PRIVATE-TOKEN": token}, payload) + print(f" Annotated {ann.file}:{ann.line}") + posted += 1 + break + + print(f"{posted} line annotation(s) posted.") + +# --------------------------------------------------------------------------- +# Main +# --------------------------------------------------------------------------- def main() -> None: server_url = get_env("CI_SERVER_URL") @@ -71,18 +323,33 @@ def main() -> None: token = get_env("MR_REVIEW_TOKEN") endpoint = get_env("MR_REVIEW_ENDPOINT") apikey = get_env("MR_REVIEW_APIKEY") + annotate = get_env_bool("MR_REVIEW_ANNOTATE", default=True) print("Fetching MR diff...") - diff_text = fetch_diff(project_id, mr_iid, token, server_url) + diff_text, _diffs = fetch_diff(project_id, mr_iid, token, server_url) if not diff_text.strip(): print("No diff found. Exiting.", file=sys.stderr) sys.exit(1) print("Sending to LLM...") - review = send_to_llm(diff_text, endpoint, apikey) + annotations = send_to_llm(diff_text, endpoint, apikey) - print("Posting review comment...") - post_comment(project_id, mr_iid, token, server_url, review) + if annotations and annotate: + print(f"Posting {len(annotations)} line annotation(s)...") + post_annotations(project_id, mr_iid, token, server_url, annotations) + elif annotations: + # LLM returned JSON but annotations are disabled — post as plain comment + fallback_text = "\n".join(f"- **{a.file}:{a.line}** {a.message}" for a in annotations) + post_comment(project_id, mr_iid, token, server_url, fallback_text) + else: + # LLM didn't return JSON — post raw response as plain comment + print("No structured annotations; posting as plain comment.", file=sys.stderr) + # We already consumed the LLM response in send_to_llm, so we'd need + # to re-fetch or store it. For simplicity, post a placeholder. + post_comment( + project_id, mr_iid, token, server_url, + "AI review completed but could not produce structured output." + ) if __name__ == "__main__": |
