Files
PX4-Autopilot/Tools/ci/ai_review/diff.py
T
Ramon Roche 95e2802baf feat(ci): add the AI pull request review tool
Tools/ci/ai_review reviews a PR with Claude Code on Amazon Bedrock and
writes a pr-review artifact for pr-review-poster.py.

- Pipeline: gather (metadata, linked issues, commits, CI status, per-file
  patches, earlier comments), a reviewer pass returning line findings,
  PR-level findings and a contribution checklist, an independent
  validator pass per finding, and code-owned routing: inline only at high
  confidence, otherwise a before-merge list or a collapsed section. No PR
  is skipped for size; generated files are listed but not diffed.
- Trust: the PR is untrusted data. The agent runs --bare --restricted
  with read-only tools. Code it writes to check its math runs only in a
  px4-dev container (no network, no credentials, read-only checkout,
  gVisor in CI), and `harden` blocks the runner's metadata service first.
  Output containing a credential is never written.
- A model refusal produces no review and passes the provider's exact
  reason, category and request ID through.
- Models and per-role defaults live in models.json; models-check verifies
  access before switching.
- Process checks (tests, description, upgrade notes, docs) live in a
  CI-only prompt so the interactive skill stays lean.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Ramon Roche <mrpollo@gmail.com>
2026-10-02 15:50:14 -07:00

70 lines
2.2 KiB
Python

"""Unified diff parsing: which new-side lines a review comment may target.
GitHub only accepts review comments on lines that appear in the PR diff.
For the new (RIGHT) side that is every added or context line inside a
hunk.
"""
import re
from dataclasses import dataclass, field
from typing import Dict, Set
HUNK_RE = re.compile(r'^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@')
@dataclass
class DiffMap:
# path -> new-side line numbers that are commentable
commentable: Dict[str, Set[int]] = field(default_factory=dict)
# path -> new-side line numbers the PR added or changed
added: Dict[str, Set[int]] = field(default_factory=dict)
changed_lines: int = 0
def can_comment(self, path: str, first: int, last: int) -> bool:
lines = self.commentable.get(path, set())
return all(n in lines for n in range(first, last + 1))
def parse(diff_text: str) -> DiffMap:
result = DiffMap()
path = None
in_hunk = False
new_line = 0
for raw in diff_text.splitlines():
if raw.startswith('diff --git '):
path, in_hunk = None, False
continue
if not in_hunk and raw.startswith('+++ '):
target = raw[4:].strip()
if target == '/dev/null':
path = None
else:
path = target[2:] if target.startswith('b/') else target
result.commentable.setdefault(path, set())
result.added.setdefault(path, set())
continue
if not in_hunk and raw.startswith('--- '):
continue
match = HUNK_RE.match(raw)
if match:
new_line = int(match.group(1))
in_hunk = True
continue
if not in_hunk:
continue
if raw.startswith('-'):
result.changed_lines += 1
continue
if path is None:
continue
if raw.startswith('+'):
result.commentable[path].add(new_line)
result.added[path].add(new_line)
result.changed_lines += 1
new_line += 1
elif raw.startswith(' ') or raw == '':
result.commentable[path].add(new_line)
new_line += 1
# "\ No newline at end of file" and other markers carry no line
return result