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

PullRequestProvider.ts

611 lines22.0 KB

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