Open History to see the last commit
1import * as Schema from "effect/Schema";
2import * as Effect from "effect/Effect";
3import * as HttpServerRespondable from "effect/http/HttpServerRespondable";
4import * as HttpServerResponse from "effect/http/HttpServerResponse";
5
6import {
7 ForwardCompatibleArray,
8 IsoDateTime,
9 NonNegativeInt,
10 PositiveInt,
11 ProjectId,
12 ThreadId,
13 TrimmedNonEmptyString,
14} from "./baseSchemas.ts";
15import { SourceControlProviderKind } from "./sourceControl.ts";
16
17export const PullRequestInvolvement = Schema.Literals(["all", "reviewing", "authored"]);
18export type PullRequestInvolvement = typeof PullRequestInvolvement.Type;
19
20export const PullRequestState = Schema.Literals(["open", "closed", "merged"]);
21export type PullRequestState = typeof PullRequestState.Type;
22
23export const PullRequestListState = Schema.Literals(["all", "open", "closed", "merged"]);
24export type PullRequestListState = typeof PullRequestListState.Type;
25
26export const PullRequestReviewDecision = Schema.Literals([
27 "approved",
28 "changes-requested",
29 "review-required",
30]);
31export type PullRequestReviewDecision = typeof PullRequestReviewDecision.Type;
32
33const PullRequestQualifierValue = TrimmedNonEmptyString.check(Schema.isMaxLength(200));
34const PullRequestQualifierValues = Schema.Array(PullRequestQualifierValue).check(
35 Schema.isMaxLength(10),
36);
37
38export const PullRequestListFilters = Schema.Struct({
39 draft: Schema.optional(Schema.Literals(["only", "hide"])),
40 review: Schema.optional(
41 Schema.Literals(["approved", "changes-requested", "review-required", "none"]),
42 ),
43 checks: Schema.optional(Schema.Literals(["passing", "failing"])),
44 /** GitHub label qualifiers: every group must match; any name within a group suffices. */
45 labels: Schema.optional(Schema.Array(PullRequestQualifierValues).check(Schema.isMaxLength(10))),
46 excludedLabels: Schema.optional(PullRequestQualifierValues),
47 author: Schema.optional(PullRequestQualifierValue),
48});
49export type PullRequestListFilters = typeof PullRequestListFilters.Type;
50
51export const PullRequestChecksState = Schema.Literals(["passing", "failing", "pending"]);
52export type PullRequestChecksState = typeof PullRequestChecksState.Type;
53
54export const PullRequestMergeability = Schema.Literals(["mergeable", "conflicting", "unknown"]);
55export type PullRequestMergeability = typeof PullRequestMergeability.Type;
56
57/** `fast-forward` refuses unless the head contains the base, where `rebase` rewrites the branch onto it. */
58export const PullRequestMergeMethod = Schema.Literals([
59 "merge",
60 "squash",
61 "rebase",
62 "fast-forward",
63]);
64export type PullRequestMergeMethod = typeof PullRequestMergeMethod.Type;
65
66export const PullRequestAction = Schema.Literals([
67 "merge",
68 "ready",
69 "draft",
70 "close",
71 "reopen",
72 "delete-branch",
73 "update-branch",
74 "enable-auto-merge",
75 "disable-auto-merge",
76 "revert",
77 "approve-workflows",
78]);
79export type PullRequestAction = typeof PullRequestAction.Type;
80
81export const PullRequestUpdateMethod = Schema.Literals(["merge", "rebase"]);
82export type PullRequestUpdateMethod = typeof PullRequestUpdateMethod.Type;
83
84export const PullRequestBaseComparison = Schema.Literals(["up-to-date", "behind", "unknown"]);
85export type PullRequestBaseComparison = typeof PullRequestBaseComparison.Type;
86
87export const PullRequestActor = Schema.Struct({
88 isBot: Schema.optional(Schema.Boolean),
89 login: TrimmedNonEmptyString,
90 name: Schema.NullOr(Schema.String),
91 avatarUrl: Schema.NullOr(Schema.String),
92});
93export type PullRequestActor = typeof PullRequestActor.Type;
94
95export const PullRequestLabel = Schema.Struct({
96 name: TrimmedNonEmptyString,
97 color: Schema.NullOr(Schema.String),
98 description: Schema.optional(Schema.NullOr(Schema.String)),
99});
100export type PullRequestLabel = typeof PullRequestLabel.Type;
101
102export const PullRequestCheckStatus = Schema.Literals([
103 "pending",
104 "action-required",
105 "success",
106 "failure",
107 "skipped",
108 "neutral",
109 "cancelled",
110]);
111export type PullRequestCheckStatus = typeof PullRequestCheckStatus.Type;
112
113export const PullRequestCheck = Schema.Struct({
114 name: TrimmedNonEmptyString,
115 status: PullRequestCheckStatus,
116 description: Schema.NullOr(Schema.String),
117 url: Schema.NullOr(Schema.String),
118 required: Schema.optional(Schema.Boolean),
119 /**
120 * Not a check: it stands for ones the host could not list, such as workflow runs awaiting
121 * approval when that read fails, so nothing waits on it or counts it as passing.
122 */
123 placeholder: Schema.optional(Schema.Boolean),
124});
125export type PullRequestCheck = typeof PullRequestCheck.Type;
126
127export const PullRequestReactionContent = Schema.Literals([
128 "thumbs-up",
129 "thumbs-down",
130 "laugh",
131 "hooray",
132 "confused",
133 "heart",
134 "rocket",
135 "eyes",
136]);
137export type PullRequestReactionContent = typeof PullRequestReactionContent.Type;
138
139export const PullRequestReaction = Schema.Struct({
140 content: PullRequestReactionContent,
141 count: PositiveInt,
142 /** Excludes the viewer's login when viewerHasReacted; count still includes them. */
143 actors: Schema.Array(TrimmedNonEmptyString),
144 viewerHasReacted: Schema.Boolean,
145});
146export type PullRequestReaction = typeof PullRequestReaction.Type;
147
148export const PullRequestCommentKind = Schema.Literals([
149 "issue-comment",
150 "review-comment",
151 "review",
152]);
153export type PullRequestCommentKind = typeof PullRequestCommentKind.Type;
154
155export const PullRequestComment = Schema.Struct({
156 id: TrimmedNonEmptyString,
157 kind: PullRequestCommentKind,
158 author: Schema.NullOr(PullRequestActor),
159 body: Schema.String,
160 createdAt: IsoDateTime,
161 editedAt: Schema.optional(Schema.NullOr(IsoDateTime)),
162 url: Schema.NullOr(Schema.String),
163 path: Schema.NullOr(Schema.String),
164 reviewState: Schema.NullOr(Schema.String),
165 reactions: Schema.optional(Schema.Array(PullRequestReaction)),
166 canEdit: Schema.optional(Schema.Boolean),
167});
168export type PullRequestComment = typeof PullRequestComment.Type;
169
170export const PullRequestDiffSide = Schema.Literals(["left", "right"]);
171export type PullRequestDiffSide = typeof PullRequestDiffSide.Type;
172
173export const PullRequestReviewVerdict = Schema.Literals(["comment", "approve", "request-changes"]);
174export type PullRequestReviewVerdict = typeof PullRequestReviewVerdict.Type;
175
176export const PullRequestThreadComment = Schema.Struct({
177 id: TrimmedNonEmptyString,
178 author: Schema.NullOr(PullRequestActor),
179 body: Schema.String,
180 createdAt: IsoDateTime,
181 editedAt: Schema.optional(Schema.NullOr(IsoDateTime)),
182 url: Schema.NullOr(Schema.String),
183 /** The review that posted the comment, so a link to a review with no text finds its comments. */
184 reviewUrl: Schema.optional(Schema.NullOr(Schema.String)),
185 reactions: Schema.optional(Schema.Array(PullRequestReaction)),
186 canEdit: Schema.optional(Schema.Boolean),
187});
188export type PullRequestThreadComment = typeof PullRequestThreadComment.Type;
189
190export const PullRequestReviewThread = Schema.Struct({
191 id: TrimmedNonEmptyString,
192 path: TrimmedNonEmptyString,
193 line: Schema.NullOr(PositiveInt),
194 side: PullRequestDiffSide,
195 isResolved: Schema.Boolean,
196 isOutdated: Schema.Boolean,
197 comments: Schema.Array(PullRequestThreadComment),
198 commentCount: Schema.optional(NonNegativeInt),
199 nextCommentsCursor: Schema.optional(TrimmedNonEmptyString),
200 /** Can only narrow repository-wide resolve permission. Absent when unreported. */
201 canResolve: Schema.optional(Schema.Boolean),
202});
203export type PullRequestReviewThread = typeof PullRequestReviewThread.Type;
204
205/** A thread on the change request as a whole, such as a GitLab overview thread. */
206export const PullRequestOverviewThread = Schema.Struct({
207 ...PullRequestReviewThread.fields,
208 path: Schema.Null,
209 line: Schema.Null,
210});
211export type PullRequestOverviewThread = typeof PullRequestOverviewThread.Type;
212
213export type PullRequestConversationThread = PullRequestReviewThread | PullRequestOverviewThread;
214
215export const PullRequestReviewerKind = Schema.Literals(["user", "team"]);
216export type PullRequestReviewerKind = typeof PullRequestReviewerKind.Type;
217
218export const PullRequestReviewerCandidate = Schema.Struct({
219 ...PullRequestActor.fields,
220 id: TrimmedNonEmptyString,
221 kind: PullRequestReviewerKind,
222 isRequested: Schema.Boolean,
223});
224export type PullRequestReviewerCandidate = typeof PullRequestReviewerCandidate.Type;
225
226export const PullRequestReviewerCandidateList = Schema.Struct({
227 candidates: Schema.Array(PullRequestReviewerCandidate),
228 truncated: Schema.Boolean,
229});
230export type PullRequestReviewerCandidateList = typeof PullRequestReviewerCandidateList.Type;
231
232export const PullRequestLabelCandidate = Schema.Struct({
233 ...PullRequestLabel.fields,
234 description: Schema.NullOr(Schema.String),
235 isApplied: Schema.Boolean,
236});
237export type PullRequestLabelCandidate = typeof PullRequestLabelCandidate.Type;
238
239export const PullRequestLabelCandidateList = Schema.Struct({
240 candidates: Schema.Array(PullRequestLabelCandidate),
241 truncated: Schema.Boolean,
242});
243export type PullRequestLabelCandidateList = typeof PullRequestLabelCandidateList.Type;
244
245export const PullRequestCommit = Schema.Struct({
246 oid: TrimmedNonEmptyString,
247 messageHeadline: Schema.String,
248 committedDate: IsoDateTime,
249 additions: Schema.optional(NonNegativeInt),
250 deletions: Schema.optional(NonNegativeInt),
251 authors: Schema.optional(Schema.Array(PullRequestActor)),
252});
253export type PullRequestCommit = typeof PullRequestCommit.Type;
254
255export const PullRequestReviewCapabilities = Schema.Struct({
256 inlineComment: Schema.Boolean,
257 /**
258 * An inline comment can cover a range of lines rather than its last one. Absent, as from a
259 * server that predates it, means one line.
260 */
261 rangeComment: Schema.optional(Schema.Boolean),
262 reply: Schema.Boolean,
263 resolve: Schema.Boolean,
264 verdicts: ForwardCompatibleArray(PullRequestReviewVerdict),
265});
266export type PullRequestReviewCapabilities = typeof PullRequestReviewCapabilities.Type;
267
268export const PullRequestEditCapabilities = Schema.Struct({
269 changeRequest: Schema.Boolean,
270 comment: Schema.Boolean,
271 maintainerEdits: Schema.optional(Schema.Boolean),
272});
273export type PullRequestEditCapabilities = typeof PullRequestEditCapabilities.Type;
274
275export const PullRequestReviewerCapabilities = Schema.Struct({
276 request: Schema.Boolean,
277 listCandidates: Schema.Boolean,
278 /**
279 * Reviewer candidates take a `query` the host matches past the first page. Absent, as from a
280 * server that predates it, means the client narrows the loaded page itself.
281 */
282 searchCandidates: Schema.optional(Schema.Boolean),
283});
284export type PullRequestReviewerCapabilities = typeof PullRequestReviewerCapabilities.Type;
285
286export const PullRequestViewedFilesStore = Schema.Literals(["host", "environment"]);
287export type PullRequestViewedFilesStore = typeof PullRequestViewedFilesStore.Type;
288
289/** `#` references issues (and GitHub pull requests); `!` references GitLab merge requests. */
290export const PullRequestReferenceSigil = Schema.Literals(["#", "!"]);
291export type PullRequestReferenceSigil = typeof PullRequestReferenceSigil.Type;
292
293export const PullRequestCapabilities = Schema.Struct({
294 diff: Schema.Boolean,
295 comment: Schema.Boolean,
296 actions: ForwardCompatibleArray(PullRequestAction),
297 mergeMethods: ForwardCompatibleArray(PullRequestMergeMethod),
298 deleteBranch: Schema.optional(Schema.Boolean),
299 /**
300 * Absent where it cannot at all, which is every host that has not said otherwise — so a
301 * provider that says nothing offers nothing.
302 */
303 updateMethods: Schema.optional(ForwardCompatibleArray(PullRequestUpdateMethod)),
304 search: Schema.Boolean,
305 reactions: Schema.optional(Schema.Boolean),
306 viewedFiles: Schema.optional(PullRequestViewedFilesStore),
307 review: PullRequestReviewCapabilities,
308 reviewers: PullRequestReviewerCapabilities,
309 edit: Schema.optional(PullRequestEditCapabilities),
310 stacks: Schema.optional(Schema.Boolean),
311 stackActions: Schema.optional(Schema.Boolean),
312 labels: Schema.optional(Schema.Boolean),
313 /** The host searches the repository's users, so composers can complete `@login`. */
314 mentions: Schema.optional(Schema.Boolean),
315 /** The sigils whose issue and change request references composers can complete. */
316 referenceSigils: Schema.optional(ForwardCompatibleArray(PullRequestReferenceSigil)),
317 /**
318 * Every head gets at least one check, so a head that has gone a while without any means none
319 * reported rather than that the repository runs none. Absent means an empty list says nothing.
320 */
321 checksAlwaysReport: Schema.optional(Schema.Boolean),
322 /** The diff can read one file the paged diff withheld as too large (`PullRequestDiffInput.path`). */
323 withheldFileDiff: Schema.optional(Schema.Boolean),
324});
325export type PullRequestCapabilities = typeof PullRequestCapabilities.Type;
326
327/** Unreported host permissions are granted. */
328export const PullRequestViewerPermissions = Schema.Struct({
329 stackRebase: Schema.optional(Schema.Boolean),
330 actions: ForwardCompatibleArray(PullRequestAction),
331 comment: Schema.Boolean,
332 resolve: Schema.Boolean,
333 verdicts: ForwardCompatibleArray(PullRequestReviewVerdict),
334 requestReviewers: Schema.Boolean,
335 updateMethods: Schema.optional(ForwardCompatibleArray(PullRequestUpdateMethod)),
336 labels: Schema.optional(Schema.Boolean),
337 editChangeRequest: Schema.optional(Schema.Boolean),
338 maintainerEdits: Schema.optional(Schema.Boolean),
339});
340export type PullRequestViewerPermissions = typeof PullRequestViewerPermissions.Type;
341
342export const PullRequestMergeCapabilities = Schema.Struct({
343 merge: Schema.Boolean,
344 squash: Schema.Boolean,
345 rebase: Schema.Boolean,
346 /** Optional so servers that predate it still decode; absent means not allowed. */
347 "fast-forward": Schema.optional(Schema.Boolean),
348});
349export type PullRequestMergeCapabilities = typeof PullRequestMergeCapabilities.Type;
350
351export const PullRequestStackMembership = Schema.Struct({
352 number: PositiveInt,
353 position: PositiveInt,
354 size: PositiveInt,
355 base: TrimmedNonEmptyString,
356});
357export type PullRequestStackMembership = typeof PullRequestStackMembership.Type;
358
359export const PullRequestListEntry = Schema.Struct({
360 stack: Schema.optional(PullRequestStackMembership),
361 provider: SourceControlProviderKind,
362 host: TrimmedNonEmptyString,
363 projectId: ProjectId,
364 projectTitle: TrimmedNonEmptyString,
365 repository: TrimmedNonEmptyString,
366 number: PositiveInt,
367 title: TrimmedNonEmptyString,
368 url: TrimmedNonEmptyString,
369 author: Schema.NullOr(PullRequestActor),
370 headBranch: TrimmedNonEmptyString,
371 baseBranch: TrimmedNonEmptyString,
372 state: PullRequestState,
373 isDraft: Schema.Boolean,
374 mergeability: PullRequestMergeability,
375 rebaseRequired: Schema.optional(Schema.Boolean),
376 /** Zero until fetched through `PullRequestListStatsInput`; GitHub listings omit counts. */
377 additions: NonNegativeInt,
378 deletions: NonNegativeInt,
379 createdAt: IsoDateTime,
380 updatedAt: IsoDateTime,
381 observedAt: Schema.optional(Schema.Finite),
382 viewerReviewRequested: Schema.Boolean,
383 labels: Schema.Array(PullRequestLabel),
384 reviewDecision: Schema.optional(PullRequestReviewDecision),
385 checksState: Schema.optional(PullRequestChecksState),
386});
387export type PullRequestListEntry = typeof PullRequestListEntry.Type;
388
389export const PullRequestListCursors = Schema.Record(
390 TrimmedNonEmptyString,
391 TrimmedNonEmptyString.check(Schema.isMaxLength(4096)),
392);
393export type PullRequestListCursors = typeof PullRequestListCursors.Type;
394
395export const PULL_REQUEST_LIST_PROJECT_LIMIT = 100;
396
397export const PullRequestListInput = Schema.Struct({
398 state: PullRequestListState,
399 involvement: Schema.optional(PullRequestInvolvement),
400 filters: Schema.optional(PullRequestListFilters),
401 projectId: Schema.optional(ProjectId),
402 projectIds: Schema.optional(
403 Schema.Array(ProjectId).check(Schema.isMaxLength(PULL_REQUEST_LIST_PROJECT_LIMIT)),
404 ),
405 host: Schema.optional(TrimmedNonEmptyString),
406 limit: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 500 }))),
407 cursors: Schema.optional(PullRequestListCursors),
408 query: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(200))),
409});
410export type PullRequestListInput = typeof PullRequestListInput.Type;
411
412export const PullRequestProviderSummary = Schema.Struct({
413 host: TrimmedNonEmptyString,
414 kind: SourceControlProviderKind,
415 searchesOnHost: Schema.Boolean,
416 actions: Schema.optional(ForwardCompatibleArray(PullRequestAction)),
417 projectCount: PositiveInt,
418 /** SSH checkouts can resolve to an HTTP authority different from their stored identity. */
419 projectIds: Schema.optional(Schema.Array(ProjectId)),
420 configured: Schema.Boolean,
421 detail: Schema.NullOr(TrimmedNonEmptyString),
422});
423export type PullRequestProviderSummary = typeof PullRequestProviderSummary.Type;
424
425export const PullRequestListProjectError = Schema.Struct({
426 projectId: ProjectId,
427 projectTitle: TrimmedNonEmptyString,
428 message: TrimmedNonEmptyString,
429});
430export type PullRequestListProjectError = typeof PullRequestListProjectError.Type;
431
432export const PullRequestListResult = Schema.Struct({
433 /** Unreadable hosts are absent; JSON codecs reject optional record values. */
434 viewers: Schema.Record(TrimmedNonEmptyString, TrimmedNonEmptyString),
435 providers: Schema.Array(PullRequestProviderSummary),
436 /** Re-sort after appending: one repository's older slice may outrank another's existing rows. */
437 entries: Schema.Array(PullRequestListEntry),
438 errors: Schema.Array(PullRequestListProjectError),
439 truncated: Schema.Boolean,
440 nextCursors: PullRequestListCursors,
441});
442export type PullRequestListResult = typeof PullRequestListResult.Type;
443
444export const PullRequestRef = Schema.Struct({
445 projectId: ProjectId,
446 host: Schema.optional(TrimmedNonEmptyString),
447 expectedAccountId: Schema.optional(TrimmedNonEmptyString),
448 allowStale: Schema.optional(Schema.Boolean),
449 repository: TrimmedNonEmptyString,
450 number: PositiveInt,
451});
452export type PullRequestRef = typeof PullRequestRef.Type;
453
454/** `query` asks the host for matches past the first page of candidates. */
455export const PullRequestReviewerCandidatesInput = Schema.Struct({
456 ...PullRequestRef.fields,
457 query: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(100))),
458});
459export type PullRequestReviewerCandidatesInput = typeof PullRequestReviewerCandidatesInput.Type;
460
461export const PullRequestRoutingIdentityInput = Schema.Struct({
462 host: TrimmedNonEmptyString,
463});
464export type PullRequestRoutingIdentityInput = typeof PullRequestRoutingIdentityInput.Type;
465
466export const PullRequestRoutingIdentityResult = Schema.Struct({
467 accountId: TrimmedNonEmptyString,
468 host: TrimmedNonEmptyString,
469 provider: SourceControlProviderKind,
470 viewer: TrimmedNonEmptyString,
471});
472export type PullRequestRoutingIdentityResult = typeof PullRequestRoutingIdentityResult.Type;
473
474export const PullRequestRoutingResult = Schema.Struct({
475 accountId: TrimmedNonEmptyString,
476 host: TrimmedNonEmptyString,
477 provider: SourceControlProviderKind,
478 viewer: TrimmedNonEmptyString,
479 projectTitle: TrimmedNonEmptyString,
480 workspaceRoot: TrimmedNonEmptyString,
481});
482export type PullRequestRoutingResult = typeof PullRequestRoutingResult.Type;
483
484export const PullRequestLinkedThreadsResult = Schema.Struct({
485 threads: Schema.Array(
486 Schema.Struct({
487 id: ThreadId,
488 projectId: ProjectId,
489 title: Schema.String,
490 archivedAt: Schema.NullOr(IsoDateTime),
491 settled: Schema.optionalKey(Schema.Boolean),
492 }),
493 ),
494});
495export type PullRequestLinkedThreadsResult = typeof PullRequestLinkedThreadsResult.Type;
496
497export const PullRequestPreview = Schema.Struct({
498 projectId: ProjectId,
499 repository: TrimmedNonEmptyString,
500 number: PositiveInt,
501 title: TrimmedNonEmptyString,
502 url: TrimmedNonEmptyString,
503 author: Schema.NullOr(PullRequestActor),
504 state: PullRequestState,
505 isDraft: Schema.Boolean,
506 createdAt: IsoDateTime,
507 updatedAt: Schema.optional(IsoDateTime),
508 observedAt: Schema.optional(Schema.Finite),
509});
510export type PullRequestPreview = typeof PullRequestPreview.Type;
511
512export const PullRequestSummary = Schema.Struct({
513 provider: SourceControlProviderKind,
514 projectId: ProjectId,
515 repository: TrimmedNonEmptyString,
516 number: PositiveInt,
517 title: TrimmedNonEmptyString,
518 url: TrimmedNonEmptyString,
519 state: PullRequestState,
520 isDraft: Schema.optional(Schema.Boolean),
521 headBranch: TrimmedNonEmptyString,
522 baseBranch: TrimmedNonEmptyString,
523 closedAt: Schema.optional(Schema.NullOr(Schema.String)),
524 mergedAt: Schema.optional(Schema.NullOr(Schema.String)),
525 updatedAt: IsoDateTime,
526 observedAt: Schema.optional(Schema.Finite),
527 author: Schema.optional(Schema.NullOr(PullRequestActor)),
528 additions: Schema.optional(NonNegativeInt),
529 deletions: Schema.optional(NonNegativeInt),
530 changedFiles: Schema.optional(NonNegativeInt),
531 reviewDecision: Schema.optional(Schema.NullOr(PullRequestReviewDecision)),
532 checksState: Schema.optional(Schema.NullOr(PullRequestChecksState)),
533 mergeability: Schema.optional(PullRequestMergeability),
534 rebaseRequired: Schema.optional(Schema.Boolean),
535 /** Null when the host says the pull request is in no stack; absent when the read did not ask. */
536 stack: Schema.optional(Schema.NullOr(PullRequestStackMembership)),
537 headSha: Schema.optional(TrimmedNonEmptyString),
538 /** Absent means the read did not report auto-merge state, not that it is off. */
539 autoMergeEnabled: Schema.optional(Schema.Boolean),
540 autoMergeMethod: Schema.optional(PullRequestMergeMethod),
541});
542export type PullRequestSummary = typeof PullRequestSummary.Type;
543
544export const PullRequestStack = Schema.Struct({
545 id: TrimmedNonEmptyString,
546 number: PositiveInt,
547 url: TrimmedNonEmptyString,
548 base: TrimmedNonEmptyString,
549 layers: Schema.Array(
550 Schema.Struct({
551 number: PositiveInt,
552 title: Schema.optional(Schema.String),
553 isDraft: Schema.optional(Schema.Boolean),
554 headSha: Schema.optional(TrimmedNonEmptyString),
555 headBranch: TrimmedNonEmptyString,
556 state: PullRequestState,
557 }),
558 ),
559});
560export type PullRequestStack = typeof PullRequestStack.Type;
561
562export const PullRequestDiffStat = Schema.Struct({
563 projectId: ProjectId,
564 repository: TrimmedNonEmptyString,
565 number: PositiveInt,
566 additions: NonNegativeInt,
567 deletions: NonNegativeInt,
568});
569export type PullRequestDiffStat = typeof PullRequestDiffStat.Type;
570
571export const PullRequestListStatsInput = Schema.Struct({
572 refs: Schema.Array(PullRequestRef).check(Schema.isMaxLength(500)),
573});
574export type PullRequestListStatsInput = typeof PullRequestListStatsInput.Type;
575
576export const PullRequestListStatsResult = Schema.Struct({
577 stats: Schema.Array(PullRequestDiffStat),
578});
579export type PullRequestListStatsResult = typeof PullRequestListStatsResult.Type;
580
581export const PullRequestFilterOptionKind = Schema.Literals(["labels", "authors", "references"]);
582export type PullRequestFilterOptionKind = typeof PullRequestFilterOptionKind.Type;
583
584/** Scoped like `PullRequestListInput`, so the options come from the repositories it lists. */
585export const PullRequestFilterOptionsInput = Schema.Struct({
586 kind: PullRequestFilterOptionKind,
587 projectId: Schema.optional(ProjectId),
588 projectIds: Schema.optional(Schema.Array(ProjectId).check(Schema.isMaxLength(100))),
589 host: Schema.optional(TrimmedNonEmptyString),
590 query: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(100))),
591 /** For `references`, the sigil typed, so a host can search only what it links. */
592 sigil: Schema.optional(PullRequestReferenceSigil),
593});
594export type PullRequestFilterOptionsInput = typeof PullRequestFilterOptionsInput.Type;
595
596/** An issue or change request a composer can reference, with the text the host links. */
597export const PullRequestReferenceCandidate = Schema.Struct({
598 reference: TrimmedNonEmptyString,
599 kind: Schema.Literals(["issue", "change-request"]),
600 number: PositiveInt,
601 title: Schema.String,
602 state: Schema.Literals(["open", "closed", "merged"]),
603});
604export type PullRequestReferenceCandidate = typeof PullRequestReferenceCandidate.Type;
605
606export const PullRequestFilterOptions = Schema.Struct({
607 labels: Schema.Array(PullRequestLabel),
608 authors: Schema.Array(PullRequestActor),
609 // Servers from before reference completion send no key.
610 references: Schema.Array(PullRequestReferenceCandidate).pipe(
611 Schema.withDecodingDefault(Effect.succeed([])),
612 ),
613 truncated: Schema.Boolean,
614});
615export type PullRequestFilterOptions = typeof PullRequestFilterOptions.Type;
616
617/**
618 * A separate request rather than a flag on the reads, so an explicit "refresh" one person
619 * presses is the only thing that spends host requests — every ordinary read shares.
620 */
621export const PullRequestInvalidateInput = Schema.Struct({
622 reference: Schema.optional(PullRequestRef),
623 filesViewedOnly: Schema.optional(Schema.Boolean),
624});
625export type PullRequestInvalidateInput = typeof PullRequestInvalidateInput.Type;
626
627export const PullRequestReportStateInput = Schema.Struct({
628 reference: PullRequestRef,
629 state: PullRequestState,
630 isDraft: Schema.optional(Schema.Boolean),
631});
632export type PullRequestReportStateInput = typeof PullRequestReportStateInput.Type;
633
634/** The host's merge queue letting go of a pull request without merging it, newest first. */
635export const PullRequestMergeQueueRemoval = Schema.Struct({
636 id: TrimmedNonEmptyString,
637 removedAt: IsoDateTime,
638 reason: Schema.NullOr(Schema.String),
639});
640export type PullRequestMergeQueueRemoval = typeof PullRequestMergeQueueRemoval.Type;
641
642export const PullRequestDetail = Schema.Struct({
643 provider: SourceControlProviderKind,
644 capabilities: PullRequestCapabilities,
645 viewerPermissions: PullRequestViewerPermissions,
646 projectId: ProjectId,
647 projectTitle: TrimmedNonEmptyString,
648 workspaceRoot: TrimmedNonEmptyString,
649 repository: TrimmedNonEmptyString,
650 number: PositiveInt,
651 title: TrimmedNonEmptyString,
652 body: Schema.String,
653 url: TrimmedNonEmptyString,
654 author: Schema.NullOr(PullRequestActor),
655 state: PullRequestState,
656 isDraft: Schema.Boolean,
657 mergeability: PullRequestMergeability,
658 rebaseRequired: Schema.optional(Schema.Boolean),
659 additions: NonNegativeInt,
660 deletions: NonNegativeInt,
661 changedFiles: NonNegativeInt,
662 headBranch: TrimmedNonEmptyString,
663 headRepositoryNameWithOwner: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
664 headSha: Schema.optional(TrimmedNonEmptyString),
665 baseBranch: TrimmedNonEmptyString,
666 createdAt: IsoDateTime,
667 updatedAt: IsoDateTime,
668 observedAt: Schema.optional(Schema.Finite),
669 mergedAt: Schema.NullOr(IsoDateTime),
670 closedAt: Schema.NullOr(IsoDateTime),
671 reviewers: Schema.Array(PullRequestActor),
672 labels: Schema.Array(PullRequestLabel),
673 checks: Schema.Array(PullRequestCheck),
674 mergeCapabilities: PullRequestMergeCapabilities,
675 viewer: Schema.optional(TrimmedNonEmptyString),
676 baseComparison: Schema.optional(PullRequestBaseComparison),
677 behindBy: Schema.optional(NonNegativeInt),
678 /** Absent means the host does not report auto-merge state, not that it is off. */
679 autoMergeEnabled: Schema.optional(Schema.Boolean),
680 autoMergeMethod: Schema.optional(PullRequestMergeMethod),
681 workflowApprovalsRequired: Schema.optional(NonNegativeInt),
682 /** Absent where the host has no merge queue or does not report one. */
683 mergeQueueRemoval: Schema.optional(PullRequestMergeQueueRemoval),
684 maintainerCanModify: Schema.optional(Schema.Boolean),
685});
686export type PullRequestDetail = typeof PullRequestDetail.Type;
687
688export const PullRequestChecks = Schema.Struct({
689 observedAt: Schema.optional(Schema.Finite),
690 state: PullRequestState,
691 checks: Schema.Array(PullRequestCheck),
692});
693export type PullRequestChecks = typeof PullRequestChecks.Type;
694
695export const PullRequestActivity = Schema.Struct({
696 author: Schema.optional(Schema.NullOr(PullRequestActor)),
697 reviewers: Schema.optional(Schema.Array(PullRequestActor)),
698 comments: Schema.Array(PullRequestComment),
699 commentCount: NonNegativeInt,
700 commentsTruncated: Schema.Boolean,
701 reviewThreadsTruncated: Schema.optional(Schema.Boolean),
702 reviewThreads: Schema.Array(PullRequestReviewThread),
703 // Apart from reviewThreads so clients that require a thread path still decode the activity.
704 overviewThreads: Schema.optional(Schema.Array(PullRequestOverviewThread)),
705 commits: Schema.Array(PullRequestCommit),
706 reactions: Schema.optional(Schema.Array(PullRequestReaction)),
707});
708export type PullRequestActivity = typeof PullRequestActivity.Type;
709
710export const pullRequestConversationThreads = (
711 activity: Pick<PullRequestActivity, "reviewThreads" | "overviewThreads">,
712): ReadonlyArray<PullRequestConversationThread> =>
713 activity.overviewThreads === undefined || activity.overviewThreads.length === 0
714 ? activity.reviewThreads
715 : [...activity.reviewThreads, ...activity.overviewThreads];
716
717export const PullRequestDetailView = Schema.Struct({
718 ...PullRequestDetail.fields,
719 ...PullRequestActivity.fields,
720 author: Schema.NullOr(PullRequestActor),
721 reviewers: Schema.Array(PullRequestActor),
722});
723export type PullRequestDetailView = typeof PullRequestDetailView.Type;
724
725/** GitHub refuses diffs past 300 files, so callers must page large changes. */
726export const PullRequestDiffInput = Schema.Struct({
727 ...PullRequestRef.fields,
728 cursor: Schema.optional(TrimmedNonEmptyString),
729 commit: Schema.optional(TrimmedNonEmptyString),
730 /** Reads one file the paged diff withheld as too large; ignores `cursor`. */
731 path: Schema.optional(TrimmedNonEmptyString),
732});
733export type PullRequestDiffInput = typeof PullRequestDiffInput.Type;
734
735export const PullRequestOmittedFileStat = Schema.Struct({
736 path: TrimmedNonEmptyString,
737 additions: Schema.Number,
738 deletions: Schema.Number,
739});
740export type PullRequestOmittedFileStat = typeof PullRequestOmittedFileStat.Type;
741
742export const PullRequestDiffResult = Schema.Struct({
743 patch: Schema.String,
744 /** Content omitted within this slice; nextCursor separately indicates more slices. */
745 truncated: Schema.Boolean,
746 nextCursor: Schema.NullOr(TrimmedNonEmptyString),
747 omittedFileStats: Schema.optional(Schema.Array(PullRequestOmittedFileStat)),
748 /** On the first page: files this diff withholds can be read one at a time with `path`. */
749 withheldFilesReadable: Schema.optional(Schema.Boolean),
750});
751export type PullRequestDiffResult = typeof PullRequestDiffResult.Type;
752
753export const PullRequestDiffFileContentsInput = Schema.Struct({
754 ...PullRequestRef.fields,
755 commit: Schema.optional(TrimmedNonEmptyString),
756 changeType: Schema.Literals(["change", "rename-pure", "rename-changed", "new", "deleted"]),
757 oldPath: Schema.NonEmptyString,
758 newPath: Schema.NonEmptyString,
759});
760export type PullRequestDiffFileContentsInput = typeof PullRequestDiffFileContentsInput.Type;
761
762export const PullRequestDiffFileContentsResult = Schema.Struct({
763 oldContents: Schema.String,
764 newContents: Schema.String,
765});
766export type PullRequestDiffFileContentsResult = typeof PullRequestDiffFileContentsResult.Type;
767
768const MAX_FILE_PATH_LENGTH = 4096;
769// Preserve filename whitespace so viewed marks match host paths.
770const FilePath = Schema.String.check(Schema.isNonEmpty(), Schema.isMaxLength(MAX_FILE_PATH_LENGTH));
771
772export const PullRequestFileViewedState = Schema.Literals(["unviewed", "viewed", "dismissed"]);
773export type PullRequestFileViewedState = typeof PullRequestFileViewedState.Type;
774
775export const PullRequestFileViewed = Schema.Struct({
776 path: FilePath,
777 state: PullRequestFileViewedState,
778});
779export type PullRequestFileViewed = typeof PullRequestFileViewed.Type;
780
781export const PullRequestFilesViewedResult = Schema.Struct({
782 /** Only the files the host reported a state for. A file missing from this list is unviewed. */
783 files: Schema.Array(PullRequestFileViewed),
784 truncated: Schema.Boolean,
785});
786export type PullRequestFilesViewedResult = typeof PullRequestFilesViewedResult.Type;
787
788const PullRequestLineCount = Schema.Struct({
789 additions: NonNegativeInt,
790 deletions: NonNegativeInt,
791});
792
793/**
794 * A change's line counts split by what the changed files are, judged from their paths. Null when
795 * the host gives no per-file counts, or has more files than one read covers: a split of part of
796 * the change would not add up to the totals shown beside it.
797 */
798export const PullRequestChangeBreakdown = Schema.NullOr(
799 Schema.Struct({
800 code: PullRequestLineCount,
801 tests: PullRequestLineCount,
802 mocks: PullRequestLineCount,
803 generated: PullRequestLineCount,
804 // Servers from before the docs split send no key; their docs lines sit in `code`.
805 docs: PullRequestLineCount.pipe(
806 Schema.withDecodingDefault(Effect.succeed({ additions: 0, deletions: 0 })),
807 ),
808 }),
809);
810export type PullRequestChangeBreakdown = typeof PullRequestChangeBreakdown.Type;
811
812const MAX_FILES_VIEWED_PRESSES = 500;
813
814export const PullRequestSetFilesViewedInput = Schema.Struct({
815 ...PullRequestRef.fields,
816 files: Schema.Array(
817 Schema.Struct({
818 path: FilePath,
819 viewed: Schema.Boolean,
820 /** The file's version in the diff the reader was shown; without it the head's is recorded. */
821 revision: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(256))),
822 }),
823 ).check(Schema.isMaxLength(MAX_FILES_VIEWED_PRESSES)),
824});
825export type PullRequestSetFilesViewedInput = typeof PullRequestSetFilesViewedInput.Type;
826
827export const PullRequestStackHead = Schema.Struct({
828 number: PositiveInt,
829 headSha: TrimmedNonEmptyString,
830});
831export type PullRequestStackHead = typeof PullRequestStackHead.Type;
832
833export const PullRequestActionInput = Schema.Struct({
834 /** Native stack scope; only send to environments advertising pullRequestStackActions. */
835 stackNumber: Schema.optional(PositiveInt),
836 expectedStackHeads: Schema.optional(Schema.Array(PullRequestStackHead)),
837 ...PullRequestRef.fields,
838 action: PullRequestAction,
839 mergeMethod: Schema.optional(PullRequestMergeMethod),
840 updateMethod: Schema.optional(PullRequestUpdateMethod),
841 /** Opt-in, because older clients decode the action's success as void and reject a summary. */
842 replyWithSummary: Schema.optional(Schema.Literal(true)),
843});
844export type PullRequestActionInput = typeof PullRequestActionInput.Type;
845
846// Preserve whitespace because it affects Markdown formatting.
847const CommentBody = Schema.String.check(Schema.isNonEmpty()).check(Schema.isMaxLength(65_536));
848
849export const PullRequestCommentInput = Schema.Struct({
850 ...PullRequestRef.fields,
851 body: CommentBody,
852});
853export type PullRequestCommentInput = typeof PullRequestCommentInput.Type;
854
855export const PullRequestUpdateInput = Schema.Struct({
856 ...PullRequestRef.fields,
857 title: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(1024))),
858 body: Schema.optional(Schema.String.check(Schema.isMaxLength(65_536))),
859 maintainerCanModify: Schema.optional(Schema.Boolean),
860});
861export type PullRequestUpdateInput = typeof PullRequestUpdateInput.Type;
862
863/** Comment kind selects the endpoint; GitHub uses separate mutations for conversation and line comments. */
864export const PullRequestCommentUpdateInput = Schema.Struct({
865 ...PullRequestRef.fields,
866 commentId: TrimmedNonEmptyString,
867 kind: Schema.Literals(["issue-comment", "review-comment"]),
868 body: CommentBody,
869});
870export type PullRequestCommentUpdateInput = typeof PullRequestCommentUpdateInput.Type;
871
872export const PullRequestReviewPosition = Schema.Union([
873 Schema.Struct({
874 kind: Schema.Literal("added"),
875 newLine: PositiveInt,
876 }),
877 Schema.Struct({
878 kind: Schema.Literal("deleted"),
879 oldLine: PositiveInt,
880 }),
881 Schema.Struct({
882 kind: Schema.Literal("context"),
883 oldLine: PositiveInt,
884 newLine: PositiveInt,
885 side: PullRequestDiffSide,
886 }),
887]);
888export type PullRequestReviewPosition = typeof PullRequestReviewPosition.Type;
889
890export const PullRequestReviewCommentDraft = Schema.Struct({
891 path: TrimmedNonEmptyString,
892 /** GitLab needs both names for renamed-file comments; hosts using one path ignore this. */
893 oldPath: Schema.optional(TrimmedNonEmptyString),
894 position: PullRequestReviewPosition,
895 /**
896 * The first line of a comment on a range, which ends at `position`. Sent only to a host whose
897 * capabilities include `review.rangeComment`.
898 */
899 startPosition: Schema.optional(PullRequestReviewPosition),
900 body: CommentBody,
901});
902export type PullRequestReviewCommentDraft = typeof PullRequestReviewCommentDraft.Type;
903
904export const PullRequestSubmitReviewInput = Schema.Struct({
905 ...PullRequestRef.fields,
906 verdict: PullRequestReviewVerdict,
907 body: Schema.String.check(Schema.isMaxLength(65_536)),
908 comments: Schema.Array(PullRequestReviewCommentDraft),
909});
910export type PullRequestSubmitReviewInput = typeof PullRequestSubmitReviewInput.Type;
911
912export const PullRequestThreadCommentsInput = Schema.Struct({
913 ...PullRequestRef.fields,
914 threadId: TrimmedNonEmptyString,
915 cursor: TrimmedNonEmptyString,
916});
917export type PullRequestThreadCommentsInput = typeof PullRequestThreadCommentsInput.Type;
918
919export const PullRequestThreadCommentsResult = Schema.Struct({
920 comments: Schema.Array(PullRequestThreadComment),
921 nextCursor: Schema.NullOr(TrimmedNonEmptyString),
922 // Comments the host hid that this page left out; the thread's `commentCount` still counts them.
923 hiddenCount: Schema.optional(NonNegativeInt),
924});
925export type PullRequestThreadCommentsResult = typeof PullRequestThreadCommentsResult.Type;
926
927export const PullRequestThreadReplyInput = Schema.Struct({
928 ...PullRequestRef.fields,
929 threadId: TrimmedNonEmptyString,
930 body: CommentBody,
931});
932export type PullRequestThreadReplyInput = typeof PullRequestThreadReplyInput.Type;
933
934export const PullRequestThreadResolutionInput = Schema.Struct({
935 ...PullRequestRef.fields,
936 threadId: TrimmedNonEmptyString,
937 resolved: Schema.Boolean,
938});
939export type PullRequestThreadResolutionInput = typeof PullRequestThreadResolutionInput.Type;
940
941export const PullRequestReactionInput = Schema.Struct({
942 ...PullRequestRef.fields,
943 subjectId: Schema.optional(TrimmedNonEmptyString),
944 content: PullRequestReactionContent,
945 reacted: Schema.Boolean,
946});
947export type PullRequestReactionInput = typeof PullRequestReactionInput.Type;
948
949/** The most labels or reviewers one write may change. */
950export const PULL_REQUEST_CHANGE_BATCH_LIMIT = 25;
951
952export const PullRequestReviewerRequestInput = Schema.Struct({
953 ...PullRequestRef.fields,
954 reviewers: Schema.Array(
955 Schema.Struct({ id: TrimmedNonEmptyString, kind: PullRequestReviewerKind }),
956 ).check(Schema.isMinLength(1), Schema.isMaxLength(PULL_REQUEST_CHANGE_BATCH_LIMIT)),
957 requested: Schema.Boolean,
958});
959export type PullRequestReviewerRequestInput = typeof PullRequestReviewerRequestInput.Type;
960
961export const PullRequestLabelChangeInput = Schema.Struct({
962 ...PullRequestRef.fields,
963 labels: Schema.Array(TrimmedNonEmptyString).check(
964 Schema.isMinLength(1),
965 Schema.isMaxLength(PULL_REQUEST_CHANGE_BATCH_LIMIT),
966 ),
967 applied: Schema.Boolean,
968});
969export type PullRequestLabelChangeInput = typeof PullRequestLabelChangeInput.Type;
970
971/**
972 * The names keep their `cli-` prefix for wire compatibility; for a host read with a credential
973 * rather than a tool they mean "no credential" and "a refused credential".
974 */
975export const PullRequestUnavailableReason = Schema.Literals([
976 "cli-missing",
977 "cli-unauthenticated",
978 "provider-unsupported",
979 "repository-without-project",
980]);
981export type PullRequestUnavailableReason = typeof PullRequestUnavailableReason.Type;
982
983/**
984 * `author:me` names whoever is signed in to the host being read, GitHub's `@me` spelled either
985 * way. A host's own search would answer it, but the page and the server both re-check an author
986 * against the row's login, so the name is resolved before either comparison rather than being
987 * matched as the literal login "me".
988 */
989export function resolvePullRequestAuthorFilter(
990 author: string,
991 viewer: string | null | undefined,
992): string {
993 const trimmed = author.trim();
994 if (!/^@?me$/i.test(trimmed)) return trimmed;
995 return viewer === null || viewer === undefined || viewer.trim().length === 0 ? trimmed : viewer;
996}
997
998export class PullRequestUnavailableError extends Schema.TaggedError<PullRequestUnavailableError>()(
999 "PullRequestUnavailableError",
1000 {
1001 reason: PullRequestUnavailableReason,
1002 provider: Schema.optional(SourceControlProviderKind),
1003 listResult: Schema.optional(PullRequestListResult),
1004 /**
1005 * What the host needs on the server, in its own words. Absent from older servers, which
1006 * shipped no such field, and for a host that names nothing.
1007 */
1008 requirement: Schema.optional(TrimmedNonEmptyString),
1009 /** The repository no project has, for `repository-without-project`. */
1010 repository: Schema.optional(TrimmedNonEmptyString),
1011 /**
1012 * For `repository-without-project` on a host read through any checkout on it: the host no
1013 * project is on.
1014 */
1015 host: Schema.optional(TrimmedNonEmptyString),
1016 cause: Schema.optional(Schema.Defect()),
1017 },
1018 { httpApiStatus: 503 },
1019) {
1020 [HttpServerRespondable.symbol]() {
1021 return HttpServerResponse.schemaJson(PullRequestUnavailableError)(this, { status: 503 });
1022 }
1023
1024 override get message(): string {
1025 switch (this.reason) {
1026 case "cli-missing":
1027 return this.requirement ?? "The tool this host is read through is not installed or set up.";
1028 case "cli-unauthenticated":
1029 return this.requirement ?? "This host has no working credentials.";
1030 case "provider-unsupported":
1031 return "Change requests cannot be browsed for this project's host yet.";
1032 case "repository-without-project":
1033 if (this.host !== undefined) {
1034 return `No project in T3 Code is on ${this.host}. Add any ${this.host} project to read its change requests.`;
1035 }
1036 return `${this.repository ?? "This repository"} is not a project in T3 Code. Add it as a project to read its change requests.`;
1037 }
1038 }
1039}
1040
1041export class PullRequestOperationError extends Schema.TaggedError<PullRequestOperationError>()(
1042 "PullRequestOperationError",
1043 {
1044 operation: Schema.String,
1045 detail: TrimmedNonEmptyString,
1046 reason: Schema.optional(Schema.Literal("not-found")),
1047 listResult: Schema.optional(PullRequestListResult),
1048 cause: Schema.optional(Schema.Defect()),
1049 },
1050 { httpApiStatus: 502 },
1051) {
1052 [HttpServerRespondable.symbol]() {
1053 return HttpServerResponse.schemaJson(PullRequestOperationError)(this, { status: 502 });
1054 }
1055
1056 override get message(): string {
1057 return `Pull request operation ${this.operation} failed: ${this.detail}`;
1058 }
1059}