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

PullRequestProvider.ts

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