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 }) => Effect.Effect<string, PullRequestProviderError>;
200
201 readonly listChangeRequests: (
202 input: ProviderRepositoryRef & {
203 readonly state: PullRequestListState;
204 readonly involvement: PullRequestInvolvement;
205 readonly viewer: string;
206 readonly limit: number;
207 /**
208 * Free text to narrow the listing by, as the host understands it. A host with no text
209 * filter of its own ignores it and answers with the page it would have answered with
210 * anyway — the caller narrows what it gets, so an unfiltered page is a wider answer
211 * rather than a wrong one.
212 */
213 readonly query?: string | undefined;
214 /**
215 * Where to carry on from, rather than reading this repository from its newest row. Absent
216 * asks for the first slice, which is every listing that has not been continued.
217 */
218 readonly cursor?: ProviderListCursor | undefined;
219 },
220 ) => Effect.Effect<ProviderChangeRequestPage, PullRequestProviderError>;
221
222 /**
223 * The same listing for a whole host in one request, for a host that has a search across
224 * repositories. Optional: three of the four hosts here have no such API, and breaking the port
225 * for them to spare GitHub a fan-out would be paying for the fix with everyone else's clarity.
226 * The caller falls back to `listChangeRequests` per repository where this is absent, and where
227 * it fails.
228 *
229 * `limit` is the whole slice rather than a size per repository, because that is the shape of
230 * the answer: the newest `limit` rows across every repository named, which is exactly the rows
231 * a page ordered by update shows.
232 *
233 * `cursor` is one boundary for all of them, so a caller with repositories standing at different
234 * boundaries asks in groups rather than in one call.
235 */
236 readonly listChangeRequestsAcross?: (input: {
237 /** Any checkout on the host, which is what the tool is run in. */
238 readonly cwd: string;
239 readonly host: string;
240 readonly repositories: ReadonlyArray<string>;
241 readonly state: PullRequestListState;
242 readonly involvement: PullRequestInvolvement;
243 readonly viewer: string;
244 readonly limit: number;
245 readonly query?: string | undefined;
246 readonly cursor?: ProviderListCursor | undefined;
247 }) => Effect.Effect<ProviderBatchedChangeRequestPage, PullRequestProviderError>;
248
249 /**
250 * The line counts for rows a listing has already handed over. Only implemented by a provider
251 * whose listing leaves them out — for everyone else the numbers arrived with the row, and the
252 * caller has nothing to ask for.
253 */
254 readonly listChangeRequestStats?: (input: {
255 readonly cwd: string;
256 readonly host: string;
257 readonly changeRequests: ReadonlyArray<{
258 readonly repository: string;
259 readonly number: number;
260 }>;
261 }) => Effect.Effect<ReadonlyArray<ProviderChangeRequestStat>, PullRequestProviderError>;
262
263 readonly getChangeRequest: (
264 input: ProviderRepositoryRef & { readonly number: number },
265 ) => Effect.Effect<ProviderChangeRequestDetail, PullRequestProviderError>;
266
267 /** Comments, line threads, and commits, kept off the critical path for the core detail. */
268 readonly getChangeRequestActivity: (
269 input: ProviderRepositoryRef & { readonly number: number },
270 ) => Effect.Effect<ProviderChangeRequestActivity, PullRequestProviderError>;
271
272 /**
273 * The same answer `getChangeRequest` carries, on its own. Asked before anything is written, so
274 * a request that reached the server without going past the page is refused by what the host
275 * says rather than by what the client claimed — and asked freshly, because access granted or
276 * taken away since the page loaded is exactly the case this guards.
277 *
278 * Implementations read the cheapest thing that answers it, which for a host with nothing to say
279 * is no request at all.
280 */
281 readonly getViewerPermissions: (
282 input: ProviderRepositoryRef & { readonly number: number },
283 ) => Effect.Effect<PullRequestViewerPermissions, PullRequestProviderError>;
284
285 /**
286 * One slice of the patch. Only called when `capabilities.diff` is true. A provider that can
287 * serve the whole diff at once answers with `nextCursor: null` and is done; one that pages
288 * hands back whatever it needs to find the next slice.
289 */
290 readonly getDiff: (
291 input: ProviderRepositoryRef & {
292 readonly number: number;
293 readonly cursor?: string | undefined;
294 /** One commit's own changes, rather than everything the change request carries. */
295 readonly commit?: string | undefined;
296 },
297 ) => Effect.Effect<ProviderDiffSlice, PullRequestProviderError>;
298
299 /**
300 * Full files at the exact revisions the host used for its patch. Optional where the provider
301 * exposes no diff at all; the service refuses expansion there just as it refuses the patch.
302 */
303 readonly getDiffFileContents?: (
304 input: ProviderRepositoryRef & {
305 readonly number: number;
306 readonly commit?: string | undefined;
307 readonly changeType: "change" | "rename-pure" | "rename-changed" | "new" | "deleted";
308 readonly oldPath: string;
309 readonly newPath: string;
310 },
311 ) => Effect.Effect<ProviderDiffFileContents, PullRequestProviderError>;
312
313 readonly runAction: (
314 input: ProviderRepositoryRef & {
315 readonly number: number;
316 readonly action: PullRequestAction;
317 readonly mergeMethod?: PullRequestMergeMethod;
318 },
319 ) => Effect.Effect<void, PullRequestProviderError>;
320
321 readonly comment: (
322 input: ProviderRepositoryRef & { readonly number: number; readonly body: string },
323 ) => Effect.Effect<void, PullRequestProviderError>;
324
325 /**
326 * Sends a whole review at once. Only called for a verdict the host declared in
327 * `capabilities.review.verdicts`, and with line comments only where it declared
328 * `inlineComment`.
329 */
330 readonly submitReview: (
331 input: ProviderRepositoryRef & {
332 readonly number: number;
333 readonly verdict: PullRequestReviewVerdict;
334 readonly body: string;
335 readonly comments: ReadonlyArray<PullRequestReviewCommentDraft>;
336 },
337 ) => Effect.Effect<void, PullRequestProviderError>;
338
339 /**
340 * The people this viewer may ask for a review, with whoever has already been asked marked as
341 * such. Only called when `capabilities.reviewers.listCandidates` is true.
342 *
343 * The author is left out by each provider rather than by the caller, because only the provider
344 * knows how the host spells the same person in a candidate list and on a pull request.
345 */
346 readonly listReviewerCandidates: (
347 input: ProviderRepositoryRef & { readonly number: number },
348 ) => Effect.Effect<PullRequestReviewerCandidateList, PullRequestProviderError>;
349
350 /**
351 * Asks for a review, or takes the request back. Only called when
352 * `capabilities.reviewers.request` is true.
353 *
354 * One call for both directions, because that is what every host does with them: GitHub posts and
355 * deletes the same collection, and GitLab and Bitbucket write the whole reviewer set either way.
356 * Asking again somebody who has already reviewed is a request like any other — which is how a
357 * re-request is made.
358 */
359 readonly setReviewerRequest: (
360 input: ProviderRepositoryRef & {
361 readonly number: number;
362 readonly reviewers: ReadonlyArray<{
363 readonly id: string;
364 readonly kind: PullRequestReviewerKind;
365 }>;
366 readonly requested: boolean;
367 },
368 ) => Effect.Effect<void, PullRequestProviderError>;
369
370 /** Only called when `capabilities.review.reply` is true. */
371 readonly replyToThread: (
372 input: ProviderRepositoryRef & {
373 readonly number: number;
374 readonly threadId: string;
375 readonly body: string;
376 },
377 ) => Effect.Effect<void, PullRequestProviderError>;
378
379 /** Only called when `capabilities.review.resolve` is true. */
380 readonly setThreadResolution: (
381 input: ProviderRepositoryRef & {
382 readonly number: number;
383 readonly threadId: string;
384 readonly resolved: boolean;
385 },
386 ) => Effect.Effect<void, PullRequestProviderError>;
387}