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

PullRequestProvider.ts

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