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

PullRequestProvider.ts

585 lines20.9 KB

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