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