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

PullRequestProvider.ts

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