diff --git a/src/loopzero/github.py b/src/loopzero/github.py index 874b79fa..f2ec21f6 100644 --- a/src/loopzero/github.py +++ b/src/loopzero/github.py @@ -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"] @@ -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 = [ @@ -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 diff --git a/tests/test_github.py b/tests/test_github.py index 3066bfb7..4e264dd5 100644 --- a/tests/test_github.py +++ b/tests/test_github.py @@ -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: + 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 --------------------------------------------------------------------------------