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

PullRequestProvider.ts

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