Open History to see the last commit
1import * as Effect from "effect/Effect";
2import * as Schema from "effect/Schema";
3import type {
4 PullRequestStackMembership,
5 PullRequestAction,
6 PullRequestStackHead,
7 PullRequestActor,
8 PullRequestBaseComparison,
9 PullRequestCapabilities,
10 PullRequestChecksState,
11 PullRequestCheck,
12 PullRequestChecks,
13 PullRequestComment,
14 PullRequestFileViewed,
15 PullRequestCommit,
16 PullRequestInvolvement,
17 PullRequestLabel,
18 PullRequestListFilters,
19 PullRequestListState,
20 PullRequestMergeCapabilities,
21 PullRequestMergeMethod,
22 PullRequestMergeability,
23 PullRequestOmittedFileStat,
24 PullRequestReaction,
25 PullRequestReactionContent,
26 PullRequestReviewCommentDraft,
27 PullRequestReviewDecision,
28 PullRequestReviewThread,
29 PullRequestThreadCommentsResult,
30 PullRequestReviewVerdict,
31 PullRequestReviewerCandidateList,
32 PullRequestReviewerKind,
33 PullRequestLabelCandidateList,
34 PullRequestState,
35 PullRequestPreview,
36 PullRequestUpdateMethod,
37 PullRequestViewerPermissions,
38 SourceControlProviderKind,
39} from "@t3tools/contracts";
40import { SourceControlProviderKind as SourceControlProviderKindSchema } from "@t3tools/contracts";
41
42export class PullRequestProviderError extends Schema.TaggedError<PullRequestProviderError>()(
43 "PullRequestProviderError",
44 {
45 provider: SourceControlProviderKindSchema,
46 operation: Schema.String,
47 reason: Schema.Literals([
48 "missing-tool",
49 "unauthenticated",
50 "rate-limited",
51 "not-found",
52 "failed",
53 ]),
54 detail: Schema.String,
55 retryAt: Schema.optional(Schema.Number),
56 cause: Schema.optional(Schema.Defect()),
57 },
58) {
59 override get message(): string {
60 return `${this.provider} failed in ${this.operation}: ${this.detail}`;
61 }
62}
63
64export interface ProviderChangeRequestWatchFingerprint {
65 readonly status: string;
66 readonly remarks: string;
67}
68
69export interface PullRequestProviderFailure {
70 readonly reason: PullRequestProviderError["reason"];
71 readonly retryAt?: number | undefined;
72}
73
74export interface ProviderChangeRequest {
75 readonly stack?: PullRequestStackMembership;
76 readonly number: number;
77 readonly title: string;
78 readonly url: string;
79 readonly author: PullRequestActor | null;
80 readonly headBranch: string;
81 readonly headRepositoryNameWithOwner?: string | null;
82 readonly baseBranch: string;
83 readonly state: PullRequestState;
84 readonly isDraft: boolean;
85 readonly mergeability: PullRequestMergeability;
86 readonly additions: number;
87 readonly deletions: number;
88 readonly createdAt: string;
89 readonly closedAt?: string | null;
90 readonly mergedAt?: string | null;
91 readonly updatedAt: string;
92 readonly reviewRequestLogins: ReadonlyArray<string>;
93 readonly labels: ReadonlyArray<PullRequestLabel>;
94 readonly reviewDecision?: PullRequestReviewDecision | null | undefined;
95 readonly checksState?: PullRequestChecksState | null | undefined;
96}
97
98export interface ProviderChangeRequestSummary {
99 readonly number: number;
100 readonly title: string;
101 readonly url: string;
102 readonly headBranch: string;
103 readonly baseBranch: string;
104 readonly state: PullRequestState;
105 readonly isDraft?: boolean;
106 readonly closedAt?: string | null;
107 readonly mergedAt?: string | null;
108 readonly updatedAt: string;
109 readonly author?: PullRequestActor | null | undefined;
110 readonly additions?: number | undefined;
111 readonly deletions?: number | undefined;
112 readonly changedFiles?: number | undefined;
113 readonly reviewDecision?: PullRequestReviewDecision | null | undefined;
114 readonly checksState?: PullRequestChecksState | null | undefined;
115 readonly mergeability?: PullRequestMergeability | undefined;
116 /** Null means no stack; absent means not requested. */
117 readonly stack?: PullRequestStackMembership | null | undefined;
118}
119
120export interface ProviderChangeRequestStackLayer {
121 readonly title?: string;
122 readonly isDraft?: boolean;
123 readonly headSha?: string;
124 readonly number: number;
125 readonly headBranch: string;
126 readonly state: PullRequestState;
127}
128
129export interface ProviderChangeRequestStack {
130 readonly id: string;
131 readonly number: number;
132 readonly url: string;
133 readonly base: string;
134 readonly layers: ReadonlyArray<ProviderChangeRequestStackLayer>;
135}
136
137export interface ProviderChangeRequestPage {
138 readonly items: ReadonlyArray<ProviderChangeRequest>;
139 readonly truncated: boolean;
140 /** Offset-paged hosts must include malformed raw rows in their advance count. */
141 readonly cursorAdvance?: number;
142 /**
143 * Where a host that can only page from the top carries on: an opaque position past the last
144 * row delivered, handed back as the cursor's `resumeAfter`. Unlike a count of rows it survives
145 * rows above it closing between reads.
146 */
147 readonly resumeAfter?: string;
148 readonly continues: boolean;
149}
150
151export interface ProviderListCursor {
152 /** Inclusive boundary: equal-time rows can span pages and must not be skipped. */
153 readonly updatedBefore: string;
154 readonly delivered: number;
155 /**
156 * Rows already handed over at `updatedBefore`. Timestamp-paged hosts can exclude these before
157 * applying their limit so a full page of tied updates does not repeat the same prefix.
158 */
159 readonly seenAt?: ReadonlyArray<number> | undefined;
160 readonly resumeAfter?: string | undefined;
161}
162
163export interface ProviderBatchedChangeRequest extends ProviderChangeRequest {
164 readonly repository: string;
165}
166
167export interface ProviderBatchedChangeRequestPage {
168 readonly items: ReadonlyArray<ProviderBatchedChangeRequest>;
169 readonly truncated: boolean;
170 /**
171 * Repositories asked about that the page cannot vouch for: its silence about them may be an
172 * omission rather than an empty answer, so the caller reads each on its own.
173 */
174 readonly unresolved?: ReadonlyArray<string>;
175}
176
177export interface ProviderChangeRequestStat {
178 readonly repository: string;
179 readonly number: number;
180 readonly additions: number;
181 readonly deletions: number;
182}
183
184export interface ProviderChangeRequestDetail extends ProviderChangeRequest {
185 readonly headSha?: string | null;
186 readonly body: string;
187 readonly changedFiles: number;
188 readonly mergedAt: string | null;
189 readonly closedAt: string | null;
190 readonly reviewers: ReadonlyArray<PullRequestActor>;
191 readonly checks: ReadonlyArray<PullRequestCheck>;
192 readonly mergeCapabilities: PullRequestMergeCapabilities;
193 readonly viewerPermissions: PullRequestViewerPermissions;
194 readonly baseComparison?: PullRequestBaseComparison;
195 readonly behindBy?: number;
196 readonly autoMergeEnabled?: boolean;
197 readonly autoMergeMethod?: PullRequestMergeMethod;
198 readonly workflowApprovalsRequired?: number;
199}
200
201export interface ProviderChangeRequestActivity {
202 readonly author?: PullRequestActor | null;
203 readonly reviewers?: ReadonlyArray<PullRequestActor>;
204 readonly comments: ReadonlyArray<PullRequestComment>;
205 readonly commentCount: number;
206 readonly commentsTruncated: boolean;
207 readonly reviewThreadsTruncated?: boolean;
208 readonly reviewThreads: ReadonlyArray<PullRequestReviewThread>;
209 readonly commits: ReadonlyArray<PullRequestCommit>;
210 readonly reactions?: ReadonlyArray<PullRequestReaction>;
211}
212
213export interface ProviderDiffSlice {
214 readonly patch: string;
215 /** Content was omitted within this slice. */
216 readonly truncated: boolean;
217 readonly nextCursor: string | null;
218 readonly omittedFileStats?: ReadonlyArray<PullRequestOmittedFileStat>;
219}
220
221export interface ProviderDiffFileContents {
222 readonly oldContents: string;
223 readonly newContents: string;
224}
225
226export interface ProviderFilesViewed {
227 readonly files: ReadonlyArray<PullRequestFileViewed>;
228 /** More files exist; missing entries are unknown, not "unviewed". */
229 readonly truncated: boolean;
230}
231
232export interface ProviderFileStats {
233 readonly files: ReadonlyArray<{
234 readonly path: string;
235 readonly additions: number;
236 readonly deletions: number;
237 }>;
238 /** The host has more files than were read. */
239 readonly truncated: boolean;
240}
241
242/**
243 * Opaque strings, compared only against one another, where the empty string is the answer for a
244 * file the change request deletes rather than a gap. A path is absent only where the read could
245 * not say, so a provider converts its host's own absences on the way here.
246 */
247export interface ProviderFileRevisions {
248 readonly revisions: ReadonlyMap<string, string>;
249 /** All revisions were read and can be reused for other paths. */
250 readonly complete?: boolean;
251}
252
253export interface ProviderFilterOptionPage<A> {
254 readonly items: ReadonlyArray<A>;
255 readonly truncated: boolean;
256}
257
258export interface ProviderRepositoryRef {
259 readonly cwd: string;
260 readonly repository: string;
261 readonly host: string;
262}
263
264export interface PullRequestProviderApi {
265 readonly withVerifiedCredential?: <A, E, R>(
266 input: { readonly cwd: string; readonly host: string },
267 use: (identity: {
268 readonly accountId: string;
269 readonly viewer: string;
270 readonly credentialFingerprint: string;
271 }) => Effect.Effect<A, E, R>,
272 ) => Effect.Effect<A, E | PullRequestProviderError, R>;
273 readonly getRoutingIdentity?: (input: {
274 readonly cwd: string;
275 readonly host: string;
276 }) => Effect.Effect<
277 { readonly accountId: string; readonly viewer: string },
278 PullRequestProviderError
279 >;
280 readonly kind: SourceControlProviderKind;
281 readonly capabilities: PullRequestCapabilities;
282 /**
283 * What a reader is told when this host cannot be read on the server: `missing` for no tool or
284 * credential, `unauthenticated` for one the host refused. Absent means a generic sentence.
285 */
286 readonly unavailableRequirement?: {
287 readonly missing: string;
288 readonly unauthenticated: string;
289 };
290 readonly mergeMessageRewrite?: (message: string) => string;
291 /** Use matching checkout keys instead of `owner/name`. */
292 readonly repositoryKey?: (input: { readonly canonicalKey: string }) => string;
293 /**
294 * Names the repository in every request and authenticates by host, so a change request on the
295 * definition's `publicHost` can be read from a project with no checkout of its repository.
296 */
297 readonly readsPublicHostWithoutCheckout?: boolean;
298
299 readonly getViewer: (input: {
300 readonly cwd: string;
301 readonly host?: string;
302 }) => Effect.Effect<string, PullRequestProviderError>;
303
304 readonly listChangeRequests: (
305 input: ProviderRepositoryRef & {
306 readonly state: PullRequestListState;
307 readonly involvement: PullRequestInvolvement;
308 readonly viewer: string;
309 readonly limit: number;
310 /** Hosts may ignore unsupported search; the caller filters returned rows. */
311 readonly query?: string | undefined;
312 readonly cursor?: ProviderListCursor | undefined;
313 /** The caller applies any filters the host ignores. */
314 readonly filters?: PullRequestListFilters | undefined;
315 },
316 ) => Effect.Effect<ProviderChangeRequestPage, PullRequestProviderError>;
317
318 /** Newest `limit` rows total across repositories, under one cursor boundary. */
319 readonly listChangeRequestsAcross?: (input: {
320 readonly cwd: string;
321 readonly host: string;
322 readonly repositories: ReadonlyArray<string>;
323 readonly state: PullRequestListState;
324 readonly involvement: PullRequestInvolvement;
325 readonly viewer: string;
326 readonly limit: number;
327 readonly query?: string | undefined;
328 readonly cursor?: ProviderListCursor | undefined;
329 readonly filters?: PullRequestListFilters | undefined;
330 }) => Effect.Effect<ProviderBatchedChangeRequestPage, PullRequestProviderError>;
331
332 readonly listChangeRequestStats?: (input: {
333 readonly cwd: string;
334 readonly host: string;
335 readonly changeRequests: ReadonlyArray<{
336 readonly repository: string;
337 readonly number: number;
338 }>;
339 }) => Effect.Effect<ReadonlyArray<ProviderChangeRequestStat>, PullRequestProviderError>;
340
341 readonly getChangeRequestChecks?: (
342 input: ProviderRepositoryRef & { readonly number: number },
343 ) => Effect.Effect<Pick<PullRequestChecks, "state" | "checks">, PullRequestProviderError>;
344
345 readonly getChangeRequest: (
346 input: ProviderRepositoryRef & { readonly number: number },
347 ) => Effect.Effect<ProviderChangeRequestDetail, PullRequestProviderError>;
348
349 readonly getChangeRequestPreview?: (
350 input: ProviderRepositoryRef & { readonly number: number },
351 ) => Effect.Effect<
352 Omit<PullRequestPreview, "projectId" | "repository">,
353 PullRequestProviderError
354 >;
355
356 readonly getChangeRequestSummary?: (
357 input: ProviderRepositoryRef & { readonly number: number },
358 ) => Effect.Effect<ProviderChangeRequestSummary, PullRequestProviderError>;
359
360 /** Null means unknown; an absent method requires full watch reads. */
361 readonly getChangeRequestWatchFingerprint?: (
362 input: ProviderRepositoryRef & { readonly number: number },
363 ) => Effect.Effect<ProviderChangeRequestWatchFingerprint | null, PullRequestProviderError>;
364
365 readonly getChangeRequestStack?: (
366 input: ProviderRepositoryRef & { readonly includeDetails?: boolean; readonly number: number },
367 ) => Effect.Effect<ProviderChangeRequestStack | null, PullRequestProviderError>;
368
369 readonly getChangeRequestActivity: (
370 input: ProviderRepositoryRef & { readonly number: number },
371 ) => Effect.Effect<ProviderChangeRequestActivity, PullRequestProviderError>;
372
373 readonly getReviewThreadComments?: (
374 input: ProviderRepositoryRef & {
375 readonly number: number;
376 readonly threadId: string;
377 readonly cursor: string;
378 },
379 ) => Effect.Effect<PullRequestThreadCommentsResult, PullRequestProviderError>;
380
381 /** Read fresh before writes; client permissions may be stale or bypassed. */
382 readonly getViewerPermissions: (
383 input: ProviderRepositoryRef & {
384 readonly number: number;
385 readonly includeUpdateBranch?: boolean;
386 },
387 ) => Effect.Effect<PullRequestViewerPermissions, PullRequestProviderError>;
388
389 readonly getDiff: (
390 input: ProviderRepositoryRef & {
391 readonly number: number;
392 readonly cursor?: string | undefined;
393 readonly commit?: string | undefined;
394 },
395 ) => Effect.Effect<ProviderDiffSlice, PullRequestProviderError>;
396
397 readonly getDiffFileContents?: (
398 input: ProviderRepositoryRef & {
399 readonly number: number;
400 readonly commit?: string | undefined;
401 readonly changeType: "change" | "rename-pure" | "rename-changed" | "new" | "deleted";
402 readonly oldPath: string;
403 readonly newPath: string;
404 },
405 ) => Effect.Effect<ProviderDiffFileContents, PullRequestProviderError>;
406
407 readonly getFilesViewed?: (
408 input: ProviderRepositoryRef & { readonly number: number },
409 ) => Effect.Effect<ProviderFilesViewed, PullRequestProviderError>;
410
411 /** Line counts for every changed file. Optional: a host without one shows no breakdown. */
412 readonly getFileStats?: (
413 input: ProviderRepositoryRef & { readonly number: number },
414 ) => Effect.Effect<ProviderFileStats, PullRequestProviderError>;
415
416 /**
417 * A host with no bulk form is still owed one round trip for the batch rather than one per
418 * file, since the point of gathering presses is that the host is asked once.
419 */
420 readonly setFilesViewed?: (
421 input: ProviderRepositoryRef & {
422 readonly number: number;
423 readonly files: ReadonlyArray<{ readonly path: string; readonly viewed: boolean }>;
424 },
425 ) => Effect.Effect<void, PullRequestProviderError>;
426
427 /** Required for `environment` viewed marks to invalidate them after changes. */
428 readonly getFileRevisions?: (
429 input: ProviderRepositoryRef & {
430 readonly number: number;
431 readonly paths: ReadonlyArray<string>;
432 },
433 ) => Effect.Effect<ProviderFileRevisions, PullRequestProviderError>;
434
435 readonly runAction: (
436 input: ProviderRepositoryRef & {
437 readonly number: number;
438 readonly action: PullRequestAction;
439 readonly stackNumber?: number;
440 readonly expectedStackHeads?: ReadonlyArray<PullRequestStackHead>;
441 readonly removeAgentCreditsOnMerge?: boolean;
442 readonly mergeMethod?: PullRequestMergeMethod;
443 readonly updateMethod?: PullRequestUpdateMethod;
444 },
445 ) => Effect.Effect<void, PullRequestProviderError>;
446
447 /** Requires edit capability and at least one field. */
448 readonly updateChangeRequest?: (
449 input: ProviderRepositoryRef & {
450 readonly number: number;
451 readonly title?: string | undefined;
452 readonly body?: string | undefined;
453 },
454 ) => Effect.Effect<void, PullRequestProviderError>;
455
456 readonly comment: (
457 input: ProviderRepositoryRef & { readonly number: number; readonly body: string },
458 ) => Effect.Effect<void, PullRequestProviderError>;
459
460 readonly updateComment?: (
461 input: ProviderRepositoryRef & {
462 readonly number: number;
463 readonly commentId: string;
464 readonly kind: "issue-comment" | "review-comment";
465 readonly body: string;
466 },
467 ) => Effect.Effect<void, PullRequestProviderError>;
468
469 readonly submitReview: (
470 input: ProviderRepositoryRef & {
471 readonly number: number;
472 readonly verdict: PullRequestReviewVerdict;
473 readonly body: string;
474 readonly comments: ReadonlyArray<PullRequestReviewCommentDraft>;
475 },
476 ) => Effect.Effect<void, PullRequestProviderError>;
477
478 /** Exclude the author using host-specific account identities. */
479 readonly listReviewerCandidates: (
480 input: ProviderRepositoryRef & { readonly number: number },
481 ) => Effect.Effect<PullRequestReviewerCandidateList, PullRequestProviderError>;
482
483 readonly setReviewerRequest: (
484 input: ProviderRepositoryRef & {
485 readonly number: number;
486 readonly reviewers: ReadonlyArray<{
487 readonly id: string;
488 readonly kind: PullRequestReviewerKind;
489 }>;
490 readonly requested: boolean;
491 },
492 ) => Effect.Effect<void, PullRequestProviderError>;
493
494 readonly listLabelCandidates?: (
495 input: ProviderRepositoryRef & { readonly number: number },
496 ) => Effect.Effect<PullRequestLabelCandidateList, PullRequestProviderError>;
497
498 /**
499 * The labels a repository offers, for narrowing a listing by. `query` narrows by name as far as
500 * the host can; the caller still matches the answer against it.
501 */
502 readonly listRepositoryLabels?: (
503 input: ProviderRepositoryRef & { readonly query?: string | undefined },
504 ) => Effect.Effect<ProviderFilterOptionPage<PullRequestLabel>, PullRequestProviderError>;
505
506 /**
507 * People who could have opened a change request in the repository, for narrowing a listing by
508 * author. A host that can only search its whole user base answers from that.
509 */
510 readonly searchRepositoryAuthors?: (
511 input: ProviderRepositoryRef & { readonly query?: string | undefined },
512 ) => Effect.Effect<ProviderFilterOptionPage<PullRequestActor>, PullRequestProviderError>;
513
514 readonly setLabels?: (
515 input: ProviderRepositoryRef & {
516 readonly number: number;
517 readonly labels: ReadonlyArray<string>;
518 readonly applied: boolean;
519 },
520 ) => Effect.Effect<void, PullRequestProviderError>;
521
522 readonly replyToThread: (
523 input: ProviderRepositoryRef & {
524 readonly number: number;
525 readonly threadId: string;
526 readonly body: string;
527 },
528 ) => Effect.Effect<void, PullRequestProviderError>;
529
530 /** `subjectId` is the conversation's remark ID; absent targets the change request description. */
531 readonly setReaction: (
532 input: ProviderRepositoryRef & {
533 readonly number: number;
534 readonly subjectId?: string | undefined;
535 readonly content: PullRequestReactionContent;
536 readonly reacted: boolean;
537 },
538 ) => Effect.Effect<void, PullRequestProviderError>;
539
540 readonly setThreadResolution: (
541 input: ProviderRepositoryRef & {
542 readonly number: number;
543 readonly threadId: string;
544 readonly resolved: boolean;
545 },
546 ) => Effect.Effect<void, PullRequestProviderError>;
547}