Repository navigation
Expand file tree
/
Copy pathPullRequestProvider.ts
More file actions
561 lines (509 loc) · 20 KB
/
Copy pathPullRequestProvider.ts
File metadata and controls
561 lines (509 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
import type {
PullRequestStackMembership,
PullRequestAction,
PullRequestStackHead,
PullRequestActor,
PullRequestBaseComparison,
PullRequestCapabilities,
PullRequestChecksState,
PullRequestCheck,
PullRequestChecks,
PullRequestComment,
PullRequestFileViewed,
PullRequestCommit,
PullRequestInvolvement,
PullRequestLabel,
PullRequestListFilters,
PullRequestListState,
PullRequestMergeCapabilities,
PullRequestMergeMethod,
PullRequestMergeability,
PullRequestOmittedFileStat,
PullRequestReaction,
PullRequestReactionContent,
PullRequestReviewCommentDraft,
PullRequestReviewDecision,
PullRequestReviewThread,
PullRequestThreadCommentsResult,
PullRequestReviewVerdict,
PullRequestReviewerCandidateList,
PullRequestReviewerKind,
PullRequestLabelCandidateList,
PullRequestState,
PullRequestPreview,
PullRequestUpdateMethod,
PullRequestViewerPermissions,
SourceControlProviderKind,
} from "@t3tools/contracts";
import { SourceControlProviderKind as SourceControlProviderKindSchema } from "@t3tools/contracts";
export class PullRequestProviderError extends Schema.TaggedError<PullRequestProviderError>()(
"PullRequestProviderError",
{
provider: SourceControlProviderKindSchema,
operation: Schema.String,
reason: Schema.Literals([
"missing-tool",
"unauthenticated",
"rate-limited",
"not-found",
"failed",
]),
detail: Schema.String,
retryAt: Schema.optional(Schema.Number),
cause: Schema.optional(Schema.Defect()),
},
) {
override get message(): string {
return `${this.provider} failed in ${this.operation}: ${this.detail}`;
}
}
export interface ProviderChangeRequestWatchFingerprint {
readonly status: string;
readonly remarks: string;
}
export interface PullRequestProviderFailure {
readonly reason: PullRequestProviderError["reason"];
readonly retryAt?: number | undefined;
}
export interface ProviderChangeRequest {
readonly stack?: PullRequestStackMembership;
readonly number: number;
readonly title: string;
readonly url: string;
readonly author: PullRequestActor | null;
readonly headBranch: string;
readonly headRepositoryNameWithOwner?: string | null;
readonly baseBranch: string;
readonly state: PullRequestState;
readonly isDraft: boolean;
readonly mergeability: PullRequestMergeability;
readonly additions: number;
readonly deletions: number;
readonly createdAt: string;
readonly closedAt?: string | null;
readonly mergedAt?: string | null;
readonly updatedAt: string;
readonly reviewRequestLogins: ReadonlyArray<string>;
readonly labels: ReadonlyArray<PullRequestLabel>;
readonly reviewDecision?: PullRequestReviewDecision | null | undefined;
readonly checksState?: PullRequestChecksState | null | undefined;
}
export interface ProviderChangeRequestSummary {
readonly number: number;
readonly title: string;
readonly url: string;
readonly headBranch: string;
readonly baseBranch: string;
readonly state: PullRequestState;
readonly isDraft?: boolean;
readonly closedAt?: string | null;
readonly mergedAt?: string | null;
readonly updatedAt: string;
readonly author?: PullRequestActor | null | undefined;
readonly additions?: number | undefined;
readonly deletions?: number | undefined;
readonly changedFiles?: number | undefined;
readonly reviewDecision?: PullRequestReviewDecision | null | undefined;
readonly checksState?: PullRequestChecksState | null | undefined;
readonly mergeability?: PullRequestMergeability | undefined;
/**
* Null when the host says it is in none; absent when the read did not ask.
*/
readonly stack?: PullRequestStackMembership | null | undefined;
}
export interface ProviderChangeRequestStackLayer {
readonly title?: string;
readonly isDraft?: boolean;
readonly headSha?: string;
readonly number: number;
readonly headBranch: string;
readonly state: PullRequestState;
}
export interface ProviderChangeRequestStack {
readonly id: string;
readonly number: number;
readonly url: string;
readonly base: string;
readonly layers: ReadonlyArray<ProviderChangeRequestStackLayer>;
}
export interface ProviderChangeRequestPage {
readonly items: ReadonlyArray<ProviderChangeRequest>;
readonly truncated: boolean;
/**
* Most hosts advance by the rows delivered after local de-duplication; an offset-paged host
* may need to count malformed raw rows it consumed too.
*/
readonly cursorAdvance?: number;
readonly continues: boolean;
}
export interface ProviderListCursor {
/**
* Asked for inclusively: several rows share one instant often enough — a bot that touches eight
* change requests writes one timestamp on all eight — and asking for strictly older would lose
* whichever of them the slice ended before.
*/
readonly updatedBefore: string;
readonly delivered: number;
/**
* Rows already handed over at `updatedBefore`. Timestamp-paged hosts can exclude these before
* applying their limit so a full page of tied updates does not repeat the same prefix.
*/
readonly seenAt?: ReadonlyArray<number> | undefined;
}
export interface ProviderBatchedChangeRequest extends ProviderChangeRequest {
readonly repository: string;
}
export interface ProviderBatchedChangeRequestPage {
readonly items: ReadonlyArray<ProviderBatchedChangeRequest>;
readonly truncated: boolean;
}
export interface ProviderChangeRequestStat {
readonly repository: string;
readonly number: number;
readonly additions: number;
readonly deletions: number;
}
export interface ProviderChangeRequestDetail extends ProviderChangeRequest {
readonly headSha?: string | null;
readonly body: string;
readonly changedFiles: number;
readonly mergedAt: string | null;
readonly closedAt: string | null;
readonly reviewers: ReadonlyArray<PullRequestActor>;
readonly checks: ReadonlyArray<PullRequestCheck>;
readonly mergeCapabilities: PullRequestMergeCapabilities;
readonly viewerPermissions: PullRequestViewerPermissions;
readonly baseComparison?: PullRequestBaseComparison;
readonly behindBy?: number;
readonly autoMergeEnabled?: boolean;
readonly autoMergeMethod?: PullRequestMergeMethod;
readonly workflowApprovalsRequired?: number;
}
export interface ProviderChangeRequestActivity {
readonly author?: PullRequestActor | null;
readonly reviewers?: ReadonlyArray<PullRequestActor>;
readonly comments: ReadonlyArray<PullRequestComment>;
readonly commentCount: number;
readonly commentsTruncated: boolean;
readonly reviewThreadsTruncated?: boolean;
readonly reviewThreads: ReadonlyArray<PullRequestReviewThread>;
readonly commits: ReadonlyArray<PullRequestCommit>;
readonly reactions?: ReadonlyArray<PullRequestReaction>;
}
export interface ProviderDiffSlice {
readonly patch: string;
/** Something in this slice could not be shown, as opposed to there being more slices. */
readonly truncated: boolean;
readonly nextCursor: string | null;
readonly omittedFileStats?: ReadonlyArray<PullRequestOmittedFileStat>;
}
export interface ProviderDiffFileContents {
readonly oldContents: string;
readonly newContents: string;
}
export interface ProviderFilesViewed {
readonly files: ReadonlyArray<PullRequestFileViewed>;
/** The host has more files than were read, so the ones missing here are not "unviewed". */
readonly truncated: boolean;
}
export interface ProviderFileStats {
readonly files: ReadonlyArray<{
readonly path: string;
readonly additions: number;
readonly deletions: number;
}>;
/** The host has more files than were read. */
readonly truncated: boolean;
}
/**
* Opaque strings, compared only against one another, where the empty string is the answer for a
* file the change request deletes rather than a gap. A path is absent only where the read could
* not say, so a provider converts its host's own absences on the way here.
*/
export interface ProviderFileRevisions {
readonly revisions: ReadonlyMap<string, string>;
/**
* A host with no per-file version reads the whole change to answer for one file, and saying so
* is what keeps the next tick, naming a path nothing asked about before, from making it read
* the whole change again. False for a read cut short, which cannot speak past the cut.
*/
readonly complete?: boolean;
}
export interface ProviderRepositoryRef {
readonly cwd: string;
readonly repository: string;
readonly host: string;
}
export interface PullRequestProviderApi {
readonly withVerifiedCredential?: <A, E, R>(
input: { readonly cwd: string; readonly host: string },
use: (identity: {
readonly accountId: string;
readonly viewer: string;
readonly credentialFingerprint: string;
}) => Effect.Effect<A, E, R>,
) => Effect.Effect<A, E | PullRequestProviderError, R>;
readonly getRoutingIdentity?: (input: {
readonly cwd: string;
readonly host: string;
}) => Effect.Effect<
{ readonly accountId: string; readonly viewer: string },
PullRequestProviderError
>;
readonly kind: SourceControlProviderKind;
readonly capabilities: PullRequestCapabilities;
readonly mergeMessageRewrite?: (message: string) => string;
/**
* Absent means `owner/name` on the host identifies it, and any checkout on the host can serve a
* change request in another repository there.
*
* A host that resolves it (Azure DevOps: `dev.azure.com/org/project/_git/repo`) derives its
* organization from the checkout, so only a checkout whose key matches the reference serves it.
*/
readonly repositoryKey?: (input: { readonly canonicalKey: string }) => string;
readonly getViewer: (input: {
readonly cwd: string;
readonly host?: string;
}) => Effect.Effect<string, PullRequestProviderError>;
readonly listChangeRequests: (
input: ProviderRepositoryRef & {
readonly state: PullRequestListState;
readonly involvement: PullRequestInvolvement;
readonly viewer: string;
readonly limit: number;
/**
* A host with no text filter of its own ignores it and answers with the page it
* would have answered with anyway — the caller narrows what it gets, so an
* unfiltered page is a wider answer rather than a wrong one.
*/
readonly query?: string | undefined;
readonly cursor?: ProviderListCursor | undefined;
/**
* Further narrowings, which a host applies as far as it can and ignores the rest of —
* an unnarrowed page is a wider answer rather than a wrong one, and the caller narrows
* what it gets for the fields a row carries.
*/
readonly filters?: PullRequestListFilters | undefined;
},
) => Effect.Effect<ProviderChangeRequestPage, PullRequestProviderError>;
/**
* `limit` is the whole slice rather than a size per repository, because that is the shape of
* the answer: the newest `limit` rows across every repository named, which is exactly the
* rows a page ordered by update shows. `cursor` is one boundary for all of them, so a caller
* with repositories standing at different boundaries asks in groups rather than in one call.
*/
readonly listChangeRequestsAcross?: (input: {
readonly cwd: string;
readonly host: string;
readonly repositories: ReadonlyArray<string>;
readonly state: PullRequestListState;
readonly involvement: PullRequestInvolvement;
readonly viewer: string;
readonly limit: number;
readonly query?: string | undefined;
readonly cursor?: ProviderListCursor | undefined;
readonly filters?: PullRequestListFilters | undefined;
}) => Effect.Effect<ProviderBatchedChangeRequestPage, PullRequestProviderError>;
readonly listChangeRequestStats?: (input: {
readonly cwd: string;
readonly host: string;
readonly changeRequests: ReadonlyArray<{
readonly repository: string;
readonly number: number;
}>;
}) => Effect.Effect<ReadonlyArray<ProviderChangeRequestStat>, PullRequestProviderError>;
readonly getChangeRequestChecks?: (
input: ProviderRepositoryRef & { readonly number: number },
) => Effect.Effect<PullRequestChecks, PullRequestProviderError>;
readonly getChangeRequest: (
input: ProviderRepositoryRef & { readonly number: number },
) => Effect.Effect<ProviderChangeRequestDetail, PullRequestProviderError>;
readonly getChangeRequestPreview?: (
input: ProviderRepositoryRef & { readonly number: number },
) => Effect.Effect<
Omit<PullRequestPreview, "projectId" | "repository">,
PullRequestProviderError
>;
readonly getChangeRequestSummary?: (
input: ProviderRepositoryRef & { readonly number: number },
) => Effect.Effect<ProviderChangeRequestSummary, PullRequestProviderError>;
/**
* Null when the host gave no answer for it. Optional: a host without one has
* its watched change requests read in full on every pass.
*/
readonly getChangeRequestWatchFingerprint?: (
input: ProviderRepositoryRef & { readonly number: number },
) => Effect.Effect<ProviderChangeRequestWatchFingerprint | null, PullRequestProviderError>;
readonly getChangeRequestStack?: (
input: ProviderRepositoryRef & { readonly includeDetails?: boolean; readonly number: number },
) => Effect.Effect<ProviderChangeRequestStack | null, PullRequestProviderError>;
readonly getChangeRequestActivity: (
input: ProviderRepositoryRef & { readonly number: number },
) => Effect.Effect<ProviderChangeRequestActivity, PullRequestProviderError>;
readonly getReviewThreadComments?: (
input: ProviderRepositoryRef & {
readonly number: number;
readonly threadId: string;
readonly cursor: string;
},
) => Effect.Effect<PullRequestThreadCommentsResult, PullRequestProviderError>;
/**
* Asked before anything is written, so a request that reached the server without going past
* the page is refused by what the host says rather than by what the client claimed — and
* asked freshly, because access granted or taken away since the page loaded is exactly the
* case this guards.
*/
readonly getViewerPermissions: (
input: ProviderRepositoryRef & {
readonly number: number;
readonly includeUpdateBranch?: boolean;
},
) => Effect.Effect<PullRequestViewerPermissions, PullRequestProviderError>;
readonly getDiff: (
input: ProviderRepositoryRef & {
readonly number: number;
readonly cursor?: string | undefined;
readonly commit?: string | undefined;
},
) => Effect.Effect<ProviderDiffSlice, PullRequestProviderError>;
readonly getDiffFileContents?: (
input: ProviderRepositoryRef & {
readonly number: number;
readonly commit?: string | undefined;
readonly changeType: "change" | "rename-pure" | "rename-changed" | "new" | "deleted";
readonly oldPath: string;
readonly newPath: string;
},
) => Effect.Effect<ProviderDiffFileContents, PullRequestProviderError>;
readonly getFilesViewed?: (
input: ProviderRepositoryRef & { readonly number: number },
) => Effect.Effect<ProviderFilesViewed, PullRequestProviderError>;
/** Line counts for every changed file. Optional: a host without one shows no breakdown. */
readonly getFileStats?: (
input: ProviderRepositoryRef & { readonly number: number },
) => Effect.Effect<ProviderFileStats, PullRequestProviderError>;
/**
* A host with no bulk form is still owed one round trip for the batch rather than one per
* file, since the point of gathering presses is that the host is asked once.
*/
readonly setFilesViewed?: (
input: ProviderRepositoryRef & {
readonly number: number;
readonly files: ReadonlyArray<{ readonly path: string; readonly viewed: boolean }>;
},
) => Effect.Effect<void, PullRequestProviderError>;
/**
* Required of a host whose `capabilities.viewedFiles` is `"environment"`, and unused by one
* that keeps the marks itself: the marks live here, but only the host can say whether what a
* reader cleared last week is still what is in front of them.
*/
readonly getFileRevisions?: (
input: ProviderRepositoryRef & {
readonly number: number;
readonly paths: ReadonlyArray<string>;
},
) => Effect.Effect<ProviderFileRevisions, PullRequestProviderError>;
readonly runAction: (
input: ProviderRepositoryRef & {
readonly number: number;
readonly action: PullRequestAction;
readonly stackNumber?: number;
readonly expectedStackHeads?: ReadonlyArray<PullRequestStackHead>;
readonly removeAgentCreditsOnMerge?: boolean;
readonly mergeMethod?: PullRequestMergeMethod;
readonly updateMethod?: PullRequestUpdateMethod;
},
) => Effect.Effect<void, PullRequestProviderError>;
/**
* Only called when `capabilities.edit.changeRequest` is true, and never with both fields
* absent — the caller refuses that before it gets here, because a host asked to change
* nothing answers differently on each of them.
*/
readonly updateChangeRequest?: (
input: ProviderRepositoryRef & {
readonly number: number;
readonly title?: string | undefined;
readonly body?: string | undefined;
},
) => Effect.Effect<void, PullRequestProviderError>;
readonly comment: (
input: ProviderRepositoryRef & { readonly number: number; readonly body: string },
) => Effect.Effect<void, PullRequestProviderError>;
/**
* Whether this remark is the reader's to rewrite is the host's own answer: no read here can
* settle it, since access can be taken away between the conversation being read and the
* rewrite being sent, and a host refuses a stranger's remark with a sentence saying so.
*/
readonly updateComment?: (
input: ProviderRepositoryRef & {
readonly number: number;
readonly commentId: string;
readonly kind: "issue-comment" | "review-comment";
readonly body: string;
},
) => Effect.Effect<void, PullRequestProviderError>;
readonly submitReview: (
input: ProviderRepositoryRef & {
readonly number: number;
readonly verdict: PullRequestReviewVerdict;
readonly body: string;
readonly comments: ReadonlyArray<PullRequestReviewCommentDraft>;
},
) => Effect.Effect<void, PullRequestProviderError>;
/**
* The author is left out by each provider rather than by the caller, because only the
* provider knows how the host spells the same person in a candidate list and on a pull
* request.
*/
readonly listReviewerCandidates: (
input: ProviderRepositoryRef & { readonly number: number },
) => Effect.Effect<PullRequestReviewerCandidateList, PullRequestProviderError>;
readonly setReviewerRequest: (
input: ProviderRepositoryRef & {
readonly number: number;
readonly reviewers: ReadonlyArray<{
readonly id: string;
readonly kind: PullRequestReviewerKind;
}>;
readonly requested: boolean;
},
) => Effect.Effect<void, PullRequestProviderError>;
readonly listLabelCandidates?: (
input: ProviderRepositoryRef & { readonly number: number },
) => Effect.Effect<PullRequestLabelCandidateList, PullRequestProviderError>;
readonly setLabels?: (
input: ProviderRepositoryRef & {
readonly number: number;
readonly labels: ReadonlyArray<string>;
readonly applied: boolean;
},
) => Effect.Effect<void, PullRequestProviderError>;
readonly replyToThread: (
input: ProviderRepositoryRef & {
readonly number: number;
readonly threadId: string;
readonly body: string;
},
) => Effect.Effect<void, PullRequestProviderError>;
/**
* `subjectId` is a remark's id as the conversation carried it; absent means the change
* request itself, whose reactions sit on its description.
*/
readonly setReaction: (
input: ProviderRepositoryRef & {
readonly number: number;
readonly subjectId?: string | undefined;
readonly content: PullRequestReactionContent;
readonly reacted: boolean;
},
) => Effect.Effect<void, PullRequestProviderError>;
readonly setThreadResolution: (
input: ProviderRepositoryRef & {
readonly number: number;
readonly threadId: string;
readonly resolved: boolean;
},
) => Effect.Effect<void, PullRequestProviderError>;
}