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
15 changes: 13 additions & 2 deletions src/loopzero/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,11 +1044,21 @@ def open_findings(repo: str, number: int,
"""


def _next_thread_cursor(page: dict, seen: set[str]) -> str:
cursor = page.get("endCursor")
if not isinstance(cursor, str) or not cursor.strip() or cursor in seen:
raise GhError(("gh", "api", "graphql"), "missing or repeated review thread cursor")
seen.add(cursor)
return cursor


def _has_thread_reply(node: dict, reply: str) -> bool:
page = node["comments"].get("pageInfo", {})
seen: set[str] = set()
while page.get("hasNextPage"):
cursor = _next_thread_cursor(page, seen)
data = _gh_json("api", "graphql", "-f", f"query={_THREAD_REPLIES_QUERY}",
"-F", f"thread={node['id']}", "-f", f"after={page['endCursor']}")
"-F", f"thread={node['id']}", "-f", f"after={cursor}")
try:
comments = data["data"]["node"]["comments"]
page = comments["pageInfo"]
Expand Down Expand Up @@ -1137,6 +1147,7 @@ def _unresolved_threads(repo: str, number: int) -> list[dict]:
def _review_threads(repo: str, number: int, *, include_resolved: bool = False) -> list[dict]:
owner, name = repo.split("/", 1)
after: str | None = None
seen: set[str] = set()
found: list[dict] = []
while True:
args = [
Expand All @@ -1153,7 +1164,7 @@ def _review_threads(repo: str, number: int, *, include_resolved: bool = False) -
found += [node for node in nodes if include_resolved or not node.get("isResolved")]
if not page["hasNextPage"]:
return found
after = page["endCursor"]
after = _next_thread_cursor(page, seen)
except (KeyError, TypeError) as exc:
raise _not_found(number, exc) from exc

Expand Down
17 changes: 17 additions & 0 deletions tests/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,23 @@ def test_open_blocking_findings_paginates(gh: FakeGh) -> None:
assert "after=C1" not in gh.argv(0) and "after=C1" in gh.argv(1)


@pytest.mark.parametrize("cursors", [(None,), ("C1", "C1"), ("C1", "C2", "C1")])
def test_open_blocking_findings_rejects_invalid_cursor(gh: FakeGh, cursors) -> None:
gh.respond("api graphql", *(threads_json(has_next=True, cursor=c) for c in cursors))
with pytest.raises(github.GhError, match="missing or repeated review thread cursor"):
github.open_blocking_findings(REPO, 7, HEAD, PUBLISHERS)
assert len(gh.calls) == len(cursors)


def test_thread_reply_read_rejects_repeated_cursor(gh: FakeGh) -> None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: No test checks that a failed reply scan blocks the reply and resolve mutations

The acceptance criteria say a failed reply scan must not post a reply or resolve the thread. The code meets this: resolve_finding calls _has_thread_reply at github.py:1084, before _REPLY_MUTATION and _RESOLVE_MUTATION, so the GhError stops the flow before either mutation runs. The new test calls the private _has_thread_reply directly, though, so nothing protects that ordering. If someone later reorders the code or catches GhError around the reply check, the tests would still pass while the change silently posts or resolves. Smallest fix: add a test that calls github.resolve_finding(...). The thread page should have a comments pageInfo with hasNextPage=True, endCursor="C1", and the reply page should repeat C1. Assert that GhError is raised and that no recorded gh call contains addPullRequestReviewThreadReply or resolveReviewThread.

page = {"hasNextPage": True, "endCursor": "C1"}
gh.respond("api graphql", {"data": {"node": {"comments": {"pageInfo": page, "nodes": []}}}})
node = {"id": "T1", "comments": {"pageInfo": page}}
with pytest.raises(github.GhError, match="missing or repeated review thread cursor"):
github._has_thread_reply(node, "reply")
assert len(gh.calls) == 1 and "after=C1" in gh.argv(0)


# --- checks --------------------------------------------------------------------------------


Expand Down
Loading