Skip to content
Merged
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
20 changes: 12 additions & 8 deletions src/autoskillit/execution/github_review/_poster_post_attempt.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,21 @@ async def _reconcile_remote_comment_set(
payload: Mapping[str, Any],
findings: tuple[AdmittedFinding, ...],
) -> _poster_support.Reconciliation:
comments = await gateway.list_review_comments(
comments = await gateway.list_pull_review_comments(
request.repository,
request.pr_number,
review_id,
)
if not comments.succeeded or not isinstance(comments.data, list):
return _poster_support.Reconciliation(
ReviewReconciliationResult.UNCERTAIN,
error=comments.error or "could not read review comments",
)
selected = _poster_support.comments_for_review_ids(comments.data, {review_id})
if selected is None:
return _poster_support.Reconciliation(
ReviewReconciliationResult.UNCERTAIN,
error=_poster_support.UNSCOPABLE_MARKER_ERROR,
)
expected_comments = payload.get("comments")
if not isinstance(expected_comments, list) or len(expected_comments) != len(findings):
return _poster_support.Reconciliation(
Expand All @@ -121,9 +126,10 @@ async def _reconcile_remote_comment_set(
)
candidates = [
item
for item in comments.data
if isinstance(item, Mapping)
and _poster_support.remote_comment_matches(item, expected, finding.digest)
for item in selected
if _poster_support.remote_comment_matches(
item, expected, finding.digest, reviewed_head_sha=request.head_sha
)
]
if len(candidates) != 1:
return _poster_support.Reconciliation(
Expand All @@ -139,9 +145,7 @@ async def _reconcile_remote_comment_set(
used_remote_ids.add(comment_id)
matched_ids.append((finding.canonical_index, comment_id))
marker_count = sum(
isinstance(item, Mapping)
and _poster_support.FINDING_MARKER.split("{digest}", 1)[0] in str(item.get("body", ""))
for item in comments.data
_poster_support.FINDING_MARKER_PREFIX in str(item.get("body", "")) for item in selected
)
if marker_count != len(findings):
return _poster_support.Reconciliation(
Expand Down
61 changes: 31 additions & 30 deletions src/autoskillit/execution/github_review/_poster_reconcile.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from __future__ import annotations

import json
from collections.abc import Mapping
from typing import TYPE_CHECKING

from autoskillit.core import (
Expand Down Expand Up @@ -210,38 +209,40 @@ async def scan_remote_findings(
reviews.error or "could not list reviews for remote deduplication",
)
found: dict[str, int] = {}
for review in reviews.data:
if (
not isinstance(review, Mapping)
or _poster_support.nested_string(review, "user", "login") != authenticated_login
):
continue
review_id = _poster_support.positive_int(review.get("id"))
if review_id is None:
return _poster_support.RemoteFindingScan(False, found, "review id was invalid")
comments = await poster.gateway.list_review_comments(
request.repository,
request.pr_number,
review_id,
authored = [
review
for review in reviews.data
if _poster_support.nested_string(review, "user", "login") == authenticated_login
]
if any(_poster_support.positive_int(review.get("id")) is None for review in authored):
return _poster_support.RemoteFindingScan(False, found, "review id was invalid")
review_ids = {review["id"] for review in authored}
if not review_ids:
return _poster_support.RemoteFindingScan(True, found)
comments = await poster.gateway.list_pull_review_comments(
request.repository, request.pr_number
)
if not comments.succeeded or not isinstance(comments.data, list):
return _poster_support.RemoteFindingScan(
False,
found,
comments.error or "could not list comments for remote deduplication",
)
if not comments.succeeded or not isinstance(comments.data, list):
selected = _poster_support.comments_for_review_ids(comments.data, review_ids)
if selected is None:
return _poster_support.RemoteFindingScan(
False, found, _poster_support.UNSCOPABLE_MARKER_ERROR
)
for comment in selected:
digest = _poster_support.finding_marker_digest(str(comment.get("body", "")))
comment_id = _poster_support.positive_int(comment.get("id"))
if digest is None or comment_id is None:
continue
previous = found.setdefault(digest, comment_id)
if previous != comment_id:
return _poster_support.RemoteFindingScan(
False,
found,
comments.error or "could not list comments for remote deduplication",
"duplicate remote finding markers made deduplication ambiguous",
)
for comment in comments.data:
if not isinstance(comment, Mapping):
continue
digest = _poster_support.finding_marker_digest(str(comment.get("body", "")))
comment_id = _poster_support.positive_int(comment.get("id"))
if digest is None or comment_id is None:
continue
previous = found.setdefault(digest, comment_id)
if previous != comment_id:
return _poster_support.RemoteFindingScan(
False,
found,
"duplicate remote finding markers made deduplication ambiguous",
)
return _poster_support.RemoteFindingScan(True, found)
36 changes: 31 additions & 5 deletions src/autoskillit/execution/github_review/_poster_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

OPERATION_MARKER = "<!-- autoskillit-review-operation:{key} -->"
FINDING_MARKER = "<!-- autoskillit-review-finding:{digest} -->"
FINDING_MARKER_PREFIX = FINDING_MARKER.split("{digest}", 1)[0]
UNSCOPABLE_MARKER_ERROR = "review comment marker association could not be verified"


@dataclass(frozen=True, slots=True)
Expand Down Expand Up @@ -134,16 +136,40 @@ def remote_comment_matches(
remote: Mapping[str, Any],
expected: Mapping[str, Any],
finding_digest: str,
*,
reviewed_head_sha: str,
) -> bool:
if remote.get("body") != expected.get("body"):
return False
if finding_marker_digest(str(remote.get("body", ""))) != finding_digest:
return False
for key in ("path", "line", "side", "start_line", "start_side"):
expected_value = expected.get(key)
if expected_value is not None and remote.get(key) != expected_value:
return False
return True
if (
remote.get("path") != expected["path"]
or remote.get("original_commit_id") != reviewed_head_sha
or positive_int(remote.get("original_line")) != expected["line"]
or "original_start_line" not in remote
or remote.get("side") != expected["side"]
or remote.get("start_side") != expected.get("start_side")
):
return False
if expected.get("start_line") is None:
return remote["original_start_line"] is None
return positive_int(remote["original_start_line"]) == expected["start_line"]


def comments_for_review_ids(
comments: list[Any], review_ids: set[int]
) -> list[Mapping[str, Any]] | None:
selected: list[Mapping[str, Any]] = []
for comment in comments:
if not isinstance(comment, Mapping):
continue
review_id = positive_int(comment.get("pull_request_review_id"))
if review_id in review_ids:
selected.append(comment)
elif review_id is None and FINDING_MARKER_PREFIX in str(comment.get("body", "")):
return None
return selected


def finding_marker_digest(body: str) -> str | None:
Expand Down
18 changes: 12 additions & 6 deletions src/autoskillit/execution/github_review/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,12 @@ async def create_review(
async def list_reviews(self, repository: str, pr_number: int) -> GatewayResult:
return await self._paginate(f"/repos/{repository}/pulls/{pr_number}/reviews?per_page=100")

async def list_review_comments(
async def list_pull_review_comments(
self,
repository: str,
pr_number: int,
review_id: int,
) -> GatewayResult:
return await self._paginate(
f"/repos/{repository}/pulls/{pr_number}/reviews/{review_id}/comments?per_page=100"
)
return await self._paginate(f"/repos/{repository}/pulls/{pr_number}/comments?per_page=100")

async def _paginate(self, path: str) -> GatewayResult:
items: list[Any] = []
Expand All @@ -109,7 +106,16 @@ async def _paginate(self, path: str) -> GatewayResult:
error="GitHub pagination response was not a list",
)
items.extend(response.data)
next_path = self._next_page_path(response.headers.get("link"))
try:
next_path = self._next_page_path(response.headers.get("link"))
except ValueError as exc:
return GatewayResult(
status_code=response.status_code,
data=None,
headers=response.headers,
response_class=ReviewResponseClass.CLIENT_ERROR,
error=str(exc),
)
return GatewayResult(
status_code=200,
data=items,
Expand Down
26 changes: 23 additions & 3 deletions src/autoskillit/server/_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

from __future__ import annotations

import asyncio
import os
import subprocess
from collections.abc import Callable
import time
from collections.abc import Awaitable, Callable
from pathlib import Path
from typing import Any

Expand Down Expand Up @@ -121,6 +123,25 @@
logger = get_logger(__name__)


def _make_github_review_poster(
*,
ledger: GitHubReviewLedger,
gateway: DefaultGitHubReviewGateway,
review_comment_cap: int = 50,
clock: Callable[[], float] = time.monotonic,
sleeper: Callable[[float], Awaitable[None]] = asyncio.sleep,
wall_clock: Callable[[], float] = time.time,
) -> DefaultGitHubReviewPoster:
"""Compose review publication with one ledger for receipts and mutation pacing."""
return DefaultGitHubReviewPoster(
ledger=ledger,
coordinator=GitHubReviewMutationCoordinator(ledger=ledger, clock=clock, sleeper=sleeper),
gateway=gateway,
review_comment_cap=review_comment_cap,
wall_clock=wall_clock,
)


def make_recipe_execution(
*,
snapshot: RecipeExecutionSnapshot,
Expand Down Expand Up @@ -437,9 +458,8 @@ def make_context(
token_factory=token_factory,
api_log=github_api_log,
)
github_review_poster = DefaultGitHubReviewPoster(
github_review_poster = _make_github_review_poster(
ledger=github_review_ledger,
coordinator=GitHubReviewMutationCoordinator(ledger=github_review_ledger),
gateway=github_review_gateway,
review_comment_cap=config.github.review_comment_cap,
)
Expand Down
18 changes: 13 additions & 5 deletions tests/execution/github_review/fakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ def __init__(
head_sha: str = "a" * 40,
authenticated_login: str = "autoskillit-bot",
pr_author_login: str = "pull-author",
next_review_id: int = 700,
next_comment_id: int = 900,
) -> None:
self.clock = clock
self.outcomes = list(outcomes or [CreateOutcome(200, commit=True)])
Expand All @@ -93,8 +95,8 @@ def __init__(
self.review_read_calls = 0
self.comment_read_calls = 0
self.fail_reads = False
self._next_review_id = 700
self._next_comment_id = 900
self._next_review_id = next_review_id
self._next_comment_id = next_comment_id

async def scope_material(self) -> CredentialScopeMaterial:
self.scope_calls += 1
Expand Down Expand Up @@ -171,11 +173,10 @@ async def list_reviews(self, repository: str, pr_number: int) -> GatewayResult:
)
return _success(list(self.reviews))

async def list_review_comments(
async def list_pull_review_comments(
self,
repository: str,
pr_number: int,
review_id: int,
) -> GatewayResult:
del repository, pr_number
self.comment_read_calls += 1
Expand All @@ -187,7 +188,9 @@ async def list_review_comments(
response_class=ReviewResponseClass.TRANSPORT_ERROR,
error="injected comment read failure",
)
return _success(list(self.comments_by_review.get(review_id, [])))
return _success(
[comment for comments in self.comments_by_review.values() for comment in comments]
)

async def create_review_comment(
self,
Expand Down Expand Up @@ -224,6 +227,11 @@ def _commit(self, payload: dict[str, Any]) -> int:
{
**comment,
"id": self._next_comment_id,
"pull_request_review_id": review_id,
"commit_id": payload["commit_id"],
"original_commit_id": payload["commit_id"],
"original_line": comment["line"],
"original_start_line": comment.get("start_line"),
}
)
self._next_comment_id += 1
Expand Down
Loading
Loading