Files
PX4-Autopilot/Tools/ci/ai_review/test_trigger.py
T
Ramon Roche a9a86dad76 feat(ci): show the AI review among the PR's checks
Comment-triggered runs belong to main, so GitHub never lists them in the
PR's checks and the only sign of a running review is a reaction on the
comment. Set an informational "AI PR Review" commit status on the PR
head, linking to the run: pending when the gate accepts, then success or
error. A run cancelled by a newer review of the same commit leaves the
newer run's status alone.

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

157 lines
6.2 KiB
Python

"""Only people with write access may start a review from a comment, a
malformed command must never start a default review by accident, and a
cancelled run must not overwrite the status of the review that replaced
it."""
import os
import tempfile
import unittest
from typing import Any, Dict, List, Optional, Tuple
from ai_review import trigger
ALIASES = {'opus': None, 'sonnet': None}
def event(body: str = '!ai-review', login: str = 'alice',
user_type: str = 'User', on_pr: bool = True,
state: str = 'open') -> Dict[str, Any]:
issue: Dict[str, Any] = {'number': 42, 'state': state}
if on_pr:
issue['pull_request'] = {'url': 'x'}
return {'issue': issue,
'comment': {'id': 7, 'body': body,
'user': {'login': login, 'type': user_type}}}
class FakeApi:
def __init__(self, permission: Optional[str],
statuses: Optional[List[Dict[str, Any]]] = None) -> None:
self.permission = permission
self.statuses = statuses or []
self.calls: List[Tuple[str, str]] = []
self.posted: List[Dict[str, Any]] = []
def __call__(self, method: str, path: str,
body: Optional[Dict[str, Any]]) -> Any:
self.calls.append((method, path))
if path.endswith('/permission'):
return {'permission': self.permission}
if '/pulls/' in path:
return {'head': {'sha': 'abc123'}}
if method == 'GET' and path.endswith('/statuses'):
return self.statuses
self.posted.append(body or {})
return None
def decide(ev: Dict[str, Any], permission: Optional[str] = 'write',
name: str = 'issue_comment') -> trigger.Decision:
return trigger.decide(name, ev, {}, ALIASES, FakeApi(permission),
'PX4/PX4-Autopilot')
class TestParse(unittest.TestCase):
def test_plain_and_with_arguments(self) -> None:
self.assertEqual(trigger.parse_command('!ai-review', ALIASES),
{'model': 'default', 'effort': 'default'})
self.assertEqual(
trigger.parse_command(' !ai-review model=sonnet effort=high\n',
ALIASES),
{'model': 'sonnet', 'effort': 'high'})
def test_command_must_be_the_whole_comment(self) -> None:
for body in ('LGTM\n!ai-review', '!ai-review\nthanks',
'> !ai-review\n!ai-review', 'Looks good. !ai-review'):
with self.subTest(body=body):
self.assertIsNone(trigger.parse_command(body, ALIASES))
def test_not_a_command(self) -> None:
# only the bang form is the command
for body in ('please run !ai-review', '!ai-reviewer', '',
'!ai-review model=fable', '!ai-review effort=ultra',
'!ai-review now', '`!ai-review`', '#ai-review',
'#!ai-review', '/ai-review', '! ai-review'):
with self.subTest(body=body):
self.assertIsNone(trigger.parse_command(body, ALIASES))
class TestDecide(unittest.TestCase):
def test_write_access_runs(self) -> None:
for level in ('write', 'maintain', 'admin'):
with self.subTest(level=level):
d = decide(event('!ai-review model=sonnet'), level)
self.assertTrue(d.run)
self.assertEqual((d.pr, d.model, d.comment_id),
(42, 'sonnet', 7))
def test_without_write_access_does_not_run(self) -> None:
for level in ('read', 'triage', 'none', None):
with self.subTest(level=level):
d = decide(event(), level)
self.assertFalse(d.run)
self.assertIn('write access is required', d.reason)
def test_permission_is_checked_through_the_api(self) -> None:
api = FakeApi('write')
trigger.decide('issue_comment', event(login='bob'), {}, ALIASES, api,
'PX4/PX4-Autopilot')
self.assertEqual(api.calls[0], (
'GET', 'repos/PX4/PX4-Autopilot/collaborators/bob/permission'))
def test_accepted_run_resolves_the_head_commit(self) -> None:
# the status must land on the commit being reviewed
self.assertEqual(decide(event()).head_sha, 'abc123')
def test_ignored_events(self) -> None:
cases = [(event(on_pr=False), 'not on a pull request'),
(event(state='closed'), 'not open'),
(event(body='nice work'), 'no !ai-review command'),
(event(user_type='Bot'), 'bot')]
for ev, reason in cases:
with self.subTest(reason=reason):
d = decide(ev)
self.assertFalse(d.run)
self.assertIn(reason, d.reason)
def test_dispatch_uses_inputs(self) -> None:
d = trigger.decide('workflow_dispatch', {},
{'pr_number': '28817', 'model': 'opus',
'effort': 'xhigh'}, ALIASES, FakeApi(None), 'r')
self.assertTrue(d.run)
self.assertEqual((d.pr, d.model, d.effort, d.comment_id),
(28817, 'opus', 'xhigh', 0))
def test_outputs_written_for_github(self) -> None:
with tempfile.TemporaryDirectory() as tmp:
path = os.path.join(tmp, 'out')
trigger.write_outputs(decide(event()), path)
text = open(path).read()
self.assertIn('run=true\n', text)
self.assertIn('pr=42\n', text)
class TestStatus(unittest.TestCase):
RUN = 'https://github.com/r/actions/runs/1'
def test_final_status_replaces_this_runs_pending(self) -> None:
api = FakeApi(None, [{'context': trigger.STATUS_CONTEXT,
'target_url': self.RUN}])
trigger.set_status(api, 'r', 'abc123', 'success', 'done', self.RUN,
only_if_ours=True)
self.assertEqual(api.posted[0]['state'], 'success')
def test_cancelled_run_leaves_a_newer_review_alone(self) -> None:
api = FakeApi(None, [{'context': trigger.STATUS_CONTEXT,
'target_url': 'https://github.com/r/runs/2'}])
trigger.set_status(api, 'r', 'abc123', 'error', 'cancelled',
self.RUN, only_if_ours=True)
self.assertEqual(api.posted, [])
if __name__ == '__main__':
unittest.main()