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
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,31 @@ 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.

## Reviewing local changes

`request_patch_review_from_git` reviews a checkout's unpushed changes, as `proofline review` does in the CLI. Nothing is pushed: GitHub needs only the base commit.

```python
from proofline import NoLocalChanges, SecretInPatch

try:
requested = client.request_patch_review_from_git(organization_id, repository_id, cwd=".")
except NoLocalChanges:
print("Nothing to review.")
except SecretInPatch as refusal:
print(refusal) # names the path, line and kind of credential, never the credential
else:
outcome = client.wait_for_review(organization_id, requested.review_id)
```

By default the patch compares tracked work-tree files, staged or not, with the merge-base of `HEAD` and its upstream branch, or with `origin`'s default branch when there is no upstream. `selection="staged"` reads only the index and `selection="untracked"` also adds untracked files Git does not ignore. `base=` names another base, and `base=` with `head=` reviews only the committed range. Revisions resolve once to full commit SHAs. `verify_finding_ids` re-verifies up to 20 open findings against the patch, and `trigger_source` names the Git hook that sent it.

Git runs once under fixed options, so personal configuration such as `diff.noprefix`, `color.ui`, `diff.relative`, `diff.external` or copy detection cannot change the patch. Selected bytes are collected in a copied index, so the repository's index is never modified, and Git takes no optional locks, so the call can run inside a commit hook. The async client runs Git in a worker thread.

Each refusal raises a `LocalChangesError`, a `ValueError`, before any request: no changes (`NoLocalChanges`), a binary change, symbolic link, submodule or unresolved conflict, text or paths that are not UTF-8, more than 3,000 files, or a patch over 5 MiB. Every uploaded line is then checked as the CLI checks it. `SecretInPatch` refuses a `.env` or `.env.*` path, a private-key marker, or a listed credential shape (AWS access-key IDs, GitHub, Slack, Google, Stripe live, model-provider and Proofline tokens) on an added, removed or context line or a hunk heading. Pass an exact repository-relative path in `allow_paths` to skip it. The check recognizes only these shapes: finding none says nothing else about the patch.

`read_local_changes` and `check_secrets` are exported for callers who build the request themselves; `LocalPatch.request()` returns the `PatchReviewRequest` body.

## Review exports

`export_reviews` reads one CSV or NDJSON page over the bounded transport. Export tokens require `analytics:read`.
Expand Down
18 changes: 18 additions & 0 deletions proofline/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@
from .client import AsyncProoflineClient, ProoflineClient, ReviewFilters
from .exports import ExportDataset, ExportFormat, ExportPage
from .knowledge import KnowledgeResult
from .local_changes import (
LocalChangesError,
LocalPatch,
LocalSelection,
NoLocalChanges,
PatchPath,
SecretInPatch,
check_secrets,
read_local_changes,
)
from .outcomes import AssessmentGap, CheckGap, ReviewOutcome

__all__ = [
Expand All @@ -57,6 +67,11 @@
"FileReviewRequest",
"FileReviewResponse",
"KnowledgeResult",
"LocalChangesError",
"LocalPatch",
"LocalSelection",
"NoLocalChanges",
"PatchPath",
"PatchReviewRequest",
"PatchReviewResponse",
"PatchTriggerSource",
Expand All @@ -74,10 +89,13 @@
"RunNowResponse",
"ScanRequestBody",
"ScanResponse",
"SecretInPatch",
"SecretStr",
"StartSession",
"StopResponse",
"UploadedReport",
"__version__",
"check_secrets",
"models",
"read_local_changes",
]
73 changes: 72 additions & 1 deletion proofline/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
from __future__ import annotations

import math
import os
from asyncio import sleep as async_sleep
from collections.abc import AsyncIterator, Awaitable, Callable, Iterator
from collections.abc import AsyncIterator, Awaitable, Callable, Iterator, Sequence
from functools import partial
from string import hexdigits
from time import monotonic, sleep
from types import TracebackType
from typing import Any, Self, TypedDict, TypeVar, Unpack, cast
from urllib.parse import quote
from uuid import UUID

import anyio
import httpx2
from pydantic import SecretStr

Expand Down Expand Up @@ -57,6 +60,7 @@
from ._generated.models.finding_fix_prompt_type_1 import FindingFixPromptType1
from ._generated.models.patch_review_request import PatchReviewRequest
from ._generated.models.patch_review_response import PatchReviewResponse
from ._generated.models.patch_trigger_source import PatchTriggerSource
from ._generated.models.repository_guidance import RepositoryGuidance
from ._generated.models.repository_names import RepositoryNames
from ._generated.models.rerun_response import RerunResponse
Expand All @@ -75,6 +79,7 @@
from .exports import ExportDataset, ExportFormat, ExportPage, decode_export
from .fixes import AsyncFixesClient, FixesClient
from .knowledge import AsyncKnowledgeClient, KnowledgeClient
from .local_changes import LocalSelection, patch_review_request_from_git
from .outcomes import ReviewOutcome, outcome

T = TypeVar("T")
Expand Down Expand Up @@ -468,6 +473,38 @@ def request_patch_review(
body=body,
)

def request_patch_review_from_git(
self,
organization_id: UUID,
repository_id: UUID,
*,
cwd: str | os.PathLike[str] = ".",
base: str | None = None,
head: str | None = None,
selection: LocalSelection = "tracked",
remote: str = "origin",
allow_paths: Sequence[str] = (),
verify_finding_ids: Sequence[UUID] = (),
trigger_source: PatchTriggerSource | None = None,
) -> PatchReviewResponse:
"""Review a checkout's local changes the way `proofline review` does.

Reads one Git snapshot under fixed options, refuses binary, symlink, submodule,
conflicted, non-UTF-8 and oversized changes, and refuses `.env` paths and known
credential shapes on any uploaded line, all before a request. Nothing is pushed.
"""
body = patch_review_request_from_git(
cwd,
base=base,
head=head,
selection=selection,
remote=remote,
allow_paths=allow_paths,
verify_finding_ids=verify_finding_ids,
trigger_source=trigger_source,
)
return self.request_patch_review(organization_id, repository_id, body=body)

def upload_coverage_reports(
self, organization_id: UUID, repository_id: UUID, *, body: CoverageUploadRequest
) -> CoverageUploadResponse:
Expand Down Expand Up @@ -859,6 +896,40 @@ async def request_patch_review(
body=body,
)

async def request_patch_review_from_git(
self,
organization_id: UUID,
repository_id: UUID,
*,
cwd: str | os.PathLike[str] = ".",
base: str | None = None,
head: str | None = None,
selection: LocalSelection = "tracked",
remote: str = "origin",
allow_paths: Sequence[str] = (),
verify_finding_ids: Sequence[UUID] = (),
trigger_source: PatchTriggerSource | None = None,
) -> PatchReviewResponse:
"""Review a checkout's local changes the way `proofline review` does.

Git runs in a worker thread, so the event loop is never blocked. The same local
refusals as the synchronous client apply before any request.
"""
body = await anyio.to_thread.run_sync(
partial(
patch_review_request_from_git,
cwd,
base=base,
head=head,
selection=selection,
remote=remote,
allow_paths=allow_paths,
verify_finding_ids=verify_finding_ids,
trigger_source=trigger_source,
)
)
return await self.request_patch_review(organization_id, repository_id, body=body)

async def upload_coverage_reports(
self, organization_id: UUID, repository_id: UUID, *, body: CoverageUploadRequest
) -> CoverageUploadResponse:
Expand Down
Loading