diff --git a/README.md b/README.md index 721949da..7e8c3d65 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,36 @@ 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. +## Deploy verifications and scans + +Inspect deployment verification records for a specific commit SHA using `get_commit_deploy_verifications`: + +```python +verifications = client.get_commit_deploy_verifications(organization_id, repository_id, commit_sha) +for deploy in verifications.deploys: + print(deploy.environment_id, deploy.state) +``` + +To request an ad-hoc scan on a repository, use `request_scan`: + +```python +from uuid import uuid4 +from proofline import ScanRequestBody + +response = client.request_scan( + organization_id, + repository_id, + body=ScanRequestBody( + budget_credits=100, + commit_sha=commit_sha, + request_id=uuid4(), + ), +) +print(response.outcome, response.resolution_id) +``` + +Both methods accept canonical `organization_id` / `repository_id` or `org_id` / `repo_id` aliases. `get_commit_deploy_verifications` validates that `commit_sha` is a full 40- or 64-character hexadecimal SHA before sending the request. + ## Review exports `export_reviews` reads one CSV or NDJSON page over the bounded transport. Export tokens require `analytics:read`. diff --git a/proofline/__init__.py b/proofline/__init__.py index 689b454d..86a4e911 100644 --- a/proofline/__init__.py +++ b/proofline/__init__.py @@ -6,6 +6,7 @@ from ._generated.models import ( AgentSession, AgentVendor, + CommitDeployVerifications, CommitReviewRequest, CommitReviewResponse, CoverageUploadRequest, @@ -44,6 +45,7 @@ "AssessmentGap", "AsyncProoflineClient", "CheckGap", + "CommitDeployVerifications", "CommitReviewRequest", "CommitReviewResponse", "CoverageUploadRequest", diff --git a/proofline/_generated/api/operations.py b/proofline/_generated/api/operations.py index 227215e7..7fa9834d 100644 --- a/proofline/_generated/api/operations.py +++ b/proofline/_generated/api/operations.py @@ -1,90 +1,11 @@ """Typed methods generated from the public operations without custom adapters.""" -from typing import cast -from uuid import UUID - -from ... import _transport from ..client import AuthenticatedClient -from ..models.commit_deploy_verifications import CommitDeployVerifications -from ..models.scan_request_body import ScanRequestBody -from ..models.scan_response import ScanResponse -from .default import get_commit_deploy_verifications as operation_get_commit_deploy_verifications -from .default import request_scan as operation_request_scan class Operations: _api: AuthenticatedClient - def get_commit_deploy_verifications( - self, - org_id: UUID, - repo_id: UUID, - commit_sha: str, - ) -> CommitDeployVerifications: - with _transport.operation_errors(): - response = operation_get_commit_deploy_verifications.sync_detailed( - client=self._api, - org_id=org_id, - repo_id=repo_id, - commit_sha=commit_sha, - ) - if not (response.status_code == 200): - raise _transport.invalid_response() - return cast(CommitDeployVerifications, response.parsed) - - def request_scan( - self, - org_id: UUID, - repo_id: UUID, - *, - body: ScanRequestBody, - ) -> ScanResponse: - with _transport.operation_errors(): - response = operation_request_scan.sync_detailed( - client=self._api, - org_id=org_id, - repo_id=repo_id, - body=body, - ) - if not (response.status_code == 200): - raise _transport.invalid_response() - return cast(ScanResponse, response.parsed) - class AsyncOperations: _api: AuthenticatedClient - - async def get_commit_deploy_verifications( - self, - org_id: UUID, - repo_id: UUID, - commit_sha: str, - ) -> CommitDeployVerifications: - with _transport.operation_errors(): - response = await operation_get_commit_deploy_verifications.asyncio_detailed( - client=self._api, - org_id=org_id, - repo_id=repo_id, - commit_sha=commit_sha, - ) - if not (response.status_code == 200): - raise _transport.invalid_response() - return cast(CommitDeployVerifications, response.parsed) - - async def request_scan( - self, - org_id: UUID, - repo_id: UUID, - *, - body: ScanRequestBody, - ) -> ScanResponse: - with _transport.operation_errors(): - response = await operation_request_scan.asyncio_detailed( - client=self._api, - org_id=org_id, - repo_id=repo_id, - body=body, - ) - if not (response.status_code == 200): - raise _transport.invalid_response() - return cast(ScanResponse, response.parsed) diff --git a/proofline/client.py b/proofline/client.py index 1d8c1519..fcb3517d 100644 --- a/proofline/client.py +++ b/proofline/client.py @@ -20,6 +20,7 @@ get_agent_session, get_agent_session_prompt, get_assessment, + get_commit_deploy_verifications, get_deployment_review, get_finding, get_finding_fix_prompt, @@ -34,6 +35,7 @@ request_file_review, request_patch_review, request_review, + request_scan, rerun_review, start_agent_session, stop_review, @@ -44,6 +46,7 @@ from ._generated.models.agent_session import AgentSession from ._generated.models.agent_session_prompt_type_0 import AgentSessionPromptType0 from ._generated.models.agent_session_prompt_type_1 import AgentSessionPromptType1 +from ._generated.models.commit_deploy_verifications import CommitDeployVerifications from ._generated.models.commit_review_request import CommitReviewRequest from ._generated.models.commit_review_response import CommitReviewResponse from ._generated.models.coverage_upload_request import CoverageUploadRequest @@ -67,6 +70,8 @@ from ._generated.models.review_summary import ReviewSummary from ._generated.models.run_now_request import RunNowRequest from ._generated.models.run_now_response import RunNowResponse +from ._generated.models.scan_request_body import ScanRequestBody +from ._generated.models.scan_response import ScanResponse from ._generated.models.start_session import StartSession from ._generated.models.stop_response import StopResponse from ._generated.types import UNSET, Response, Unset @@ -153,6 +158,23 @@ def _page_limits(page_size: int, max_pages: int, max_items: int) -> None: ) +def _resolve_id( + canonical: UUID | None, + alias: UUID | None, + name: str, + alias_name: str, +) -> UUID: + if canonical is not None and alias is not None: + if canonical != alias: + raise ValueError(f"Conflicting arguments for {name} and {alias_name}.") + return canonical + if canonical is not None: + return canonical + if alias is not None: + return alias + raise TypeError(f"Missing required argument: {name!r} (or {alias_name!r}).") + + def _head_commit(value: str | None) -> None: # Only a full SHA names one exact commit; a branch or an abbreviation does not. if value is not None and ( @@ -507,6 +529,49 @@ def request_commit_review( def rerun_review(self, organization_id: UUID, review_id: UUID) -> RerunResponse: return self._call(rerun_review.sync_detailed, RerunResponse, organization_id, review_id) + def get_commit_deploy_verifications( + self, + organization_id: UUID | None = None, + repository_id: UUID | None = None, + commit_sha: str | None = None, + *, + org_id: UUID | None = None, + repo_id: UUID | None = None, + ) -> CommitDeployVerifications: + """Read deployment verifications for a specific commit SHA.""" + resolved_org_id = _resolve_id(organization_id, org_id, "organization_id", "org_id") + resolved_repo_id = _resolve_id(repository_id, repo_id, "repository_id", "repo_id") + if commit_sha is None: + raise TypeError("Missing required argument: 'commit_sha'.") + _head_commit(commit_sha) + return self._call( + get_commit_deploy_verifications.sync_detailed, + CommitDeployVerifications, + resolved_org_id, + resolved_repo_id, + commit_sha, + ) + + def request_scan( + self, + organization_id: UUID | None = None, + repository_id: UUID | None = None, + *, + body: ScanRequestBody, + org_id: UUID | None = None, + repo_id: UUID | None = None, + ) -> ScanResponse: + """Request an asynchronous security or configuration scan for a repository.""" + resolved_org_id = _resolve_id(organization_id, org_id, "organization_id", "org_id") + resolved_repo_id = _resolve_id(repository_id, repo_id, "repository_id", "repo_id") + return self._call( + request_scan.sync_detailed, + ScanResponse, + resolved_org_id, + resolved_repo_id, + body=body, + ) + def iter_reviews( self, organization_id: UUID, @@ -902,6 +967,49 @@ async def rerun_review(self, organization_id: UUID, review_id: UUID) -> RerunRes rerun_review.asyncio_detailed, RerunResponse, organization_id, review_id ) + async def get_commit_deploy_verifications( + self, + organization_id: UUID | None = None, + repository_id: UUID | None = None, + commit_sha: str | None = None, + *, + org_id: UUID | None = None, + repo_id: UUID | None = None, + ) -> CommitDeployVerifications: + """Read deployment verifications for a specific commit SHA.""" + resolved_org_id = _resolve_id(organization_id, org_id, "organization_id", "org_id") + resolved_repo_id = _resolve_id(repository_id, repo_id, "repository_id", "repo_id") + if commit_sha is None: + raise TypeError("Missing required argument: 'commit_sha'.") + _head_commit(commit_sha) + return await self._call( + get_commit_deploy_verifications.asyncio_detailed, + CommitDeployVerifications, + resolved_org_id, + resolved_repo_id, + commit_sha, + ) + + async def request_scan( + self, + organization_id: UUID | None = None, + repository_id: UUID | None = None, + *, + body: ScanRequestBody, + org_id: UUID | None = None, + repo_id: UUID | None = None, + ) -> ScanResponse: + """Request an asynchronous security or configuration scan for a repository.""" + resolved_org_id = _resolve_id(organization_id, org_id, "organization_id", "org_id") + resolved_repo_id = _resolve_id(repository_id, repo_id, "repository_id", "repo_id") + return await self._call( + request_scan.asyncio_detailed, + ScanResponse, + resolved_org_id, + resolved_repo_id, + body=body, + ) + async def iter_reviews( self, organization_id: UUID, diff --git a/tests/test_http.py b/tests/test_http.py index 0af9d60f..b4171496 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -28,6 +28,7 @@ from proofline import ( AsyncProoflineClient, + CommitDeployVerifications, CommitReviewRequest, CommitReviewResponse, CoverageUploadRequest, @@ -48,6 +49,8 @@ RunNowMode, RunNowRequest, RunNowResponse, + ScanRequestBody, + ScanResponse, StopResponse, UploadedReport, _sync_http, @@ -2586,3 +2589,283 @@ 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_get_commit_deploy_verifications_first_class(mode: str) -> None: + valid_sha = "a" * 40 + deploy_data = {"deploys": [], "recorded": True} + expected_path = ( + f"/api/v1/organizations/{ORG}/repositories/{REPOSITORY}" + f"/commits/{valid_sha}/deploy-verifications" + ) + + def respond(request: Request, index: int) -> Reply: + assert request.method == "GET" + assert request.path == expected_path + return Reply(body=deploy_data) + + with Server(respond) as server: + async with session(mode, server.url) as client: + # 1. Canonical keyword arguments + result = await call( + client, + "get_commit_deploy_verifications", + organization_id=ORG, + repository_id=REPOSITORY, + commit_sha=valid_sha, + ) + assert isinstance(result, CommitDeployVerifications) + assert result.recorded is True + assert result.deploys == [] + + # 2. Backward-compatible alias keyword arguments + result_alias = await call( + client, + "get_commit_deploy_verifications", + org_id=ORG, + repo_id=REPOSITORY, + commit_sha=valid_sha, + ) + assert isinstance(result_alias, CommitDeployVerifications) + assert result_alias.recorded is True + + # 3. Positional arguments + result_pos = await call( + client, + "get_commit_deploy_verifications", + ORG, + REPOSITORY, + valid_sha, + ) + assert isinstance(result_pos, CommitDeployVerifications) + assert result_pos.recorded is True + + # 4. Both canonical and alias provided with matching values + result_both = await call( + client, + "get_commit_deploy_verifications", + organization_id=ORG, + repository_id=REPOSITORY, + commit_sha=valid_sha, + org_id=ORG, + repo_id=REPOSITORY, + ) + assert isinstance(result_both, CommitDeployVerifications) + + +@MODES +async def test_get_commit_deploy_verifications_validations(mode: str) -> None: + other_uuid = UUID("00000000-0000-7000-8000-000000000099") + valid_sha = "b" * 40 + + with Server(lambda r, i: Reply(body={"deploys": [], "recorded": True})) as server: + async with session(mode, server.url) as client: + # Conflicting organization_id and org_id + with pytest.raises( + ValueError, match="Conflicting arguments for organization_id and org_id." + ): + await call( + client, + "get_commit_deploy_verifications", + organization_id=ORG, + org_id=other_uuid, + repository_id=REPOSITORY, + commit_sha=valid_sha, + ) + + # Conflicting repository_id and repo_id + with pytest.raises( + ValueError, match="Conflicting arguments for repository_id and repo_id." + ): + await call( + client, + "get_commit_deploy_verifications", + organization_id=ORG, + repository_id=REPOSITORY, + repo_id=other_uuid, + commit_sha=valid_sha, + ) + + # Missing organization_id / org_id + with pytest.raises(TypeError, match="Missing required argument: 'organization_id'"): + await call( + client, + "get_commit_deploy_verifications", + repository_id=REPOSITORY, + commit_sha=valid_sha, + ) + + # Missing repository_id / repo_id + with pytest.raises(TypeError, match="Missing required argument: 'repository_id'"): + await call( + client, + "get_commit_deploy_verifications", + organization_id=ORG, + commit_sha=valid_sha, + ) + + # Missing commit_sha + with pytest.raises(TypeError, match="Missing required argument: 'commit_sha'"): + await call( + client, + "get_commit_deploy_verifications", + organization_id=ORG, + repository_id=REPOSITORY, + ) + + # Invalid commit_sha: too short + with pytest.raises( + ValueError, + match="The head commit must be a full 40 or 64 character hexadecimal SHA.", + ): + await call( + client, + "get_commit_deploy_verifications", + organization_id=ORG, + repository_id=REPOSITORY, + commit_sha="not-a-40-char-sha", + ) + + # Invalid commit_sha: non-hex + with pytest.raises( + ValueError, + match="The head commit must be a full 40 or 64 character hexadecimal SHA.", + ): + await call( + client, + "get_commit_deploy_verifications", + organization_id=ORG, + repository_id=REPOSITORY, + commit_sha="g" * 40, + ) + + +@MODES +async def test_request_scan_first_class(mode: str) -> None: + scan_body = ScanRequestBody( + budget_credits=100, + request_id=UUID("00000000-0000-7000-8000-000000000042"), + commit_sha="c" * 40, + ) + scan_data = { + "outcome": "CREATED", + "resolution_id": "00000000-0000-7000-8000-000000000042", + "review_id": "00000000-0000-7000-8000-000000000043", + } + expected_path = f"/api/v1/organizations/{ORG}/repositories/{REPOSITORY}/scans" + + def respond(request: Request, index: int) -> Reply: + assert request.method == "POST" + assert request.path == expected_path + assert request.headers.get("Content-Type") == "application/json" + body = json.loads(request.body) + assert body["budget_credits"] == 100 + assert body["commit_sha"] == "c" * 40 + return Reply(body=scan_data) + + with Server(respond) as server: + async with session(mode, server.url) as client: + # 1. Canonical keyword arguments + result = await call( + client, + "request_scan", + organization_id=ORG, + repository_id=REPOSITORY, + body=scan_body, + ) + assert isinstance(result, ScanResponse) + assert result.outcome == "CREATED" + assert str(result.resolution_id) == "00000000-0000-7000-8000-000000000042" + + # 2. Backward-compatible alias keyword arguments + result_alias = await call( + client, + "request_scan", + org_id=ORG, + repo_id=REPOSITORY, + body=scan_body, + ) + assert isinstance(result_alias, ScanResponse) + assert result_alias.outcome == "CREATED" + + # 3. Positional arguments + result_pos = await call( + client, + "request_scan", + ORG, + REPOSITORY, + body=scan_body, + ) + assert isinstance(result_pos, ScanResponse) + assert result_pos.outcome == "CREATED" + + # 4. Both canonical and alias provided with matching values + result_both = await call( + client, + "request_scan", + organization_id=ORG, + repository_id=REPOSITORY, + org_id=ORG, + repo_id=REPOSITORY, + body=scan_body, + ) + assert isinstance(result_both, ScanResponse) + + +@MODES +async def test_request_scan_validations(mode: str) -> None: + other_uuid = UUID("00000000-0000-7000-8000-000000000099") + scan_body = ScanRequestBody( + budget_credits=50, + request_id=UUID("00000000-0000-7000-8000-000000000042"), + ref="main", + ) + + with Server( + lambda r, i: Reply(body={"outcome": "CREATED", "resolution_id": str(ORG)}) + ) as server: + async with session(mode, server.url) as client: + # Conflicting organization_id and org_id + with pytest.raises( + ValueError, match="Conflicting arguments for organization_id and org_id." + ): + await call( + client, + "request_scan", + organization_id=ORG, + org_id=other_uuid, + repository_id=REPOSITORY, + body=scan_body, + ) + + # Conflicting repository_id and repo_id + with pytest.raises( + ValueError, match="Conflicting arguments for repository_id and repo_id." + ): + await call( + client, + "request_scan", + organization_id=ORG, + repository_id=REPOSITORY, + repo_id=other_uuid, + body=scan_body, + ) + + # Missing organization_id / org_id + with pytest.raises(TypeError, match="Missing required argument: 'organization_id'"): + await call( + client, + "request_scan", + repository_id=REPOSITORY, + body=scan_body, + ) + + # Missing repository_id / repo_id + with pytest.raises(TypeError, match="Missing required argument: 'repository_id'"): + await call( + client, + "request_scan", + organization_id=ORG, + body=scan_body, + )