From 4798844d31c45b3cd053d77306dc4db380c5fe5a Mon Sep 17 00:00:00 2001 From: arjunsreelatha Date: Tue, 24 Nov 2026 16:33:07 +0530 Subject: [PATCH] Add iter_pull_request_reviews with tests and README --- README.md | 21 ++++++++++ proofline/client.py | 89 ++++++++++++++++++++++++++++++++++++++++++ tests/test_http.py | 94 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 204 insertions(+) diff --git a/README.md b/README.md index 721949da..a5eeb1f5 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,27 @@ 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`. If a bound is reached while more reviews remain, the iterator raises `urn:proofline:sdk:pagination-limit`, so a partial listing never looks complete. Finishing exactly at a bound is a normal end. + +```python +import os +from uuid import UUID +from proofline import ProoflineClient + +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..3c1553f8 100644 --- a/proofline/client.py +++ b/proofline/client.py @@ -147,6 +147,9 @@ def _review_url(base_url: str, organization_id: UUID, review_id: UUID) -> str: def _page_limits(page_size: int, max_pages: int, max_items: int) -> None: + for value in (page_size, max_pages, max_items): + if isinstance(value, bool) or not isinstance(value, int): + raise ValueError("Pagination bounds must be integers.") if not 1 <= page_size <= 100 or not 1 <= max_pages <= 1_000 or not 1 <= max_items <= 100_000: raise ValueError( "Pagination requires page_size 1..100, max_pages 1..1000, max_items 1..100000." @@ -358,6 +361,46 @@ 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 + if count == max_items: + # More reviews exist and the item budget is spent: stop without another request. + raise _pagination_limit() + 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: @@ -536,6 +579,9 @@ def iter_reviews( cursor = _next_cursor(page, seen) if cursor is None: return + if count == max_items: + # More reviews exist and the item budget is spent: stop without another request. + raise _pagination_limit() params["cursor"] = cursor raise _pagination_limit() @@ -732,6 +778,46 @@ 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 + if count == max_items: + # More reviews exist and the item budget is spent: stop without another request. + raise _pagination_limit() + 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: @@ -931,6 +1017,9 @@ async def iter_reviews( cursor = _next_cursor(page, seen) if cursor is None: return + if count == max_items: + # More reviews exist and the item budget is spent: stop without another request. + raise _pagination_limit() params["cursor"] = cursor raise _pagination_limit() diff --git a/tests/test_http.py b/tests/test_http.py index 0af9d60f..305a0772 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -1739,6 +1739,100 @@ def respond(request: Request, index: int) -> Reply: assert raised.value.type == "urn:proofline:sdk:pagination-limit" +FIRST_PAGE_REVIEW = UUID(int=101) +SECOND_PAGE_REVIEW = UUID(int=102) + + +@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"] + body = page(cursor="pr+cursor" if index == 0 else None, truncated=index == 0) + body["items"][0]["id"] = str(FIRST_PAGE_REVIEW if index == 0 else SECOND_PAGE_REVIEW) + return Reply(body=body) + + with Server(respond) as server: + async with session(mode, server.url) as client: + items = await collect_pull_request( + client, + page_size=2, + max_pages=2, + max_items=2, + filters={"status": "COMPLETED"}, + ) + assert [item.id for item in items] == [FIRST_PAGE_REVIEW, SECOND_PAGE_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" + # A repeated cursor is refused on its second page; a missing one on the first. + assert len(server.requests) == (2 if cursor == "repeated" else 1) + if cursor == "repeated": + second = parse_qs(urlsplit(server.requests[1].path).query) + assert second["cursor"] == ["repeated"] + + 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 + + # One review per page: the budget is spent on the first page, which is truncated. + with Server( + lambda request, index: Reply( + body=page(cursor="pr+cursor" if index == 0 else None, truncated=index == 0) + ) + ) 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" + # The budget is spent on the first page, so the second page is never requested. + assert len(server.requests) == 1 + + 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) + with pytest.raises(ValueError): + await collect_pull_request(client, max_pages=1.5) + assert server.requests == [] + + def test_token_environment_and_https_origin_rejection(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("PROOFLINE_TOKEN", TOKEN) with ProoflineClient() as client: