mlai-code 0.3.3 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/checkpoints.mjs +217 -0
- package/src/cli.mjs +153 -26
- package/src/cloud.mjs +2 -0
- package/src/engine.mjs +1 -1
- package/src/headless.mjs +25 -9
- package/src/home.mjs +3 -2
- package/src/host.mjs +141 -13
- package/src/ide-quick.mjs +147 -0
- package/src/ide.mjs +182 -0
- package/src/netbox.mjs +1 -1
- package/src/parity.mjs +17 -0
- package/src/relay.mjs +1 -1
- package/src/render.mjs +9 -2
- package/src/runtime.mjs +2 -0
- package/src/server.mjs +46 -7
- package/src/session.mjs +31 -8
- package/src/tui/acp.mjs +121 -19
- package/src/tui/app.mjs +2 -1
- package/src/tui/chat.mjs +35 -10
- package/src/tui/cmd-config.mjs +3 -1
- package/src/tui/cmd-core.mjs +9 -5
- package/src/tui/cmd-extensibility.mjs +5 -0
- package/src/tui/extensions-local.mjs +2 -0
- package/src/tui/mcp-config.mjs +22 -4
- package/src/tui/review-cli.mjs +15 -4
- package/src/tui/snapshots.mjs +57 -16
- package/src/two-words.mjs +14 -8
- package/src/view-sync.mjs +48 -0
- package/src/winenv.mjs +1 -1
- package/src/workspace-api.mjs +21 -4
- package/vendor/client-sdk/index.cjs +2 -1
- package/vendor/cloud-cli/mlai.mjs +23 -11
- package/worker/architecture_worker.py +33 -4
- package/worker/browser_tool.py +19 -1
- package/worker/chat_fork.py +4 -1
- package/worker/checkpoints.py +71 -8
- package/worker/command_safety.py +56 -3
- package/worker/connected_turn.py +30 -0
- package/worker/conversation.py +81 -11
- package/worker/git_sync.py +4 -4
- package/worker/image_generation.py +2 -2
- package/worker/kimi_bridge.py +247 -27
- package/worker/kimi_gates.py +25 -6
- package/worker/kimi_hook.py +153 -12
- package/worker/media_tools.py +4 -1
- package/worker/mlai_hooks.py +222 -3
- package/worker/mlai_mcp.py +26 -6
- package/worker/mlai_review/README.md +35 -0
- package/worker/mlai_review/__init__.py +1 -0
- package/worker/mlai_review/__main__.py +261 -0
- package/worker/mlai_review/_compat.py +29 -0
- package/worker/mlai_review/analysis/__init__.py +3 -0
- package/worker/mlai_review/analysis/detect.py +50 -0
- package/worker/mlai_review/analysis/main.py +128 -0
- package/worker/mlai_review/analysis/messages.py +8 -0
- package/worker/mlai_review/analysis/tools.py +194 -0
- package/worker/mlai_review/application/__init__.py +1 -0
- package/worker/mlai_review/application/asking.py +53 -0
- package/worker/mlai_review/application/challengers.py +142 -0
- package/worker/mlai_review/application/context_builder.py +194 -0
- package/worker/mlai_review/application/errors.py +17 -0
- package/worker/mlai_review/application/feedback.py +52 -0
- package/worker/mlai_review/application/matching.py +46 -0
- package/worker/mlai_review/application/messages.py +41 -0
- package/worker/mlai_review/application/ports.py +222 -0
- package/worker/mlai_review/application/prompts.py +342 -0
- package/worker/mlai_review/application/review_service.py +200 -0
- package/worker/mlai_review/application/reviewers.py +438 -0
- package/worker/mlai_review/application/streaming.py +91 -0
- package/worker/mlai_review/application/tools.py +138 -0
- package/worker/mlai_review/application/usage.py +145 -0
- package/worker/mlai_review/application/write_check.py +46 -0
- package/worker/mlai_review/contracts/__init__.py +5 -0
- package/worker/mlai_review/contracts/analysis.py +57 -0
- package/worker/mlai_review/contracts/review.py +42 -0
- package/worker/mlai_review/domain/__init__.py +4 -0
- package/worker/mlai_review/domain/changes.py +77 -0
- package/worker/mlai_review/domain/context.py +147 -0
- package/worker/mlai_review/domain/diagnostics.py +239 -0
- package/worker/mlai_review/domain/feedback.py +216 -0
- package/worker/mlai_review/domain/findings.py +505 -0
- package/worker/mlai_review/domain/lifecycle.py +85 -0
- package/worker/mlai_review/domain/partition.py +42 -0
- package/worker/mlai_review/domain/relations.py +166 -0
- package/worker/mlai_review/domain/relevance.py +202 -0
- package/worker/mlai_review/domain/report.py +99 -0
- package/worker/mlai_review/domain/structure.py +56 -0
- package/worker/mlai_review/domain/writes.py +86 -0
- package/worker/mlai_review/eval.py +221 -0
- package/worker/mlai_review/eval_cases/clean-rename.json +11 -0
- package/worker/mlai_review/eval_cases/missing-ownership-check.json +22 -0
- package/worker/mlai_review/eval_cases/off-by-one.json +22 -0
- package/worker/mlai_review/eval_cases/sql-injection.json +20 -0
- package/worker/mlai_review/feedback_report.py +60 -0
- package/worker/mlai_review/infrastructure/__init__.py +1 -0
- package/worker/mlai_review/infrastructure/analysis/__init__.py +1 -0
- package/worker/mlai_review/infrastructure/analysis/none.py +12 -0
- package/worker/mlai_review/infrastructure/analysis/translate.py +17 -0
- package/worker/mlai_review/infrastructure/github/__init__.py +0 -0
- package/worker/mlai_review/infrastructure/github/client.py +445 -0
- package/worker/mlai_review/infrastructure/github/feedback.py +212 -0
- package/worker/mlai_review/infrastructure/github/http.py +31 -0
- package/worker/mlai_review/infrastructure/github/rendering.py +398 -0
- package/worker/mlai_review/infrastructure/github/token.py +20 -0
- package/worker/mlai_review/infrastructure/local/__init__.py +1 -0
- package/worker/mlai_review/infrastructure/local/analyzer.py +48 -0
- package/worker/mlai_review/infrastructure/local/events.py +100 -0
- package/worker/mlai_review/infrastructure/local/git_reader.py +233 -0
- package/worker/mlai_review/infrastructure/local/publisher.py +138 -0
- package/worker/mlai_review/infrastructure/messages.py +54 -0
- package/worker/mlai_review/infrastructure/models/__init__.py +0 -0
- package/worker/mlai_review/infrastructure/models/openai_compatible.py +269 -0
- package/worker/mlai_review/infrastructure/structure/__init__.py +1 -0
- package/worker/mlai_review/infrastructure/structure/heuristic.py +186 -0
- package/worker/mlai_review/infrastructure/structure/tree_sitter.py +186 -0
- package/worker/mlai_review/infrastructure/workspace/__init__.py +1 -0
- package/worker/mlai_review/infrastructure/workspace/local.py +105 -0
- package/worker/mlai_review/infrastructure/workspace/memory.py +77 -0
- package/worker/mlai_review/infrastructure/workspace/paths.py +17 -0
- package/worker/mlai_review/pipeline.py +89 -0
- package/worker/mlai_review/runtime.py +194 -0
- package/worker/mlai_review/settings.py +139 -0
- package/worker/preview.py +15 -0
- package/worker/product_facts.py +3 -1
- package/worker/repo_policy.py +5 -0
- package/worker/roles.py +29 -2
- package/worker/runner.py +126 -18
- package/worker/runtime_control.py +64 -3
- package/worker/secret_scan.py +3 -2
- package/worker/server_memory.py +122 -0
- package/worker/subagents.py +6 -2
- package/worker/tool_catalog.py +1 -1
- package/worker/verification.py +113 -8
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
"""GitHub adapters for the orchestrator's ports."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
from typing import Any, ClassVar
|
|
7
|
+
|
|
8
|
+
import httpx
|
|
9
|
+
|
|
10
|
+
from mlai_review.contracts.review import ReviewPullRequestInput
|
|
11
|
+
from mlai_review.application.errors import ReviewError
|
|
12
|
+
from mlai_review.application.ports import PullRequestReader, ReviewPublisher
|
|
13
|
+
from mlai_review.domain.changes import ChangeSet, FileChange
|
|
14
|
+
from mlai_review.domain.context import PullRequestInfo
|
|
15
|
+
from mlai_review.domain.findings import Finding
|
|
16
|
+
from mlai_review.domain.lifecycle import PriorFinding
|
|
17
|
+
from mlai_review.domain.report import ReviewReport, Verdict
|
|
18
|
+
from mlai_review.infrastructure import messages
|
|
19
|
+
from mlai_review.infrastructure.github import rendering
|
|
20
|
+
from mlai_review.infrastructure.github.token import StaticTokenProvider
|
|
21
|
+
from mlai_review.infrastructure.github.http import github_headers, raise_for_github_status
|
|
22
|
+
|
|
23
|
+
_log = logging.getLogger("mlai.orchestrator.github")
|
|
24
|
+
_PAGE_SIZE = 100
|
|
25
|
+
_MAX_PAGES = 30 # GitHub returns at most 3,000 files for a pull request.
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class GitHubPullRequestReader(PullRequestReader):
|
|
29
|
+
_PR: ClassVar[dict[str, str]] = {"pull_requests": "read"}
|
|
30
|
+
_CONTENTS: ClassVar[dict[str, str]] = {"contents": "read"}
|
|
31
|
+
|
|
32
|
+
def __init__(self, *, http: httpx.AsyncClient, tokens: StaticTokenProvider) -> None:
|
|
33
|
+
self._http = http
|
|
34
|
+
self._tokens = tokens
|
|
35
|
+
|
|
36
|
+
async def pull_request(self, target: ReviewPullRequestInput) -> PullRequestInfo:
|
|
37
|
+
response = await self._http.get(
|
|
38
|
+
f"/repos/{target.owner}/{target.repo}/pulls/{target.pull_number}",
|
|
39
|
+
headers=await _auth(self._tokens, target, self._PR),
|
|
40
|
+
)
|
|
41
|
+
raise_for_github_status(
|
|
42
|
+
response, messages.OP_READ_PULL_REQUEST.format(number=target.pull_number)
|
|
43
|
+
)
|
|
44
|
+
body = response.json()
|
|
45
|
+
return PullRequestInfo(title=body.get("title") or "", body=body.get("body") or "")
|
|
46
|
+
|
|
47
|
+
async def head(self, target: ReviewPullRequestInput) -> tuple[str, str]:
|
|
48
|
+
response = await self._http.get(
|
|
49
|
+
f"/repos/{target.owner}/{target.repo}/pulls/{target.pull_number}",
|
|
50
|
+
headers=await _auth(self._tokens, target, self._PR),
|
|
51
|
+
)
|
|
52
|
+
raise_for_github_status(
|
|
53
|
+
response, messages.OP_READ_PULL_REQUEST.format(number=target.pull_number)
|
|
54
|
+
)
|
|
55
|
+
body = response.json()
|
|
56
|
+
return str(body["head"]["sha"]), str(body["base"]["sha"])
|
|
57
|
+
|
|
58
|
+
async def changed_files(
|
|
59
|
+
self, target: ReviewPullRequestInput, *, since: str | None = None
|
|
60
|
+
) -> ChangeSet:
|
|
61
|
+
if since is not None:
|
|
62
|
+
incremental = await self._changed_since(target, since)
|
|
63
|
+
if incremental is not None:
|
|
64
|
+
return incremental
|
|
65
|
+
headers = await _auth(self._tokens, target, self._PR)
|
|
66
|
+
operation = messages.OP_LIST_FILES.format(repository=target.repository_full_name)
|
|
67
|
+
files: list[FileChange] = []
|
|
68
|
+
for page in range(1, _MAX_PAGES + 1):
|
|
69
|
+
response = await self._http.get(
|
|
70
|
+
f"/repos/{target.owner}/{target.repo}/pulls/{target.pull_number}/files",
|
|
71
|
+
params={"per_page": _PAGE_SIZE, "page": page},
|
|
72
|
+
headers=headers,
|
|
73
|
+
)
|
|
74
|
+
raise_for_github_status(response, operation)
|
|
75
|
+
batch = response.json()
|
|
76
|
+
files += _file_changes(batch)
|
|
77
|
+
if len(batch) < _PAGE_SIZE:
|
|
78
|
+
break
|
|
79
|
+
return ChangeSet(files=tuple(files))
|
|
80
|
+
|
|
81
|
+
async def _changed_since(self, target: ReviewPullRequestInput, since: str) -> ChangeSet | None:
|
|
82
|
+
"""The files changed between a commit and the head, or None when the commit is not
|
|
83
|
+
an ancestor of the head (a force push), in which case the whole change is needed."""
|
|
84
|
+
headers = await _auth(self._tokens, target, self._CONTENTS)
|
|
85
|
+
operation = messages.OP_COMPARE.format(since=since[:12], head=target.head_sha[:12])
|
|
86
|
+
files: list[FileChange] = []
|
|
87
|
+
for page in range(1, _MAX_PAGES + 1):
|
|
88
|
+
response = await self._http.get(
|
|
89
|
+
f"/repos/{target.owner}/{target.repo}/compare/{since}...{target.head_sha}",
|
|
90
|
+
params={"per_page": _PAGE_SIZE, "page": page},
|
|
91
|
+
headers=headers,
|
|
92
|
+
)
|
|
93
|
+
if response.status_code == 404:
|
|
94
|
+
return None # the commit is gone from the branch
|
|
95
|
+
raise_for_github_status(response, operation)
|
|
96
|
+
body = response.json()
|
|
97
|
+
if body.get("status") == "diverged":
|
|
98
|
+
return None
|
|
99
|
+
batch = body.get("files") or []
|
|
100
|
+
files += _file_changes(batch)
|
|
101
|
+
if len(batch) < _PAGE_SIZE:
|
|
102
|
+
break
|
|
103
|
+
return ChangeSet(files=tuple(files), since=since)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _file_changes(batch: list[dict[str, Any]]) -> list[FileChange]:
|
|
107
|
+
return [
|
|
108
|
+
FileChange(
|
|
109
|
+
path=f["filename"],
|
|
110
|
+
status=f["status"],
|
|
111
|
+
additions=f["additions"],
|
|
112
|
+
deletions=f["deletions"],
|
|
113
|
+
patch=f.get("patch"),
|
|
114
|
+
)
|
|
115
|
+
for f in batch
|
|
116
|
+
]
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
# The publication id of a review shown as a commit status rather than a check run.
|
|
120
|
+
STATUS_PUBLICATION = 0
|
|
121
|
+
# Neither a check run nor a commit status could be written (a personal token without the Commit
|
|
122
|
+
# statuses permission): the review still runs and posts its comments and summary.
|
|
123
|
+
NO_PUBLICATION = -1
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
class GitHubReviewPublisher(ReviewPublisher):
|
|
127
|
+
"""Publishes to two GitHub surfaces: the check run (always) and a pull request review with
|
|
128
|
+
inline comments (for findings on lines the diff shows)."""
|
|
129
|
+
|
|
130
|
+
_CHECKS: ClassVar[dict[str, str]] = {"checks": "write"}
|
|
131
|
+
_REVIEWS: ClassVar[dict[str, str]] = {"pull_requests": "write"}
|
|
132
|
+
|
|
133
|
+
def __init__(
|
|
134
|
+
self,
|
|
135
|
+
*,
|
|
136
|
+
http: httpx.AsyncClient,
|
|
137
|
+
tokens: StaticTokenProvider,
|
|
138
|
+
web_base_url: str = "https://github.com",
|
|
139
|
+
details_url: str | None = None,
|
|
140
|
+
) -> None:
|
|
141
|
+
self._http = http
|
|
142
|
+
self._tokens = tokens
|
|
143
|
+
self._web = web_base_url.rstrip("/")
|
|
144
|
+
self._details_url = details_url
|
|
145
|
+
self._login: str | None = None
|
|
146
|
+
self._login_read = False
|
|
147
|
+
|
|
148
|
+
async def _mine(self, target: ReviewPullRequestInput, comment: dict[str, Any]) -> bool:
|
|
149
|
+
"""Whether a comment was written by the account this review posts as. Markers alone
|
|
150
|
+
can be copied by anyone who may comment, so a pasted summary or finding marker must
|
|
151
|
+
not steer the review. When the account cannot be read, markers are trusted as before."""
|
|
152
|
+
if not self._login_read:
|
|
153
|
+
self._login_read = True
|
|
154
|
+
response = await self._http.get("/user", headers=await _auth(self._tokens, target, {}))
|
|
155
|
+
if response.is_success:
|
|
156
|
+
login = (response.json() or {}).get("login")
|
|
157
|
+
self._login = str(login).lower() if login else None
|
|
158
|
+
if self._login is None:
|
|
159
|
+
return True
|
|
160
|
+
return str((comment.get("user") or {}).get("login") or "").lower() == self._login
|
|
161
|
+
|
|
162
|
+
async def start(self, target: ReviewPullRequestInput) -> int:
|
|
163
|
+
"""A check run when the token may create one (a GitHub App's); otherwise a commit status
|
|
164
|
+
under the same name, which is what an account's own token can write."""
|
|
165
|
+
response = await self._http.post(
|
|
166
|
+
f"/repos/{target.owner}/{target.repo}/check-runs",
|
|
167
|
+
headers=await _auth(self._tokens, target, self._CHECKS),
|
|
168
|
+
json={
|
|
169
|
+
"name": rendering.CHECK_NAME,
|
|
170
|
+
"head_sha": target.head_sha,
|
|
171
|
+
"status": "in_progress",
|
|
172
|
+
"external_id": target.triggered_by_delivery,
|
|
173
|
+
"output": {
|
|
174
|
+
"title": rendering.IN_PROGRESS_TITLE,
|
|
175
|
+
"summary": rendering.IN_PROGRESS_SUMMARY,
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
)
|
|
179
|
+
if response.status_code in (403, 404):
|
|
180
|
+
if not await self._status(target, "pending", rendering.IN_PROGRESS_SUMMARY):
|
|
181
|
+
_log.warning(messages.STATUS_SKIPPED, target.pull_number)
|
|
182
|
+
return NO_PUBLICATION
|
|
183
|
+
return STATUS_PUBLICATION
|
|
184
|
+
raise_for_github_status(
|
|
185
|
+
response, messages.OP_CREATE_CHECK.format(repository=target.repository_full_name)
|
|
186
|
+
)
|
|
187
|
+
check_id: int = response.json()["id"]
|
|
188
|
+
return check_id
|
|
189
|
+
|
|
190
|
+
async def _status(self, target: ReviewPullRequestInput, state: str, description: str) -> bool:
|
|
191
|
+
"""Write the commit status; False when the token may not (403 or 404)."""
|
|
192
|
+
body: dict[str, Any] = {
|
|
193
|
+
"state": state,
|
|
194
|
+
"context": rendering.CHECK_NAME,
|
|
195
|
+
"description": description[: rendering.MAX_STATUS_CHARS],
|
|
196
|
+
}
|
|
197
|
+
if self._details_url:
|
|
198
|
+
body["target_url"] = self._details_url
|
|
199
|
+
response = await self._http.post(
|
|
200
|
+
f"/repos/{target.owner}/{target.repo}/statuses/{target.head_sha}",
|
|
201
|
+
headers=await _auth(self._tokens, target, self._CHECKS),
|
|
202
|
+
json=body,
|
|
203
|
+
)
|
|
204
|
+
if response.status_code in (403, 404) and not _rate_limited(response):
|
|
205
|
+
return False
|
|
206
|
+
raise_for_github_status(
|
|
207
|
+
response, messages.OP_CREATE_CHECK.format(repository=target.repository_full_name)
|
|
208
|
+
)
|
|
209
|
+
return True
|
|
210
|
+
|
|
211
|
+
async def complete(
|
|
212
|
+
self, target: ReviewPullRequestInput, publication_id: int, report: ReviewReport
|
|
213
|
+
) -> None:
|
|
214
|
+
for prior in report.resolved:
|
|
215
|
+
await self._resolve(target, prior)
|
|
216
|
+
if publication_id == STATUS_PUBLICATION:
|
|
217
|
+
await self._status(target, rendering.status_state(report), rendering.title(report))
|
|
218
|
+
elif publication_id != NO_PUBLICATION:
|
|
219
|
+
await self._complete_check(target, publication_id, report)
|
|
220
|
+
try:
|
|
221
|
+
await self._write_overview(target, report)
|
|
222
|
+
except ReviewError as exc:
|
|
223
|
+
# Best effort: the check run already carries the same list.
|
|
224
|
+
_log.warning(messages.OVERVIEW_FAILED, target.pull_number, exc)
|
|
225
|
+
|
|
226
|
+
async def _complete_check(
|
|
227
|
+
self, target: ReviewPullRequestInput, publication_id: int, report: ReviewReport
|
|
228
|
+
) -> None:
|
|
229
|
+
response = await self._http.patch(
|
|
230
|
+
f"/repos/{target.owner}/{target.repo}/check-runs/{publication_id}",
|
|
231
|
+
headers=await _auth(self._tokens, target, self._CHECKS),
|
|
232
|
+
json={
|
|
233
|
+
"status": "completed",
|
|
234
|
+
"conclusion": rendering.conclusion(report),
|
|
235
|
+
"output": {"title": rendering.title(report), "summary": rendering.summary(report)},
|
|
236
|
+
},
|
|
237
|
+
)
|
|
238
|
+
raise_for_github_status(
|
|
239
|
+
response, messages.OP_COMPLETE_CHECK.format(check_id=publication_id)
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
async def _write_overview(self, target: ReviewPullRequestInput, report: ReviewReport) -> None:
|
|
243
|
+
"""One conversation comment per pull request, rewritten each review: created when
|
|
244
|
+
there is something worth a look, updated (to say so) when there no longer is."""
|
|
245
|
+
if report.verdict not in (Verdict.ISSUES_FOUND, Verdict.NO_ISSUES, Verdict.TOO_LARGE):
|
|
246
|
+
return
|
|
247
|
+
existing = await self._overview_id(target)
|
|
248
|
+
body = rendering.overview(
|
|
249
|
+
report,
|
|
250
|
+
head_sha=target.head_sha,
|
|
251
|
+
blob_base=f"{self._web}/{target.owner}/{target.repo}/blob",
|
|
252
|
+
)
|
|
253
|
+
headers = await _auth(self._tokens, target, self._REVIEWS)
|
|
254
|
+
base = f"/repos/{target.owner}/{target.repo}/issues"
|
|
255
|
+
response = (
|
|
256
|
+
await self._http.patch(
|
|
257
|
+
f"{base}/comments/{existing}", headers=headers, json={"body": body}
|
|
258
|
+
)
|
|
259
|
+
if existing is not None
|
|
260
|
+
else await self._http.post(
|
|
261
|
+
f"{base}/{target.pull_number}/comments", headers=headers, json={"body": body}
|
|
262
|
+
)
|
|
263
|
+
)
|
|
264
|
+
raise_for_github_status(
|
|
265
|
+
response, messages.OP_WRITE_OVERVIEW.format(number=target.pull_number)
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
async def _overview_id(self, target: ReviewPullRequestInput) -> int | None:
|
|
269
|
+
found = await self._overview(target)
|
|
270
|
+
return int(found["id"]) if found is not None else None
|
|
271
|
+
|
|
272
|
+
async def last_reviewed(self, target: ReviewPullRequestInput) -> str | None:
|
|
273
|
+
"""The commit the summary comment says was last reviewed. Best effort: without it the
|
|
274
|
+
whole change is reviewed, which is never wrong."""
|
|
275
|
+
try:
|
|
276
|
+
found = await self._overview(target)
|
|
277
|
+
except ReviewError:
|
|
278
|
+
return None
|
|
279
|
+
return rendering.reviewed_in(found.get("body") or "") if found is not None else None
|
|
280
|
+
|
|
281
|
+
async def _overview(self, target: ReviewPullRequestInput) -> dict[str, Any] | None:
|
|
282
|
+
headers = await _auth(self._tokens, target, self._REVIEWS)
|
|
283
|
+
for page in range(1, _MAX_PAGES + 1):
|
|
284
|
+
response = await self._http.get(
|
|
285
|
+
f"/repos/{target.owner}/{target.repo}/issues/{target.pull_number}/comments",
|
|
286
|
+
params={"per_page": _PAGE_SIZE, "page": page},
|
|
287
|
+
headers=headers,
|
|
288
|
+
)
|
|
289
|
+
raise_for_github_status(
|
|
290
|
+
response, messages.OP_LIST_ISSUE_COMMENTS.format(number=target.pull_number)
|
|
291
|
+
)
|
|
292
|
+
batch = response.json()
|
|
293
|
+
for comment in batch:
|
|
294
|
+
# Posted as the connected account, so the marker alone says it is ours.
|
|
295
|
+
if rendering.is_overview(comment.get("body") or "") and await self._mine(
|
|
296
|
+
target, comment
|
|
297
|
+
):
|
|
298
|
+
return dict(comment)
|
|
299
|
+
if len(batch) < _PAGE_SIZE:
|
|
300
|
+
return None
|
|
301
|
+
return None
|
|
302
|
+
|
|
303
|
+
async def reviewed(self, target: ReviewPullRequestInput, sha: str) -> bool:
|
|
304
|
+
response = await self._http.get(
|
|
305
|
+
f"/repos/{target.owner}/{target.repo}/commits/{sha}/check-runs",
|
|
306
|
+
params={"check_name": rendering.CHECK_NAME, "per_page": 10},
|
|
307
|
+
headers=await _auth(self._tokens, target, self._CHECKS),
|
|
308
|
+
)
|
|
309
|
+
if response.status_code in (404, 422):
|
|
310
|
+
return False # unknown commit, or one git no longer has
|
|
311
|
+
if response.status_code == 403 and not _rate_limited(response):
|
|
312
|
+
runs = [] # a personal token without the Checks permission: look at statuses only
|
|
313
|
+
else:
|
|
314
|
+
raise_for_github_status(response, messages.OP_LIST_CHECKS.format(sha=sha[:12]))
|
|
315
|
+
runs = response.json().get("check_runs") or []
|
|
316
|
+
if any(
|
|
317
|
+
r.get("status") == "completed" and r.get("conclusion") in rendering.REVIEWED
|
|
318
|
+
for r in runs
|
|
319
|
+
):
|
|
320
|
+
return True
|
|
321
|
+
statuses = await self._http.get(
|
|
322
|
+
f"/repos/{target.owner}/{target.repo}/commits/{sha}/statuses",
|
|
323
|
+
params={"per_page": 100},
|
|
324
|
+
headers=await _auth(self._tokens, target, self._CHECKS),
|
|
325
|
+
)
|
|
326
|
+
if not statuses.is_success:
|
|
327
|
+
return False
|
|
328
|
+
return any(
|
|
329
|
+
s.get("context") == rendering.CHECK_NAME
|
|
330
|
+
and rendering.status_reviewed(s.get("state") or "", s.get("description") or "")
|
|
331
|
+
for s in statuses.json()
|
|
332
|
+
)
|
|
333
|
+
|
|
334
|
+
async def acknowledge(self, target: ReviewPullRequestInput) -> None:
|
|
335
|
+
"""An eyes reaction on the comment that asked for the review. Best effort: a reaction that cannot be
|
|
336
|
+
added must not stop the review."""
|
|
337
|
+
if target.requested_in_comment is None:
|
|
338
|
+
return
|
|
339
|
+
response = await self._http.post(
|
|
340
|
+
f"/repos/{target.owner}/{target.repo}/issues/comments/"
|
|
341
|
+
f"{target.requested_in_comment}/reactions",
|
|
342
|
+
headers=await _auth(self._tokens, target, self._REVIEWS),
|
|
343
|
+
json={"content": "eyes"},
|
|
344
|
+
)
|
|
345
|
+
if not response.is_success:
|
|
346
|
+
_log.warning(
|
|
347
|
+
messages.ACKNOWLEDGE_SKIPPED, target.requested_in_comment, response.status_code
|
|
348
|
+
)
|
|
349
|
+
|
|
350
|
+
async def post_finding(self, target: ReviewPullRequestInput, finding: Finding) -> None:
|
|
351
|
+
"""One comment per finding, posted as soon as it is confirmed: on its line when the
|
|
352
|
+
diff shows it (GitHub allows nothing else there), otherwise on the file."""
|
|
353
|
+
body: dict[str, Any] = {"commit_id": target.head_sha, **_comment(finding)}
|
|
354
|
+
if not finding.in_diff:
|
|
355
|
+
body = {
|
|
356
|
+
"commit_id": target.head_sha,
|
|
357
|
+
"path": finding.path,
|
|
358
|
+
"subject_type": "file",
|
|
359
|
+
"body": rendering.file_comment(finding),
|
|
360
|
+
}
|
|
361
|
+
response = await self._http.post(
|
|
362
|
+
f"/repos/{target.owner}/{target.repo}/pulls/{target.pull_number}/comments",
|
|
363
|
+
headers=await _auth(self._tokens, target, self._REVIEWS),
|
|
364
|
+
json=body,
|
|
365
|
+
)
|
|
366
|
+
raise_for_github_status(response, messages.OP_POST_COMMENT.format(path=finding.path))
|
|
367
|
+
|
|
368
|
+
async def prior_findings(self, target: ReviewPullRequestInput) -> tuple[PriorFinding, ...]:
|
|
369
|
+
headers = await _auth(self._tokens, target, self._REVIEWS)
|
|
370
|
+
found: list[PriorFinding] = []
|
|
371
|
+
for page in range(1, _MAX_PAGES + 1):
|
|
372
|
+
response = await self._http.get(
|
|
373
|
+
f"/repos/{target.owner}/{target.repo}/pulls/{target.pull_number}/comments",
|
|
374
|
+
params={"per_page": _PAGE_SIZE, "page": page},
|
|
375
|
+
headers=headers,
|
|
376
|
+
)
|
|
377
|
+
raise_for_github_status(
|
|
378
|
+
response, messages.OP_LIST_COMMENTS.format(number=target.pull_number)
|
|
379
|
+
)
|
|
380
|
+
batch = response.json()
|
|
381
|
+
for comment in batch:
|
|
382
|
+
fp = rendering.fingerprint_in(comment.get("body") or "")
|
|
383
|
+
if (
|
|
384
|
+
fp is not None
|
|
385
|
+
and comment.get("in_reply_to_id") is None
|
|
386
|
+
and await self._mine(target, comment)
|
|
387
|
+
):
|
|
388
|
+
found.append(
|
|
389
|
+
PriorFinding(
|
|
390
|
+
fingerprint=fp,
|
|
391
|
+
path=str(comment["path"]),
|
|
392
|
+
comment_id=int(comment["id"]),
|
|
393
|
+
line=comment.get("line"),
|
|
394
|
+
file_level=comment.get("subject_type") == "file",
|
|
395
|
+
title=rendering.title_in(comment.get("body") or ""),
|
|
396
|
+
explanation=rendering.explanation_in(comment.get("body") or ""),
|
|
397
|
+
)
|
|
398
|
+
)
|
|
399
|
+
if len(batch) < _PAGE_SIZE:
|
|
400
|
+
break
|
|
401
|
+
return tuple(found)
|
|
402
|
+
|
|
403
|
+
async def _resolve(self, target: ReviewPullRequestInput, prior: PriorFinding) -> None:
|
|
404
|
+
"""Reply on the earlier comment so the author can resolve the thread. GitHub does not
|
|
405
|
+
let an App resolve review threads ("Resource not accessible by integration"), so the
|
|
406
|
+
reply is the whole of what we can do. Best effort: a comment that is gone must not fail
|
|
407
|
+
the review."""
|
|
408
|
+
headers = await _auth(self._tokens, target, self._REVIEWS)
|
|
409
|
+
base = f"/repos/{target.owner}/{target.repo}/pulls/{target.pull_number}"
|
|
410
|
+
reply = await self._http.post(
|
|
411
|
+
f"{base}/comments/{prior.comment_id}/replies",
|
|
412
|
+
headers=headers,
|
|
413
|
+
json={"body": rendering.resolved_reply(target.head_sha)},
|
|
414
|
+
)
|
|
415
|
+
if not reply.is_success:
|
|
416
|
+
_log.warning(messages.RESOLVE_SKIPPED, prior.comment_id, reply.status_code)
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
def _rate_limited(response: httpx.Response) -> bool:
|
|
420
|
+
return response.headers.get("x-ratelimit-remaining") == "0"
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
def _comment(finding: Finding) -> dict[str, Any]:
|
|
424
|
+
"""A suggestion must sit on exactly the lines it replaces; other comments sit on the
|
|
425
|
+
finding's line."""
|
|
426
|
+
fix = finding.fix
|
|
427
|
+
comment: dict[str, Any] = {
|
|
428
|
+
"path": finding.path,
|
|
429
|
+
"line": finding.line,
|
|
430
|
+
"side": "RIGHT",
|
|
431
|
+
"body": rendering.inline_comment(finding),
|
|
432
|
+
}
|
|
433
|
+
if fix is not None and fix.suggestable:
|
|
434
|
+
comment["line"] = fix.end
|
|
435
|
+
if fix.start < fix.end:
|
|
436
|
+
comment["start_line"] = fix.start
|
|
437
|
+
comment["start_side"] = "RIGHT"
|
|
438
|
+
return comment
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
async def _auth(
|
|
442
|
+
tokens: StaticTokenProvider, target: ReviewPullRequestInput, permissions: dict[str, str]
|
|
443
|
+
) -> dict[str, str]:
|
|
444
|
+
token = await tokens.token(target.installation_id, target.repository_id, permissions)
|
|
445
|
+
return {"Authorization": f"Bearer {token}", **github_headers()}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"""Feedback on ML AI's comments, read back from GitHub: reactions and replies come with the
|
|
2
|
+
review comments; thread resolution needs GraphQL; review times come from the check runs."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from collections import defaultdict
|
|
7
|
+
from datetime import datetime
|
|
8
|
+
from typing import Any, ClassVar
|
|
9
|
+
|
|
10
|
+
import httpx
|
|
11
|
+
|
|
12
|
+
from mlai_review.application.ports import FeedbackSource, Repository
|
|
13
|
+
from mlai_review.domain.feedback import PostedComment, ReviewRun
|
|
14
|
+
from mlai_review.infrastructure import messages
|
|
15
|
+
from mlai_review.infrastructure.github import rendering
|
|
16
|
+
from mlai_review.infrastructure.github.token import StaticTokenProvider
|
|
17
|
+
from mlai_review.infrastructure.github.http import github_headers, raise_for_github_status
|
|
18
|
+
|
|
19
|
+
_PAGE_SIZE = 100
|
|
20
|
+
# Repository-wide comments are listed newest first; a review only needs recent ones and a
|
|
21
|
+
# report rarely needs more than a few thousand.
|
|
22
|
+
_MAX_COMMENT_PAGES = 30
|
|
23
|
+
_MAX_PULLS = 100
|
|
24
|
+
_MAX_COMMITS = 50
|
|
25
|
+
_THREADS = """
|
|
26
|
+
query($owner: String!, $name: String!, $number: Int!) {
|
|
27
|
+
repository(owner: $owner, name: $name) {
|
|
28
|
+
pullRequest(number: $number) {
|
|
29
|
+
reviewThreads(first: 100) {
|
|
30
|
+
nodes { isResolved comments(first: 1) { nodes { databaseId } } }
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}"""
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class GitHubFeedbackSource(FeedbackSource):
|
|
38
|
+
_READ: ClassVar[dict[str, str]] = {"pull_requests": "read", "checks": "read"}
|
|
39
|
+
|
|
40
|
+
def __init__(self, *, http: httpx.AsyncClient, tokens: StaticTokenProvider) -> None:
|
|
41
|
+
self._http = http
|
|
42
|
+
self._tokens = tokens
|
|
43
|
+
|
|
44
|
+
async def locate(self, full_name: str) -> Repository:
|
|
45
|
+
owner, name = full_name.split("/", 1)
|
|
46
|
+
installation_id, repository_id = await self._tokens.locate(owner, name)
|
|
47
|
+
return Repository(full_name, installation_id, repository_id)
|
|
48
|
+
|
|
49
|
+
async def posted_comments(
|
|
50
|
+
self, repository: Repository, since: datetime, *, with_threads: bool = True
|
|
51
|
+
) -> tuple[PostedComment, ...]:
|
|
52
|
+
raw = await self._comments_since(repository, since)
|
|
53
|
+
ours = [
|
|
54
|
+
c
|
|
55
|
+
for c in raw
|
|
56
|
+
if c.get("in_reply_to_id") is None
|
|
57
|
+
and rendering.fingerprint_in(c.get("body") or "") is not None
|
|
58
|
+
and _created(c) >= since
|
|
59
|
+
]
|
|
60
|
+
replies: dict[int, list[dict[str, Any]]] = defaultdict(list)
|
|
61
|
+
for c in raw:
|
|
62
|
+
if c.get("in_reply_to_id") is not None:
|
|
63
|
+
replies[int(c["in_reply_to_id"])].append(c)
|
|
64
|
+
resolved: dict[int, bool] = {}
|
|
65
|
+
if with_threads:
|
|
66
|
+
for number in sorted({_pull_number(c) for c in ours}):
|
|
67
|
+
resolved |= await self._thread_states(repository, number)
|
|
68
|
+
return tuple(_posted(c, replies[int(c["id"])], resolved.get(int(c["id"]))) for c in ours)
|
|
69
|
+
|
|
70
|
+
async def review_runs(self, repository: Repository, since: datetime) -> tuple[ReviewRun, ...]:
|
|
71
|
+
headers = await self._headers(repository)
|
|
72
|
+
base = f"/repos/{repository.owner}/{repository.name}"
|
|
73
|
+
pulls = await self._http.get(
|
|
74
|
+
f"{base}/pulls",
|
|
75
|
+
params={"state": "all", "sort": "updated", "direction": "desc", "per_page": _MAX_PULLS},
|
|
76
|
+
headers=headers,
|
|
77
|
+
)
|
|
78
|
+
raise_for_github_status(
|
|
79
|
+
pulls, messages.OP_LIST_PULLS.format(repository=repository.full_name)
|
|
80
|
+
)
|
|
81
|
+
runs: list[ReviewRun] = []
|
|
82
|
+
for pull in pulls.json():
|
|
83
|
+
if _parse(pull["updated_at"]) < since:
|
|
84
|
+
break
|
|
85
|
+
commits = await self._http.get(
|
|
86
|
+
f"{base}/pulls/{pull['number']}/commits",
|
|
87
|
+
params={"per_page": _MAX_COMMITS},
|
|
88
|
+
headers=headers,
|
|
89
|
+
)
|
|
90
|
+
raise_for_github_status(commits, messages.OP_LIST_COMMITS.format(number=pull["number"]))
|
|
91
|
+
for commit in commits.json():
|
|
92
|
+
runs += await self._runs_on(base, int(pull["number"]), commit["sha"], headers)
|
|
93
|
+
return tuple(r for r in runs if r.started_at >= since)
|
|
94
|
+
|
|
95
|
+
async def _runs_on(
|
|
96
|
+
self, base: str, number: int, sha: str, headers: dict[str, str]
|
|
97
|
+
) -> list[ReviewRun]:
|
|
98
|
+
response = await self._http.get(
|
|
99
|
+
f"{base}/commits/{sha}/check-runs",
|
|
100
|
+
# filter=all: by default GitHub lists only the latest run per check name.
|
|
101
|
+
params={"check_name": rendering.CHECK_NAME, "filter": "all", "per_page": _PAGE_SIZE},
|
|
102
|
+
headers=headers,
|
|
103
|
+
)
|
|
104
|
+
if response.status_code in (404, 422):
|
|
105
|
+
return []
|
|
106
|
+
raise_for_github_status(response, messages.OP_LIST_CHECKS.format(sha=sha[:12]))
|
|
107
|
+
incomplete = rendering.INCOMPLETE_TITLE
|
|
108
|
+
return [
|
|
109
|
+
ReviewRun(
|
|
110
|
+
pull_number=number,
|
|
111
|
+
head_sha=sha,
|
|
112
|
+
started_at=_parse(run["started_at"]),
|
|
113
|
+
completed_at=_parse(run["completed_at"]),
|
|
114
|
+
completed=(run.get("output") or {}).get("title") != incomplete,
|
|
115
|
+
)
|
|
116
|
+
for run in response.json().get("check_runs") or []
|
|
117
|
+
if run.get("status") == "completed" and run.get("completed_at")
|
|
118
|
+
]
|
|
119
|
+
|
|
120
|
+
async def _comments_since(
|
|
121
|
+
self, repository: Repository, since: datetime
|
|
122
|
+
) -> list[dict[str, Any]]:
|
|
123
|
+
headers = await self._headers(repository)
|
|
124
|
+
url = f"/repos/{repository.owner}/{repository.name}/pulls/comments"
|
|
125
|
+
found: list[dict[str, Any]] = []
|
|
126
|
+
for page in range(1, _MAX_COMMENT_PAGES + 1):
|
|
127
|
+
response = await self._http.get(
|
|
128
|
+
url,
|
|
129
|
+
params={
|
|
130
|
+
"sort": "created",
|
|
131
|
+
"direction": "desc",
|
|
132
|
+
"since": since.isoformat(),
|
|
133
|
+
"per_page": _PAGE_SIZE,
|
|
134
|
+
"page": page,
|
|
135
|
+
},
|
|
136
|
+
headers=headers,
|
|
137
|
+
)
|
|
138
|
+
raise_for_github_status(
|
|
139
|
+
response, messages.OP_LIST_REPO_COMMENTS.format(repository=repository.full_name)
|
|
140
|
+
)
|
|
141
|
+
batch = response.json()
|
|
142
|
+
found += batch
|
|
143
|
+
if len(batch) < _PAGE_SIZE:
|
|
144
|
+
break
|
|
145
|
+
return found
|
|
146
|
+
|
|
147
|
+
async def _thread_states(self, repository: Repository, number: int) -> dict[int, bool]:
|
|
148
|
+
response = await self._http.post(
|
|
149
|
+
"/graphql",
|
|
150
|
+
headers=await self._headers(repository),
|
|
151
|
+
json={
|
|
152
|
+
"query": _THREADS,
|
|
153
|
+
"variables": {"owner": repository.owner, "name": repository.name, "number": number},
|
|
154
|
+
},
|
|
155
|
+
)
|
|
156
|
+
raise_for_github_status(response, messages.OP_READ_THREADS.format(number=number))
|
|
157
|
+
pull = ((response.json().get("data") or {}).get("repository") or {}).get("pullRequest")
|
|
158
|
+
states: dict[int, bool] = {}
|
|
159
|
+
for thread in ((pull or {}).get("reviewThreads") or {}).get("nodes") or []:
|
|
160
|
+
first = ((thread.get("comments") or {}).get("nodes") or [{}])[0]
|
|
161
|
+
if first.get("databaseId") is not None:
|
|
162
|
+
states[int(first["databaseId"])] = bool(thread.get("isResolved"))
|
|
163
|
+
return states
|
|
164
|
+
|
|
165
|
+
async def _headers(self, repository: Repository) -> dict[str, str]:
|
|
166
|
+
token = await self._tokens.token(
|
|
167
|
+
repository.installation_id, repository.repository_id, self._READ
|
|
168
|
+
)
|
|
169
|
+
return {"Authorization": f"Bearer {token}", **github_headers()}
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def _posted(
|
|
173
|
+
comment: dict[str, Any], replies: list[dict[str, Any]], resolved: bool | None
|
|
174
|
+
) -> PostedComment:
|
|
175
|
+
reactions = comment.get("reactions") or {}
|
|
176
|
+
people = [r for r in replies if not _by_bot(r)]
|
|
177
|
+
ours = [r for r in replies if _by_bot(r)]
|
|
178
|
+
body = comment.get("body") or ""
|
|
179
|
+
return PostedComment(
|
|
180
|
+
comment_id=int(comment["id"]),
|
|
181
|
+
pull_number=_pull_number(comment),
|
|
182
|
+
path=str(comment["path"]),
|
|
183
|
+
fingerprint=rendering.fingerprint_in(body) or "",
|
|
184
|
+
title=rendering.title_in(body),
|
|
185
|
+
created_at=_created(comment),
|
|
186
|
+
thumbs_up=int(reactions.get("+1", 0)),
|
|
187
|
+
thumbs_down=int(reactions.get("-1", 0)),
|
|
188
|
+
replies=tuple(str(r.get("body") or "") for r in sorted(people, key=_created)),
|
|
189
|
+
outdated=comment.get("line") is None and comment.get("subject_type") != "file",
|
|
190
|
+
fixed_by_push=any(rendering.is_resolved_reply(r.get("body") or "") for r in ours),
|
|
191
|
+
thread_resolved=resolved,
|
|
192
|
+
tentative=rendering.is_tentative(body),
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def _by_bot(comment: dict[str, Any]) -> bool:
|
|
197
|
+
"""Ours: posted by an App, or, when posted as the connected account, carrying our wording."""
|
|
198
|
+
if (comment.get("user") or {}).get("type") == "Bot":
|
|
199
|
+
return True
|
|
200
|
+
return rendering.is_resolved_reply(comment.get("body") or "")
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def _pull_number(comment: dict[str, Any]) -> int:
|
|
204
|
+
return int(str(comment["pull_request_url"]).rstrip("/").rsplit("/", 1)[1])
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def _created(comment: dict[str, Any]) -> datetime:
|
|
208
|
+
return _parse(comment["created_at"])
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def _parse(value: str) -> datetime:
|
|
212
|
+
return datetime.fromisoformat(value.replace("Z", "+00:00"))
|