diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2026-06-22 20:24:00 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2026-06-22 20:24:00 +0300 |
| commit | fc79e6bcb1f851d532b09ba3b4fdde4bddeb2305 (patch) | |
| tree | a3e7d9b1c276a7f293e3fadf7b2ac0fbf6d49098 | |
| parent | fde5709c464b3a754b69a778c859c4a48700ce65 (diff) | |
| download | the-epube-fc79e6bcb1f851d532b09ba3b4fdde4bddeb2305.tar.gz the-epube-fc79e6bcb1f851d532b09ba3b4fdde4bddeb2305.tar.xz | |
more stuff
| -rw-r--r-- | review_mr.py | 47 |
1 files changed, 34 insertions, 13 deletions
diff --git a/review_mr.py b/review_mr.py index 457497b..a082ec6 100644 --- a/review_mr.py +++ b/review_mr.py @@ -9,6 +9,8 @@ from typing import Optional import requests +TIMEOUT = 30 # seconds for all HTTP requests + # --------------------------------------------------------------------------- # Env helpers @@ -29,6 +31,12 @@ def get_env_bool(name: str, default: bool = False) -> bool: 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 # --------------------------------------------------------------------------- @@ -46,13 +54,13 @@ class LineAnnotation: # --------------------------------------------------------------------------- def _api_get(url: str, headers: dict) -> dict: - resp = requests.get(url, headers=headers) + 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) + resp = requests.post(url, headers=headers, json=payload, timeout=TIMEOUT) resp.raise_for_status() return resp.json() @@ -163,6 +171,19 @@ SYSTEM_PROMPT = ( ' "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" @@ -233,6 +254,7 @@ def send_to_llm(diff_text: str, endpoint: str, apikey: str) -> list[LineAnnotati "Authorization": f"Bearer {apikey}", }, json=payload, + timeout=TIMEOUT, ) resp.raise_for_status() raw = resp.json()["choices"][0]["message"]["content"].strip() @@ -351,10 +373,10 @@ def _fetch_position_metadata( "position_type": "text", } if lookup: - print(f" (Using /positions endpoint: {len(lookup)} entries)", file=sys.stderr) + _dbg(f" (Using /positions endpoint: {len(lookup)} entries)") return lookup except requests.HTTPError as exc: - print(f" (positions endpoint failed: {exc.response.status_code} {exc.response.text[:200]})", file=sys.stderr) + _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}" @@ -363,7 +385,7 @@ def _fetch_position_metadata( 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) - print(f" (Fallback: base={base_sha[:8]}... head={head_sha[:8]}... start={start_sha[:8]}...)", file=sys.stderr) + _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" @@ -412,12 +434,12 @@ def post_annotations( f"{server_url}/api/v4/projects/{project_id}/merge_requests/{mr_iid}/discussions" ) - print(" Fetching diff position metadata...") + _dbg(" Fetching diff position metadata...") pos_lookup = _fetch_position_metadata(project_id, mr_iid, token, server_url) - print(f" Position entries: {len(pos_lookup)}", file=sys.stderr) + _dbg(f" Position entries: {len(pos_lookup)}") # Show sample positions for i, (key, val) in enumerate(list(pos_lookup.items())[:3]): - print(f" Sample: {key} -> {val}", file=sys.stderr) + _dbg(f" Sample: {key} -> {val}") posted = 0 for ann in annotations: @@ -441,8 +463,8 @@ def post_annotations( "body": ann.message, "position": position, } - print(f" Posting: file={ann.file} line={ann.line}", file=sys.stderr) - print(f" Payload: {json.dumps(payload, indent=2)}", file=sys.stderr) + _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: ...}]} @@ -454,10 +476,9 @@ def post_annotations( has_position = result.get("position") is not None is_system = result.get("system") is True if not has_position or is_system: - print(f" WARNING: inline annotation NOT created " + _dbg(f" WARNING: inline annotation NOT created " f"(has_position={has_position}, system={is_system}). " - f"Response keys: {list(result.keys())}", - file=sys.stderr) + f"Response keys: {list(result.keys())}") print(f" Annotated {ann.file}:{ann.line}") posted += 1 break |
