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 maintainerCanModify?: boolean;
186 readonly headSha?: string | null;
187 readonly body: string;
188 readonly changedFiles: number;
189 readonly mergedAt: string | null;
190 readonly closedAt: string | null;
191 readonly reviewers: ReadonlyArray<PullRequestActor>;
192 readonly checks: ReadonlyArray<PullRequestCheck>;
193 readonly mergeCapabilities: PullRequestMergeCapabilities;
194 readonly viewerPermissions: PullRequestViewerPermissions;
195 readonly baseComparison?: PullRequestBaseComparison;
196 readonly behindBy?: number;
197 readonly autoMergeEnabled?: boolean;
198 readonly autoMergeMethod?: PullRequestMergeMethod;
199 readonly workflowApprovalsRequired?: number;
200}
201
202export interface ProviderChangeRequestActivity {
203 readonly author?: PullRequestActor | null;
204 readonly reviewers?: ReadonlyArray<PullRequestActor>;
205 readonly comments: ReadonlyArray<PullRequestComment>;
206 readonly commentCount: number;
207 readonly commentsTruncated: boolean;
208 readonly reviewThreadsTruncated?: boolean;
209 readonly reviewThreads: ReadonlyArray<PullRequestReviewThread>;
210 readonly commits: ReadonlyArray<PullRequestCommit>;
211 readonly reactions?: ReadonlyArray<PullRequestReaction>;
212}
213
214export interface ProviderDiffSlice {
215 readonly patch: string;
216 /** Content was omitted within this slice. */
217 readonly truncated: boolean;
218 readonly nextCursor: string | null;
219 readonly omittedFileStats?: ReadonlyArray<PullRequestOmittedFileStat>;
220}
221
222export interface ProviderDiffFileContents {
223 readonly oldContents: string;
224 readonly newContents: string;
225}
226
227export interface ProviderDiffFilePaths {
228 readonly files: ReadonlyArray<{ readonly oldPath: string; readonly newPath: string }>;
229 readonly nextCursor: string | null;
230}
231
232export interface ProviderFilesViewed {
233 readonly files: ReadonlyArray<PullRequestFileViewed>;
234 /** More files exist; missing entries are unknown, not "unviewed". */
235 readonly truncated: boolean;
236}
237
238export interface ProviderFileStats {
239 readonly files: ReadonlyArray<{
240 readonly path: string;
241 readonly additions: number;
242 readonly deletions: number;
243 }>;
244 /** The host has more files than were read. */
245 readonly truncated: boolean;
246}
247
248/**
249 * Opaque strings, compared only against one another, where the empty string is the answer for a
250 * file the change request deletes rather than a gap. A path is absent only where the read could
251 * not say, so a provider converts its host's own absences on the way here.
252 */
253export interface ProviderFileRevisions {
254 readonly revisions: ReadonlyMap<string, string>;
255 /** All revisions were read and can be reused for other paths. */
256 readonly complete?: boolean;
257}
258
259export interface ProviderFilterOptionPage<A> {
260 readonly items: ReadonlyArray<A>;
261 readonly truncated: boolean;
262}
263
264export interface ProviderRepositoryRef {
265 readonly cwd: string;
266 readonly repository: string;
267 readonly host: string;
268}
269
270export interface PullRequestProviderApi {
271 readonly withVerifiedCredential?: <A, E, R>(
272 /** `repository` lets a host pick the credential that can reach it. */
273 input: { readonly cwd: string; readonly host: string; readonly repository?: string },
274 use: (identity: {
275 readonly accountId: string;
276 readonly viewer: string;
277 readonly credentialFingerprint: string;
278 }) => Effect.Effect<A, E, R>,
279 ) => Effect.Effect<A, E | PullRequestProviderError, R>;
280 readonly getRoutingIdentity?: (input: {
281 readonly cwd: string;
282 readonly host: string;
283 }) => Effect.Effect<
284 { readonly accountId: string; readonly viewer: string },
285 PullRequestProviderError
286 >;
287 readonly kind: SourceControlProviderKind;
288 readonly capabilities: PullRequestCapabilities;
289 /**
290 * What a reader is told when this host cannot be read on the server: `missing` for no tool or
291 * credential, `unauthenticated` for one the host refused. Absent means a generic sentence.
292 */
293 readonly unavailableRequirement?: {
294 readonly missing: string;
295 readonly unauthenticated: string;
296 };
297 readonly mergeMessageRewrite?: (message: string) => string;
298 /** Use matching checkout keys instead of `owner/name`. */
299 readonly repositoryKey?: (input: { readonly canonicalKey: string }) => string;
300 /**
301 * Names the repository in every request and authenticates by host, so a change request on the
302 * definition's `publicHost` can be read from a project with no checkout of its repository.
303 */
304 readonly readsPublicHostWithoutCheckout?: boolean;
305
306 readonly getViewer: (input: {
307 readonly cwd: string;
308 readonly host?: string;
309 }) => Effect.Effect<string, PullRequestProviderError>;
310
311 readonly listChangeRequests: (
312 input: ProviderRepositoryRef & {
313 readonly state: PullRequestListState;
314 readonly involvement: PullRequestInvolvement;
315 readonly viewer: string;
316 readonly limit: number;
317 /** Hosts may ignore unsupported search; the caller filters returned rows. */
318 readonly query?: string | undefined;
319 readonly cursor?: ProviderListCursor | undefined;
320 /** The caller applies any filters the host ignores. */
321 readonly filters?: PullRequestListFilters | undefined;
322 },
323 ) => Effect.Effect<ProviderChangeRequestPage, PullRequestProviderError>;
324
325 /** Newest `limit` rows total across repositories, under one cursor boundary. */
326 readonly listChangeRequestsAcross?: (input: {
327 readonly cwd: string;
328 readonly host: string;
329 readonly repositories: ReadonlyArray<string>;
330 readonly state: PullRequestListState;
331 readonly involvement: PullRequestInvolvement;
332 readonly viewer: string;
333 readonly limit: number;
334 readonly query?: string | undefined;
335 readonly cursor?: ProviderListCursor | undefined;
336 readonly filters?: PullRequestListFilters | undefined;
337 }) => Effect.Effect<ProviderBatchedChangeRequestPage, PullRequestProviderError>;
338
339 readonly listChangeRequestStats?: (input: {
340 readonly cwd: string;
341 readonly host: string;
342 readonly changeRequests: ReadonlyArray<{
343 readonly repository: string;
344 readonly number: number;
345 }>;
346 }) => Effect.Effect<ReadonlyArray<ProviderChangeRequestStat>, PullRequestProviderError>;
347
348 readonly getChangeRequestChecks?: (
349 input: ProviderRepositoryRef & { readonly number: number },
350 ) => Effect.Effect<Pick<PullRequestChecks, "state" | "checks">, PullRequestProviderError>;
351
352 readonly getChangeRequest: (
353 input: ProviderRepositoryRef & { readonly number: number },
354 ) => Effect.Effect<ProviderChangeRequestDetail, PullRequestProviderError>;
355
356 /**
357 * For a provider that holds change request reads of its own: called when the service drops its
358 * reads, so a refresh or a finished turn reaches the host. No scope forgets everything; a scope
359 * without a number forgets the repository's change requests.
360 */
361 readonly forgetChangeRequestReads?: (scope?: {
362 readonly repository: string;
363 readonly number?: number;
364 }) => Effect.Effect<void>;
365
366 readonly getChangeRequestPreview?: (
367 input: ProviderRepositoryRef & { readonly number: number },
368 ) => Effect.Effect<
369 Omit<PullRequestPreview, "projectId" | "repository">,
370 PullRequestProviderError
371 >;
372
373 readonly getChangeRequestSummary?: (
374 input: ProviderRepositoryRef & { readonly number: number },
375 ) => Effect.Effect<ProviderChangeRequestSummary, PullRequestProviderError>;
376
377 /** Null means unknown; an absent method requires full watch reads. */
378 readonly getChangeRequestWatchFingerprint?: (
379 input: ProviderRepositoryRef & { readonly number: number },
380 ) => Effect.Effect<ProviderChangeRequestWatchFingerprint | null, PullRequestProviderError>;
381
382 readonly getChangeRequestStack?: (
383 input: ProviderRepositoryRef & { readonly includeDetails?: boolean; readonly number: number },
384 ) => Effect.Effect<ProviderChangeRequestStack | null, PullRequestProviderError>;
385
386 readonly getChangeRequestActivity: (
387 input: ProviderRepositoryRef & { readonly number: number },
388 ) => Effect.Effect<ProviderChangeRequestActivity, PullRequestProviderError>;
389
390 readonly getReviewThreadComments?: (
391 input: ProviderRepositoryRef & {
392 readonly number: number;
393 readonly threadId: string;
394 readonly cursor: string;
395 },
396 ) => Effect.Effect<PullRequestThreadCommentsResult, PullRequestProviderError>;
397
398 /** Read fresh before writes; client permissions may be stale or bypassed. */
399 readonly getViewerPermissions: (
400 input: ProviderRepositoryRef & {
401 readonly number: number;
402 readonly includeUpdateBranch?: boolean;
403 },
404 ) => Effect.Effect<PullRequestViewerPermissions, PullRequestProviderError>;
405
406 readonly getDiff: (
407 input: ProviderRepositoryRef & {
408 readonly number: number;
409 readonly cursor?: string | undefined;
410 readonly commit?: string | undefined;
411 },
412 ) => Effect.Effect<ProviderDiffSlice, PullRequestProviderError>;
413
414 /** Exact changed path pairs, including renames, without fetching patch contents. */
415 readonly getDiffFilePaths?: (
416 input: ProviderRepositoryRef & {
417 readonly number: number;
418 readonly cursor?: string | undefined;
419 readonly commit?: string | undefined;
420 },
421 ) => Effect.Effect<ProviderDiffFilePaths, PullRequestProviderError>;
422
423 readonly getDiffFileContents?: (
424 input: ProviderRepositoryRef & {
425 readonly number: number;
426 readonly commit?: string | undefined;
427 readonly changeType: "change" | "rename-pure" | "rename-changed" | "new" | "deleted";
428 readonly oldPath: string;
429 readonly newPath: string;
430 },
431 ) => Effect.Effect<ProviderDiffFileContents, PullRequestProviderError>;
432
433 readonly getFilesViewed?: (
434 input: ProviderRepositoryRef & { readonly number: number },
435 ) => Effect.Effect<ProviderFilesViewed, PullRequestProviderError>;
436
437 /** Line counts for every changed file. Optional: a host without one shows no breakdown. */
438 readonly getFileStats?: (
439 input: ProviderRepositoryRef & { readonly number: number },
440 ) => Effect.Effect<ProviderFileStats, PullRequestProviderError>;
441
442 /**
443 * A host with no bulk form is still owed one round trip for the batch rather than one per
444 * file, since the point of gathering presses is that the host is asked once.
445 */
446 readonly setFilesViewed?: (
447 input: ProviderRepositoryRef & {
448 readonly number: number;
449 readonly files: ReadonlyArray<{ readonly path: string; readonly viewed: boolean }>;
450 },
451 ) => Effect.Effect<void, PullRequestProviderError>;
452
453 /** Required for `environment` viewed marks to invalidate them after changes. */
454 readonly getFileRevisions?: (
455 input: ProviderRepositoryRef & {
456 readonly number: number;
457 readonly paths: ReadonlyArray<string>;
458 },
459 ) => Effect.Effect<ProviderFileRevisions, PullRequestProviderError>;
460
461 readonly runAction: (
462 input: ProviderRepositoryRef & {
463 readonly number: number;
464 readonly action: PullRequestAction;
465 readonly stackNumber?: number;
466 readonly expectedStackHeads?: ReadonlyArray<PullRequestStackHead>;
467 readonly removeAgentCreditsOnMerge?: boolean;
468 readonly mergeMethod?: PullRequestMergeMethod;
469 readonly updateMethod?: PullRequestUpdateMethod;
470 },
471 ) => Effect.Effect<void, PullRequestProviderError>;
472
473 /** Requires edit capability and at least one field. */
474 readonly updateChangeRequest?: (
475 input: ProviderRepositoryRef & {
476 readonly maintainerCanModify?: boolean | undefined;
477 readonly number: number;
478 readonly title?: string | undefined;
479 readonly body?: string | undefined;
480 },
481 ) => Effect.Effect<void, PullRequestProviderError>;
482
483 readonly comment: (
484 input: ProviderRepositoryRef & { readonly number: number; readonly body: string },
485 ) => Effect.Effect<void, PullRequestProviderError>;
486
487 readonly updateComment?: (
488 input: ProviderRepositoryRef & {
489 readonly number: number;
490 readonly commentId: string;
491 readonly kind: "issue-comment" | "review-comment";
492 readonly body: string;
493 },
494 ) => Effect.Effect<void, PullRequestProviderError>;
495
496 readonly submitReview: (
497 input: ProviderRepositoryRef & {
498 readonly number: number;
499 readonly verdict: PullRequestReviewVerdict;
500 readonly body: string;
501 readonly comments: ReadonlyArray<PullRequestReviewCommentDraft>;
502 },
503 ) => Effect.Effect<void, PullRequestProviderError>;
504
505 /** Exclude the author using host-specific account identities. */
506 readonly listReviewerCandidates: (
507 input: ProviderRepositoryRef & { readonly number: number },
508 ) => Effect.Effect<PullRequestReviewerCandidateList, PullRequestProviderError>;
509
510 readonly setReviewerRequest: (
511 input: ProviderRepositoryRef & {
512 readonly number: number;
513 readonly reviewers: ReadonlyArray<{
514 readonly id: string;
515 readonly kind: PullRequestReviewerKind;
516 }>;
517 readonly requested: boolean;
518 },
519 ) => Effect.Effect<void, PullRequestProviderError>;
520
521 readonly listLabelCandidates?: (
522 input: ProviderRepositoryRef & { readonly number: number },
523 ) => Effect.Effect<PullRequestLabelCandidateList, PullRequestProviderError>;
524
525 /**
526 * The labels a repository offers, for narrowing a listing by. `query` narrows by name as far as
527 * the host can; the caller still matches the answer against it.
528 */
529 readonly listRepositoryLabels?: (
530 input: ProviderRepositoryRef & { readonly query?: string | undefined },
531 ) => Effect.Effect<ProviderFilterOptionPage<PullRequestLabel>, PullRequestProviderError>;
532
533 /**
534 * People who could have opened a change request in the repository, for narrowing a listing by
535 * author. A host that can only search its whole user base answers from that.
536 */
537 readonly searchRepositoryAuthors?: (
538 input: ProviderRepositoryRef & { readonly query?: string | undefined },
539 ) => Effect.Effect<ProviderFilterOptionPage<PullRequestActor>, PullRequestProviderError>;
540
541 readonly setLabels?: (
542 input: ProviderRepositoryRef & {
543 readonly number: number;
544 readonly labels: ReadonlyArray<string>;
545 readonly applied: boolean;
546 },
547 ) => Effect.Effect<void, PullRequestProviderError>;
548
549 readonly replyToThread: (
550 input: ProviderRepositoryRef & {
551 readonly number: number;
552 readonly threadId: string;
553 readonly body: string;
554 },
555 ) => Effect.Effect<void, PullRequestProviderError>;
556
557 /** `subjectId` is the conversation's remark ID; absent targets the change request description. */
558 readonly setReaction: (
559 input: ProviderRepositoryRef & {
560 readonly number: number;
561 readonly subjectId?: string | undefined;
562 readonly content: PullRequestReactionContent;
563 readonly reacted: boolean;
564 },
565 ) => Effect.Effect<void, PullRequestProviderError>;
566
567 readonly setThreadResolution: (
568 input: ProviderRepositoryRef & {
569 readonly number: number;
570 readonly threadId: string;
571 readonly resolved: boolean;
572 },
573 ) => Effect.Effect<void, PullRequestProviderError>;
574}