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

PullRequestProvider.ts

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