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

PullRequestProvider.ts

546 lines19.5 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;
158 /**
159 * Rows already handed over at `updatedBefore`. Timestamp-paged hosts can exclude these before
160 * applying their limit so a full page of tied updates does not repeat the same prefix.
161 */
162 readonly seenAt?: ReadonlyArray<number> | undefined;
164​
165export interface ProviderBatchedChangeRequest extends ProviderChangeRequest {
166 readonly repository: string;
168​
169export interface ProviderBatchedChangeRequestPage {
170 readonly items: ReadonlyArray<ProviderBatchedChangeRequest>;
171 readonly truncated: boolean;
173​
174export interface ProviderChangeRequestStat {
175 readonly repository: string;
176 readonly number: number;
177 readonly additions: number;
178 readonly deletions: number;
180​
181export interface ProviderChangeRequestDetail extends ProviderChangeRequest {
182 readonly headSha?: string | null;
183 readonly body: string;
184 readonly changedFiles: number;
185 readonly mergedAt: string | null;
186 readonly closedAt: string | null;
187 readonly reviewers: ReadonlyArray<PullRequestActor>;
188 readonly checks: ReadonlyArray<PullRequestCheck>;
189 readonly mergeCapabilities: PullRequestMergeCapabilities;
190 readonly viewerPermissions: PullRequestViewerPermissions;
191 readonly baseComparison?: PullRequestBaseComparison;
192 readonly behindBy?: number;
193 readonly autoMergeEnabled?: boolean;
194 readonly autoMergeMethod?: PullRequestMergeMethod;
195 readonly workflowApprovalsRequired?: number;
197​
198export interface ProviderChangeRequestActivity {
199 readonly author?: PullRequestActor | null;
200 readonly reviewers?: ReadonlyArray<PullRequestActor>;
201 readonly comments: ReadonlyArray<PullRequestComment>;
202 readonly commentCount: number;
203 readonly commentsTruncated: boolean;
204 readonly reviewThreadsTruncated?: boolean;
205 readonly reviewThreads: ReadonlyArray<PullRequestReviewThread>;
206 readonly commits: ReadonlyArray<PullRequestCommit>;
207 readonly reactions?: ReadonlyArray<PullRequestReaction>;
209​
210export interface ProviderDiffSlice {
211 readonly patch: string;
212 /** Something in this slice could not be shown, as opposed to there being more slices. */
213 readonly truncated: boolean;
214 readonly nextCursor: string | null;
215 readonly omittedFileStats?: ReadonlyArray<PullRequestOmittedFileStat>;
217​
218export interface ProviderDiffFileContents {
219 readonly oldContents: string;
220 readonly newContents: string;
222​
223export interface ProviderFilesViewed {
224 readonly files: ReadonlyArray<PullRequestFileViewed>;
225 /** The host has more files than were read, so the ones missing here are not "unviewed". */
226 readonly truncated: boolean;
228​
229/**
230 * Opaque strings, compared only against one another, where the empty string is the answer for a
231 * file the change request deletes rather than a gap. A path is absent only where the read could
232 * not say, so a provider converts its host's own absences on the way here.
233 */
234export interface ProviderFileRevisions {
235 readonly revisions: ReadonlyMap<string, string>;
236 /**
237 * A host with no per-file version reads the whole change to answer for one file, and saying so
238 * is what keeps the next tick, naming a path nothing asked about before, from making it read
239 * the whole change again. False for a read cut short, which cannot speak past the cut.
240 */
241 readonly complete?: boolean;
243​
244export interface ProviderRepositoryRef {
245 readonly cwd: string;
246 readonly repository: string;
247 readonly host: string;
249​
250export interface PullRequestProviderApi {
251 readonly withVerifiedCredential?: <A, E, R>(
252 input: { readonly cwd: string; readonly host: string },
253 use: (identity: {
254 readonly accountId: string;
255 readonly viewer: string;
256 readonly credentialFingerprint: string;
257 }) => Effect.Effect<A, E, R>,
258 ) => Effect.Effect<A, E | PullRequestProviderError, R>;
259 readonly getRoutingIdentity?: (input: {
260 readonly cwd: string;
261 readonly host: string;
262 }) => Effect.Effect<
263 { readonly accountId: string; readonly viewer: string },
264 PullRequestProviderError
265 >;
266 readonly kind: SourceControlProviderKind;
267 readonly capabilities: PullRequestCapabilities;
268 readonly mergeMessageRewrite?: (message: string) => string;
269 /**
270 * Absent means `owner/name` on the host identifies it, and any checkout on the host can serve a
271 * change request in another repository there.
272 *
273 * A host that resolves it (Azure DevOps: `dev.azure.com/org/project/_git/repo`) derives its
274 * organization from the checkout, so only a checkout whose key matches the reference serves it.
275 */
276 readonly repositoryKey?: (input: { readonly canonicalKey: string }) => string;
277​
278 readonly getViewer: (input: {
279 readonly cwd: string;
280 readonly host?: string;
281 }) => Effect.Effect<string, PullRequestProviderError>;
282​
283 readonly listChangeRequests: (
284 input: ProviderRepositoryRef & {
285 readonly state: PullRequestListState;
286 readonly involvement: PullRequestInvolvement;
287 readonly viewer: string;
288 readonly limit: number;
289 /**
290 * A host with no text filter of its own ignores it and answers with the page it
291 * would have answered with anyway — the caller narrows what it gets, so an
292 * unfiltered page is a wider answer rather than a wrong one.
293 */
294 readonly query?: string | undefined;
295 readonly cursor?: ProviderListCursor | undefined;
296 /**
297 * Further narrowings, which a host applies as far as it can and ignores the rest of —
298 * an unnarrowed page is a wider answer rather than a wrong one, and the caller narrows
299 * what it gets for the fields a row carries.
300 */
301 readonly filters?: PullRequestListFilters | undefined;
302 },
303 ) => Effect.Effect<ProviderChangeRequestPage, PullRequestProviderError>;
304​
305 /**
306 * `limit` is the whole slice rather than a size per repository, because that is the shape of
307 * the answer: the newest `limit` rows across every repository named, which is exactly the
308 * rows a page ordered by update shows. `cursor` is one boundary for all of them, so a caller
309 * with repositories standing at different boundaries asks in groups rather than in one call.
310 */
311 readonly listChangeRequestsAcross?: (input: {
312 readonly cwd: string;
313 readonly host: string;
314 readonly repositories: ReadonlyArray<string>;
315 readonly state: PullRequestListState;
316 readonly involvement: PullRequestInvolvement;
317 readonly viewer: string;
318 readonly limit: number;
319 readonly query?: string | undefined;
320 readonly cursor?: ProviderListCursor | undefined;
321 readonly filters?: PullRequestListFilters | undefined;
322 }) => Effect.Effect<ProviderBatchedChangeRequestPage, PullRequestProviderError>;
323​
324 readonly listChangeRequestStats?: (input: {
325 readonly cwd: string;
326 readonly host: string;
327 readonly changeRequests: ReadonlyArray<{
328 readonly repository: string;
329 readonly number: number;
330 }>;
331 }) => Effect.Effect<ReadonlyArray<ProviderChangeRequestStat>, PullRequestProviderError>;
332​
333 readonly getChangeRequestChecks?: (
334 input: ProviderRepositoryRef & { readonly number: number },
335 ) => Effect.Effect<PullRequestChecks, PullRequestProviderError>;
336​
337 readonly getChangeRequest: (
338 input: ProviderRepositoryRef & { readonly number: number },
339 ) => Effect.Effect<ProviderChangeRequestDetail, PullRequestProviderError>;
340​
341 readonly getChangeRequestPreview?: (
342 input: ProviderRepositoryRef & { readonly number: number },
343 ) => Effect.Effect<
344 Omit<PullRequestPreview, "projectId" | "repository">,
345 PullRequestProviderError
346 >;
347​
348 readonly getChangeRequestSummary?: (
349 input: ProviderRepositoryRef & { readonly number: number },
350 ) => Effect.Effect<ProviderChangeRequestSummary, PullRequestProviderError>;
351​
352 /**
353 * Null when the host gave no answer for it. Optional: a host without one has
354 * its watched change requests read in full on every pass.
355 */
356 readonly getChangeRequestWatchFingerprint?: (
357 input: ProviderRepositoryRef & { readonly number: number },
358 ) => Effect.Effect<ProviderChangeRequestWatchFingerprint | null, PullRequestProviderError>;
359​
360 readonly getChangeRequestStack?: (
361 input: ProviderRepositoryRef & { readonly includeDetails?: boolean; readonly number: number },
362 ) => Effect.Effect<ProviderChangeRequestStack | null, PullRequestProviderError>;
363​
364 readonly getChangeRequestActivity: (
365 input: ProviderRepositoryRef & { readonly number: number },
366 ) => Effect.Effect<ProviderChangeRequestActivity, PullRequestProviderError>;
367​
368 readonly getReviewThreadComments?: (
369 input: ProviderRepositoryRef & {
370 readonly number: number;
371 readonly threadId: string;
372 readonly cursor: string;
373 },
374 ) => Effect.Effect<PullRequestThreadCommentsResult, PullRequestProviderError>;
375​
376 /**
377 * Asked before anything is written, so a request that reached the server without going past
378 * the page is refused by what the host says rather than by what the client claimed — and
379 * asked freshly, because access granted or taken away since the page loaded is exactly the
380 * case this guards.
381 */
382 readonly getViewerPermissions: (
383 input: ProviderRepositoryRef & {
384 readonly number: number;
385 readonly includeUpdateBranch?: boolean;
386 },
387 ) => Effect.Effect<PullRequestViewerPermissions, PullRequestProviderError>;
388​
389 readonly getDiff: (
390 input: ProviderRepositoryRef & {
391 readonly number: number;
392 readonly cursor?: string | undefined;
393 readonly commit?: string | undefined;
394 },
395 ) => Effect.Effect<ProviderDiffSlice, PullRequestProviderError>;
396​
397 readonly getDiffFileContents?: (
398 input: ProviderRepositoryRef & {
399 readonly number: number;
400 readonly commit?: string | undefined;
401 readonly changeType: "change" | "rename-pure" | "rename-changed" | "new" | "deleted";
402 readonly oldPath: string;
403 readonly newPath: string;
404 },
405 ) => Effect.Effect<ProviderDiffFileContents, PullRequestProviderError>;
406​
407 readonly getFilesViewed?: (
408 input: ProviderRepositoryRef & { readonly number: number },
409 ) => Effect.Effect<ProviderFilesViewed, PullRequestProviderError>;
410​
411 /**
412 * A host with no bulk form is still owed one round trip for the batch rather than one per
413 * file, since the point of gathering presses is that the host is asked once.
414 */
415 readonly setFilesViewed?: (
416 input: ProviderRepositoryRef & {
417 readonly number: number;
418 readonly files: ReadonlyArray<{ readonly path: string; readonly viewed: boolean }>;
419 },
420 ) => Effect.Effect<void, PullRequestProviderError>;
421​
422 /**
423 * Required of a host whose `capabilities.viewedFiles` is `"environment"`, and unused by one
424 * that keeps the marks itself: the marks live here, but only the host can say whether what a
425 * reader cleared last week is still what is in front of them.
426 */
427 readonly getFileRevisions?: (
428 input: ProviderRepositoryRef & {
429 readonly number: number;
430 readonly paths: ReadonlyArray<string>;
431 },
432 ) => Effect.Effect<ProviderFileRevisions, PullRequestProviderError>;
433​
434 readonly runAction: (
435 input: ProviderRepositoryRef & {
436 readonly number: number;
437 readonly action: PullRequestAction;
438 readonly stackNumber?: number;
439 readonly expectedStackHeads?: ReadonlyArray<PullRequestStackHead>;
440 readonly removeAgentCreditsOnMerge?: boolean;
441 readonly mergeMethod?: PullRequestMergeMethod;
442 readonly updateMethod?: PullRequestUpdateMethod;
443 },
444 ) => Effect.Effect<void, PullRequestProviderError>;
445​
446 /**
447 * Only called when `capabilities.edit.changeRequest` is true, and never with both fields
448 * absent — the caller refuses that before it gets here, because a host asked to change
449 * nothing answers differently on each of them.
450 */
451 readonly updateChangeRequest?: (
452 input: ProviderRepositoryRef & {
453 readonly number: number;
454 readonly title?: string | undefined;
455 readonly body?: string | undefined;
456 },
457 ) => Effect.Effect<void, PullRequestProviderError>;
458​
459 readonly comment: (
460 input: ProviderRepositoryRef & { readonly number: number; readonly body: string },
461 ) => Effect.Effect<void, PullRequestProviderError>;
462​
463 /**
464 * Whether this remark is the reader's to rewrite is the host's own answer: no read here can
465 * settle it, since access can be taken away between the conversation being read and the
466 * rewrite being sent, and a host refuses a stranger's remark with a sentence saying so.
467 */
468 readonly updateComment?: (
469 input: ProviderRepositoryRef & {
470 readonly number: number;
471 readonly commentId: string;
472 readonly kind: "issue-comment" | "review-comment";
473 readonly body: string;
474 },
475 ) => Effect.Effect<void, PullRequestProviderError>;
476​
477 readonly submitReview: (
478 input: ProviderRepositoryRef & {
479 readonly number: number;
480 readonly verdict: PullRequestReviewVerdict;
481 readonly body: string;
482 readonly comments: ReadonlyArray<PullRequestReviewCommentDraft>;
483 },
484 ) => Effect.Effect<void, PullRequestProviderError>;
485​
486 /**
487 * The author is left out by each provider rather than by the caller, because only the
488 * provider knows how the host spells the same person in a candidate list and on a pull
489 * request.
490 */
491 readonly listReviewerCandidates: (
492 input: ProviderRepositoryRef & { readonly number: number },
493 ) => Effect.Effect<PullRequestReviewerCandidateList, PullRequestProviderError>;
494​
495 readonly setReviewerRequest: (
496 input: ProviderRepositoryRef & {
497 readonly number: number;
498 readonly reviewers: ReadonlyArray<{
499 readonly id: string;
500 readonly kind: PullRequestReviewerKind;
501 }>;
502 readonly requested: boolean;
503 },
504 ) => Effect.Effect<void, PullRequestProviderError>;
505​
506 readonly listLabelCandidates?: (
507 input: ProviderRepositoryRef & { readonly number: number },
508 ) => Effect.Effect<PullRequestLabelCandidateList, PullRequestProviderError>;
509​
510 readonly setLabels?: (
511 input: ProviderRepositoryRef & {
512 readonly number: number;
513 readonly labels: ReadonlyArray<string>;
514 readonly applied: boolean;
515 },
516 ) => Effect.Effect<void, PullRequestProviderError>;
517​
518 readonly replyToThread: (
519 input: ProviderRepositoryRef & {
520 readonly number: number;
521 readonly threadId: string;
522 readonly body: string;
523 },
524 ) => Effect.Effect<void, PullRequestProviderError>;
525​
526 /**
527 * `subjectId` is a remark's id as the conversation carried it; absent means the change
528 * request itself, whose reactions sit on its description.
529 */
530 readonly setReaction: (
531 input: ProviderRepositoryRef & {
532 readonly number: number;
533 readonly subjectId?: string | undefined;
534 readonly content: PullRequestReactionContent;
535 readonly reacted: boolean;
536 },
537 ) => Effect.Effect<void, PullRequestProviderError>;
538​
539 readonly setThreadResolution: (
540 input: ProviderRepositoryRef & {
541 readonly number: number;
542 readonly threadId: string;
543 readonly resolved: boolean;
544 },
545 ) => Effect.Effect<void, PullRequestProviderError>;