Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions scripts/pr_review_hold.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
runs_open,
utc_instant,
)
from pr_review_rounds import READS_AS_REVIEWER, TRUSTED_ASSOCIATIONS
from pr_review_payload import trusted_comment_nodes
from pr_review_rounds import READS_AS_REVIEWER


def side_command(name: str, command: str = "review") -> str:
Expand All @@ -38,14 +39,7 @@ def side_command(name: str, command: str = "review") -> str:

def side_asks(pr: dict, body: str) -> list[dict]:
"""Each trusted, shown comment whose whole body is the request `body`, oldest first."""
nodes = (pr.get("comments") or {}).get("nodes") or []
asks = [
n
for n in nodes
if (n.get("authorAssociation") or "") in TRUSTED_ASSOCIATIONS
and not n.get("isMinimized")
and (n.get("body") or "").strip() == body
]
asks = [n for n in trusted_comment_nodes(pr) if (n.get("body") or "").strip() == body]
return sorted(asks, key=lambda n: n.get("createdAt") or "")


Expand Down
15 changes: 15 additions & 0 deletions scripts/pr_review_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
`gh`. So the key and the two reads every consumer of it repeats live here instead. The writer
and the readers name this one constant, so they cannot disagree on it. The two pagers take the
walk as a callable for the same reason, so nothing here makes a call.

The one test for a conversation comment the gate trusts lives here too, as a read off the payload
that needs nothing else. `pr_review_rounds` and `pr_review_hold` both build their comment readers
on it, so the two cannot disagree on whose comment counts.
"""

from __future__ import annotations
Expand All @@ -15,6 +19,17 @@
POLICY_KEY = "_reviewPolicy"
FILES_UNREAD = "unread"
FILES_CAP = 3000
TRUSTED_ASSOCIATIONS = frozenset({"OWNER", "MEMBER", "COLLABORATOR"})


def trusted_comment_nodes(pr: dict) -> list[dict]:
"""Each conversation comment an owner, member, or collaborator wrote and nobody hid, as read."""
return [
node
for node in (pr.get("comments") or {}).get("nodes") or []
if (node.get("authorAssociation") or "") in TRUSTED_ASSOCIATIONS
and not node.get("isMinimized")
]


def default_branch(pr: dict) -> str:
Expand Down
12 changes: 4 additions & 8 deletions scripts/pr_review_rounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import pr_review_copilot as adapter
from pr_review_copilot import first_comment
from pr_review_payload import FILES_UNREAD, bot_entry, default_branch
from pr_review_payload import FILES_UNREAD, bot_entry, default_branch, trusted_comment_nodes

# Read from the vocabulary, so a login rename is one edit that the reader and the queries share.
REVIEWER = adapter.VOCABULARIES["copilot"]["login"]
Expand All @@ -30,7 +30,6 @@
r"^<!-- fleet-local-review: head=([0-9a-f]{40}) base=(\S+) findings=(\d+|unknown) -->$",
re.MULTILINE,
)
TRUSTED_ASSOCIATIONS = frozenset({"OWNER", "MEMBER", "COLLABORATOR"})
ACK_MARKER = "<!-- fleet-shape-ack: fingerprint={} -->"
CARRIED_PARTIAL = "an earlier round states partial coverage"

Expand Down Expand Up @@ -374,12 +373,9 @@ def answering_comments(pr: dict) -> Iterator[list[str]]:

def trusted_comments(pr: dict) -> Iterator[tuple[str, list[str]]]:
"""Each owner's, member's, or collaborator's shown comment body, with its HTML lines outside code."""
for node in (pr.get("comments") or {}).get("nodes") or []:
if (node.get("authorAssociation") or "") in TRUSTED_ASSOCIATIONS and not node.get(
"isMinimized"
):
body = node.get("body") or ""
yield body, adapter.html_lines(body)
for node in trusted_comment_nodes(pr):
body = node.get("body") or ""
yield body, adapter.html_lines(body)


def partial_on_record(pr: dict) -> bool:
Expand Down
22 changes: 22 additions & 0 deletions tests/test_pr_review_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,28 @@ def test_a_request_from_a_passer_by_or_with_extra_words_is_not_one(self) -> None
pr = fixtures.payload([], comments=[stranger, chatty])
self.assertEqual([], side.side_asks(pr, "/review"))

def test_the_asks_and_the_rounds_trust_the_same_comments(self) -> None:
"""Each reader keeps only shown comments from write access, so the two cannot drift.

Every body strips to the ask, and its padding tells the comments apart.
"""
cast = [("OWNER", False), ("MEMBER", False), ("COLLABORATOR", False), ("OWNER", True)]
cast += [("CONTRIBUTOR", False), ("NONE", False), ("", False)]
nodes = [
{
**trusted(" " * i + "/review", fixtures.LATE),
"authorAssociation": who,
"isMinimized": hid,
}
for i, (who, hid) in enumerate(cast)
]
pr = fixtures.payload([], comments=nodes)
kept = nodes[:3]
self.assertEqual(kept, payload.trusted_comment_nodes(pr))
self.assertEqual(kept, side.side_asks(pr, "/review"))
bodies = [body for body, _ in rounds.trusted_comments(pr)]
self.assertEqual([n["body"] for n in kept], bodies)

def test_coderabbit_answers_with_a_review_on_the_head_commit(self) -> None:
body = side.side_command("coderabbit", "full review")
self.assertEqual("@coderabbitai full review", body)
Expand Down
Loading