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

PullRequestProvider.ts

523 lines21.7 KB

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