eddieland-canopy-github 0.14.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.
- canopy_github/__init__.py +504 -0
- canopy_github/connect.py +267 -0
- canopy_github/issues.py +682 -0
- canopy_github/post.py +866 -0
- canopy_github/read.py +656 -0
- canopy_github/repository.py +91 -0
- eddieland_canopy_github-0.14.0.dist-info/METADATA +115 -0
- eddieland_canopy_github-0.14.0.dist-info/RECORD +10 -0
- eddieland_canopy_github-0.14.0.dist-info/WHEEL +4 -0
- eddieland_canopy_github-0.14.0.dist-info/entry_points.txt +3 -0
canopy_github/read.py
ADDED
|
@@ -0,0 +1,656 @@
|
|
|
1
|
+
"""Reading PRs whole, many to a query, each page of each.
|
|
2
|
+
|
|
3
|
+
Each PR's state, labels, and times, the checks at its head, its reviews, its review threads with every comment, and
|
|
4
|
+
the comments on its conversation.
|
|
5
|
+
`canopy-connect-github` prints what this reads; `canopy-post-github` reads the threads to find the one each reply
|
|
6
|
+
answers.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from collections.abc import Iterator, Mapping, Sequence
|
|
12
|
+
from dataclasses import dataclass
|
|
13
|
+
from datetime import datetime
|
|
14
|
+
from typing import Final
|
|
15
|
+
|
|
16
|
+
from canopy_github import (
|
|
17
|
+
Client,
|
|
18
|
+
GitHubError,
|
|
19
|
+
Ref,
|
|
20
|
+
Reply,
|
|
21
|
+
ServerError,
|
|
22
|
+
backward,
|
|
23
|
+
cut,
|
|
24
|
+
error_types,
|
|
25
|
+
items,
|
|
26
|
+
login,
|
|
27
|
+
more,
|
|
28
|
+
number,
|
|
29
|
+
obj,
|
|
30
|
+
pages,
|
|
31
|
+
parse_time,
|
|
32
|
+
refusal,
|
|
33
|
+
text,
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@dataclass(frozen=True)
|
|
38
|
+
class Pages:
|
|
39
|
+
"""How many of a PR's heaviest nodes one query asks for: its review threads, their comments, and its reviews."""
|
|
40
|
+
|
|
41
|
+
#: Review threads a page, each with its first page of comments.
|
|
42
|
+
threads: int
|
|
43
|
+
#: Comments in a thread's first page, and reviews and conversation comments in the PR's.
|
|
44
|
+
comments: int
|
|
45
|
+
|
|
46
|
+
def variables(self) -> dict[str, object]:
|
|
47
|
+
"""The variables of a query that asks for these pages.
|
|
48
|
+
|
|
49
|
+
Returns:
|
|
50
|
+
The variables, by name.
|
|
51
|
+
"""
|
|
52
|
+
return {"threads": self.threads, "comments": self.comments}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
#: The pages a PR is read in.
|
|
56
|
+
PAGES: Final = Pages(threads=50, comments=100)
|
|
57
|
+
#: The pages a PR is read in once GitHub could not answer a query of it alone in `PAGES`, as it cannot one too heavy to
|
|
58
|
+
#: run within its time limit: some 240 comments and reviews a query rather than 5,200. Later pages bring the rest, so
|
|
59
|
+
#: the PR is read as whole.
|
|
60
|
+
SMALL_PAGES: Final = Pages(threads=10, comments=20)
|
|
61
|
+
#: PRs one query reads: GitHub refuses a query that could return more than 500,000 nodes, and each PR asks for some
|
|
62
|
+
#: 5,500 (`PAGES`' 50 threads of 100 comments, and a page each of reviews, conversation comments, labels, checks, and
|
|
63
|
+
#: timeline events).
|
|
64
|
+
BATCH: Final = 20
|
|
65
|
+
#: The most labels a PR's line carries: canopy refuses a line with more, so a PR with more is not read whole.
|
|
66
|
+
MAX_LABELS: Final = 100
|
|
67
|
+
#: The longest link a `check` line carries: canopy refuses a line with a longer one.
|
|
68
|
+
MAX_URL: Final = 2000
|
|
69
|
+
#: A check's description is cut to this, so its writer cannot make the line too long to read; canopy cuts it shorter.
|
|
70
|
+
MAX_DESCRIPTION: Final = 1000
|
|
71
|
+
|
|
72
|
+
_REVIEW: Final = """
|
|
73
|
+
fragment Review on PullRequestReview {
|
|
74
|
+
id fullDatabaseId state submittedAt createdAt body url author { __typename login }
|
|
75
|
+
}
|
|
76
|
+
"""
|
|
77
|
+
|
|
78
|
+
_COMMENT: Final = """
|
|
79
|
+
fragment ThreadComment on PullRequestReviewComment {
|
|
80
|
+
id fullDatabaseId body createdAt url author { __typename login } pullRequestReview { fullDatabaseId }
|
|
81
|
+
}
|
|
82
|
+
"""
|
|
83
|
+
|
|
84
|
+
_PR_COMMENT: Final = """
|
|
85
|
+
fragment PrComment on IssueComment {
|
|
86
|
+
id fullDatabaseId body createdAt url author { __typename login }
|
|
87
|
+
}
|
|
88
|
+
"""
|
|
89
|
+
|
|
90
|
+
_THREAD: Final = (
|
|
91
|
+
"""
|
|
92
|
+
fragment Thread on PullRequestReviewThread {
|
|
93
|
+
id path line diffSide isOutdated isResolved
|
|
94
|
+
comments(first: $comments) { pageInfo { hasNextPage endCursor } nodes { ...ThreadComment } }
|
|
95
|
+
}
|
|
96
|
+
"""
|
|
97
|
+
+ _COMMENT
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
#: Every named context, a check run or a commit status: never the combined status, which stops at 30 (DESIGN §30).
|
|
101
|
+
_CONTEXTS: Final = """
|
|
102
|
+
fragment Contexts on StatusCheckRollupContextConnection {
|
|
103
|
+
pageInfo { hasNextPage endCursor }
|
|
104
|
+
nodes {
|
|
105
|
+
__typename
|
|
106
|
+
... on CheckRun { name status conclusion databaseId detailsUrl url title }
|
|
107
|
+
... on StatusContext { context state targetUrl description }
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
"""
|
|
111
|
+
|
|
112
|
+
#: The timeline events a PR's ready and review-requested times are read from, read from the newest, as the latest ready
|
|
113
|
+
#: event matters and nothing before it does.
|
|
114
|
+
_EVENTS: Final = "itemTypes: [READY_FOR_REVIEW_EVENT, REVIEW_REQUESTED_EVENT], last: 100"
|
|
115
|
+
_TIMELINE: Final = """
|
|
116
|
+
fragment Timeline on PullRequestTimelineItemsConnection {
|
|
117
|
+
pageInfo { hasPreviousPage startCursor }
|
|
118
|
+
nodes { __typename ... on ReadyForReviewEvent { createdAt } ... on ReviewRequestedEvent { createdAt } }
|
|
119
|
+
}
|
|
120
|
+
"""
|
|
121
|
+
|
|
122
|
+
_PULL: Final = (
|
|
123
|
+
f"""
|
|
124
|
+
fragment Pull on PullRequest {{
|
|
125
|
+
id url number title state isDraft headRefOid baseRefName mergeable mergeStateStatus createdAt mergedAt closedAt
|
|
126
|
+
author {{ __typename login }}
|
|
127
|
+
labels(first: 100) {{ pageInfo {{ hasNextPage endCursor }} nodes {{ name }} }}
|
|
128
|
+
reviews(first: $comments) {{ pageInfo {{ hasNextPage endCursor }} nodes {{ ...Review }} }}
|
|
129
|
+
reviewThreads(first: $threads) {{ pageInfo {{ hasNextPage endCursor }} nodes {{ ...Thread }} }}
|
|
130
|
+
comments(first: $comments) {{ pageInfo {{ hasNextPage endCursor }} nodes {{ ...PrComment }} }}
|
|
131
|
+
commits(last: 1) {{ nodes {{ commit {{ id oid statusCheckRollup {{ contexts(first: 100) {{ ...Contexts }} }} }} }} }}
|
|
132
|
+
timelineItems({_EVENTS}) {{ ...Timeline }}
|
|
133
|
+
}}
|
|
134
|
+
"""
|
|
135
|
+
+ _REVIEW
|
|
136
|
+
+ _THREAD
|
|
137
|
+
+ _PR_COMMENT
|
|
138
|
+
+ _CONTEXTS
|
|
139
|
+
+ _TIMELINE
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
_REVIEWS: Final = (
|
|
143
|
+
"""
|
|
144
|
+
query Reviews($id: ID!, $after: String) {
|
|
145
|
+
node(id: $id) {
|
|
146
|
+
... on PullRequest { reviews(first: 100, after: $after) { pageInfo { hasNextPage endCursor } nodes { ...Review } } }
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
"""
|
|
150
|
+
+ _REVIEW
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
_PR_COMMENTS: Final = (
|
|
154
|
+
"""
|
|
155
|
+
query PrComments($id: ID!, $after: String) {
|
|
156
|
+
node(id: $id) {
|
|
157
|
+
... on PullRequest {
|
|
158
|
+
comments(first: 100, after: $after) { pageInfo { hasNextPage endCursor } nodes { ...PrComment } }
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
"""
|
|
163
|
+
+ _PR_COMMENT
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
_THREADS: Final = (
|
|
167
|
+
"""
|
|
168
|
+
query Threads($owner: String!, $name: String!, $number: Int!, $after: String, $threads: Int!, $comments: Int!) {
|
|
169
|
+
repository(owner: $owner, name: $name) {
|
|
170
|
+
pullRequest(number: $number) {
|
|
171
|
+
reviewThreads(first: $threads, after: $after) { pageInfo { hasNextPage endCursor } nodes { ...Thread } }
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
"""
|
|
176
|
+
+ _THREAD
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
_THREAD_COMMENTS: Final = (
|
|
180
|
+
"""
|
|
181
|
+
query ThreadComments($id: ID!, $after: String) {
|
|
182
|
+
node(id: $id) {
|
|
183
|
+
... on PullRequestReviewThread {
|
|
184
|
+
comments(first: 100, after: $after) { pageInfo { hasNextPage endCursor } nodes { ...ThreadComment } }
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
"""
|
|
189
|
+
+ _COMMENT
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
_MORE_CONTEXTS: Final = (
|
|
193
|
+
"""
|
|
194
|
+
query Contexts($id: ID!, $after: String) {
|
|
195
|
+
node(id: $id) { ... on Commit { statusCheckRollup { contexts(first: 100, after: $after) { ...Contexts } } } }
|
|
196
|
+
}
|
|
197
|
+
"""
|
|
198
|
+
+ _CONTEXTS
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
_MORE_TIMELINE: Final = (
|
|
202
|
+
f"""
|
|
203
|
+
query Timeline($id: ID!, $before: String) {{
|
|
204
|
+
node(id: $id) {{ ... on PullRequest {{ timelineItems({_EVENTS}, before: $before) {{ ...Timeline }} }} }}
|
|
205
|
+
}}
|
|
206
|
+
"""
|
|
207
|
+
+ _TIMELINE
|
|
208
|
+
)
|
|
209
|
+
|
|
210
|
+
#: A commit status's state, as a check's status and conclusion: one expected is one no context has written yet.
|
|
211
|
+
_STATUS_STATES: Final[Mapping[str, tuple[str, str | None]]] = {
|
|
212
|
+
"EXPECTED": ("pending", None),
|
|
213
|
+
"PENDING": ("pending", None),
|
|
214
|
+
"SUCCESS": ("completed", "success"),
|
|
215
|
+
"FAILURE": ("completed", "failure"),
|
|
216
|
+
"ERROR": ("completed", "error"),
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
@dataclass(frozen=True)
|
|
221
|
+
class Thread:
|
|
222
|
+
"""A review thread: its node id, where it is, and its comments' lines, oldest first."""
|
|
223
|
+
|
|
224
|
+
id: str
|
|
225
|
+
comments: tuple[Mapping[str, object], ...]
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
@dataclass(frozen=True)
|
|
229
|
+
class Pulled:
|
|
230
|
+
"""One PR as read: its lines, the `pr` line first, or why it was not read whole."""
|
|
231
|
+
|
|
232
|
+
lines: Sequence[Mapping[str, object]] = ()
|
|
233
|
+
error: GitHubError | None = None
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def pull_requests(client: Client, refs: Sequence[Ref]) -> dict[str, Pulled]:
|
|
237
|
+
"""Read each of `refs`, all on `client`'s host, whole: `BATCH` to a query, then any page past each one's first.
|
|
238
|
+
|
|
239
|
+
The lines are DESIGN §30's: ids, states, times, and bodies cut at `MAX_BODY`, never a diff or a log. A PR GitHub
|
|
240
|
+
does not return, or one a later page of fails, is not read whole, and the rest still are. A query GitHub answers
|
|
241
|
+
with a server error is not sent again as it was, as one too heavy to answer in time would fail each time: a query
|
|
242
|
+
of many PRs is read again in halves, and one of a lone PR in `SMALL_PAGES`, which alone is sent again.
|
|
243
|
+
|
|
244
|
+
Returns:
|
|
245
|
+
Each PR's read, by its URL.
|
|
246
|
+
"""
|
|
247
|
+
found: dict[str, Pulled] = {}
|
|
248
|
+
for start in range(0, len(refs), BATCH):
|
|
249
|
+
chunk = refs[start : start + BATCH]
|
|
250
|
+
found |= _read(client, chunk, _ask(client, chunk, PAGES))
|
|
251
|
+
return found
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def threads(client: Client, ref: Ref) -> list[Thread]:
|
|
255
|
+
"""Every review thread of `ref`, each with every comment.
|
|
256
|
+
|
|
257
|
+
Returns:
|
|
258
|
+
The threads, in GitHub's order.
|
|
259
|
+
|
|
260
|
+
Raises:
|
|
261
|
+
GitHubError: GitHub refused a page, could not be reached, or has no such PR.
|
|
262
|
+
"""
|
|
263
|
+
return list(_threads(client, ref, None, PAGES))
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
def _ask(client: Client, refs: Sequence[Ref], sizes: Pages) -> Reply | GitHubError:
|
|
267
|
+
# A batch's halves, or a lone PR's smaller pages, are its new tries: sending it again as it was would spend the
|
|
268
|
+
# connector's time on what may fail each time.
|
|
269
|
+
try:
|
|
270
|
+
return client.ask(_batch(len(refs)), _variables(refs) | sizes.variables(), retry=sizes == SMALL_PAGES)
|
|
271
|
+
except GitHubError as e:
|
|
272
|
+
return e
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
def _read(
|
|
276
|
+
client: Client,
|
|
277
|
+
refs: Sequence[Ref],
|
|
278
|
+
reply: Reply | GitHubError,
|
|
279
|
+
*,
|
|
280
|
+
sizes: Pages = PAGES,
|
|
281
|
+
split: bool = True,
|
|
282
|
+
) -> dict[str, Pulled]:
|
|
283
|
+
"""Each of `refs` from the reply to the query that read them in `sizes`, or, for a server error, read again.
|
|
284
|
+
|
|
285
|
+
A server error to a query in `PAGES` reads its PRs again, in halves, or, for a lone PR, in `SMALL_PAGES`, unless
|
|
286
|
+
`split` is false.
|
|
287
|
+
|
|
288
|
+
Returns:
|
|
289
|
+
Each PR's read, by its URL.
|
|
290
|
+
"""
|
|
291
|
+
if split and isinstance(reply, ServerError) and sizes == PAGES:
|
|
292
|
+
if len(refs) > 1:
|
|
293
|
+
return _halves(client, refs)
|
|
294
|
+
return _read(client, refs, _ask(client, refs, SMALL_PAGES), sizes=SMALL_PAGES)
|
|
295
|
+
if isinstance(reply, GitHubError):
|
|
296
|
+
return {ref.url: Pulled(error=reply) for ref in refs}
|
|
297
|
+
found: dict[str, Pulled] = {}
|
|
298
|
+
for alias, ref in zip(_aliases(len(refs)), refs, strict=True):
|
|
299
|
+
try:
|
|
300
|
+
found[ref.url] = Pulled(_pull(client, ref, _aliased(reply, alias, ref), sizes))
|
|
301
|
+
except GitHubError as e:
|
|
302
|
+
found[ref.url] = Pulled(error=e)
|
|
303
|
+
return found
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
def _halves(client: Client, refs: Sequence[Ref]) -> dict[str, Pulled]:
|
|
307
|
+
"""Read `refs`, whose query GitHub answered with a server error, in two halves, a half that fails split again.
|
|
308
|
+
|
|
309
|
+
When both halves fail, GitHub is failing whatever it is asked rather than one PR too heavy for a query, and neither
|
|
310
|
+
is split again, nor read in smaller pages: a GitHub that is down is sent three queries a batch.
|
|
311
|
+
|
|
312
|
+
Returns:
|
|
313
|
+
Each PR's read, by its URL.
|
|
314
|
+
"""
|
|
315
|
+
middle = len(refs) // 2
|
|
316
|
+
halves = (refs[:middle], refs[middle:])
|
|
317
|
+
replies = [_ask(client, half, PAGES) for half in halves]
|
|
318
|
+
split = not all(isinstance(reply, GitHubError) for reply in replies)
|
|
319
|
+
found: dict[str, Pulled] = {}
|
|
320
|
+
for half, reply in zip(halves, replies, strict=True):
|
|
321
|
+
found |= _read(client, half, reply, split=split)
|
|
322
|
+
return found
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
def _batch(count: int) -> str:
|
|
326
|
+
"""The query that reads `count` PRs, each under its alias, with its owner, name, and number as variables.
|
|
327
|
+
|
|
328
|
+
Its pages are variables too, `Pages.variables`.
|
|
329
|
+
|
|
330
|
+
Returns:
|
|
331
|
+
The query.
|
|
332
|
+
"""
|
|
333
|
+
params = ", ".join(
|
|
334
|
+
["$threads: Int!, $comments: Int!"] + [f"$o{i}: String!, $n{i}: String!, $k{i}: Int!" for i in range(count)],
|
|
335
|
+
)
|
|
336
|
+
fields = "\n".join(
|
|
337
|
+
f" {alias}: repository(owner: $o{i}, name: $n{i}) {{ pullRequest(number: $k{i}) {{ ...Pull }} }}"
|
|
338
|
+
for i, alias in enumerate(_aliases(count))
|
|
339
|
+
)
|
|
340
|
+
return f"query PullRequests({params}) {{\n{fields}\n}}\n{_PULL}"
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
def _aliases(count: int) -> list[str]:
|
|
344
|
+
return [f"p{i}" for i in range(count)]
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
def _variables(refs: Sequence[Ref]) -> dict[str, object]:
|
|
348
|
+
found: dict[str, object] = {}
|
|
349
|
+
for i, ref in enumerate(refs):
|
|
350
|
+
found |= {f"o{i}": ref.owner, f"n{i}": ref.name, f"k{i}": ref.number}
|
|
351
|
+
return found
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
def _aliased(reply: Reply, alias: str, ref: Ref) -> Mapping[str, object]:
|
|
355
|
+
"""The PR under `alias` in a batch's reply.
|
|
356
|
+
|
|
357
|
+
Returns:
|
|
358
|
+
The PR's node.
|
|
359
|
+
|
|
360
|
+
Raises:
|
|
361
|
+
GitHubError: An error the reply carries is about it, or about no one PR, or GitHub returned no such PR.
|
|
362
|
+
"""
|
|
363
|
+
errors = [e for e in reply.errors if _about(e, alias)]
|
|
364
|
+
if errors:
|
|
365
|
+
raise GitHubError(refusal(errors), certain=True, types=error_types(errors))
|
|
366
|
+
repository = obj(reply.data.get(alias), f"repository {ref.owner}/{ref.name}")
|
|
367
|
+
return obj(repository.get("pullRequest"), f"pull request {ref.url}")
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
def _about(error: Mapping[str, object], alias: str) -> bool:
|
|
371
|
+
# An error with no path is about the whole query, so about every PR in it.
|
|
372
|
+
path = error.get("path")
|
|
373
|
+
return not isinstance(path, list) or not path or path[0] == alias
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
def _pull(client: Client, ref: Ref, pr: Mapping[str, object], sizes: Pages) -> list[Mapping[str, object]]:
|
|
377
|
+
"""Read the rest of one PR from its first page: its `pr` line, checks, reviews, review comments, and PR comments.
|
|
378
|
+
|
|
379
|
+
Its later pages of threads are read in `sizes`, as its first was.
|
|
380
|
+
|
|
381
|
+
Returns:
|
|
382
|
+
The lines, the `pr` line first.
|
|
383
|
+
|
|
384
|
+
Raises:
|
|
385
|
+
GitHubError: A later page could not be read, or the PR has more labels than its line may carry.
|
|
386
|
+
"""
|
|
387
|
+
labels = obj(pr.get("labels"), "labels")
|
|
388
|
+
if obj(labels.get("pageInfo"), "pageInfo").get("hasNextPage") is True:
|
|
389
|
+
raise GitHubError(f"it has more than {MAX_LABELS} labels", certain=True)
|
|
390
|
+
node = text(pr.get("id")) or ""
|
|
391
|
+
|
|
392
|
+
def reviews_after(after: str) -> Mapping[str, object]:
|
|
393
|
+
found = obj(client.query(_REVIEWS, {"id": node, "after": after}).get("node"), "pull request")
|
|
394
|
+
return obj(found.get("reviews"), "reviews")
|
|
395
|
+
|
|
396
|
+
def comments_after(after: str) -> Mapping[str, object]:
|
|
397
|
+
found = obj(client.query(_PR_COMMENTS, {"id": node, "after": after}).get("node"), "pull request")
|
|
398
|
+
return obj(found.get("comments"), "comments")
|
|
399
|
+
|
|
400
|
+
lines: list[Mapping[str, object]] = [_pr_line(ref, pr, items(labels.get("nodes")), _ready(client, pr))]
|
|
401
|
+
lines.extend(_checks(client, ref, pr))
|
|
402
|
+
lines.extend(_review(ref, review) for review in more(obj(pr.get("reviews"), "reviews"), reviews_after))
|
|
403
|
+
first = obj(pr.get("reviewThreads"), "reviewThreads")
|
|
404
|
+
lines.extend(comment for thread in _threads(client, ref, first, sizes) for comment in thread.comments)
|
|
405
|
+
lines.extend(_pr_comment(ref, c) for c in more(obj(pr.get("comments"), "comments"), comments_after))
|
|
406
|
+
return lines
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
def _threads(client: Client, ref: Ref, first: Mapping[str, object] | None, sizes: Pages) -> Iterator[Thread]:
|
|
410
|
+
"""Each review thread of `ref`, from its first page when in hand, each with every comment, a page in `sizes`.
|
|
411
|
+
|
|
412
|
+
Yields:
|
|
413
|
+
Each thread, in GitHub's order.
|
|
414
|
+
"""
|
|
415
|
+
|
|
416
|
+
def fetch(after: str | None) -> Mapping[str, object]:
|
|
417
|
+
if after is None and first is not None:
|
|
418
|
+
return first
|
|
419
|
+
variables = {"owner": ref.owner, "name": ref.name, "number": ref.number, "after": after} | sizes.variables()
|
|
420
|
+
repository = obj(client.query(_THREADS, variables).get("repository"), f"repository {ref.owner}/{ref.name}")
|
|
421
|
+
pr = obj(repository.get("pullRequest"), f"pull request {ref.url}")
|
|
422
|
+
return obj(pr.get("reviewThreads"), "reviewThreads")
|
|
423
|
+
|
|
424
|
+
for node in pages(fetch):
|
|
425
|
+
yield _thread(client, ref, node)
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
def _pr_line(
|
|
429
|
+
ref: Ref,
|
|
430
|
+
pr: Mapping[str, object],
|
|
431
|
+
labels: Sequence[Mapping[str, object]],
|
|
432
|
+
ready: tuple[str | None, str | None],
|
|
433
|
+
) -> Mapping[str, object]:
|
|
434
|
+
state = text(pr.get("state"))
|
|
435
|
+
mergeable = text(pr.get("mergeable"))
|
|
436
|
+
merge_state = text(pr.get("mergeStateStatus"))
|
|
437
|
+
return {
|
|
438
|
+
"kind": "pr",
|
|
439
|
+
"url": ref.url,
|
|
440
|
+
"node": text(pr.get("id")),
|
|
441
|
+
"repository": f"{ref.owner}/{ref.name}",
|
|
442
|
+
"number": ref.number,
|
|
443
|
+
"head_sha": text(pr.get("headRefOid")),
|
|
444
|
+
"base_ref": text(pr.get("baseRefName")),
|
|
445
|
+
"state": state.lower() if state else None,
|
|
446
|
+
"draft": pr.get("isDraft") is True,
|
|
447
|
+
"mergeable": mergeable.lower() if mergeable else None,
|
|
448
|
+
"mergeable_state": merge_state.lower() if merge_state else None,
|
|
449
|
+
"author": login(pr.get("author")),
|
|
450
|
+
# The author's text, which canopy cuts and marks; for display only.
|
|
451
|
+
"title": text(pr.get("title")),
|
|
452
|
+
"labels": [name for label in labels if (name := text(label.get("name"))) is not None],
|
|
453
|
+
"created_at": text(pr.get("createdAt")),
|
|
454
|
+
"ready_at": ready[0],
|
|
455
|
+
"review_requested_at": ready[1],
|
|
456
|
+
"merged_at": text(pr.get("mergedAt")),
|
|
457
|
+
"closed_at": text(pr.get("closedAt")),
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
def _ready(client: Client, pr: Mapping[str, object]) -> tuple[str | None, str | None]:
|
|
462
|
+
"""When the PR last became ready for review, and when a review of it was first asked for at or after that.
|
|
463
|
+
|
|
464
|
+
Its timeline is read from the newest event back to the latest ready event, every page of it when it has none. A PR
|
|
465
|
+
opened ready has no ready event, so its creation is its ready time; a draft is not ready, and has neither time.
|
|
466
|
+
|
|
467
|
+
Returns:
|
|
468
|
+
The two times as GitHub wrote them, each None where there is none.
|
|
469
|
+
|
|
470
|
+
Raises:
|
|
471
|
+
GitHubError: An earlier page of the timeline could not be read.
|
|
472
|
+
"""
|
|
473
|
+
if pr.get("isDraft") is True:
|
|
474
|
+
return None, None
|
|
475
|
+
node = text(pr.get("id")) or ""
|
|
476
|
+
|
|
477
|
+
def before(cursor: str) -> Mapping[str, object]:
|
|
478
|
+
found = obj(client.query(_MORE_TIMELINE, {"id": node, "before": cursor}).get("node"), "pull request")
|
|
479
|
+
return obj(found.get("timelineItems"), "timelineItems")
|
|
480
|
+
|
|
481
|
+
ready: tuple[datetime, str] | None = None
|
|
482
|
+
asked: list[tuple[datetime, str]] = []
|
|
483
|
+
for page in backward(obj(pr.get("timelineItems"), "timelineItems"), before):
|
|
484
|
+
for event in page:
|
|
485
|
+
at = text(event.get("createdAt"))
|
|
486
|
+
moment = parse_time(at)
|
|
487
|
+
if at is None or moment is None:
|
|
488
|
+
continue
|
|
489
|
+
if event.get("__typename") == "ReadyForReviewEvent" and (ready is None or ready[0] < moment):
|
|
490
|
+
ready = (moment, at)
|
|
491
|
+
elif event.get("__typename") == "ReviewRequestedEvent":
|
|
492
|
+
asked.append((moment, at))
|
|
493
|
+
# The latest ready event is on the newest page that holds one; every earlier page came before it.
|
|
494
|
+
if ready is not None:
|
|
495
|
+
break
|
|
496
|
+
created = text(pr.get("createdAt"))
|
|
497
|
+
opened = parse_time(created)
|
|
498
|
+
if ready is None and created is not None and opened is not None:
|
|
499
|
+
ready = (opened, created)
|
|
500
|
+
after = [a for a in asked if ready is None or a[0] >= ready[0]]
|
|
501
|
+
return (None if ready is None else ready[1]), (min(after)[1] if after else None)
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
def _checks(client: Client, ref: Ref, pr: Mapping[str, object]) -> list[Mapping[str, object]]:
|
|
505
|
+
"""A `check` line for each context at the PR's head, every page of them; one per name, the newest run of it.
|
|
506
|
+
|
|
507
|
+
A name can come back more than once, as when a workflow ran for two events; canopy keeps one row a name, so the
|
|
508
|
+
line is the check run GitHub made last, which is the one its page shows.
|
|
509
|
+
|
|
510
|
+
Returns:
|
|
511
|
+
The lines, in GitHub's order.
|
|
512
|
+
"""
|
|
513
|
+
commits = items(obj(pr.get("commits"), "commits").get("nodes"))
|
|
514
|
+
commit: Mapping[str, object] = obj(commits[-1].get("commit"), "commit") if commits else {}
|
|
515
|
+
sha = text(commit.get("oid"))
|
|
516
|
+
rollup = commit.get("statusCheckRollup")
|
|
517
|
+
if sha is None or not isinstance(rollup, dict):
|
|
518
|
+
# Nothing has written a check or a status at the head yet.
|
|
519
|
+
return []
|
|
520
|
+
first = obj(obj(rollup, "statusCheckRollup").get("contexts"), "contexts")
|
|
521
|
+
commit_id = text(commit.get("id")) or ""
|
|
522
|
+
|
|
523
|
+
def after(cursor: str) -> Mapping[str, object]:
|
|
524
|
+
found = obj(client.query(_MORE_CONTEXTS, {"id": commit_id, "after": cursor}).get("node"), "commit")
|
|
525
|
+
return obj(obj(found.get("statusCheckRollup"), "statusCheckRollup").get("contexts"), "contexts")
|
|
526
|
+
|
|
527
|
+
newest: dict[str, tuple[int, Mapping[str, object]]] = {}
|
|
528
|
+
for node in more(first, after):
|
|
529
|
+
found = _context(ref, sha, node)
|
|
530
|
+
if found is None:
|
|
531
|
+
continue
|
|
532
|
+
rank, line = found
|
|
533
|
+
name = str(line["name"])
|
|
534
|
+
if name not in newest or rank >= newest[name][0]:
|
|
535
|
+
newest[name] = (rank, line)
|
|
536
|
+
return [line for _, line in newest.values()]
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
def _context(ref: Ref, sha: str, node: Mapping[str, object]) -> tuple[int, Mapping[str, object]] | None:
|
|
540
|
+
"""A context's `check` line, and its rank among contexts of its name: a check run's id, a status's 0.
|
|
541
|
+
|
|
542
|
+
Returns:
|
|
543
|
+
The rank and the line, or None for a context with no name.
|
|
544
|
+
"""
|
|
545
|
+
line: dict[str, object] = {"kind": "check", "pr": ref.url, "head_sha": sha}
|
|
546
|
+
if node.get("__typename") == "StatusContext":
|
|
547
|
+
name, state = text(node.get("context")), text(node.get("state")) or ""
|
|
548
|
+
status, conclusion = _STATUS_STATES.get(state, (state.lower(), None))
|
|
549
|
+
line |= {"name": name, "status": status, "conclusion": conclusion, "run_id": None}
|
|
550
|
+
line["url"] = _url(node.get("targetUrl"))
|
|
551
|
+
# Its writer's text, which canopy cuts to one line; for display only, as a merge queue's or a sign-off's state.
|
|
552
|
+
line["description"] = _description(node.get("description"))
|
|
553
|
+
rank = 0
|
|
554
|
+
else:
|
|
555
|
+
name, status, conclusion = text(node.get("name")), text(node.get("status")), text(node.get("conclusion"))
|
|
556
|
+
run_id = number(node.get("databaseId"))
|
|
557
|
+
line |= {
|
|
558
|
+
"name": name,
|
|
559
|
+
"status": status.lower() if status else None,
|
|
560
|
+
"conclusion": conclusion.lower() if conclusion else None,
|
|
561
|
+
"run_id": run_id,
|
|
562
|
+
"url": _url(node.get("detailsUrl")) or _url(node.get("url")),
|
|
563
|
+
# Its output's title, its writer's text, which canopy cuts to one line; for display only.
|
|
564
|
+
"description": _description(node.get("title")),
|
|
565
|
+
}
|
|
566
|
+
rank = run_id or 0
|
|
567
|
+
return None if name is None else (rank, line)
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
def _url(value: object) -> str | None:
|
|
571
|
+
# A context's link is whatever its writer set; one canopy would refuse is left out rather than refuse the PR.
|
|
572
|
+
found = text(value)
|
|
573
|
+
if found is None or not found.startswith("https://") or len(found) > MAX_URL or any(c.isspace() for c in found):
|
|
574
|
+
return None
|
|
575
|
+
return found
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
def _description(value: object) -> str | None:
|
|
579
|
+
found = text(value)
|
|
580
|
+
return None if found is None else found[:MAX_DESCRIPTION]
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
def _review(ref: Ref, node: Mapping[str, object]) -> Mapping[str, object]:
|
|
584
|
+
state = text(node.get("state"))
|
|
585
|
+
return {
|
|
586
|
+
"kind": "review",
|
|
587
|
+
"item": "review",
|
|
588
|
+
"pr": ref.url,
|
|
589
|
+
"id": number(node.get("fullDatabaseId")),
|
|
590
|
+
"node": text(node.get("id")),
|
|
591
|
+
"author": login(node.get("author")),
|
|
592
|
+
"bot": _bot(node.get("author")),
|
|
593
|
+
"state": state.lower() if state else None,
|
|
594
|
+
# A pending review has not been submitted.
|
|
595
|
+
"time": text(node.get("submittedAt")) or text(node.get("createdAt")),
|
|
596
|
+
"body": cut(node.get("body")),
|
|
597
|
+
"url": text(node.get("url")),
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
def _thread(client: Client, ref: Ref, node: Mapping[str, object]) -> Thread:
|
|
602
|
+
thread_id = text(node.get("id")) or ""
|
|
603
|
+
side = text(node.get("diffSide"))
|
|
604
|
+
where: Mapping[str, object] = {
|
|
605
|
+
"id": thread_id,
|
|
606
|
+
"path": text(node.get("path")),
|
|
607
|
+
"line": number(node.get("line")),
|
|
608
|
+
"side": side.lower() if side else None,
|
|
609
|
+
"outdated": node.get("isOutdated") is True,
|
|
610
|
+
"resolved": node.get("isResolved") is True,
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
def after(cursor: str) -> Mapping[str, object]:
|
|
614
|
+
found = obj(client.query(_THREAD_COMMENTS, {"id": thread_id, "after": cursor}).get("node"), "thread")
|
|
615
|
+
return obj(found.get("comments"), "comments")
|
|
616
|
+
|
|
617
|
+
comments = tuple(_comment(ref, where, comment) for comment in more(obj(node.get("comments"), "comments"), after))
|
|
618
|
+
return Thread(id=thread_id, comments=comments)
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
def _comment(ref: Ref, where: Mapping[str, object], node: Mapping[str, object]) -> Mapping[str, object]:
|
|
622
|
+
review = node.get("pullRequestReview")
|
|
623
|
+
return {
|
|
624
|
+
"kind": "review",
|
|
625
|
+
"item": "comment",
|
|
626
|
+
"pr": ref.url,
|
|
627
|
+
"id": number(node.get("fullDatabaseId")),
|
|
628
|
+
"node": text(node.get("id")),
|
|
629
|
+
"author": login(node.get("author")),
|
|
630
|
+
"bot": _bot(node.get("author")),
|
|
631
|
+
"time": text(node.get("createdAt")),
|
|
632
|
+
"body": cut(node.get("body")),
|
|
633
|
+
"url": text(node.get("url")),
|
|
634
|
+
"review": number(obj(review, "review").get("fullDatabaseId")) if isinstance(review, dict) else None,
|
|
635
|
+
"thread": dict(where),
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
def _pr_comment(ref: Ref, node: Mapping[str, object]) -> Mapping[str, object]:
|
|
640
|
+
# A comment on the PR's conversation, on no line of its diff: GitHub keeps it as the PR's issue's comment.
|
|
641
|
+
return {
|
|
642
|
+
"kind": "review",
|
|
643
|
+
"item": "pr_comment",
|
|
644
|
+
"pr": ref.url,
|
|
645
|
+
"id": number(node.get("fullDatabaseId")),
|
|
646
|
+
"node": text(node.get("id")),
|
|
647
|
+
"author": login(node.get("author")),
|
|
648
|
+
"bot": _bot(node.get("author")),
|
|
649
|
+
"time": text(node.get("createdAt")),
|
|
650
|
+
"body": cut(node.get("body")),
|
|
651
|
+
"url": text(node.get("url")),
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
|
|
655
|
+
def _bot(author: object) -> bool:
|
|
656
|
+
return isinstance(author, dict) and obj(author, "author").get("__typename") == "Bot"
|