diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2026-06-22 20:48:37 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2026-06-22 20:48:37 +0300 |
| commit | 75fa3b03218bdbeba7ba9bcad82b15b95a3b9468 (patch) | |
| tree | 7dd2a97b3b51cd98f411a165222904368aa7088e | |
| parent | fc79e6bcb1f851d532b09ba3b4fdde4bddeb2305 (diff) | |
| download | the-epube-protected/mr-review-test.tar.gz the-epube-protected/mr-review-test.tar.xz | |
shared repo protected/mr-review-test
| -rw-r--r-- | .gitlab-ci.yml | 4 | ||||
| -rw-r--r-- | review_mr.py | 530 |
2 files changed, 3 insertions, 531 deletions
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index cf83e28..4a812c0 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -195,4 +195,6 @@ mr-review: rules: - if: $CI_PIPELINE_SOURCE == "merge_request_event" && $MR_REVIEW_TOKEN != null script: - - python3 ./review_mr.py + - git config --global --add url."https://gitlab-ci-token:${CI_JOB_TOKEN}@${CI_SERVER_HOST}/".insteadOf "git@${CI_SERVER_HOST}:" + - git clone git@${CI_SERVER_HOST}:ci/mr-review.git mr-review + - python3 ./mr-review/mr-review.py diff --git a/review_mr.py b/review_mr.py deleted file mode 100644 index a082ec6..0000000 --- a/review_mr.py +++ /dev/null @@ -1,530 +0,0 @@ -#!/usr/bin/env python3 -"""GitLab MR Review Bot — fetches diff, sends to LLM, posts line annotations.""" - -import json -import os -import sys -from dataclasses import dataclass -from typing import Optional - -import requests - -TIMEOUT = 30 # seconds for all HTTP requests - - -# --------------------------------------------------------------------------- -# Env helpers -# --------------------------------------------------------------------------- - -def get_env(name: str) -> str: - """Get required environment variable, exit if missing.""" - value = os.environ.get(name) - if not value: - print(f"Error: {name} is not set", file=sys.stderr) - sys.exit(1) - return value - - -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") - - -def _dbg(msg: str) -> None: - """Print debug message if MR_REVIEW_DEBUG is set.""" - if get_env_bool("MR_REVIEW_DEBUG", default=False): - print(msg, file=sys.stderr) - - -# --------------------------------------------------------------------------- -# 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, timeout=TIMEOUT) - resp.raise_for_status() - return resp.json() - - -def _api_post(url: str, headers: dict, payload: dict) -> dict: - resp = requests.post(url, headers=headers, json=payload, timeout=TIMEOUT) - resp.raise_for_status() - return resp.json() - - -# --------------------------------------------------------------------------- -# Diff fetching -# --------------------------------------------------------------------------- - -def _format_diff_with_line_numbers(diff_text: str) -> str: - """Format a unified diff string with absolute line numbers. - - Parses @@ hunk headers to compute line numbers, then prepends them. - Only lines that exist in the new file (additions and context) get - line numbers; removal lines are left without a number. - - Example output: - === src/App.php === - @@ -10,7 +10,7 @@ - - return $user->name; - 10 + return $user->name ?? ""; - """ - import re as _re - - lines = diff_text.split("\n") - hunk_re = _re.compile(r"^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@") - - output = [] - new_line = None # current line number in the new file - - for line in lines: - m = hunk_re.match(line) - if m: - new_line = int(m.group(1)) - output.append(line) # keep hunk header as-is - continue - - if new_line is None: - output.append(line) - continue - - if line.startswith("+"): - output.append(f"{new_line:>5} {line}") - new_line += 1 - elif line.startswith(" "): - output.append(f"{new_line:>5}{line}") - new_line += 1 - elif line.startswith("-"): - # Removal — no line number in the new file - output.append(f"{'':>5} {line}") - else: - # Other lines (e.g., index, new file, etc.) - output.append(line) - - return "\n".join(output) - - -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. - The formatted text includes file headers and line numbers for each diff hunk. - """ - 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 and line numbers 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(_format_diff_with_line_numbers(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 object with an \"annotations\" key containing an array of " - "line annotations.\n\n" - "The diff is formatted with absolute line numbers in the new file " - "padded to 5 characters on the left. Only lines with a line number " - "are valid annotation targets.\n\n" - "JSON Schema:\n" - "```json\n" - "{\n" - ' "type": "object",\n' - ' "properties": {\n' - ' "annotations": {\n' - ' "type": "array",\n' - ' "items": {\n' - ' "type": "object",\n' - ' "properties": {\n' - ' "file": {"type": "string"},\n' - ' "line": {"type": "integer"},\n' - ' "message": {"type": "string"}\n' - ' },\n' - ' "required": ["file", "line", "message"]\n' - ' }\n' - ' }\n' - ' },\n' - ' "required": ["annotations"],\n' - ' "additionalProperties": false\n' - '}\n' - "```\n\n" - "Focus ONLY on significant issues. Skip style preferences, naming, " - "formatting, and minor suggestions. Comment on:\n" - "- Security vulnerabilities (XSS, SQL injection, auth bypass, etc.)\n" - "- Logic bugs, race conditions, off-by-one errors\n" - "- Performance problems (N+1 queries, unnecessary loops, etc.)\n" - "- Missing error handling or input validation\n" - "- Violations of established patterns or conventions in this codebase\n" - "- Incorrect API usage or deprecated methods\n\n" - "Do NOT comment on:\n" - "- Variable naming conventions\n" - "- Code formatting or indentation\n" - "- Minor refactoring suggestions\n" - "- Style preferences\n\n" - "Rules:\n" - "- Use the line numbers shown in the diff output (the 5-char number on the left).\n" - "- Only annotate lines that have a line number prefix.\n" - "- Be specific — reference the code you're commenting on.\n" - "- Skip unchanged lines.\n" - "- Return ONLY the JSON object, no markdown wrapping, no explanation." -) - -ANNOTATIONS_SCHEMA = { - "type": "object", - "properties": { - "annotations": { - "type": "array", - "items": { - "type": "object", - "properties": { - "file": {"type": "string"}, - "line": {"type": "integer"}, - "message": {"type": "string"}, - }, - "required": ["file", "line", "message"], - "additionalProperties": False, - }, - }, - }, - "required": ["annotations"], - "additionalProperties": False, -} - - -def _build_llm_payload(diff_text: str) -> dict: - """Build the LLM request payload with JSON schema enforcement.""" - prompt = ( - "Review the following code diff for potential bugs, security issues, " - "or improvements:\n\n" - f"{diff_text}" - ) - return { - "messages": [ - {"role": "system", "content": SYSTEM_PROMPT}, - {"role": "user", "content": prompt}, - ], - "max_tokens": 2048, - "response_format": { - "type": "json_schema", - "json_schema": { - "name": "code_review_annotations", - "schema": ANNOTATIONS_SCHEMA, - }, - }, - "chat_template_kwargs": {"enable_thinking": False}, - } - - -def send_to_llm(diff_text: str, endpoint: str, apikey: str) -> list[LineAnnotation] | None: - """Send diff to LLM and return parsed annotations. - - Uses JSON schema enforcement so the LLM is constrained to return - {"annotations": [{"file": ..., "line": ..., "message": ...}, ...]}. - Falls back to None on any parse error. - """ - payload = _build_llm_payload(diff_text) - - resp = requests.post( - f"{endpoint}/chat/completions", - headers={ - "Content-Type": "application/json", - "Authorization": f"Bearer {apikey}", - }, - json=payload, - timeout=TIMEOUT, - ) - resp.raise_for_status() - raw = resp.json()["choices"][0]["message"]["content"].strip() - - # The LLM should return {"annotations": [...]} - try: - data = json.loads(raw) - except json.JSONDecodeError as exc: - print(f"LLM JSON parse error: {exc}", file=sys.stderr) - print(f" Raw response: {raw[:300]}", file=sys.stderr) - return None - - if not isinstance(data, dict) or "annotations" not in data: - print(f"LLM response missing 'annotations' key. Raw: {raw[:200]}", file=sys.stderr) - return None - - annotations = [] - for item in data["annotations"]: - if not isinstance(item, dict): - continue - try: - annotations.append(LineAnnotation( - file=str(item["file"]), - line=int(item["line"]), - message=str(item["message"]), - )) - except (KeyError, ValueError, TypeError): - continue - - return annotations if annotations else None - - -# --------------------------------------------------------------------------- -# 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}/discussions" - ) - payload = {"body": f"### AI Code Review Summary\n\n{body}"} - _api_post(url, {"PRIVATE-TOKEN": token}, payload) - print("Plain review comment posted.") - - -def _build_positions_from_diff( - diffs: list[dict], base_sha: str, head_sha: str, start_sha: str -) -> dict[tuple, dict]: - """Build position lookup from diff entries by parsing hunk headers. - - Returns a mapping of (old_path, new_path, old_line, new_line) -> position dict. - Each position includes base_sha, head_sha, and start_sha. - """ - import re as _re - - hunk_re = _re.compile(r"^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@") - lookup: dict[tuple, dict] = {} - base = {"base_sha": base_sha, "head_sha": head_sha, "start_sha": start_sha, "position_type": "text"} - - for d in diffs: - old_path = d.get("old_path", "") - new_path = d.get("new_path", old_path) - diff_text = d.get("diff", "") - if not diff_text: - continue - - new_line = None - for line in diff_text.split("\n"): - m = hunk_re.match(line) - if m: - new_line = int(m.group(1)) - continue - if new_line is None: - continue - if line.startswith("+"): - key = (old_path, new_path, None, new_line) - lookup[key] = {**base, "old_line": None, "new_line": new_line} - new_line += 1 - elif line.startswith(" "): - key = (old_path, new_path, new_line, new_line) - lookup[key] = {**base, "old_line": new_line, "new_line": new_line} - new_line += 1 - # removal lines (-) have no new_line, skip - - return lookup - - -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. - """ - 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: - _dbg(f" (Using /positions endpoint: {len(lookup)} entries)") - return lookup - except requests.HTTPError as exc: - _dbg(f" (positions endpoint failed: {exc.response.status_code} {exc.response.text[:200]})") - - # Fallback: get MR info and parse diff text for line numbers - mr_url = f"{server_url}/api/v4/projects/{project_id}/merge_requests/{mr_iid}" - mr = _api_get(mr_url, {"PRIVATE-TOKEN": token}) - diff_refs = mr.get("diff_refs", {}) - base_sha = diff_refs.get("base_sha", mr.get("sha", "")) - head_sha = diff_refs.get("head_sha", mr.get("sha", "")) - start_sha = diff_refs.get("start_sha", base_sha) - _dbg(f" (Fallback: base={base_sha[:8]}... head={head_sha[:8]}... start={start_sha[:8]}...)") - - diffs_url = ( - f"{server_url}/api/v4/projects/{project_id}/merge_requests/{mr_iid}/diffs" - ) - diffs = _api_get(diffs_url, {"PRIVATE-TOKEN": token}) - lookup = _build_positions_from_diff(diffs, base_sha, head_sha, start_sha) - 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 Discussions API.""" - url = ( - f"{server_url}/api/v4/projects/{project_id}/merge_requests/{mr_iid}/discussions" - ) - - _dbg(" Fetching diff position metadata...") - pos_lookup = _fetch_position_metadata(project_id, mr_iid, token, server_url) - _dbg(f" Position entries: {len(pos_lookup)}") - # Show sample positions - for i, (key, val) in enumerate(list(pos_lookup.items())[:3]): - _dbg(f" Sample: {key} -> {val}") - - 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 - for key, pos_data in pos_lookup.items(): - if pos_data is pos: - old_path, new_path, old_line, new_line = key - position = { - **pos_data, - "old_path": old_path, - "new_path": new_path, - "old_line": old_line, - "new_line": new_line, - } - payload = { - "body": ann.message, - "position": position, - } - _dbg(f" Posting: file={ann.file} line={ann.line}") - _dbg(f" Payload: {json.dumps(payload, indent=2)}") - result = _api_post(url, {"PRIVATE-TOKEN": token}, payload) - # Check if the comment was created with a position - # Discussions API returns {notes: [{position: ..., body: ...}]} - notes = result.get("notes", []) - if notes: - has_position = notes[0].get("position") is not None - is_system = notes[0].get("system") is True - else: - has_position = result.get("position") is not None - is_system = result.get("system") is True - if not has_position or is_system: - _dbg(f" WARNING: inline annotation NOT created " - f"(has_position={has_position}, system={is_system}). " - f"Response keys: {list(result.keys())}") - 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") - project_id = get_env("CI_PROJECT_ID") - mr_iid = get_env("CI_MERGE_REQUEST_IID") - 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, _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...") - annotations = send_to_llm(diff_text, endpoint, apikey) - - 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__": - main() |
