Skip to content
Closed
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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
74 changes: 74 additions & 0 deletions proofline/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
63 changes: 63 additions & 0 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down