Skip to content

Commit 68f3aa2

Browse files
Arjunarjunsreelatha
authored andcommitted
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.
1 parent 17ae9dd commit 68f3aa2

3 files changed

Lines changed: 154 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,23 @@ if outcome.status != "COMPLETED":
7676

7777
`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.
7878

79+
## Pull request reviews
80+
81+
`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.
82+
83+
```python
84+
with ProoflineClient() as client:
85+
for review in client.iter_pull_request_reviews(
86+
UUID(os.environ["PROOFLINE_ORGANIZATION_ID"]),
87+
"owner/name",
88+
12,
89+
filters={"status": "COMPLETED"},
90+
):
91+
print(review.id)
92+
```
93+
94+
Use `async for` with `AsyncProoflineClient`. `list_pull_request_reviews` still returns a single page.
95+
7996
## Review exports
8097

8198
`export_reviews` reads one CSV or NDJSON page over the bounded transport. Export tokens require `analytics:read`.

‎proofline/client.py‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,43 @@ def list_pull_request_reviews(
358358
**params,
359359
)
360360

361+
def iter_pull_request_reviews(
362+
self,
363+
organization_id: UUID,
364+
repository: str,
365+
pull_request_number: int,
366+
*,
367+
page_size: int = 100,
368+
max_pages: int = 100,
369+
max_items: int = 10_000,
370+
filters: ReviewFilters | None = None,
371+
) -> Iterator[ReviewSummary]:
372+
"""Yield every review of one pull request across pages, bounded like iter_reviews."""
373+
_page_limits(page_size, max_pages, max_items)
374+
params: ReviewFilters = {}
375+
if filters:
376+
params.update(filters)
377+
params["limit"] = page_size
378+
seen: set[str] = set()
379+
cursor = params.get("cursor")
380+
if cursor:
381+
seen.add(cursor)
382+
count = 0
383+
for _ in range(max_pages):
384+
page = self.list_pull_request_reviews(
385+
organization_id, repository, pull_request_number, filters=params
386+
)
387+
for item in page.items:
388+
if count == max_items:
389+
raise _pagination_limit()
390+
yield item
391+
count += 1
392+
cursor = _next_cursor(page, seen)
393+
if cursor is None:
394+
return
395+
params["cursor"] = cursor
396+
raise _pagination_limit()
397+
361398
def get_pull_request_review(
362399
self, organization_id: UUID, repository: str, pull_request_number: int, run_number: int
363400
) -> CustomerReviewDetail:
@@ -732,6 +769,43 @@ async def list_pull_request_reviews(
732769
**params,
733770
)
734771

772+
async def iter_pull_request_reviews(
773+
self,
774+
organization_id: UUID,
775+
repository: str,
776+
pull_request_number: int,
777+
*,
778+
page_size: int = 100,
779+
max_pages: int = 100,
780+
max_items: int = 10_000,
781+
filters: ReviewFilters | None = None,
782+
) -> AsyncIterator[ReviewSummary]:
783+
"""Yield every review of one pull request across pages, bounded like iter_reviews."""
784+
_page_limits(page_size, max_pages, max_items)
785+
params: ReviewFilters = {}
786+
if filters:
787+
params.update(filters)
788+
params["limit"] = page_size
789+
seen: set[str] = set()
790+
cursor = params.get("cursor")
791+
if cursor:
792+
seen.add(cursor)
793+
count = 0
794+
for _ in range(max_pages):
795+
page = await self.list_pull_request_reviews(
796+
organization_id, repository, pull_request_number, filters=params
797+
)
798+
for item in page.items:
799+
if count == max_items:
800+
raise _pagination_limit()
801+
yield item
802+
count += 1
803+
cursor = _next_cursor(page, seen)
804+
if cursor is None:
805+
return
806+
params["cursor"] = cursor
807+
raise _pagination_limit()
808+
735809
async def get_pull_request_review(
736810
self, organization_id: UUID, repository: str, pull_request_number: int, run_number: int
737811
) -> CustomerReviewDetail:

‎tests/test_http.py‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,69 @@ def respond(request: Request, index: int) -> Reply:
17391739
assert raised.value.type == "urn:proofline:sdk:pagination-limit"
17401740

17411741

1742+
@MODES
1743+
async def test_pull_request_review_iterator_follows_cursor_and_refuses_cycles_or_bounds(
1744+
mode: str,
1745+
) -> None:
1746+
async def collect_pull_request(client: Client, **kwargs: Any) -> list[Any]:
1747+
iterator = client.iter_pull_request_reviews(ORG, "repo/name", 42, **kwargs)
1748+
if hasattr(iterator, "__aiter__"):
1749+
return [item async for item in iterator]
1750+
return list(iterator)
1751+
1752+
def respond(request: Request, index: int) -> Reply:
1753+
parsed = urlsplit(request.path)
1754+
assert parsed.path == f"/api/v1/organizations/{ORG}/reviews/pr/repo%2Fname/42"
1755+
assert request.headers["Authorization"] == f"Bearer {TOKEN}"
1756+
query = parse_qs(parsed.query)
1757+
assert query["limit"] == ["2"]
1758+
assert query["status"] == ["COMPLETED"]
1759+
if index == 0:
1760+
assert "cursor" not in query
1761+
else:
1762+
assert query["cursor"] == ["pr+cursor"]
1763+
return Reply(body=page(cursor="pr+cursor" if index == 0 else None, truncated=index == 0))
1764+
1765+
with Server(respond) as server:
1766+
async with session(mode, server.url) as client:
1767+
items = await collect_pull_request(client, page_size=2, filters={"status": "COMPLETED"})
1768+
assert [item.id for item in items] == [REVIEW, REVIEW]
1769+
assert len(server.requests) == 2
1770+
1771+
for cursor in ["repeated", None]:
1772+
with Server(
1773+
lambda request, index: Reply(body=page(cursor=cursor, truncated=True))
1774+
) as server:
1775+
async with session(mode, server.url) as client:
1776+
with pytest.raises(ProoflineError) as raised:
1777+
await collect_pull_request(client)
1778+
assert raised.value.type == "urn:proofline:sdk:invalid-pagination"
1779+
assert len(server.requests) <= 2
1780+
1781+
with Server(
1782+
lambda request, index: Reply(body=page(cursor=str(index), truncated=True))
1783+
) as server:
1784+
async with session(mode, server.url) as client:
1785+
with pytest.raises(ProoflineError) as raised:
1786+
await collect_pull_request(client, max_pages=2)
1787+
assert raised.value.type == "urn:proofline:sdk:pagination-limit"
1788+
assert len(server.requests) == 2
1789+
1790+
two_items = page()
1791+
two_items["items"].append(dict(two_items["items"][0]))
1792+
with Server(lambda request, index: Reply(body=two_items)) as server:
1793+
async with session(mode, server.url) as client:
1794+
with pytest.raises(ProoflineError) as raised:
1795+
await collect_pull_request(client, max_items=1)
1796+
assert raised.value.type == "urn:proofline:sdk:pagination-limit"
1797+
1798+
with Server(lambda request, index: Reply(body=page())) as server:
1799+
async with session(mode, server.url) as client:
1800+
with pytest.raises(ValueError):
1801+
await collect_pull_request(client, page_size=0)
1802+
assert server.requests == []
1803+
1804+
17421805
def test_token_environment_and_https_origin_rejection(monkeypatch: pytest.MonkeyPatch) -> None:
17431806
monkeypatch.setenv("PROOFLINE_TOKEN", TOKEN)
17441807
with ProoflineClient() as client:

0 commit comments

Comments
 (0)