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
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,21 @@ except ProoflineError as error:
`request_review` names a pull request, and its response names no commit: the review belongs to the head the pull request has when the request arrives. To accept a result only for the commit you hold, pass that commit to `wait_for_review`.

```python
from proofline import format_review_outcome

requested = client.request_review(organization_id, repository_id, 12)
outcome = client.wait_for_review(
organization_id,
requested.review_id,
head_commit_sha=head_commit_sha,
)
if outcome.status != "COMPLETED":
print(outcome.status, outcome.reason)
print(format_review_outcome(outcome))
```

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

`format_review_outcome` emits one safe CI line with the outcome status, the actual reviewed Git SHA only when the recorded review has both a documented Git review kind and its commit URL, and identifier-shaped reason codes. A patch review never claims a commit: its derived ID is emitted as `pinned_revision` instead. Missing, malformed, or multiline recorded identities render as `unknown`; the requested SHA, the provider's moving current head, and server prose are never logged. For a review that recorded a different head, it prints `proofline_review status=INCOMPLETE reviewed_commit=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb pinned_revision=unknown reason_codes=HEAD_COMMIT_MISMATCH`.

## Review exports

`export_reviews` reads one CSV or NDJSON page over the bounded transport. Export tokens require `analytics:read`.
Expand Down
3 changes: 2 additions & 1 deletion proofline/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from .client import AsyncProoflineClient, ProoflineClient, ReviewFilters
from .exports import ExportDataset, ExportFormat, ExportPage
from .knowledge import KnowledgeResult
from .outcomes import AssessmentGap, CheckGap, ReviewOutcome
from .outcomes import AssessmentGap, CheckGap, ReviewOutcome, format_review_outcome

__all__ = [
"AgentSession",
Expand All @@ -56,6 +56,7 @@
"ExportPage",
"FileReviewRequest",
"FileReviewResponse",
"format_review_outcome",
"KnowledgeResult",
"PatchReviewRequest",
"PatchReviewResponse",
Expand Down
75 changes: 75 additions & 0 deletions proofline/outcomes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from dataclasses import dataclass, field
from typing import Literal
from urllib.parse import urlsplit
from uuid import UUID

from ._generated.models.customer_code_assessment import CustomerCodeAssessment
Expand All @@ -12,6 +13,7 @@
from ._transport import ProoflineError

OutcomeStatus = Literal["COMPLETED", "INCOMPLETE", "FAILED", "STOPPED", "TIMED_OUT"]
_OUTCOME_STATUSES = frozenset(("COMPLETED", "INCOMPLETE", "FAILED", "STOPPED", "TIMED_OUT"))
COMPLETE_CHECKS = frozenset(("READY", "BLOCKED", "NOT_APPLICABLE"))
COMPLETE_LIFECYCLES = frozenset(("GATING", "SHADOW", "QUARANTINED", "RETIRED"))
_KNOWN_ASSESSMENTS = frozenset(
Expand All @@ -22,6 +24,12 @@
"HEAD_COMMIT_MISMATCH": "The review is pinned to another head commit.",
"HEAD_COMMIT_UNAVAILABLE": "The review names no head commit.",
}
_HEX_DIGITS = frozenset("0123456789abcdefABCDEF")
_REASON_CODE_CHARS = frozenset("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_")
_REVISION_CHARS = frozenset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._:-")
_GIT_COMMIT_KINDS = frozenset(
("PULL_REQUEST", "DEPLOYMENT", "COMMIT", "FILES", "MERGE_GROUP", "REVISION_SCAN")
)


@dataclass(frozen=True)
Expand Down Expand Up @@ -52,6 +60,73 @@ class ReviewOutcome:
reason: str | None = None


def _is_commit_sha(value: str) -> bool:
return len(value) in (40, 64) and all(char in _HEX_DIGITS for char in value)


def _is_reason_code(value: str) -> bool:
return (
1 <= len(value) <= 128
and value[0] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
and all(char in _REASON_CODE_CHARS for char in value)
)


def _is_pinned_revision(value: str) -> bool:
return 1 <= len(value) <= 128 and all(char in _REVISION_CHARS for char in value)


def _is_recorded_commit_url(value: str) -> bool:
if "\r" in value or "\n" in value:
return False
try:
parsed = urlsplit(value)
except ValueError:
return False
return (
parsed.scheme in ("http", "https")
and bool(parsed.netloc)
and parsed.username is None
and parsed.password is None
)


def format_review_outcome(outcome: ReviewOutcome) -> str:
"""Return a safe, single-line summary suitable for a CI log.

`reviewed_commit` requires the recorded commit URL and a documented Git
review kind. A patch's derived revision is reported as `pinned_revision`
instead. The requested and provider-current heads are never inputs.
"""

reviewed_commit = "unknown"
pinned_revision = "unknown"
status = outcome.status if outcome.status in _OUTCOME_STATUSES else "unknown"
if outcome.review is not None:
head = text(outcome.review.head_commit_sha)
kind = outcome.review.kind
if (
kind in _GIT_COMMIT_KINDS
and head is not None
and _is_commit_sha(head)
and (commit_url := text(outcome.review.head_commit_url)) is not None
and _is_recorded_commit_url(commit_url)
):
reviewed_commit = head
elif kind == "PATCH" and head is not None and _is_pinned_revision(head):
pinned_revision = head
reason_codes = tuple(
dict.fromkeys(
code for gap in outcome.gaps for code in gap.reason_codes if _is_reason_code(code)
)
)
return (
f"proofline_review status={status} reviewed_commit={reviewed_commit} "
f"pinned_revision={pinned_revision} "
f"reason_codes={','.join(reason_codes) if reason_codes else 'unknown'}"
)


def text(value: object) -> str | None:
return value if isinstance(value, str) and value else None

Expand Down
238 changes: 238 additions & 0 deletions tests/test_outcome_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
from types import SimpleNamespace
from typing import cast

import pytest

from proofline import AssessmentGap, ReviewOutcome, format_review_outcome
from proofline.outcomes import OutcomeStatus


@pytest.mark.parametrize(
("outcome", "expected"),
[
(
ReviewOutcome(
"COMPLETED",
"https://example.invalid/reviews/1",
review=SimpleNamespace(
kind="PULL_REQUEST",
head_commit_sha="a" * 40,
head_commit_url="https://github.example/commit/a",
),
),
"proofline_review status=COMPLETED reviewed_commit="
+ "a" * 40
+ " pinned_revision=unknown reason_codes=unknown",
),
(
ReviewOutcome(
"INCOMPLETE",
"https://example.invalid/reviews/1",
review=SimpleNamespace(
kind="PULL_REQUEST",
head_commit_sha="b" * 40,
head_commit_url="https://github.example/commit/b",
),
gaps=(AssessmentGap(None, "UNKNOWN", ("HEAD_COMMIT_MISMATCH",)),),
),
"proofline_review status=INCOMPLETE reviewed_commit="
+ "b" * 40
+ " pinned_revision=unknown reason_codes=HEAD_COMMIT_MISMATCH",
),
(
ReviewOutcome(
"INCOMPLETE",
"https://example.invalid/reviews/1",
review=SimpleNamespace(
kind="PULL_REQUEST", head_commit_sha=None, head_commit_url=None
),
gaps=(AssessmentGap(None, "UNKNOWN", ("HEAD_COMMIT_UNAVAILABLE",)),),
),
"proofline_review status=INCOMPLETE reviewed_commit=unknown pinned_revision=unknown "
"reason_codes=HEAD_COMMIT_UNAVAILABLE",
),
(
ReviewOutcome("TIMED_OUT", "https://example.invalid/reviews/1"),
"proofline_review status=TIMED_OUT reviewed_commit=unknown pinned_revision=unknown "
"reason_codes=unknown",
),
],
)
def test_format_review_outcome_reports_operator_facts(
outcome: ReviewOutcome, expected: str
) -> None:
assert format_review_outcome(outcome) == expected


@pytest.mark.parametrize("status", ("COMPLETED", "INCOMPLETE", "FAILED", "STOPPED", "TIMED_OUT"))
def test_format_review_outcome_keeps_recognized_statuses(status: str) -> None:
outcome = ReviewOutcome(cast(OutcomeStatus, status), "https://example.invalid/reviews/1")

assert format_review_outcome(outcome).startswith(f"proofline_review status={status} ")


def test_format_review_outcome_replaces_an_unsupported_status_without_mutating_it() -> None:
outcome = ReviewOutcome(
cast(OutcomeStatus, "FORGED\nSTATUS"), "https://example.invalid/reviews/1"
)

rendered = format_review_outcome(outcome)

assert rendered.startswith("proofline_review status=unknown ")
assert "\n" not in rendered
assert outcome.status == "FORGED\nSTATUS"


def test_format_review_outcome_does_not_disclose_untrusted_values() -> None:
outcome = ReviewOutcome(
"INCOMPLETE",
"https://example.invalid/reviews/1",
review=SimpleNamespace(
kind="PULL_REQUEST",
head_commit_sha="requested-sha-should-not-appear",
head_commit_url="https://github.example/commit/not-a-sha",
),
gaps=(AssessmentGap(None, "UNKNOWN", ("<server prose>",)),),
)

assert format_review_outcome(outcome) == (
"proofline_review status=INCOMPLETE reviewed_commit=unknown pinned_revision=unknown "
"reason_codes=unknown"
)


@pytest.mark.parametrize(
"head_commit_url",
(
"not-a-url",
"https://github.example/commit/a\nFORGED",
),
)
def test_format_review_outcome_requires_a_safe_recorded_commit_url(
head_commit_url: str,
) -> None:
outcome = ReviewOutcome(
"COMPLETED",
"https://example.invalid/reviews/1",
review=SimpleNamespace(
kind="PULL_REQUEST",
head_commit_sha="a" * 40,
head_commit_url=head_commit_url,
),
)

assert format_review_outcome(outcome) == (
"proofline_review status=COMPLETED reviewed_commit=unknown pinned_revision=unknown "
"reason_codes=unknown"
)


def test_format_review_outcome_preserves_summary_when_commit_url_parsing_fails(
monkeypatch: pytest.MonkeyPatch,
) -> None:
def reject_url(value: str) -> None:
raise ValueError("could not parse commit URL")

monkeypatch.setattr("proofline.outcomes.urlsplit", reject_url)
outcome = ReviewOutcome(
"COMPLETED",
"https://example.invalid/reviews/1",
review=SimpleNamespace(
kind="PULL_REQUEST",
head_commit_sha="a" * 40,
head_commit_url="https://github.example/commit/a",
),
)

assert format_review_outcome(outcome) == (
"proofline_review status=COMPLETED reviewed_commit=unknown pinned_revision=unknown "
"reason_codes=unknown"
)


def test_format_review_outcome_reports_a_patch_revision_without_claiming_a_commit() -> None:
outcome = ReviewOutcome(
"INCOMPLETE",
"https://example.invalid/reviews/1",
review=SimpleNamespace(
kind="PATCH",
head_commit_sha="derived-revision-42",
head_commit_url=None,
),
gaps=(AssessmentGap(None, "UNKNOWN", ("HEAD_COMMIT_MISMATCH",)),),
)

assert format_review_outcome(outcome) == (
"proofline_review status=INCOMPLETE reviewed_commit=unknown "
"pinned_revision=derived-revision-42 reason_codes=HEAD_COMMIT_MISMATCH"
)


def test_format_review_outcome_uses_the_recorded_commit_not_the_moving_provider_head() -> None:
outcome = ReviewOutcome(
"COMPLETED",
"https://example.invalid/reviews/1",
review=SimpleNamespace(
kind="PULL_REQUEST",
head_commit_sha="a" * 40,
head_commit_url="https://github.example/commit/a",
current_head_commit_sha="c" * 40,
),
)

rendered = format_review_outcome(outcome)

assert "reviewed_commit=" + "a" * 40 in rendered
assert "c" * 40 not in rendered


def test_format_review_outcome_keeps_multiline_values_to_one_log_record() -> None:
outcome = ReviewOutcome(
"INCOMPLETE",
"https://example.invalid/reviews/1",
review=SimpleNamespace(
kind="PATCH",
head_commit_sha="derived\nrevision",
head_commit_url=None,
),
gaps=(AssessmentGap(None, "UNKNOWN", ("HEAD_COMMIT_MISMATCH\nFORGED",)),),
)
before = outcome

rendered = format_review_outcome(outcome)

assert rendered == (
"proofline_review status=INCOMPLETE reviewed_commit=unknown pinned_revision=unknown "
"reason_codes=unknown"
)
assert "\n" not in rendered
assert outcome is before


@pytest.mark.parametrize(
("review", "expected"),
[
(
None,
"proofline_review status=TIMED_OUT reviewed_commit=unknown pinned_revision=unknown "
"reason_codes=unknown",
),
(
SimpleNamespace(
kind="PULL_REQUEST",
head_commit_sha="d" * 40,
head_commit_url="https://github.example/commit/d",
),
"proofline_review status=TIMED_OUT reviewed_commit="
+ "d" * 40
+ " pinned_revision=unknown reason_codes=unknown",
),
],
)
def test_format_review_outcome_keeps_timeout_status_with_or_without_a_retained_review(
review: object, expected: str
) -> None:
outcome = ReviewOutcome("TIMED_OUT", "https://example.invalid/reviews/1", review=review)

assert format_review_outcome(outcome) == expected
assert outcome.status == "TIMED_OUT"