Skip to content
Open
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
1 change: 1 addition & 0 deletions proofline/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@

class ReviewFilters(TypedDict, total=False):
attention: str | None
audits: str | None
author: str | None
cursor: str | None
environment: UUID | None
Expand Down
38 changes: 37 additions & 1 deletion scripts/contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,28 @@
import json
import sys
from pathlib import Path
from typing import Any, get_type_hints
from uuid import UUID

from generate import HTTP_METHODS, json_operations

ROOT = Path(__file__).resolve().parents[1]
REVIEW_LIST_PATHS = (
"/api/v1/organizations/{org_id}/reviews",
"/api/v1/organizations/{org_id}/reviews/pr/{repository}/{pull_request_number}",
)


def _filter_type(schema: dict[str, Any]) -> object:
kind = schema.get("type")
fmt = schema.get("format")
if kind == ["string", "null"] and fmt == "uuid":
return UUID | None
if kind == ["string", "null"] and fmt is None:
return str | None
if kind == ["integer", "null"]:
return int | None
return object()


def main() -> None:
Expand All @@ -28,7 +46,7 @@ def main() -> None:
if declared != generated:
raise SystemExit("Generated JSON operations differ from the public contract.")
sys.path.insert(0, str(ROOT))
from proofline import AsyncProoflineClient, ProoflineClient
from proofline import AsyncProoflineClient, ProoflineClient, ReviewFilters

for client in (ProoflineClient, AsyncProoflineClient):
helpers = {"close", "aclose", "iter_export_pages", "iter_reviews", "wait_for_review"}
Expand All @@ -39,6 +57,24 @@ def main() -> None:
}
if exposed != declared | {"export_reviews"}:
raise SystemExit(f"{client.__name__} operations differ from the public contract.")
filter_hints = get_type_hints(ReviewFilters)
for review_path in REVIEW_LIST_PATHS:
query_parameters = [
parameter
for parameter in document["paths"][review_path]["get"].get("parameters", [])
if parameter.get("in") == "query"
]
expected_filters = {
parameter["name"]: _filter_type(parameter.get("schema", {}))
for parameter in query_parameters
if not parameter.get("required", False)
}
if (
len(expected_filters) != len(query_parameters)
or filter_hints != expected_filters
or ReviewFilters.__optional_keys__ != set(expected_filters)
):
raise SystemExit("ReviewFilters differs from the public review-list query parameters.")
prohibited = {"safe", "passed", "clean"}
for path in (ROOT / "proofline").rglob("*.py"):
for node in ast.walk(ast.parse(path.read_text())):
Expand Down
95 changes: 95 additions & 0 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -2586,3 +2586,98 @@ def respond(request: Request, index: int) -> Reply:
assert len(exported.rows) == 1
assert exported.rows[0]["claim"] == row["claim"]
assert exported.next_cursor == "next-page"


@MODES
async def test_review_filters_forward_audits_across_lists_and_pagination(mode: str) -> None:
def respond(request: Request, index: int) -> Reply:
assert request.headers["Authorization"] == f"Bearer {TOKEN}"
parsed = urlsplit(request.path)
query = parse_qs(parsed.query)
if index == 0:
assert parsed.path == f"/api/v1/organizations/{ORG}/reviews"
assert query == {"kind": ["REVISION_SCAN"], "audits": ["none"]}
return Reply(body=page())
if index == 1:
assert parsed.path == f"/api/v1/organizations/{ORG}/reviews/pr/repo%2Fname/42"
assert query == {"audits": ["only"], "repository": [str(REPOSITORY)]}
return Reply(body=page())
if index == 2:
assert parsed.path == f"/api/v1/organizations/{ORG}/reviews"
assert query == {}
return Reply(body=page())
assert parsed.path == f"/api/v1/organizations/{ORG}/reviews"
assert query["kind"] == ["REVISION_SCAN"]
assert query["audits"] == ["none"]
assert query["limit"] == ["2"]
if index == 3:
assert "cursor" not in query
return Reply(body=page(cursor="opaque+/cursor", truncated=True))
assert query["cursor"] == ["opaque+/cursor"]
return Reply(body=page())

with Server(respond) as server:
async with session(mode, server.url) as client:
listed = await call(client, "list_reviews", ORG, kind="REVISION_SCAN", audits="none")
assert [item.id for item in listed.items] == [REVIEW]
pr_listed = await call(
client,
"list_pull_request_reviews",
ORG,
"repo/name",
42,
filters={"audits": "only", "repository": REPOSITORY},
)
assert [item.id for item in pr_listed.items] == [REVIEW]
unfiltered = await call(client, "list_reviews", ORG, audits=None)
assert [item.id for item in unfiltered.items] == [REVIEW]
items = await collect(
client,
page_size=2,
filters={"kind": "REVISION_SCAN", "audits": "none"},
)
assert [item.id for item in items] == [REVIEW, REVIEW]
assert len(server.requests) == 5


def test_review_filter_public_types_accept_contract_parameters(tmp_path: Path) -> None:
lines = [
"from uuid import UUID",
"from proofline import AsyncProoflineClient, ProoflineClient, ReviewFilters, ReviewPage",
"",
"def build_filters(org: UUID) -> ReviewFilters:",
" return {",
' "attention": "needed",',
' "audits": "none",',
' "author": "mine",',
' "cursor": None,',
' "environment": org,',
' "findings": "present",',
' "kind": "REVISION_SCAN",',
' "limit": 25,',
' "query": "status:COMPLETED",',
' "repository": org,',
' "search": "timeout",',
' "status": "COMPLETED",',
" }",
"",
"def use_sync(client: ProoflineClient, org: UUID) -> None:",
" filters = build_filters(org)",
' page: ReviewPage = client.list_reviews(org, kind="REVISION_SCAN", audits="none")',
" page = client.list_reviews(org, **filters)",
' page = client.list_pull_request_reviews(org, "repo", 7, filters={"audits": "only"})',
" for _ in client.iter_reviews(org, filters=filters):",
" pass",
"",
"async def use_async(client: AsyncProoflineClient, org: UUID) -> None:",
" filters = build_filters(org)",
' scan = client.list_reviews(org, kind="REVISION_SCAN", audits="none")',
" page: ReviewPage = await scan",
" page = await client.list_reviews(org, **filters)",
' await client.list_pull_request_reviews(org, "repo", 7, filters={"audits": "only"})',
" async for _ in client.iter_reviews(org, filters=filters):",
" pass",
]
fixture = tmp_path / "review_filter_types.py"
fixture.write_text("\n".join(lines))
subprocess.run([sys.executable, "-m", "mypy", str(fixture)], check=True)