From 68f3aa20c71667eb7e93abbd005f31e2a2b37565 Mon Sep 17 00:00:00 2001 From: Arjun Date: Tue, 24 Nov 2026 14:01:20 +0530 Subject: [PATCH] Add iter_pull_request_reviews for sync and async clients list_pull_request_reviews returns one page, and no iterator followed its cursor. Add iter_pull_request_reviews to both clients. It mirrors iter_reviews: it keeps filters on every request, bounds pages and items, refuses repeated or missing cursors, and raises pagination-limit instead of appearing complete when a bound is exhausted. Tests cover cursor following with filters preserved, cursor cycles, max_pages and max_items bounds, and invalid page_size rejected before any request. --- README.md | 17 +++++++++++ proofline/client.py | 74 +++++++++++++++++++++++++++++++++++++++++++++ tests/test_http.py | 63 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+) diff --git a/README.md b/README.md index 721949da..6f727653 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,23 @@ if outcome.status != "COMPLETED": `head_commit_sha` is a full SHA of 40 or 64 hexadecimal characters; any other value raises `ValueError` before a request. A review pinned to another head ends the wait on the read that names it. Its outcome is `INCOMPLETE` whatever the review's own status, its last gap has the reason code `HEAD_COMMIT_MISMATCH`, and `reason` says that the review is pinned to another head commit. A settled review that names no head has `HEAD_COMMIT_UNAVAILABLE` instead and is not `COMPLETED` either. `outcome.review.head_commit_sha` is the commit the review is pinned to. A patch review's head is a derived revision ID, so no commit matches it. In a GitHub Actions `pull_request` job, `GITHUB_SHA` is the merge commit; pass the pull request's head. +## Pull request reviews + +`iter_pull_request_reviews` yields every review of one pull request, following continuation cursors the same way `iter_reviews` does. It keeps the organization, repository, and filters on every request, and it accepts the same `page_size`, `max_pages`, and `max_items` bounds. A cursor that repeats or is missing while the page is truncated raises `urn:proofline:sdk:invalid-pagination`. Exhausting a bound raises `urn:proofline:sdk:pagination-limit`, so a partial listing never looks complete. + +```python +with ProoflineClient() as client: + for review in client.iter_pull_request_reviews( + UUID(os.environ["PROOFLINE_ORGANIZATION_ID"]), + "owner/name", + 12, + filters={"status": "COMPLETED"}, + ): + print(review.id) +``` + +Use `async for` with `AsyncProoflineClient`. `list_pull_request_reviews` still returns a single page. + ## Review exports `export_reviews` reads one CSV or NDJSON page over the bounded transport. Export tokens require `analytics:read`. diff --git a/proofline/client.py b/proofline/client.py index 1d8c1519..8717eccf 100644 --- a/proofline/client.py +++ b/proofline/client.py @@ -358,6 +358,43 @@ def list_pull_request_reviews( **params, ) + def iter_pull_request_reviews( + self, + organization_id: UUID, + repository: str, + pull_request_number: int, + *, + page_size: int = 100, + max_pages: int = 100, + max_items: int = 10_000, + filters: ReviewFilters | None = None, + ) -> Iterator[ReviewSummary]: + """Yield every review of one pull request across pages, bounded like iter_reviews.""" + _page_limits(page_size, max_pages, max_items) + params: ReviewFilters = {} + if filters: + params.update(filters) + params["limit"] = page_size + seen: set[str] = set() + cursor = params.get("cursor") + if cursor: + seen.add(cursor) + count = 0 + for _ in range(max_pages): + page = self.list_pull_request_reviews( + organization_id, repository, pull_request_number, filters=params + ) + for item in page.items: + if count == max_items: + raise _pagination_limit() + yield item + count += 1 + cursor = _next_cursor(page, seen) + if cursor is None: + return + params["cursor"] = cursor + raise _pagination_limit() + def get_pull_request_review( self, organization_id: UUID, repository: str, pull_request_number: int, run_number: int ) -> CustomerReviewDetail: @@ -732,6 +769,43 @@ async def list_pull_request_reviews( **params, ) + async def iter_pull_request_reviews( + self, + organization_id: UUID, + repository: str, + pull_request_number: int, + *, + page_size: int = 100, + max_pages: int = 100, + max_items: int = 10_000, + filters: ReviewFilters | None = None, + ) -> AsyncIterator[ReviewSummary]: + """Yield every review of one pull request across pages, bounded like iter_reviews.""" + _page_limits(page_size, max_pages, max_items) + params: ReviewFilters = {} + if filters: + params.update(filters) + params["limit"] = page_size + seen: set[str] = set() + cursor = params.get("cursor") + if cursor: + seen.add(cursor) + count = 0 + for _ in range(max_pages): + page = await self.list_pull_request_reviews( + organization_id, repository, pull_request_number, filters=params + ) + for item in page.items: + if count == max_items: + raise _pagination_limit() + yield item + count += 1 + cursor = _next_cursor(page, seen) + if cursor is None: + return + params["cursor"] = cursor + raise _pagination_limit() + async def get_pull_request_review( self, organization_id: UUID, repository: str, pull_request_number: int, run_number: int ) -> CustomerReviewDetail: diff --git a/tests/test_http.py b/tests/test_http.py index 0af9d60f..96e0b57c 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -1739,6 +1739,69 @@ def respond(request: Request, index: int) -> Reply: assert raised.value.type == "urn:proofline:sdk:pagination-limit" +@MODES +async def test_pull_request_review_iterator_follows_cursor_and_refuses_cycles_or_bounds( + mode: str, +) -> None: + async def collect_pull_request(client: Client, **kwargs: Any) -> list[Any]: + iterator = client.iter_pull_request_reviews(ORG, "repo/name", 42, **kwargs) + if hasattr(iterator, "__aiter__"): + return [item async for item in iterator] + return list(iterator) + + def respond(request: Request, index: int) -> Reply: + parsed = urlsplit(request.path) + assert parsed.path == f"/api/v1/organizations/{ORG}/reviews/pr/repo%2Fname/42" + assert request.headers["Authorization"] == f"Bearer {TOKEN}" + query = parse_qs(parsed.query) + assert query["limit"] == ["2"] + assert query["status"] == ["COMPLETED"] + if index == 0: + assert "cursor" not in query + else: + assert query["cursor"] == ["pr+cursor"] + return Reply(body=page(cursor="pr+cursor" if index == 0 else None, truncated=index == 0)) + + with Server(respond) as server: + async with session(mode, server.url) as client: + items = await collect_pull_request(client, page_size=2, filters={"status": "COMPLETED"}) + assert [item.id for item in items] == [REVIEW, REVIEW] + assert len(server.requests) == 2 + + for cursor in ["repeated", None]: + with Server( + lambda request, index: Reply(body=page(cursor=cursor, truncated=True)) + ) as server: + async with session(mode, server.url) as client: + with pytest.raises(ProoflineError) as raised: + await collect_pull_request(client) + assert raised.value.type == "urn:proofline:sdk:invalid-pagination" + assert len(server.requests) <= 2 + + with Server( + lambda request, index: Reply(body=page(cursor=str(index), truncated=True)) + ) as server: + async with session(mode, server.url) as client: + with pytest.raises(ProoflineError) as raised: + await collect_pull_request(client, max_pages=2) + assert raised.value.type == "urn:proofline:sdk:pagination-limit" + assert len(server.requests) == 2 + + two_items = page() + two_items["items"].append(dict(two_items["items"][0])) + with Server(lambda request, index: Reply(body=two_items)) as server: + async with session(mode, server.url) as client: + with pytest.raises(ProoflineError) as raised: + await collect_pull_request(client, max_items=1) + assert raised.value.type == "urn:proofline:sdk:pagination-limit" + + with Server(lambda request, index: Reply(body=page())) as server: + async with session(mode, server.url) as client: + with pytest.raises(ValueError): + await collect_pull_request(client, page_size=0) + assert server.requests == [] + + def test_token_environment_and_https_origin_rejection(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("PROOFLINE_TOKEN", TOKEN) with ProoflineClient() as client: