Skip to content

Commit c6690b2

Browse files
committed
enforce comment when used as direct PR trigger
1 parent cf2ed94 commit c6690b2

5 files changed

Lines changed: 72 additions & 5 deletions

File tree

reviewbot/action_runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def main() -> int:
133133
if req.inline is not None:
134134
run_followup(cfg, gh, req)
135135
else:
136-
run_review(cfg, gh, req)
136+
run_review(cfg, gh, req, force_comment_event=True)
137137
except LLMResponseError as exc:
138138
message = _format_llm_response_error(exc)
139139
log.warning("review failed: %s", message)

reviewbot/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def _review_worker(installation_id: int, req: ReviewRequest) -> None:
5151
if req.inline is not None:
5252
run_followup(cfg, gh, req)
5353
else:
54-
run_review(cfg, gh, req)
54+
run_review(cfg, gh, req, force_comment_event=True)
5555
except Exception:
5656
log.exception(
5757
"review worker crashed for %s/%s#%d", req.owner, req.repo, req.number

reviewbot/reviewer.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,13 @@ def publish_review(
13941394
)
13951395

13961396

1397-
def run_review(cfg: Config, gh: GitHubClient, req: ReviewRequest) -> None:
1397+
def run_review(
1398+
cfg: Config,
1399+
gh: GitHubClient,
1400+
req: ReviewRequest,
1401+
*,
1402+
force_comment_event: bool = False,
1403+
) -> None:
13981404
"""Webhook + Action entry point. Unchanged behavior: prepares the
13991405
review, then immediately publishes it. Renders a fallback issue
14001406
comment if the LLM output is unparseable."""
@@ -1410,7 +1416,8 @@ def run_review(cfg: Config, gh: GitHubClient, req: ReviewRequest) -> None:
14101416
return
14111417
if draft is None:
14121418
return
1413-
publish_review(cfg, gh, draft)
1419+
edits = ReviewEdits(event="COMMENT") if force_comment_event else None
1420+
publish_review(cfg, gh, draft, edits=edits)
14141421

14151422

14161423
_FOLLOWUP_FORCE_FINAL_MESSAGE = (

tests/test_action_runner.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@ def _inline_comment_payload(*, fork: bool = False) -> dict:
4343
return payload
4444

4545

46+
def _issue_comment_payload() -> dict:
47+
return {
48+
"action": "created",
49+
"comment": {
50+
"id": 123,
51+
"body": "@askserge please review",
52+
"author_association": "MEMBER",
53+
"user": {"login": "reviewer"},
54+
},
55+
"issue": {
56+
"number": 13827,
57+
"state": "open",
58+
"pull_request": {
59+
"url": "https://api.github.com/repos/huggingface/diffusers/pulls/13827"
60+
},
61+
},
62+
"repository": {"full_name": "huggingface/diffusers"},
63+
}
64+
65+
4666
class ActionRunnerTests(unittest.TestCase):
4767
def test_empty_llm_api_key_fails_before_review(self) -> None:
4868
event_path = _write_event(_inline_comment_payload())
@@ -121,6 +141,26 @@ def test_llm_api_key_is_stripped_before_review(self) -> None:
121141
cfg = run_followup.call_args.args[0]
122142
self.assertEqual(cfg.llm_api_key, "token-with-newline")
123143

144+
def test_direct_pr_review_forces_comment_event(self) -> None:
145+
event_path = _write_event(_issue_comment_payload())
146+
self.addCleanup(os.remove, event_path)
147+
env = {
148+
"GITHUB_EVENT_NAME": "issue_comment",
149+
"GITHUB_EVENT_PATH": event_path,
150+
"GITHUB_TOKEN": "github-token",
151+
"LLM_API_KEY": "token",
152+
}
153+
154+
with (
155+
patch.dict(os.environ, env, clear=True),
156+
patch("reviewbot.action_runner.run_review") as run_review,
157+
patch("reviewbot.action_runner.GitHubClient"),
158+
):
159+
code = action_runner.main()
160+
161+
self.assertEqual(code, 0)
162+
self.assertTrue(run_review.call_args.kwargs["force_comment_event"])
163+
124164
def test_llm_response_error_is_logged_without_traceback(self) -> None:
125165
event_path = _write_event(_inline_comment_payload())
126166
self.addCleanup(os.remove, event_path)

tests/test_publish_review.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
and edit-application rules."""
44

55
import unittest
6-
from unittest.mock import MagicMock
6+
from unittest.mock import MagicMock, patch
77

88
from reviewbot.config import Config
99
from reviewbot.reviewer import (
1010
DraftComment,
1111
ReviewDraft,
1212
ReviewEdits,
13+
ReviewRequest,
1314
publish_review,
15+
run_review,
1416
)
1517

1618

@@ -174,6 +176,24 @@ def test_empty_summary_renders_fallback(self) -> None:
174176
body = gh.create_review.call_args.kwargs["body"]
175177
self.assertIn("(no overall summary provided)", body)
176178

179+
def test_run_review_can_force_comment_event_for_direct_publish(self) -> None:
180+
cfg = _make_cfg()
181+
draft = _make_draft(event="REQUEST_CHANGES")
182+
gh = MagicMock()
183+
req = ReviewRequest(
184+
owner="acme",
185+
repo="widgets",
186+
number=42,
187+
trigger_comment_id=123,
188+
trigger_comment_body="@askserge please review",
189+
commenter="reviewer",
190+
)
191+
192+
with patch("reviewbot.reviewer.prepare_review", return_value=draft):
193+
run_review(cfg, gh, req, force_comment_event=True)
194+
195+
self.assertEqual(gh.create_review.call_args.kwargs["event"], "COMMENT")
196+
177197

178198
if __name__ == "__main__":
179199
unittest.main()

0 commit comments

Comments
 (0)