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

PullRequestProvider.ts

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