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

PullRequestProvider.ts

505 lines21.0 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 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 }
59​
60export interface PullRequestProviderFailure {
61 readonly reason: PullRequestProviderError["reason"];
62 readonly retryAt?: number | undefined;
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;
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;
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;
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;
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;
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;
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;
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 /** The strategy stored with an armed auto-merge, where the host reports it. */
181 readonly autoMergeMethod?: PullRequestMergeMethod;
182 /** Workflow runs on this head commit that still need a maintainer's approval. */
183 readonly workflowApprovalsRequired?: number;
185​
186/** The conversation-shaped half of a detail, loaded after the core can already render. */
187export interface ProviderChangeRequestActivity {
188 /** An optional richer actor, e.g. after GitHub's GraphQL read supplies an avatar. */
189 readonly author?: PullRequestActor | null;
190 /** Optional because most hosts already report their reviewer list in the core detail. */
191 readonly reviewers?: ReadonlyArray<PullRequestActor>;
192 readonly comments: ReadonlyArray<PullRequestComment>;
193 /**
194 * The host's own count of the conversation, which a bounded read can fall short of. A host
195 * that reports no count of its own answers with what it handed over, which is the same number
196 * once the read went to the end.
197 */
198 readonly commentCount: number;
199 readonly commentsTruncated: boolean;
200 readonly reviewThreads: ReadonlyArray<PullRequestReviewThread>;
201 readonly commits: ReadonlyArray<PullRequestCommit>;
202 /** The change request's own reactions, from a host that has them. */
203 readonly reactions?: ReadonlyArray<PullRequestReaction>;
205​
206export interface ProviderDiffSlice {
207 readonly patch: string;
208 /** Something in this slice could not be shown, as opposed to there being more slices. */
209 readonly truncated: boolean;
210 readonly nextCursor: string | null;
211 /** The host's own counts for the files whose hunks it withheld from this slice. */
212 readonly omittedFileStats?: ReadonlyArray<PullRequestOmittedFileStat>;
214​
215export interface ProviderDiffFileContents {
216 readonly oldContents: string;
217 readonly newContents: string;
219​
220export interface ProviderRepositoryRef {
221 readonly cwd: string;
222 /** Provider-native repository identity, e.g. `owner/repo` or `group/subgroup/project`. */
223 readonly repository: string;
224 /**
225 * The host it lives on, which `repository` deliberately leaves out — the same `owner/repo`
226 * exists on github.com and on a GitHub Enterprise install, and only the caller knows which
227 * one a project's remote points at.
228 */
229 readonly host: string;
231​
232/**
233 * One host's change requests. Implementations own their own tool and JSON shapes and hand back
234 * the neutral types above; anything a host cannot do is declared in `capabilities` rather than
235 * failing at call time.
236 */
237export interface PullRequestProviderApi {
238 readonly kind: SourceControlProviderKind;
239 readonly capabilities: PullRequestCapabilities;
240​
241 /** The signed-in account, which is what involvement filtering compares against. */
242 readonly getViewer: (input: {
243 readonly cwd: string;
244 }) => Effect.Effect<string, PullRequestProviderError>;
245​
246 readonly listChangeRequests: (
247 input: ProviderRepositoryRef & {
248 readonly state: PullRequestListState;
249 readonly involvement: PullRequestInvolvement;
250 readonly viewer: string;
251 readonly limit: number;
252 /**
253 * Free text to narrow the listing by, as the host understands it. A host with no text
254 * filter of its own ignores it and answers with the page it would have answered with
255 * anyway — the caller narrows what it gets, so an unfiltered page is a wider answer
256 * rather than a wrong one.
257 */
258 readonly query?: string | undefined;
259 /**
260 * Where to carry on from, rather than reading this repository from its newest row. Absent
261 * asks for the first slice, which is every listing that has not been continued.
262 */
263 readonly cursor?: ProviderListCursor | undefined;
264 /**
265 * Further narrowings, which a host applies as far as it can and ignores the rest of —
266 * an unnarrowed page is a wider answer rather than a wrong one, and the caller narrows
267 * what it gets for the fields a row carries.
268 */
269 readonly filters?: PullRequestListFilters | undefined;
270 },
271 ) => Effect.Effect<ProviderChangeRequestPage, PullRequestProviderError>;
272​
273 /**
274 * The same listing for a whole host in one request, for a host that has a search across
275 * repositories. Optional: three of the four hosts here have no such API, and breaking the port
276 * for them to spare GitHub a fan-out would be paying for the fix with everyone else's clarity.
277 * The caller falls back to `listChangeRequests` per repository where this is absent, and where
278 * it fails.
279 *
280 * `limit` is the whole slice rather than a size per repository, because that is the shape of
281 * the answer: the newest `limit` rows across every repository named, which is exactly the rows
282 * a page ordered by update shows.
283 *
284 * `cursor` is one boundary for all of them, so a caller with repositories standing at different
285 * boundaries asks in groups rather than in one call.
286 */
287 readonly listChangeRequestsAcross?: (input: {
288 /** Any checkout on the host, which is what the tool is run in. */
289 readonly cwd: string;
290 readonly host: string;
291 readonly repositories: ReadonlyArray<string>;
292 readonly state: PullRequestListState;
293 readonly involvement: PullRequestInvolvement;
294 readonly viewer: string;
295 readonly limit: number;
296 readonly query?: string | undefined;
297 readonly cursor?: ProviderListCursor | undefined;
298 readonly filters?: PullRequestListFilters | undefined;
299 }) => Effect.Effect<ProviderBatchedChangeRequestPage, PullRequestProviderError>;
300​
301 /**
302 * The line counts for rows a listing has already handed over. Only implemented by a provider
303 * whose listing leaves them out — for everyone else the numbers arrived with the row, and the
304 * caller has nothing to ask for.
305 */
306 readonly listChangeRequestStats?: (input: {
307 readonly cwd: string;
308 readonly host: string;
309 readonly changeRequests: ReadonlyArray<{
310 readonly repository: string;
311 readonly number: number;
312 }>;
313 }) => Effect.Effect<ReadonlyArray<ProviderChangeRequestStat>, PullRequestProviderError>;
314​
315 readonly getChangeRequest: (
316 input: ProviderRepositoryRef & { readonly number: number },
317 ) => Effect.Effect<ProviderChangeRequestDetail, PullRequestProviderError>;
318​
319 /**
320 * The cheap live fields used by linked threads. Optional because a provider without a narrow
321 * endpoint can fall back to its full detail read at the service boundary.
322 */
323 readonly getChangeRequestSummary?: (
324 input: ProviderRepositoryRef & { readonly number: number },
325 ) => Effect.Effect<ProviderChangeRequestSummary, PullRequestProviderError>;
326​
327 /** Comments, line threads, and commits, kept off the critical path for the core detail. */
328 readonly getChangeRequestActivity: (
329 input: ProviderRepositoryRef & { readonly number: number },
330 ) => Effect.Effect<ProviderChangeRequestActivity, PullRequestProviderError>;
331​
332 /** One explicit page after a reader asks to continue an unfinished review thread. */
333 readonly getReviewThreadComments?: (
334 input: ProviderRepositoryRef & {
335 readonly number: number;
336 readonly threadId: string;
337 readonly cursor: string;
338 },
339 ) => Effect.Effect<PullRequestThreadCommentsResult, PullRequestProviderError>;
340​
341 /**
342 * The same answer `getChangeRequest` carries, on its own. Asked before anything is written, so
343 * a request that reached the server without going past the page is refused by what the host
344 * says rather than by what the client claimed — and asked freshly, because access granted or
345 * taken away since the page loaded is exactly the case this guards.
346 *
347 * Implementations read the cheapest thing that answers it, which for a host with nothing to say
348 * is no request at all.
349 */
350 readonly getViewerPermissions: (
351 input: ProviderRepositoryRef & { readonly number: number },
352 ) => Effect.Effect<PullRequestViewerPermissions, PullRequestProviderError>;
353​
354 /**
355 * One slice of the patch. Only called when `capabilities.diff` is true. A provider that can
356 * serve the whole diff at once answers with `nextCursor: null` and is done; one that pages
357 * hands back whatever it needs to find the next slice.
358 */
359 readonly getDiff: (
360 input: ProviderRepositoryRef & {
361 readonly number: number;
362 readonly cursor?: string | undefined;
363 /** One commit's own changes, rather than everything the change request carries. */
364 readonly commit?: string | undefined;
365 },
366 ) => Effect.Effect<ProviderDiffSlice, PullRequestProviderError>;
367​
368 /**
369 * Full files at the exact revisions the host used for its patch. Optional where the provider
370 * exposes no diff at all; the service refuses expansion there just as it refuses the patch.
371 */
372 readonly getDiffFileContents?: (
373 input: ProviderRepositoryRef & {
374 readonly number: number;
375 readonly commit?: string | undefined;
376 readonly changeType: "change" | "rename-pure" | "rename-changed" | "new" | "deleted";
377 readonly oldPath: string;
378 readonly newPath: string;
379 },
380 ) => Effect.Effect<ProviderDiffFileContents, PullRequestProviderError>;
381​
382 readonly runAction: (
383 input: ProviderRepositoryRef & {
384 readonly number: number;
385 readonly action: PullRequestAction;
386 /** Meaningful for `merge` and `enable-auto-merge`; absent takes the host's own default. */
387 readonly mergeMethod?: PullRequestMergeMethod;
388 /** Only meaningful for `update-branch`; absent takes the host's own default. */
389 readonly updateMethod?: PullRequestUpdateMethod;
390 },
391 ) => Effect.Effect<void, PullRequestProviderError>;
392​
393 /**
394 * Rewrites the change request's own words. Only called when `capabilities.edit.changeRequest`
395 * is true, and never with both fields absent — the caller refuses that before it gets here,
396 * because a host asked to change nothing answers differently on each of them.
397 */
398 readonly updateChangeRequest?: (
399 input: ProviderRepositoryRef & {
400 readonly number: number;
401 readonly title?: string | undefined;
402 readonly body?: string | undefined;
403 },
404 ) => Effect.Effect<void, PullRequestProviderError>;
405​
406 readonly comment: (
407 input: ProviderRepositoryRef & { readonly number: number; readonly body: string },
408 ) => Effect.Effect<void, PullRequestProviderError>;
409​
410 /**
411 * Rewrites a remark somebody already posted. Only called when `capabilities.edit.comment` is
412 * true, with an id exactly as the conversation carried it.
413 *
414 * Whether this remark is the reader's to rewrite is the host's own answer: no read here can
415 * settle it, since access can be taken away between the conversation being read and the
416 * rewrite being sent, and a host refuses a stranger's remark with a sentence saying so.
417 */
418 readonly updateComment?: (
419 input: ProviderRepositoryRef & {
420 readonly number: number;
421 readonly commentId: string;
422 readonly kind: "issue-comment" | "review-comment";
423 readonly body: string;
424 },
425 ) => Effect.Effect<void, PullRequestProviderError>;
426​
427 /**
428 * Sends a whole review at once. Only called for a verdict the host declared in
429 * `capabilities.review.verdicts`, and with line comments only where it declared
430 * `inlineComment`.
431 */
432 readonly submitReview: (
433 input: ProviderRepositoryRef & {
434 readonly number: number;
435 readonly verdict: PullRequestReviewVerdict;
436 readonly body: string;
437 readonly comments: ReadonlyArray<PullRequestReviewCommentDraft>;
438 },
439 ) => Effect.Effect<void, PullRequestProviderError>;
440​
441 /**
442 * The people this viewer may ask for a review, with whoever has already been asked marked as
443 * such. Only called when `capabilities.reviewers.listCandidates` is true.
444 *
445 * The author is left out by each provider rather than by the caller, because only the provider
446 * knows how the host spells the same person in a candidate list and on a pull request.
447 */
448 readonly listReviewerCandidates: (
449 input: ProviderRepositoryRef & { readonly number: number },
450 ) => Effect.Effect<PullRequestReviewerCandidateList, PullRequestProviderError>;
451​
452 /**
453 * Asks for a review, or takes the request back. Only called when
454 * `capabilities.reviewers.request` is true.
455 *
456 * One call for both directions, because that is what every host does with them: GitHub posts and
457 * deletes the same collection, and GitLab and Bitbucket write the whole reviewer set either way.
458 * Asking again somebody who has already reviewed is a request like any other — which is how a
459 * re-request is made.
460 */
461 readonly setReviewerRequest: (
462 input: ProviderRepositoryRef & {
463 readonly number: number;
464 readonly reviewers: ReadonlyArray<{
465 readonly id: string;
466 readonly kind: PullRequestReviewerKind;
467 }>;
468 readonly requested: boolean;
469 },
470 ) => Effect.Effect<void, PullRequestProviderError>;
471​
472 /** Only called when `capabilities.review.reply` is true. */
473 readonly replyToThread: (
474 input: ProviderRepositoryRef & {
475 readonly number: number;
476 readonly threadId: string;
477 readonly body: string;
478 },
479 ) => Effect.Effect<void, PullRequestProviderError>;
480​
481 /**
482 * Adds a reaction, or takes it back. Only called when `capabilities.reactions` is true.
483 *
484 * `subjectId` is a remark's id as the conversation carried it; absent means the change request
485 * itself, whose reactions sit on its description. Whatever a host needs to address either of
486 * them is worked out here, because the id a conversation travels with is the one the reader has.
487 */
488 readonly setReaction: (
489 input: ProviderRepositoryRef & {
490 readonly number: number;
491 readonly subjectId?: string | undefined;
492 readonly content: PullRequestReactionContent;
493 readonly reacted: boolean;
494 },
495 ) => Effect.Effect<void, PullRequestProviderError>;
496​
497 /** Only called when `capabilities.review.resolve` is true. */
498 readonly setThreadResolution: (
499 input: ProviderRepositoryRef & {
500 readonly number: number;
501 readonly threadId: string;
502 readonly resolved: boolean;
503 },
504 ) => Effect.Effect<void, PullRequestProviderError>;