Open History to see the last commit
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 PullRequestComment,
13 PullRequestCommit,
14 PullRequestInvolvement,
15 PullRequestLabel,
16 PullRequestListFilters,
17 PullRequestListState,
18 PullRequestMergeCapabilities,
19 PullRequestMergeMethod,
20 PullRequestMergeability,
21 PullRequestOmittedFileStat,
22 PullRequestReaction,
23 PullRequestReactionContent,
24 PullRequestReviewCommentDraft,
25 PullRequestReviewDecision,
26 PullRequestReviewThread,
27 PullRequestThreadCommentsResult,
28 PullRequestReviewVerdict,
29 PullRequestReviewerCandidateList,
30 PullRequestReviewerKind,
31 PullRequestLabelCandidateList,
32 PullRequestState,
33 PullRequestUpdateMethod,
34 PullRequestViewerPermissions,
35 SourceControlProviderKind,
36} from "@t3tools/contracts";
37import { SourceControlProviderKind as SourceControlProviderKindSchema } from "@t3tools/contracts";
38
39/**
40 * The one failure shape every provider reports, so the service can decide what a failure means
41 * without knowing which CLI or API produced it.
42 *
43 * `reason` is the part the service acts on: a missing or unauthenticated tool disables the
44 * provider for the whole workspace, a rate limit pauses its host, and anything else is specific
45 * to the request.
46 */
47export class PullRequestProviderError extends Schema.TaggedError<PullRequestProviderError>()(
48 "PullRequestProviderError",
49 {
50 provider: SourceControlProviderKindSchema,
51 operation: Schema.String,
52 reason: Schema.Literals(["missing-tool", "unauthenticated", "rate-limited", "failed"]),
53 detail: Schema.String,
54 retryAt: Schema.optional(Schema.Number),
55 cause: Schema.optional(Schema.Defect()),
56 },
57) {
58 override get message(): string {
59 return `${this.provider} failed in ${this.operation}: ${this.detail}`;
60 }
61}
62
63export interface PullRequestProviderFailure {
64 readonly reason: PullRequestProviderError["reason"];
65 readonly retryAt?: number | undefined;
66}
67
68/** A change request as the provider sees it, before the service attaches project context. */
69export interface ProviderChangeRequest {
70 readonly stack?: PullRequestStackMembership;
71 readonly number: number;
72 readonly title: string;
73 readonly url: string;
74 readonly author: PullRequestActor | null;
75 readonly headBranch: string;
76 readonly headRepositoryNameWithOwner?: string | null;
77 readonly baseBranch: string;
78 readonly state: PullRequestState;
79 readonly isDraft: boolean;
80 readonly mergeability: PullRequestMergeability;
81 readonly additions: number;
82 readonly deletions: number;
83 readonly createdAt: string;
84 readonly closedAt?: string | null;
85 readonly mergedAt?: string | null;
86 readonly updatedAt: string;
87 /** Accounts with a review requested. Team-level requests are excluded by each provider. */
88 readonly reviewRequestLogins: ReadonlyArray<string>;
89 readonly labels: ReadonlyArray<PullRequestLabel>;
90 /** Absent from a host that does not summarise its reviews, which is every host but GitHub. */
91 readonly reviewDecision?: PullRequestReviewDecision | null | undefined;
92 /** Absent from a host that reports no check rollup on its listings. */
93 readonly checksState?: PullRequestChecksState | null | undefined;
94}
95
96/** The fields needed to keep a linked thread's pull request status live. */
97export interface ProviderChangeRequestSummary {
98 readonly number: number;
99 readonly title: string;
100 readonly url: string;
101 readonly headBranch: string;
102 readonly baseBranch: string;
103 readonly state: PullRequestState;
104 /** Present when the host says an open pull request is still a draft. */
105 readonly isDraft?: boolean;
106 readonly closedAt?: string | null;
107 readonly mergedAt?: string | null;
108 readonly updatedAt: string;
109 /** Overview fields, present where the host's single read returns them at no extra cost. */
110 readonly author?: PullRequestActor | null | undefined;
111 readonly additions?: number | undefined;
112 readonly deletions?: number | undefined;
113 readonly changedFiles?: number | undefined;
114 readonly reviewDecision?: PullRequestReviewDecision | null | undefined;
115 readonly checksState?: PullRequestChecksState | null | undefined;
116 readonly mergeability?: PullRequestMergeability | undefined;
117}
118
119/** One layer of a host-native stack, bottom to top order is the array's. */
120export interface ProviderChangeRequestStackLayer {
121 readonly title?: string;
122 readonly isDraft?: boolean;
123 readonly headSha?: string;
124 readonly number: number;
125 readonly headBranch: string;
126 readonly state: PullRequestState;
127}
128
129/**
130 * A host-native stack: an ordered set of change requests the host itself merges and retargets as
131 * a unit. Only GitHub offers one today; the neutral shape lets the sync reactor and the UI stay
132 * ignorant of which host said so.
133 */
134export interface ProviderChangeRequestStack {
135 readonly id: string;
136 readonly number: number;
137 readonly url: string;
138 readonly base: string;
139 readonly layers: ReadonlyArray<ProviderChangeRequestStackLayer>;
140}
141
142export interface ProviderChangeRequestPage {
143 readonly items: ReadonlyArray<ProviderChangeRequest>;
144 /** True when the host has more rows than the page size asked for. */
145 readonly truncated: boolean;
146 /**
147 * Optional count-based cursor advance. Most hosts advance by the rows delivered after local
148 * de-duplication; an offset-paged host may need to count malformed raw rows it consumed too.
149 */
150 readonly cursorAdvance?: number;
151 /**
152 * This page can be carried on from, so the service may hand the caller a cursor for it. False
153 * where the host answered in an order a cursor means nothing in, which leaves a larger `limit`
154 * as the only way to the rest — what every listing did before there were cursors.
155 */
156 readonly continues: boolean;
157}
158
159/**
160 * Where a repository's next slice starts, as the provider that has to ask for it needs it. Built
161 * by the service out of the slice it just handed over, so the boundary that decides whether a row
162 * arrives twice or not at all is decided in one place rather than in four.
163 */
164export interface ProviderListCursor {
165 /**
166 * The instant of the oldest row already handed over, checked against a timestamp's shape before
167 * it gets here because it goes into a host's own filter. Asked for inclusively: several rows
168 * share one instant often enough — a bot that touches eight change requests writes one timestamp
169 * on all eight — and asking for strictly older would lose whichever of them the slice ended
170 * before. The service drops the ones it has already sent.
171 */
172 readonly updatedBefore: string;
173 /**
174 * How many provider rows this repository has consumed so far, for a host that carries on by
175 * counting rather than by date. Usually this is the number handed over; malformed raw rows may
176 * count too when the provider reports a `cursorAdvance`.
177 */
178 readonly delivered: number;
179}
180
181/** One repository's row inside an answer that spans several of them. */
182export interface ProviderBatchedChangeRequest extends ProviderChangeRequest {
183 /** Provider-native identity, exactly as it was asked for, so the caller can file the row. */
184 readonly repository: string;
185}
186
187/**
188 * One slice of a host read across several repositories at once, newest update first across all
189 * of them. There is no per-repository page here because the host was asked one question: the
190 * caller splits the rows by `repository` and works out where each of them carries on from the
191 * oldest row in the slice, which every repository the slice covers is now read up to.
192 */
193export interface ProviderBatchedChangeRequestPage {
194 readonly items: ReadonlyArray<ProviderBatchedChangeRequest>;
195 /** True when the host has more rows than the slice asked for, for any of the repositories. */
196 readonly truncated: boolean;
197}
198
199/** The line counts for one change request, which a listing may leave for a second read. */
200export interface ProviderChangeRequestStat {
201 readonly repository: string;
202 readonly number: number;
203 readonly additions: number;
204 readonly deletions: number;
205}
206
207export interface ProviderChangeRequestDetail extends ProviderChangeRequest {
208 readonly body: string;
209 readonly changedFiles: number;
210 readonly mergedAt: string | null;
211 readonly closedAt: string | null;
212 readonly reviewers: ReadonlyArray<PullRequestActor>;
213 readonly checks: ReadonlyArray<PullRequestCheck>;
214 readonly mergeCapabilities: PullRequestMergeCapabilities;
215 readonly viewerPermissions: PullRequestViewerPermissions;
216 /** Absent from a host that cannot compare the branch with its base, which is most of them. */
217 readonly baseComparison?: PullRequestBaseComparison;
218 readonly behindBy?: number;
219 /** Absent from a host that does not report whether it is armed to merge this on its own. */
220 readonly autoMergeEnabled?: boolean;
221 /** The strategy stored with an armed auto-merge, where the host reports it. */
222 readonly autoMergeMethod?: PullRequestMergeMethod;
223 /** Workflow runs on this head commit that still need a maintainer's approval. */
224 readonly workflowApprovalsRequired?: number;
225}
226
227/** The conversation-shaped half of a detail, loaded after the core can already render. */
228export interface ProviderChangeRequestActivity {
229 /** An optional richer actor, e.g. after GitHub's GraphQL read supplies an avatar. */
230 readonly author?: PullRequestActor | null;
231 /** Optional because most hosts already report their reviewer list in the core detail. */
232 readonly reviewers?: ReadonlyArray<PullRequestActor>;
233 readonly comments: ReadonlyArray<PullRequestComment>;
234 /**
235 * The host's own count of the conversation, which a bounded read can fall short of. A host
236 * that reports no count of its own answers with what it handed over, which is the same number
237 * once the read went to the end.
238 */
239 readonly commentCount: number;
240 readonly commentsTruncated: boolean;
241 readonly reviewThreads: ReadonlyArray<PullRequestReviewThread>;
242 readonly commits: ReadonlyArray<PullRequestCommit>;
243 /** The change request's own reactions, from a host that has them. */
244 readonly reactions?: ReadonlyArray<PullRequestReaction>;
245}
246
247export interface ProviderDiffSlice {
248 readonly patch: string;
249 /** Something in this slice could not be shown, as opposed to there being more slices. */
250 readonly truncated: boolean;
251 readonly nextCursor: string | null;
252 /** The host's own counts for the files whose hunks it withheld from this slice. */
253 readonly omittedFileStats?: ReadonlyArray<PullRequestOmittedFileStat>;
254}
255
256export interface ProviderDiffFileContents {
257 readonly oldContents: string;
258 readonly newContents: string;
259}
260
261export interface ProviderRepositoryRef {
262 readonly cwd: string;
263 /** Provider-native repository identity, e.g. `owner/repo` or `group/subgroup/project`. */
264 readonly repository: string;
265 /**
266 * The host it lives on, which `repository` deliberately leaves out — the same `owner/repo`
267 * exists on github.com and on a GitHub Enterprise install, and only the caller knows which
268 * one a project's remote points at.
269 */
270 readonly host: string;
271}
272
273/**
274 * One host's change requests. Implementations own their own tool and JSON shapes and hand back
275 * the neutral types above; anything a host cannot do is declared in `capabilities` rather than
276 * failing at call time.
277 */
278export interface PullRequestProviderApi {
279 readonly withVerifiedCredential?: <A, E, R>(
280 input: { readonly cwd: string; readonly host: string },
281 use: (identity: {
282 readonly accountId: string;
283 readonly viewer: string;
284 readonly credentialFingerprint: string;
285 }) => Effect.Effect<A, E, R>,
286 ) => Effect.Effect<A, E | PullRequestProviderError, R>;
287 readonly getRoutingIdentity?: (input: {
288 readonly cwd: string;
289 readonly host: string;
290 }) => Effect.Effect<
291 { readonly accountId: string; readonly viewer: string },
292 PullRequestProviderError
293 >;
294 readonly kind: SourceControlProviderKind;
295 readonly capabilities: PullRequestCapabilities;
296
297 /** The signed-in account, which is what involvement filtering compares against. */
298 readonly getViewer: (input: {
299 readonly cwd: string;
300 readonly host?: string;
301 }) => Effect.Effect<string, PullRequestProviderError>;
302
303 readonly listChangeRequests: (
304 input: ProviderRepositoryRef & {
305 readonly state: PullRequestListState;
306 readonly involvement: PullRequestInvolvement;
307 readonly viewer: string;
308 readonly limit: number;
309 /**
310 * Free text to narrow the listing by, as the host understands it. A host with no text
311 * filter of its own ignores it and answers with the page it would have answered with
312 * anyway — the caller narrows what it gets, so an unfiltered page is a wider answer
313 * rather than a wrong one.
314 */
315 readonly query?: string | undefined;
316 /**
317 * Where to carry on from, rather than reading this repository from its newest row. Absent
318 * asks for the first slice, which is every listing that has not been continued.
319 */
320 readonly cursor?: ProviderListCursor | undefined;
321 /**
322 * Further narrowings, which a host applies as far as it can and ignores the rest of —
323 * an unnarrowed page is a wider answer rather than a wrong one, and the caller narrows
324 * what it gets for the fields a row carries.
325 */
326 readonly filters?: PullRequestListFilters | undefined;
327 },
328 ) => Effect.Effect<ProviderChangeRequestPage, PullRequestProviderError>;
329
330 /**
331 * The same listing for a whole host in one request, for a host that has a search across
332 * repositories. Optional: three of the four hosts here have no such API, and breaking the port
333 * for them to spare GitHub a fan-out would be paying for the fix with everyone else's clarity.
334 * The caller falls back to `listChangeRequests` per repository where this is absent, and where
335 * it fails.
336 *
337 * `limit` is the whole slice rather than a size per repository, because that is the shape of
338 * the answer: the newest `limit` rows across every repository named, which is exactly the rows
339 * a page ordered by update shows.
340 *
341 * `cursor` is one boundary for all of them, so a caller with repositories standing at different
342 * boundaries asks in groups rather than in one call.
343 */
344 readonly listChangeRequestsAcross?: (input: {
345 /** Any checkout on the host, which is what the tool is run in. */
346 readonly cwd: string;
347 readonly host: string;
348 readonly repositories: ReadonlyArray<string>;
349 readonly state: PullRequestListState;
350 readonly involvement: PullRequestInvolvement;
351 readonly viewer: string;
352 readonly limit: number;
353 readonly query?: string | undefined;
354 readonly cursor?: ProviderListCursor | undefined;
355 readonly filters?: PullRequestListFilters | undefined;
356 }) => Effect.Effect<ProviderBatchedChangeRequestPage, PullRequestProviderError>;
357
358 /**
359 * The line counts for rows a listing has already handed over. Only implemented by a provider
360 * whose listing leaves them out — for everyone else the numbers arrived with the row, and the
361 * caller has nothing to ask for.
362 */
363 readonly listChangeRequestStats?: (input: {
364 readonly cwd: string;
365 readonly host: string;
366 readonly changeRequests: ReadonlyArray<{
367 readonly repository: string;
368 readonly number: number;
369 }>;
370 }) => Effect.Effect<ReadonlyArray<ProviderChangeRequestStat>, PullRequestProviderError>;
371
372 readonly getChangeRequest: (
373 input: ProviderRepositoryRef & { readonly number: number },
374 ) => Effect.Effect<ProviderChangeRequestDetail, PullRequestProviderError>;
375
376 /**
377 * The cheap live fields used by linked threads. Optional because a provider without a narrow
378 * endpoint can fall back to its full detail read at the service boundary.
379 */
380 readonly getChangeRequestSummary?: (
381 input: ProviderRepositoryRef & { readonly number: number },
382 ) => Effect.Effect<ProviderChangeRequestSummary, PullRequestProviderError>;
383
384 /**
385 * The host-native stack a change request belongs to, or null when it is not stacked. Optional
386 * because most hosts have no such object; the service derives chains from base branches there.
387 */
388 readonly getChangeRequestStack?: (
389 input: ProviderRepositoryRef & { readonly includeDetails?: boolean; readonly number: number },
390 ) => Effect.Effect<ProviderChangeRequestStack | null, PullRequestProviderError>;
391
392 /** Comments, line threads, and commits, kept off the critical path for the core detail. */
393 readonly getChangeRequestActivity: (
394 input: ProviderRepositoryRef & { readonly number: number },
395 ) => Effect.Effect<ProviderChangeRequestActivity, PullRequestProviderError>;
396
397 /** One explicit page after a reader asks to continue an unfinished review thread. */
398 readonly getReviewThreadComments?: (
399 input: ProviderRepositoryRef & {
400 readonly number: number;
401 readonly threadId: string;
402 readonly cursor: string;
403 },
404 ) => Effect.Effect<PullRequestThreadCommentsResult, PullRequestProviderError>;
405
406 /**
407 * The same answer `getChangeRequest` carries, on its own. Asked before anything is written, so
408 * a request that reached the server without going past the page is refused by what the host
409 * says rather than by what the client claimed — and asked freshly, because access granted or
410 * taken away since the page loaded is exactly the case this guards.
411 *
412 * Implementations read the cheapest thing that answers it, which for a host with nothing to say
413 * is no request at all.
414 */
415 readonly getViewerPermissions: (
416 input: ProviderRepositoryRef & {
417 readonly number: number;
418 /** Skip branch comparison when checking permission for an unrelated operation. */
419 readonly includeUpdateBranch?: boolean;
420 },
421 ) => Effect.Effect<PullRequestViewerPermissions, PullRequestProviderError>;
422
423 /**
424 * One slice of the patch. Only called when `capabilities.diff` is true. A provider that can
425 * serve the whole diff at once answers with `nextCursor: null` and is done; one that pages
426 * hands back whatever it needs to find the next slice.
427 */
428 readonly getDiff: (
429 input: ProviderRepositoryRef & {
430 readonly number: number;
431 readonly cursor?: string | undefined;
432 /** One commit's own changes, rather than everything the change request carries. */
433 readonly commit?: string | undefined;
434 },
435 ) => Effect.Effect<ProviderDiffSlice, PullRequestProviderError>;
436
437 /**
438 * Full files at the exact revisions the host used for its patch. Optional where the provider
439 * exposes no diff at all; the service refuses expansion there just as it refuses the patch.
440 */
441 readonly getDiffFileContents?: (
442 input: ProviderRepositoryRef & {
443 readonly number: number;
444 readonly commit?: string | undefined;
445 readonly changeType: "change" | "rename-pure" | "rename-changed" | "new" | "deleted";
446 readonly oldPath: string;
447 readonly newPath: string;
448 },
449 ) => Effect.Effect<ProviderDiffFileContents, PullRequestProviderError>;
450
451 readonly runAction: (
452 input: ProviderRepositoryRef & {
453 readonly number: number;
454 readonly action: PullRequestAction;
455 readonly stackNumber?: number;
456 readonly expectedStackHeads?: ReadonlyArray<PullRequestStackHead>;
457 /** Meaningful for `merge` and `enable-auto-merge`; absent takes the host's own default. */
458 readonly mergeMethod?: PullRequestMergeMethod;
459 /** Only meaningful for `update-branch`; absent takes the host's own default. */
460 readonly updateMethod?: PullRequestUpdateMethod;
461 },
462 ) => Effect.Effect<void, PullRequestProviderError>;
463
464 /**
465 * Rewrites the change request's own words. Only called when `capabilities.edit.changeRequest`
466 * is true, and never with both fields absent — the caller refuses that before it gets here,
467 * because a host asked to change nothing answers differently on each of them.
468 */
469 readonly updateChangeRequest?: (
470 input: ProviderRepositoryRef & {
471 readonly number: number;
472 readonly title?: string | undefined;
473 readonly body?: string | undefined;
474 },
475 ) => Effect.Effect<void, PullRequestProviderError>;
476
477 readonly comment: (
478 input: ProviderRepositoryRef & { readonly number: number; readonly body: string },
479 ) => Effect.Effect<void, PullRequestProviderError>;
480
481 /**
482 * Rewrites a remark somebody already posted. Only called when `capabilities.edit.comment` is
483 * true, with an id exactly as the conversation carried it.
484 *
485 * Whether this remark is the reader's to rewrite is the host's own answer: no read here can
486 * settle it, since access can be taken away between the conversation being read and the
487 * rewrite being sent, and a host refuses a stranger's remark with a sentence saying so.
488 */
489 readonly updateComment?: (
490 input: ProviderRepositoryRef & {
491 readonly number: number;
492 readonly commentId: string;
493 readonly kind: "issue-comment" | "review-comment";
494 readonly body: string;
495 },
496 ) => Effect.Effect<void, PullRequestProviderError>;
497
498 /**
499 * Sends a whole review at once. Only called for a verdict the host declared in
500 * `capabilities.review.verdicts`, and with line comments only where it declared
501 * `inlineComment`.
502 */
503 readonly submitReview: (
504 input: ProviderRepositoryRef & {
505 readonly number: number;
506 readonly verdict: PullRequestReviewVerdict;
507 readonly body: string;
508 readonly comments: ReadonlyArray<PullRequestReviewCommentDraft>;
509 },
510 ) => Effect.Effect<void, PullRequestProviderError>;
511
512 /**
513 * The people this viewer may ask for a review, with whoever has already been asked marked as
514 * such. Only called when `capabilities.reviewers.listCandidates` is true.
515 *
516 * The author is left out by each provider rather than by the caller, because only the provider
517 * knows how the host spells the same person in a candidate list and on a pull request.
518 */
519 readonly listReviewerCandidates: (
520 input: ProviderRepositoryRef & { readonly number: number },
521 ) => Effect.Effect<PullRequestReviewerCandidateList, PullRequestProviderError>;
522
523 /**
524 * Asks for a review, or takes the request back. Only called when
525 * `capabilities.reviewers.request` is true.
526 *
527 * One call for both directions, because that is what every host does with them: GitHub posts and
528 * deletes the same collection, and GitLab and Bitbucket write the whole reviewer set either way.
529 * Asking again somebody who has already reviewed is a request like any other — which is how a
530 * re-request is made.
531 */
532 readonly setReviewerRequest: (
533 input: ProviderRepositoryRef & {
534 readonly number: number;
535 readonly reviewers: ReadonlyArray<{
536 readonly id: string;
537 readonly kind: PullRequestReviewerKind;
538 }>;
539 readonly requested: boolean;
540 },
541 ) => Effect.Effect<void, PullRequestProviderError>;
542
543 /**
544 * The repository's labels, with the ones already on the change request marked. Present with
545 * `setLabels` only where `capabilities.labels` is true; the service refuses both without it.
546 */
547 readonly listLabelCandidates?: (
548 input: ProviderRepositoryRef & { readonly number: number },
549 ) => Effect.Effect<PullRequestLabelCandidateList, PullRequestProviderError>;
550
551 /** Puts labels on the change request, or takes them off. One call for both directions. */
552 readonly setLabels?: (
553 input: ProviderRepositoryRef & {
554 readonly number: number;
555 readonly labels: ReadonlyArray<string>;
556 readonly applied: boolean;
557 },
558 ) => Effect.Effect<void, PullRequestProviderError>;
559
560 /** Only called when `capabilities.review.reply` is true. */
561 readonly replyToThread: (
562 input: ProviderRepositoryRef & {
563 readonly number: number;
564 readonly threadId: string;
565 readonly body: string;
566 },
567 ) => Effect.Effect<void, PullRequestProviderError>;
568
569 /**
570 * Adds a reaction, or takes it back. Only called when `capabilities.reactions` is true.
571 *
572 * `subjectId` is a remark's id as the conversation carried it; absent means the change request
573 * itself, whose reactions sit on its description. Whatever a host needs to address either of
574 * them is worked out here, because the id a conversation travels with is the one the reader has.
575 */
576 readonly setReaction: (
577 input: ProviderRepositoryRef & {
578 readonly number: number;
579 readonly subjectId?: string | undefined;
580 readonly content: PullRequestReactionContent;
581 readonly reacted: boolean;
582 },
583 ) => Effect.Effect<void, PullRequestProviderError>;
584
585 /** Only called when `capabilities.review.resolve` is true. */
586 readonly setThreadResolution: (
587 input: ProviderRepositoryRef & {
588 readonly number: number;
589 readonly threadId: string;
590 readonly resolved: boolean;
591 },
592 ) => Effect.Effect<void, PullRequestProviderError>;
593}