Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PullRequestProvider.ts

541 lines19.2 KB

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