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