plotroom 0.1.0__py3-none-any.whl
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.
- plotroom/__init__.py +0 -0
- plotroom/__main__.py +8 -0
- plotroom/_secret_file.py +8 -0
- plotroom/adapters/__init__.py +0 -0
- plotroom/adapters/claude_desktop.py +219 -0
- plotroom/adapters/embedding_http.py +175 -0
- plotroom/adapters/github/__init__.py +337 -0
- plotroom/adapters/github/app_credential.py +209 -0
- plotroom/adapters/github/ci.py +738 -0
- plotroom/adapters/github/commit_statuses.py +63 -0
- plotroom/adapters/github/deployments.py +213 -0
- plotroom/adapters/github/details.py +867 -0
- plotroom/adapters/github/issue_details.py +368 -0
- plotroom/adapters/github/junit.py +210 -0
- plotroom/adapters/github/junit_xml.py +162 -0
- plotroom/adapters/github/kodus_review.py +347 -0
- plotroom/adapters/github/pr_details.py +192 -0
- plotroom/adapters/github/pull_comparison.py +108 -0
- plotroom/adapters/github/read.py +530 -0
- plotroom/adapters/github/ticket_tree.py +336 -0
- plotroom/adapters/mcp/__init__.py +5 -0
- plotroom/adapters/mcp/proxy.py +722 -0
- plotroom/adapters/mcp/server.py +931 -0
- plotroom/adapters/openrouter.py +151 -0
- plotroom/adapters/store_sqlite/__init__.py +6 -0
- plotroom/adapters/store_sqlite/actions_sql.py +148 -0
- plotroom/adapters/store_sqlite/asks_sql.py +153 -0
- plotroom/adapters/store_sqlite/backfill_sql.py +138 -0
- plotroom/adapters/store_sqlite/budgets_sql.py +102 -0
- plotroom/adapters/store_sqlite/checklists_sql.py +77 -0
- plotroom/adapters/store_sqlite/claims_sql.py +54 -0
- plotroom/adapters/store_sqlite/embedding_sql.py +275 -0
- plotroom/adapters/store_sqlite/github_sql.py +331 -0
- plotroom/adapters/store_sqlite/goals_sql.py +57 -0
- plotroom/adapters/store_sqlite/human_queue_sql.py +60 -0
- plotroom/adapters/store_sqlite/items_sql.py +129 -0
- plotroom/adapters/store_sqlite/lanes_sql.py +85 -0
- plotroom/adapters/store_sqlite/ledger_sql.py +506 -0
- plotroom/adapters/store_sqlite/migrate.py +194 -0
- plotroom/adapters/store_sqlite/migrations/env.py +36 -0
- plotroom/adapters/store_sqlite/migrations/script.py.mako +19 -0
- plotroom/adapters/store_sqlite/migrations/versions/0001_project_meta.py +23 -0
- plotroom/adapters/store_sqlite/migrations/versions/0002_event_log.py +40 -0
- plotroom/adapters/store_sqlite/migrations/versions/0003_next_items.py +49 -0
- plotroom/adapters/store_sqlite/migrations/versions/0004_actions.py +28 -0
- plotroom/adapters/store_sqlite/migrations/versions/0005_lanes.py +36 -0
- plotroom/adapters/store_sqlite/migrations/versions/0006_work_items.py +22 -0
- plotroom/adapters/store_sqlite/migrations/versions/0007_item_changes.py +33 -0
- plotroom/adapters/store_sqlite/migrations/versions/0008_claude_turn_aliases.py +30 -0
- plotroom/adapters/store_sqlite/migrations/versions/0009_github_projection.py +38 -0
- plotroom/adapters/store_sqlite/migrations/versions/0010_github_pull_freshness.py +37 -0
- plotroom/adapters/store_sqlite/migrations/versions/0011_github_resources.py +27 -0
- plotroom/adapters/store_sqlite/migrations/versions/0012_local_triggers_outbox.py +80 -0
- plotroom/adapters/store_sqlite/migrations/versions/0013_lane_claims.py +22 -0
- plotroom/adapters/store_sqlite/migrations/versions/0015_recurring_triggers.py +45 -0
- plotroom/adapters/store_sqlite/migrations/versions/0016_merge_controller.py +28 -0
- plotroom/adapters/store_sqlite/migrations/versions/0017_lane_checklists.py +36 -0
- plotroom/adapters/store_sqlite/migrations/versions/0018_guardrails.py +33 -0
- plotroom/adapters/store_sqlite/migrations/versions/0019_clerk_import_metadata.py +16 -0
- plotroom/adapters/store_sqlite/migrations/versions/0020_search.py +30 -0
- plotroom/adapters/store_sqlite/migrations/versions/0021_human_queue.py +46 -0
- plotroom/adapters/store_sqlite/migrations/versions/0022_search_records.py +77 -0
- plotroom/adapters/store_sqlite/migrations/versions/0023_operator_goals.py +25 -0
- plotroom/adapters/store_sqlite/migrations/versions/0024_embedding_rejections.py +36 -0
- plotroom/adapters/store_sqlite/outbox_sql.py +78 -0
- plotroom/adapters/store_sqlite/routes_sql.py +26 -0
- plotroom/adapters/store_sqlite/search_sql.py +116 -0
- plotroom/adapters/store_sqlite/store.py +623 -0
- plotroom/adapters/store_sqlite/triggers_sql.py +111 -0
- plotroom/adapters/store_sqlite/work_items_sql.py +36 -0
- plotroom/adapters/switchboard.py +152 -0
- plotroom/adapters/usage_fixture.py +11 -0
- plotroom/adapters/work_items_bmad.py +256 -0
- plotroom/adapters/work_items_github_issues.py +323 -0
- plotroom/app/__init__.py +0 -0
- plotroom/app/bridge.py +162 -0
- plotroom/app/clerk_import.py +349 -0
- plotroom/app/cli.py +290 -0
- plotroom/app/clock.py +12 -0
- plotroom/app/config.py +984 -0
- plotroom/app/daemon_start.py +453 -0
- plotroom/app/lock.py +77 -0
- plotroom/app/logging.py +93 -0
- plotroom/app/portfile.py +218 -0
- plotroom/app/runtime.py +518 -0
- plotroom/app/serve.py +671 -0
- plotroom/app/session_intake.py +51 -0
- plotroom/app/sqlite_floor.py +35 -0
- plotroom/app/tokens.py +265 -0
- plotroom/core/__init__.py +0 -0
- plotroom/core/actions.py +67 -0
- plotroom/core/ask_checks.py +124 -0
- plotroom/core/asks.py +123 -0
- plotroom/core/branch_updates.py +207 -0
- plotroom/core/bridge.py +112 -0
- plotroom/core/briefs.py +83 -0
- plotroom/core/budgets.py +182 -0
- plotroom/core/catalog.py +464 -0
- plotroom/core/checklists.py +151 -0
- plotroom/core/claims.py +296 -0
- plotroom/core/clerk_import.py +379 -0
- plotroom/core/commands.py +85 -0
- plotroom/core/deploy_window.py +21 -0
- plotroom/core/envelope.py +20 -0
- plotroom/core/events.py +227 -0
- plotroom/core/github.py +306 -0
- plotroom/core/github_projection.py +310 -0
- plotroom/core/goal_language.py +374 -0
- plotroom/core/goal_projection.py +146 -0
- plotroom/core/human_queue.py +119 -0
- plotroom/core/intake.py +106 -0
- plotroom/core/kodus_review.py +53 -0
- plotroom/core/lanes.py +281 -0
- plotroom/core/ledger.py +808 -0
- plotroom/core/merge_controller.py +344 -0
- plotroom/core/merge_flakes.py +422 -0
- plotroom/core/merge_gate.py +724 -0
- plotroom/core/merge_lane.py +70 -0
- plotroom/core/merge_overlap.py +76 -0
- plotroom/core/next_items.py +60 -0
- plotroom/core/operator_goals.py +103 -0
- plotroom/core/outbox.py +70 -0
- plotroom/core/pull_overlaps.py +152 -0
- plotroom/core/reports.py +62 -0
- plotroom/core/roles.py +100 -0
- plotroom/core/routing.py +55 -0
- plotroom/core/run_reports.py +53 -0
- plotroom/core/search.py +266 -0
- plotroom/core/search_sources.py +94 -0
- plotroom/core/snapshot.py +204 -0
- plotroom/core/trigger_actions.py +89 -0
- plotroom/core/triggers.py +550 -0
- plotroom/core/unit_of_work.py +541 -0
- plotroom/core/watchers.py +35 -0
- plotroom/core/work_items.py +23 -0
- plotroom/core/writer.py +164 -0
- plotroom/ports/__init__.py +6 -0
- plotroom/ports/clock.py +12 -0
- plotroom/ports/github_write.py +72 -0
- plotroom/ports/search.py +197 -0
- plotroom/ports/session_reader.py +23 -0
- plotroom/ports/store.py +323 -0
- plotroom/ports/usage_meter.py +35 -0
- plotroom/ports/work_items.py +22 -0
- plotroom/py.typed +0 -0
- plotroom-0.1.0.dist-info/METADATA +230 -0
- plotroom-0.1.0.dist-info/RECORD +150 -0
- plotroom-0.1.0.dist-info/WHEEL +4 -0
- plotroom-0.1.0.dist-info/entry_points.txt +2 -0
- plotroom-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,530 @@
|
|
|
1
|
+
"""Bounded, repository-scoped GitHub reads shared by the detail fetchers."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from collections.abc import AsyncIterator, Mapping
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from datetime import datetime
|
|
7
|
+
from typing import Any, cast
|
|
8
|
+
|
|
9
|
+
import httpx
|
|
10
|
+
|
|
11
|
+
from plotroom.adapters.github import GitHubRateLimited, GitHubUnavailable, is_rate_limited
|
|
12
|
+
from plotroom.core.github import PullRequest, valid_git_sha
|
|
13
|
+
|
|
14
|
+
type Row = dict[str, Any]
|
|
15
|
+
MAX_PAGES = 30
|
|
16
|
+
MAX_RESPONSE = 8 * 1024 * 1024
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def obj(value: Any) -> Row:
|
|
20
|
+
if not isinstance(value, dict):
|
|
21
|
+
raise ValueError("expected an object")
|
|
22
|
+
return cast("Row", value)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _array(value: Any) -> list[Any]:
|
|
26
|
+
if not isinstance(value, list):
|
|
27
|
+
raise ValueError("expected a list")
|
|
28
|
+
return cast("list[Any]", value)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def rows(value: Any) -> list[Row]:
|
|
32
|
+
return [obj(row) for row in _array(value)]
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def string(value: Any) -> str:
|
|
36
|
+
if not isinstance(value, str):
|
|
37
|
+
raise ValueError("expected a string")
|
|
38
|
+
return value
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def number(value: Any) -> int:
|
|
42
|
+
if type(value) is not int or not 0 < value <= 2**63 - 1:
|
|
43
|
+
raise ValueError("expected a positive integer")
|
|
44
|
+
return value
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _merged_at(value: Any) -> bool:
|
|
48
|
+
return datetime.fromisoformat(string(value)).utcoffset() is not None
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _merge_oid(value: Any) -> str | None:
|
|
52
|
+
if value is None:
|
|
53
|
+
return None
|
|
54
|
+
oid = string(value)
|
|
55
|
+
if not valid_git_sha(oid):
|
|
56
|
+
raise ValueError("invalid merge OID")
|
|
57
|
+
return oid
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class GitHubNotFound(GitHubUnavailable):
|
|
61
|
+
"""A configured GitHub read endpoint did not return its resource."""
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@dataclass(frozen=True)
|
|
65
|
+
class ReviewThreadPull:
|
|
66
|
+
node_id: str
|
|
67
|
+
author_login: str
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@dataclass(frozen=True)
|
|
71
|
+
class _ObservedPage:
|
|
72
|
+
records: list[Any]
|
|
73
|
+
more: bool
|
|
74
|
+
etag: str | None
|
|
75
|
+
last_page: int | None
|
|
76
|
+
has_next: bool
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class GitHubReadClient:
|
|
80
|
+
def __init__(self, api_url: str, repository: str) -> None:
|
|
81
|
+
self.api_url = api_url
|
|
82
|
+
self.repository = repository
|
|
83
|
+
self.endpoint = f"{api_url}/repos/{repository}"
|
|
84
|
+
|
|
85
|
+
async def _read(
|
|
86
|
+
self,
|
|
87
|
+
client: httpx.AsyncClient,
|
|
88
|
+
token: str,
|
|
89
|
+
url: str,
|
|
90
|
+
*,
|
|
91
|
+
params: Mapping[str, Any] | None = None,
|
|
92
|
+
query: Row | None = None,
|
|
93
|
+
etag: str | None = None,
|
|
94
|
+
artifact_redirect: bool = False,
|
|
95
|
+
) -> tuple[Any, httpx.Response]:
|
|
96
|
+
headers = {
|
|
97
|
+
"Authorization": f"Bearer {token}",
|
|
98
|
+
"Accept": "application/vnd.github+json",
|
|
99
|
+
"X-GitHub-Api-Version": "2026-03-10",
|
|
100
|
+
}
|
|
101
|
+
if etag is not None:
|
|
102
|
+
headers["If-None-Match"] = etag
|
|
103
|
+
async with client.stream(
|
|
104
|
+
"GET" if query is None else "POST",
|
|
105
|
+
url,
|
|
106
|
+
params=params,
|
|
107
|
+
json=query,
|
|
108
|
+
headers=headers,
|
|
109
|
+
follow_redirects=False,
|
|
110
|
+
) as response:
|
|
111
|
+
if (response.status_code == 304 and etag is not None) or (
|
|
112
|
+
response.status_code == 302 and artifact_redirect
|
|
113
|
+
):
|
|
114
|
+
return None, response
|
|
115
|
+
body = bytearray()
|
|
116
|
+
async for chunk in response.aiter_bytes():
|
|
117
|
+
body.extend(chunk)
|
|
118
|
+
if len(body) > MAX_RESPONSE:
|
|
119
|
+
raise GitHubUnavailable("GitHub response exceeded the byte limit")
|
|
120
|
+
if response.status_code == 429 or (
|
|
121
|
+
response.status_code == 403
|
|
122
|
+
and (
|
|
123
|
+
is_rate_limited(
|
|
124
|
+
httpx.Response(403, headers=response.headers, content=bytes(body))
|
|
125
|
+
)
|
|
126
|
+
)
|
|
127
|
+
):
|
|
128
|
+
raise GitHubRateLimited(response.headers)
|
|
129
|
+
if response.status_code == 404:
|
|
130
|
+
raise GitHubNotFound("GitHub resource is unavailable")
|
|
131
|
+
if response.status_code != 200:
|
|
132
|
+
raise GitHubUnavailable(f"GitHub read returned HTTP {response.status_code}")
|
|
133
|
+
try:
|
|
134
|
+
return json.loads(body), response
|
|
135
|
+
except ValueError:
|
|
136
|
+
raise GitHubUnavailable("GitHub returned invalid JSON") from None
|
|
137
|
+
|
|
138
|
+
async def get(
|
|
139
|
+
self,
|
|
140
|
+
client: httpx.AsyncClient,
|
|
141
|
+
token: str,
|
|
142
|
+
path: str,
|
|
143
|
+
params: Mapping[str, Any] | None = None,
|
|
144
|
+
) -> Row | list[Any]:
|
|
145
|
+
value, _ = await self._read(client, token, self.endpoint + path, params=params)
|
|
146
|
+
if isinstance(value, list):
|
|
147
|
+
return cast("list[Any]", value)
|
|
148
|
+
return obj(value)
|
|
149
|
+
|
|
150
|
+
async def artifact_location(self, client: httpx.AsyncClient, token: str, artifact: int) -> str:
|
|
151
|
+
"""Read a signed artifact location without following it with API credentials."""
|
|
152
|
+
_, response = await self._read(
|
|
153
|
+
client,
|
|
154
|
+
token,
|
|
155
|
+
f"{self.endpoint}/actions/artifacts/{number(artifact)}/zip",
|
|
156
|
+
artifact_redirect=True,
|
|
157
|
+
)
|
|
158
|
+
locations = response.headers.get_list("location")
|
|
159
|
+
if response.status_code != 302 or len(locations) != 1:
|
|
160
|
+
raise GitHubUnavailable("GitHub did not return one artifact download location")
|
|
161
|
+
return locations[0]
|
|
162
|
+
|
|
163
|
+
async def page(
|
|
164
|
+
self,
|
|
165
|
+
client: httpx.AsyncClient,
|
|
166
|
+
token: str,
|
|
167
|
+
path: str,
|
|
168
|
+
page: int,
|
|
169
|
+
*,
|
|
170
|
+
params: Mapping[str, Any] | None = None,
|
|
171
|
+
key: str | None = None,
|
|
172
|
+
verify_total: bool = False,
|
|
173
|
+
verify_terminal: bool = False,
|
|
174
|
+
) -> tuple[list[Row], bool]:
|
|
175
|
+
records, more = await self.raw_page(
|
|
176
|
+
client,
|
|
177
|
+
token,
|
|
178
|
+
path,
|
|
179
|
+
page,
|
|
180
|
+
params=params,
|
|
181
|
+
key=key,
|
|
182
|
+
verify_total=verify_total,
|
|
183
|
+
verify_terminal=verify_terminal,
|
|
184
|
+
)
|
|
185
|
+
return rows(records), more
|
|
186
|
+
|
|
187
|
+
async def raw_page(
|
|
188
|
+
self,
|
|
189
|
+
client: httpx.AsyncClient,
|
|
190
|
+
token: str,
|
|
191
|
+
path: str,
|
|
192
|
+
page: int,
|
|
193
|
+
*,
|
|
194
|
+
params: Mapping[str, Any] | None = None,
|
|
195
|
+
key: str | None = None,
|
|
196
|
+
verify_total: bool = False,
|
|
197
|
+
verify_terminal: bool = False,
|
|
198
|
+
) -> tuple[list[Any], bool]:
|
|
199
|
+
"""Read a validated page with its members available for individual decoding."""
|
|
200
|
+
result = await self._observe_page(
|
|
201
|
+
client,
|
|
202
|
+
token,
|
|
203
|
+
path,
|
|
204
|
+
page,
|
|
205
|
+
params=params,
|
|
206
|
+
key=key,
|
|
207
|
+
verify_total=verify_total,
|
|
208
|
+
verify_terminal=verify_terminal,
|
|
209
|
+
)
|
|
210
|
+
assert result is not None
|
|
211
|
+
return result.records, result.more
|
|
212
|
+
|
|
213
|
+
async def _observe_page(
|
|
214
|
+
self,
|
|
215
|
+
client: httpx.AsyncClient,
|
|
216
|
+
token: str,
|
|
217
|
+
path: str,
|
|
218
|
+
page: int,
|
|
219
|
+
*,
|
|
220
|
+
params: Mapping[str, Any] | None,
|
|
221
|
+
key: str | None,
|
|
222
|
+
verify_total: bool,
|
|
223
|
+
verify_terminal: bool,
|
|
224
|
+
etag: str | None = None,
|
|
225
|
+
) -> _ObservedPage | None:
|
|
226
|
+
value, response = await self._read(
|
|
227
|
+
client,
|
|
228
|
+
token,
|
|
229
|
+
self.endpoint + path,
|
|
230
|
+
params={**(params or {}), "per_page": 100, "page": page},
|
|
231
|
+
etag=etag,
|
|
232
|
+
)
|
|
233
|
+
if response.status_code == 304:
|
|
234
|
+
return None
|
|
235
|
+
current_etag = response.headers.get("ETag")
|
|
236
|
+
records = _array(obj(value)[key] if key is not None else value)
|
|
237
|
+
if len(records) > 100:
|
|
238
|
+
raise GitHubUnavailable("GitHub returned an oversized page")
|
|
239
|
+
link = response.links.get("next")
|
|
240
|
+
if verify_total:
|
|
241
|
+
total = obj(value).get("total_count")
|
|
242
|
+
covered = (page - 1) * 100 + len(records)
|
|
243
|
+
if type(total) is not int or not (
|
|
244
|
+
total == covered if link is None else len(records) == 100 and total > covered
|
|
245
|
+
):
|
|
246
|
+
raise GitHubUnavailable("GitHub returned an incomplete result count")
|
|
247
|
+
last = response.links.get("last") if verify_terminal else None
|
|
248
|
+
last_page = None
|
|
249
|
+
if last is not None:
|
|
250
|
+
try:
|
|
251
|
+
last_page = int(httpx.URL(last["url"]).params["page"])
|
|
252
|
+
except KeyError, ValueError, httpx.InvalidURL:
|
|
253
|
+
raise GitHubUnavailable("GitHub returned invalid pagination") from None
|
|
254
|
+
valid_last = last_page == page if link is None else last_page > page
|
|
255
|
+
if not valid_last:
|
|
256
|
+
raise GitHubUnavailable("GitHub returned incomplete pagination")
|
|
257
|
+
if link is None:
|
|
258
|
+
# Array endpoints cannot give a total. A full page might be the
|
|
259
|
+
# last one, so probe by number instead of trusting an absent Link.
|
|
260
|
+
return _ObservedPage(
|
|
261
|
+
records, verify_terminal and len(records) == 100, current_etag, last_page, False
|
|
262
|
+
)
|
|
263
|
+
try:
|
|
264
|
+
next_page = int(httpx.URL(link["url"]).params["page"])
|
|
265
|
+
except KeyError, ValueError, httpx.InvalidURL:
|
|
266
|
+
raise GitHubUnavailable("GitHub returned invalid pagination") from None
|
|
267
|
+
if next_page != page + 1:
|
|
268
|
+
raise GitHubUnavailable("GitHub returned invalid pagination")
|
|
269
|
+
return _ObservedPage(records, True, current_etag, last_page, True)
|
|
270
|
+
|
|
271
|
+
async def pages(
|
|
272
|
+
self,
|
|
273
|
+
client: httpx.AsyncClient,
|
|
274
|
+
token: str,
|
|
275
|
+
path: str,
|
|
276
|
+
*,
|
|
277
|
+
params: Mapping[str, Any] | None = None,
|
|
278
|
+
key: str | None = None,
|
|
279
|
+
recent: bool = False,
|
|
280
|
+
verify_total: bool = False,
|
|
281
|
+
verify_terminal: bool = False,
|
|
282
|
+
verify_boundaries: bool = False,
|
|
283
|
+
max_pages: int = MAX_PAGES,
|
|
284
|
+
require_full_pages: bool = False,
|
|
285
|
+
) -> AsyncIterator[Row]:
|
|
286
|
+
async def read(index: int, etag: str | None = None) -> _ObservedPage | None:
|
|
287
|
+
return await self._observe_page(
|
|
288
|
+
client,
|
|
289
|
+
token,
|
|
290
|
+
path,
|
|
291
|
+
index,
|
|
292
|
+
params=params,
|
|
293
|
+
key=key,
|
|
294
|
+
verify_total=verify_total,
|
|
295
|
+
verify_terminal=verify_terminal,
|
|
296
|
+
etag=etag,
|
|
297
|
+
)
|
|
298
|
+
|
|
299
|
+
prior = None
|
|
300
|
+
advertised_last = None
|
|
301
|
+
expected_nonempty = False
|
|
302
|
+
for page in range(1, max_pages + 1 + verify_terminal):
|
|
303
|
+
observed = await read(page)
|
|
304
|
+
assert observed is not None
|
|
305
|
+
records, more = observed.records, observed.more
|
|
306
|
+
if page > max_pages and (records or more):
|
|
307
|
+
break
|
|
308
|
+
if require_full_pages:
|
|
309
|
+
if expected_nonempty and not records:
|
|
310
|
+
raise GitHubUnavailable("GitHub returned an empty advertised page")
|
|
311
|
+
expected_nonempty = observed.has_next
|
|
312
|
+
if more and len(records) != 100:
|
|
313
|
+
raise GitHubUnavailable("GitHub returned a short nonterminal page")
|
|
314
|
+
if observed.last_page is not None:
|
|
315
|
+
if observed.last_page > max_pages:
|
|
316
|
+
raise GitHubUnavailable("GitHub read exceeded the page limit")
|
|
317
|
+
if advertised_last not in (None, observed.last_page):
|
|
318
|
+
raise GitHubUnavailable("GitHub returned conflicting last pages")
|
|
319
|
+
advertised_last = observed.last_page
|
|
320
|
+
if advertised_last is not None and (
|
|
321
|
+
(page < advertised_last and not more)
|
|
322
|
+
or (
|
|
323
|
+
page == advertised_last
|
|
324
|
+
and (observed.has_next or (page > 1 and not records))
|
|
325
|
+
)
|
|
326
|
+
or (page > advertised_last and (records or more))
|
|
327
|
+
):
|
|
328
|
+
raise GitHubUnavailable("GitHub returned incomplete pagination")
|
|
329
|
+
if verify_boundaries and prior is not None:
|
|
330
|
+
confirmed = await read(page - 1, prior.etag)
|
|
331
|
+
if confirmed is not None and (
|
|
332
|
+
(confirmed.records, confirmed.more) != (prior.records, prior.more)
|
|
333
|
+
or (
|
|
334
|
+
require_full_pages
|
|
335
|
+
and (confirmed.last_page, confirmed.has_next)
|
|
336
|
+
!= (prior.last_page, prior.has_next)
|
|
337
|
+
)
|
|
338
|
+
):
|
|
339
|
+
raise GitHubUnavailable("GitHub pages shifted during the read")
|
|
340
|
+
for row in rows(records):
|
|
341
|
+
yield row
|
|
342
|
+
if recent or not more:
|
|
343
|
+
return
|
|
344
|
+
prior = observed if verify_boundaries else None
|
|
345
|
+
raise GitHubUnavailable("GitHub read exceeded the page limit")
|
|
346
|
+
|
|
347
|
+
async def pull_merge_sha(
|
|
348
|
+
self, client: httpx.AsyncClient, token: str, pull: PullRequest
|
|
349
|
+
) -> str | None:
|
|
350
|
+
repository = self.repository.lower()
|
|
351
|
+
if pull.repository.lower() != repository or not valid_git_sha(pull.head_sha):
|
|
352
|
+
return None
|
|
353
|
+
owner, name = self.repository.split("/")
|
|
354
|
+
result, response = await self._read(
|
|
355
|
+
client,
|
|
356
|
+
token,
|
|
357
|
+
f"{self.api_url}/graphql",
|
|
358
|
+
query={
|
|
359
|
+
"query": PULL_MERGE_SHA_QUERY,
|
|
360
|
+
"variables": {"owner": owner, "name": name, "number": number(pull.number)},
|
|
361
|
+
},
|
|
362
|
+
)
|
|
363
|
+
document = obj(result)
|
|
364
|
+
if "errors" in document and (errors := rows(document["errors"])):
|
|
365
|
+
if response.headers.get("X-RateLimit-Remaining") == "0" or any(
|
|
366
|
+
error.get("type") == "RATE_LIMITED"
|
|
367
|
+
or (
|
|
368
|
+
isinstance(message := error.get("message"), str)
|
|
369
|
+
and "rate limit" in message.lower()
|
|
370
|
+
)
|
|
371
|
+
for error in errors
|
|
372
|
+
):
|
|
373
|
+
raise GitHubRateLimited(response.headers)
|
|
374
|
+
return None
|
|
375
|
+
base = obj(obj(document["data"])["repository"])
|
|
376
|
+
current = obj(base["pullRequest"])
|
|
377
|
+
if (
|
|
378
|
+
string(base["nameWithOwner"]).lower() != repository
|
|
379
|
+
or string(obj(current["repository"])["nameWithOwner"]).lower() != repository
|
|
380
|
+
or number(current["number"]) != pull.number
|
|
381
|
+
or current["merged"] is not True
|
|
382
|
+
or current["state"] != "MERGED"
|
|
383
|
+
or not _merged_at(current["mergedAt"])
|
|
384
|
+
or string(current["headRefOid"]) != pull.head_sha
|
|
385
|
+
):
|
|
386
|
+
return None
|
|
387
|
+
commit = current["mergeCommit"]
|
|
388
|
+
if commit is not None:
|
|
389
|
+
return _merge_oid(string(obj(commit)["oid"]))
|
|
390
|
+
|
|
391
|
+
detail = obj(await self.get(client, token, f"/pulls/{pull.number}"))
|
|
392
|
+
if (
|
|
393
|
+
number(detail["number"]) != pull.number
|
|
394
|
+
or detail["merged"] is not True
|
|
395
|
+
or detail["state"] != "closed"
|
|
396
|
+
or not _merged_at(detail["merged_at"])
|
|
397
|
+
or string(obj(obj(detail["base"])["repo"])["full_name"]).lower() != repository
|
|
398
|
+
or string(obj(detail["head"])["sha"]) != pull.head_sha
|
|
399
|
+
):
|
|
400
|
+
return None
|
|
401
|
+
return _merge_oid(detail["merge_commit_sha"])
|
|
402
|
+
|
|
403
|
+
async def review_threads(
|
|
404
|
+
self,
|
|
405
|
+
client: httpx.AsyncClient,
|
|
406
|
+
token: str,
|
|
407
|
+
pull: int,
|
|
408
|
+
head_sha: str,
|
|
409
|
+
*,
|
|
410
|
+
cadence: ReviewThreadPull | None = None,
|
|
411
|
+
) -> AsyncIterator[Row]:
|
|
412
|
+
owner, name = self.repository.split("/")
|
|
413
|
+
cursor: str | None = None
|
|
414
|
+
cursors: set[str] = set()
|
|
415
|
+
identifiers: set[str] = set()
|
|
416
|
+
expected: int | None = None
|
|
417
|
+
for _ in range(MAX_PAGES):
|
|
418
|
+
result, response = await self._read(
|
|
419
|
+
client,
|
|
420
|
+
token,
|
|
421
|
+
f"{self.api_url}/graphql",
|
|
422
|
+
query={
|
|
423
|
+
"query": REVIEW_THREADS_QUERY if cadence is None else CADENCE_THREADS_QUERY,
|
|
424
|
+
"variables": {"owner": owner, "name": name, "number": pull, "after": cursor},
|
|
425
|
+
},
|
|
426
|
+
)
|
|
427
|
+
document = obj(result)
|
|
428
|
+
if document.get("errors"):
|
|
429
|
+
errors = rows(document["errors"])
|
|
430
|
+
if any(error.get("type") == "RATE_LIMITED" for error in errors):
|
|
431
|
+
raise GitHubRateLimited(response.headers)
|
|
432
|
+
raise GitHubUnavailable("GitHub review thread query failed")
|
|
433
|
+
repository = obj(obj(document["data"])["repository"])
|
|
434
|
+
current = obj(repository["pullRequest"])
|
|
435
|
+
if cadence is not None and (
|
|
436
|
+
string(repository["nameWithOwner"]).lower() != self.repository.lower()
|
|
437
|
+
or number(current["number"]) != pull
|
|
438
|
+
or current["id"] != cadence.node_id
|
|
439
|
+
or string(obj(current["author"])["login"]).lower() != cadence.author_login
|
|
440
|
+
):
|
|
441
|
+
raise GitHubUnavailable("GitHub returned threads for a different pull request")
|
|
442
|
+
if string(current["headRefOid"]) != head_sha:
|
|
443
|
+
raise GitHubUnavailable("PR head moved during review thread poll")
|
|
444
|
+
connection = obj(current["reviewThreads"])
|
|
445
|
+
total = connection["totalCount"]
|
|
446
|
+
if type(total) is not int or total < 0 or (expected is not None and total != expected):
|
|
447
|
+
raise GitHubUnavailable("GitHub returned an inconsistent review thread count")
|
|
448
|
+
expected = total
|
|
449
|
+
nodes = rows(connection["nodes"])
|
|
450
|
+
if len(nodes) > 100:
|
|
451
|
+
raise GitHubUnavailable("GitHub returned an oversized review thread page")
|
|
452
|
+
for row in nodes:
|
|
453
|
+
identifier = string(row["id"])
|
|
454
|
+
if not identifier or identifier in identifiers:
|
|
455
|
+
raise GitHubUnavailable("GitHub returned an invalid review thread ID")
|
|
456
|
+
identifiers.add(identifier)
|
|
457
|
+
yield row
|
|
458
|
+
page = obj(connection["pageInfo"])
|
|
459
|
+
more = page["hasNextPage"]
|
|
460
|
+
if more is False and len(identifiers) == expected:
|
|
461
|
+
return
|
|
462
|
+
if more is not True or not nodes or len(identifiers) >= expected:
|
|
463
|
+
raise GitHubUnavailable("GitHub returned an incomplete review thread list")
|
|
464
|
+
cursor = string(page["endCursor"])
|
|
465
|
+
if not cursor or cursor in cursors:
|
|
466
|
+
raise GitHubUnavailable("GitHub returned invalid review thread pagination")
|
|
467
|
+
cursors.add(cursor)
|
|
468
|
+
raise GitHubUnavailable("GitHub review threads exceeded the page limit")
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
PULL_MERGE_SHA_QUERY = """
|
|
472
|
+
query PullMergeSha($owner: String!, $name: String!, $number: Int!) {
|
|
473
|
+
repository(owner: $owner, name: $name) {
|
|
474
|
+
nameWithOwner
|
|
475
|
+
pullRequest(number: $number) {
|
|
476
|
+
repository { nameWithOwner }
|
|
477
|
+
number
|
|
478
|
+
merged
|
|
479
|
+
state
|
|
480
|
+
mergedAt
|
|
481
|
+
headRefOid
|
|
482
|
+
mergeCommit { oid }
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
"""
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
REVIEW_THREADS_QUERY = """
|
|
490
|
+
query PullReviewThreads($owner: String!, $name: String!, $number: Int!, $after: String) {
|
|
491
|
+
repository(owner: $owner, name: $name) {
|
|
492
|
+
pullRequest(number: $number) {
|
|
493
|
+
headRefOid
|
|
494
|
+
reviewThreads(first: 100, after: $after) {
|
|
495
|
+
totalCount
|
|
496
|
+
nodes { id isResolved path line }
|
|
497
|
+
pageInfo { hasNextPage endCursor }
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
"""
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
CADENCE_THREADS_QUERY = """
|
|
506
|
+
query PullCadenceThreads($owner: String!, $name: String!, $number: Int!, $after: String) {
|
|
507
|
+
repository(owner: $owner, name: $name) {
|
|
508
|
+
nameWithOwner
|
|
509
|
+
pullRequest(number: $number) {
|
|
510
|
+
id
|
|
511
|
+
number
|
|
512
|
+
author { login }
|
|
513
|
+
headRefOid
|
|
514
|
+
reviewThreads(first: 100, after: $after) {
|
|
515
|
+
totalCount
|
|
516
|
+
nodes {
|
|
517
|
+
id
|
|
518
|
+
isResolved
|
|
519
|
+
comments(first: 100) {
|
|
520
|
+
totalCount
|
|
521
|
+
nodes { id author { __typename login } authorAssociation replyTo { id } }
|
|
522
|
+
pageInfo { hasNextPage }
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
pageInfo { hasNextPage endCursor }
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
"""
|