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 /**
45 * Labels as GitHub's own search reads them: each group is one `label:` qualifier, a row must
46 * satisfy every group, and a group holding several names is satisfied by any one of them —
47 * `label:size:S,size:XS` finds either size.
48 */
49 labels: Schema.optional(Schema.Array(PullRequestQualifierValues).check(Schema.isMaxLength(10))),
50 excludedLabels: Schema.optional(PullRequestQualifierValues),
51 author: Schema.optional(PullRequestQualifierValue),
52});
53export type PullRequestListFilters = typeof PullRequestListFilters.Type;
54
55export const PullRequestChecksState = Schema.Literals(["passing", "failing", "pending"]);
56export type PullRequestChecksState = typeof PullRequestChecksState.Type;
57
58export const PullRequestMergeability = Schema.Literals(["mergeable", "conflicting", "unknown"]);
59export type PullRequestMergeability = typeof PullRequestMergeability.Type;
60
61export const PullRequestMergeMethod = Schema.Literals(["merge", "squash", "rebase"]);
62export type PullRequestMergeMethod = typeof PullRequestMergeMethod.Type;
63
64export const PullRequestAction = Schema.Literals([
65 "merge",
66 "ready",
67 "draft",
68 "close",
69 "reopen",
70 "delete-branch",
71 "update-branch",
72 "enable-auto-merge",
73 "disable-auto-merge",
74 "revert",
75 "approve-workflows",
76]);
77export type PullRequestAction = typeof PullRequestAction.Type;
78
79export const PullRequestUpdateMethod = Schema.Literals(["merge", "rebase"]);
80export type PullRequestUpdateMethod = typeof PullRequestUpdateMethod.Type;
81
82export const PullRequestBaseComparison = Schema.Literals(["up-to-date", "behind", "unknown"]);
83export type PullRequestBaseComparison = typeof PullRequestBaseComparison.Type;
84
85export const PullRequestActor = Schema.Struct({
86 isBot: Schema.optional(Schema.Boolean),
87 login: TrimmedNonEmptyString,
88 name: Schema.NullOr(Schema.String),
89 avatarUrl: Schema.NullOr(Schema.String),
90});
91export type PullRequestActor = typeof PullRequestActor.Type;
92
93export const PullRequestLabel = Schema.Struct({
94 name: TrimmedNonEmptyString,
95 color: Schema.NullOr(Schema.String),
96});
97export type PullRequestLabel = typeof PullRequestLabel.Type;
98
99export const PullRequestCheckStatus = Schema.Literals([
100 "pending",
101 "action-required",
102 "success",
103 "failure",
104 "skipped",
105 "neutral",
106 "cancelled",
107]);
108export type PullRequestCheckStatus = typeof PullRequestCheckStatus.Type;
109
110export const PullRequestCheck = Schema.Struct({
111 name: TrimmedNonEmptyString,
112 status: PullRequestCheckStatus,
113 description: Schema.NullOr(Schema.String),
114 url: Schema.NullOr(Schema.String),
115 required: Schema.optional(Schema.Boolean),
116});
117export type PullRequestCheck = typeof PullRequestCheck.Type;
118
119export const PullRequestReactionContent = Schema.Literals([
120 "thumbs-up",
121 "thumbs-down",
122 "laugh",
123 "hooray",
124 "confused",
125 "heart",
126 "rocket",
127 "eyes",
128]);
129export type PullRequestReactionContent = typeof PullRequestReactionContent.Type;
130
131export const PullRequestReaction = Schema.Struct({
132 content: PullRequestReactionContent,
133 count: PositiveInt,
134 /**
135 * When `viewerHasReacted` is true, the viewer's own login is left out of this list — the
136 * page names them "You" instead — but `count` still counts them along with everyone else.
137 */
138 actors: Schema.Array(TrimmedNonEmptyString),
139 viewerHasReacted: Schema.Boolean,
140});
141export type PullRequestReaction = typeof PullRequestReaction.Type;
142
143export const PullRequestCommentKind = Schema.Literals([
144 "issue-comment",
145 "review-comment",
146 "review",
147]);
148export type PullRequestCommentKind = typeof PullRequestCommentKind.Type;
149
150export const PullRequestComment = Schema.Struct({
151 id: TrimmedNonEmptyString,
152 kind: PullRequestCommentKind,
153 author: Schema.NullOr(PullRequestActor),
154 body: Schema.String,
155 createdAt: IsoDateTime,
156 editedAt: Schema.optional(Schema.NullOr(IsoDateTime)),
157 url: Schema.NullOr(Schema.String),
158 path: Schema.NullOr(Schema.String),
159 reviewState: Schema.NullOr(Schema.String),
160 reactions: Schema.optional(Schema.Array(PullRequestReaction)),
161 canEdit: Schema.optional(Schema.Boolean),
162});
163export type PullRequestComment = typeof PullRequestComment.Type;
164
165export const PullRequestDiffSide = Schema.Literals(["left", "right"]);
166export type PullRequestDiffSide = typeof PullRequestDiffSide.Type;
167
168export const PullRequestReviewVerdict = Schema.Literals(["comment", "approve", "request-changes"]);
169export type PullRequestReviewVerdict = typeof PullRequestReviewVerdict.Type;
170
171export const PullRequestThreadComment = Schema.Struct({
172 id: TrimmedNonEmptyString,
173 author: Schema.NullOr(PullRequestActor),
174 body: Schema.String,
175 createdAt: IsoDateTime,
176 editedAt: Schema.optional(Schema.NullOr(IsoDateTime)),
177 url: Schema.NullOr(Schema.String),
178 reactions: Schema.optional(Schema.Array(PullRequestReaction)),
179 canEdit: Schema.optional(Schema.Boolean),
180});
181export type PullRequestThreadComment = typeof PullRequestThreadComment.Type;
182
183export const PullRequestReviewThread = Schema.Struct({
184 id: TrimmedNonEmptyString,
185 path: TrimmedNonEmptyString,
186 line: Schema.NullOr(PositiveInt),
187 side: PullRequestDiffSide,
188 isResolved: Schema.Boolean,
189 isOutdated: Schema.Boolean,
190 comments: Schema.Array(PullRequestThreadComment),
191 commentCount: Schema.optional(NonNegativeInt),
192 nextCommentsCursor: Schema.optional(TrimmedNonEmptyString),
193 /**
194 * It can only narrow the reader's repository-wide `resolve` permission, never widen
195 * it. Absent where the host doesn't say per thread.
196 */
197 canResolve: Schema.optional(Schema.Boolean),
198});
199export type PullRequestReviewThread = typeof PullRequestReviewThread.Type;
200
201export const PullRequestReviewerKind = Schema.Literals(["user", "team"]);
202export type PullRequestReviewerKind = typeof PullRequestReviewerKind.Type;
203
204export const PullRequestReviewerCandidate = Schema.Struct({
205 ...PullRequestActor.fields,
206 id: TrimmedNonEmptyString,
207 kind: PullRequestReviewerKind,
208 isRequested: Schema.Boolean,
209});
210export type PullRequestReviewerCandidate = typeof PullRequestReviewerCandidate.Type;
211
212export const PullRequestReviewerCandidateList = Schema.Struct({
213 candidates: Schema.Array(PullRequestReviewerCandidate),
214 truncated: Schema.Boolean,
215});
216export type PullRequestReviewerCandidateList = typeof PullRequestReviewerCandidateList.Type;
217
218export const PullRequestLabelCandidate = Schema.Struct({
219 ...PullRequestLabel.fields,
220 description: Schema.NullOr(Schema.String),
221 isApplied: Schema.Boolean,
222});
223export type PullRequestLabelCandidate = typeof PullRequestLabelCandidate.Type;
224
225export const PullRequestLabelCandidateList = Schema.Struct({
226 candidates: Schema.Array(PullRequestLabelCandidate),
227 truncated: Schema.Boolean,
228});
229export type PullRequestLabelCandidateList = typeof PullRequestLabelCandidateList.Type;
230
231export const PullRequestCommit = Schema.Struct({
232 oid: TrimmedNonEmptyString,
233 messageHeadline: Schema.String,
234 committedDate: IsoDateTime,
235 additions: Schema.optional(NonNegativeInt),
236 deletions: Schema.optional(NonNegativeInt),
237 authors: Schema.optional(Schema.Array(PullRequestActor)),
238});
239export type PullRequestCommit = typeof PullRequestCommit.Type;
240
241export const PullRequestReviewCapabilities = Schema.Struct({
242 inlineComment: Schema.Boolean,
243 reply: Schema.Boolean,
244 resolve: Schema.Boolean,
245 verdicts: ForwardCompatibleArray(PullRequestReviewVerdict),
246});
247export type PullRequestReviewCapabilities = typeof PullRequestReviewCapabilities.Type;
248
249export const PullRequestEditCapabilities = Schema.Struct({
250 changeRequest: Schema.Boolean,
251 comment: Schema.Boolean,
252});
253export type PullRequestEditCapabilities = typeof PullRequestEditCapabilities.Type;
254
255export const PullRequestReviewerCapabilities = Schema.Struct({
256 request: Schema.Boolean,
257 listCandidates: Schema.Boolean,
258});
259export type PullRequestReviewerCapabilities = typeof PullRequestReviewerCapabilities.Type;
260
261export const PullRequestViewedFilesStore = Schema.Literals(["host", "environment"]);
262export type PullRequestViewedFilesStore = typeof PullRequestViewedFilesStore.Type;
263
264export const PullRequestCapabilities = Schema.Struct({
265 diff: Schema.Boolean,
266 comment: Schema.Boolean,
267 actions: ForwardCompatibleArray(PullRequestAction),
268 mergeMethods: ForwardCompatibleArray(PullRequestMergeMethod),
269 deleteBranch: Schema.optional(Schema.Boolean),
270 /**
271 * Absent where it cannot at all, which is every host that has not said otherwise — so a
272 * provider that says nothing offers nothing.
273 */
274 updateMethods: Schema.optional(ForwardCompatibleArray(PullRequestUpdateMethod)),
275 search: Schema.Boolean,
276 reactions: Schema.optional(Schema.Boolean),
277 viewedFiles: Schema.optional(PullRequestViewedFilesStore),
278 review: PullRequestReviewCapabilities,
279 reviewers: PullRequestReviewerCapabilities,
280 edit: Schema.optional(PullRequestEditCapabilities),
281 stacks: Schema.optional(Schema.Boolean),
282 stackActions: Schema.optional(Schema.Boolean),
283 labels: Schema.optional(Schema.Boolean),
284});
285export type PullRequestCapabilities = typeof PullRequestCapabilities.Type;
286
287/**
288 * A permission the host reports nothing about is granted rather than withheld.
289 */
290export const PullRequestViewerPermissions = Schema.Struct({
291 stackRebase: Schema.optional(Schema.Boolean),
292 actions: ForwardCompatibleArray(PullRequestAction),
293 comment: Schema.Boolean,
294 resolve: Schema.Boolean,
295 verdicts: ForwardCompatibleArray(PullRequestReviewVerdict),
296 requestReviewers: Schema.Boolean,
297 updateMethods: Schema.optional(ForwardCompatibleArray(PullRequestUpdateMethod)),
298 labels: Schema.optional(Schema.Boolean),
299 editChangeRequest: Schema.optional(Schema.Boolean),
300});
301export type PullRequestViewerPermissions = typeof PullRequestViewerPermissions.Type;
302
303export const PullRequestMergeCapabilities = Schema.Struct({
304 merge: Schema.Boolean,
305 squash: Schema.Boolean,
306 rebase: Schema.Boolean,
307});
308export type PullRequestMergeCapabilities = typeof PullRequestMergeCapabilities.Type;
309
310export const PullRequestStackMembership = Schema.Struct({
311 number: PositiveInt,
312 position: PositiveInt,
313 size: PositiveInt,
314 base: TrimmedNonEmptyString,
315});
316export type PullRequestStackMembership = typeof PullRequestStackMembership.Type;
317
318export const PullRequestListEntry = Schema.Struct({
319 stack: Schema.optional(PullRequestStackMembership),
320 provider: SourceControlProviderKind,
321 host: TrimmedNonEmptyString,
322 projectId: ProjectId,
323 projectTitle: TrimmedNonEmptyString,
324 repository: TrimmedNonEmptyString,
325 number: PositiveInt,
326 title: TrimmedNonEmptyString,
327 url: TrimmedNonEmptyString,
328 author: Schema.NullOr(PullRequestActor),
329 headBranch: TrimmedNonEmptyString,
330 baseBranch: TrimmedNonEmptyString,
331 state: PullRequestState,
332 isDraft: Schema.Boolean,
333 mergeability: PullRequestMergeability,
334 /**
335 * Zero where the host has not been asked for the counts yet, which on GitHub is every row a
336 * listing hands over: they are read afterwards, through `PullRequestListStatsInput`.
337 */
338 additions: NonNegativeInt,
339 deletions: NonNegativeInt,
340 createdAt: IsoDateTime,
341 updatedAt: IsoDateTime,
342 observedAt: Schema.optional(Schema.Finite),
343 viewerReviewRequested: Schema.Boolean,
344 labels: Schema.Array(PullRequestLabel),
345 reviewDecision: Schema.optional(PullRequestReviewDecision),
346 checksState: Schema.optional(PullRequestChecksState),
347});
348export type PullRequestListEntry = typeof PullRequestListEntry.Type;
349
350export const PullRequestListCursors = Schema.Record(
351 TrimmedNonEmptyString,
352 TrimmedNonEmptyString.check(Schema.isMaxLength(4096)),
353);
354export type PullRequestListCursors = typeof PullRequestListCursors.Type;
355
356export const PullRequestListInput = Schema.Struct({
357 state: PullRequestListState,
358 involvement: Schema.optional(PullRequestInvolvement),
359 filters: Schema.optional(PullRequestListFilters),
360 projectId: Schema.optional(ProjectId),
361 projectIds: Schema.optional(Schema.Array(ProjectId).check(Schema.isMaxLength(100))),
362 host: Schema.optional(TrimmedNonEmptyString),
363 limit: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 500 }))),
364 cursors: Schema.optional(PullRequestListCursors),
365 query: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(200))),
366});
367export type PullRequestListInput = typeof PullRequestListInput.Type;
368
369export const PullRequestProviderSummary = Schema.Struct({
370 host: TrimmedNonEmptyString,
371 kind: SourceControlProviderKind,
372 searchesOnHost: Schema.Boolean,
373 actions: Schema.optional(ForwardCompatibleArray(PullRequestAction)),
374 projectCount: PositiveInt,
375 configured: Schema.Boolean,
376 detail: Schema.NullOr(TrimmedNonEmptyString),
377});
378export type PullRequestProviderSummary = typeof PullRequestProviderSummary.Type;
379
380export const PullRequestListProjectError = Schema.Struct({
381 projectId: ProjectId,
382 projectTitle: TrimmedNonEmptyString,
383 message: TrimmedNonEmptyString,
384});
385export type PullRequestListProjectError = typeof PullRequestListProjectError.Type;
386
387export const PullRequestListResult = Schema.Struct({
388 /**
389 * A host that could not be read is absent rather than present-and-undefined, because an
390 * open-keyed record cannot carry an optional value through the JSON codec.
391 */
392 viewers: Schema.Record(TrimmedNonEmptyString, TrimmedNonEmptyString),
393 providers: Schema.Array(PullRequestProviderSummary),
394 /**
395 * A page that appends a continuation sorts what it then holds the same way: a repository's
396 * next slice is older than that repository's last row, but can still be newer than another
397 * repository's.
398 */
399 entries: Schema.Array(PullRequestListEntry),
400 errors: Schema.Array(PullRequestListProjectError),
401 truncated: Schema.Boolean,
402 nextCursors: PullRequestListCursors,
403});
404export type PullRequestListResult = typeof PullRequestListResult.Type;
405
406export const PullRequestRef = Schema.Struct({
407 projectId: ProjectId,
408 host: Schema.optional(TrimmedNonEmptyString),
409 expectedAccountId: Schema.optional(TrimmedNonEmptyString),
410 allowStale: Schema.optional(Schema.Boolean),
411 repository: TrimmedNonEmptyString,
412 number: PositiveInt,
413});
414export type PullRequestRef = typeof PullRequestRef.Type;
415
416export const PullRequestRoutingIdentityInput = Schema.Struct({
417 host: TrimmedNonEmptyString,
418});
419export type PullRequestRoutingIdentityInput = typeof PullRequestRoutingIdentityInput.Type;
420
421export const PullRequestRoutingIdentityResult = Schema.Struct({
422 accountId: TrimmedNonEmptyString,
423 host: TrimmedNonEmptyString,
424 provider: Schema.Literal("github"),
425 viewer: TrimmedNonEmptyString,
426});
427export type PullRequestRoutingIdentityResult = typeof PullRequestRoutingIdentityResult.Type;
428
429export const PullRequestRoutingResult = Schema.Struct({
430 accountId: TrimmedNonEmptyString,
431 host: TrimmedNonEmptyString,
432 provider: SourceControlProviderKind,
433 viewer: TrimmedNonEmptyString,
434 projectTitle: TrimmedNonEmptyString,
435 workspaceRoot: TrimmedNonEmptyString,
436});
437export type PullRequestRoutingResult = typeof PullRequestRoutingResult.Type;
438
439export const PullRequestLinkedThreadsResult = Schema.Struct({
440 threads: Schema.Array(
441 Schema.Struct({
442 id: ThreadId,
443 projectId: ProjectId,
444 title: Schema.String,
445 archivedAt: Schema.NullOr(IsoDateTime),
446 }),
447 ),
448});
449export type PullRequestLinkedThreadsResult = typeof PullRequestLinkedThreadsResult.Type;
450
451export const PullRequestPreview = Schema.Struct({
452 projectId: ProjectId,
453 repository: TrimmedNonEmptyString,
454 number: PositiveInt,
455 title: TrimmedNonEmptyString,
456 url: TrimmedNonEmptyString,
457 author: Schema.NullOr(PullRequestActor),
458 state: PullRequestState,
459 isDraft: Schema.Boolean,
460 createdAt: IsoDateTime,
461});
462export type PullRequestPreview = typeof PullRequestPreview.Type;
463
464export const PullRequestSummary = Schema.Struct({
465 provider: SourceControlProviderKind,
466 projectId: ProjectId,
467 repository: TrimmedNonEmptyString,
468 number: PositiveInt,
469 title: TrimmedNonEmptyString,
470 url: TrimmedNonEmptyString,
471 state: PullRequestState,
472 isDraft: Schema.optional(Schema.Boolean),
473 headBranch: TrimmedNonEmptyString,
474 baseBranch: TrimmedNonEmptyString,
475 closedAt: Schema.optional(Schema.NullOr(Schema.String)),
476 mergedAt: Schema.optional(Schema.NullOr(Schema.String)),
477 updatedAt: IsoDateTime,
478 observedAt: Schema.optional(Schema.Finite),
479 author: Schema.optional(Schema.NullOr(PullRequestActor)),
480 additions: Schema.optional(NonNegativeInt),
481 deletions: Schema.optional(NonNegativeInt),
482 changedFiles: Schema.optional(NonNegativeInt),
483 reviewDecision: Schema.optional(Schema.NullOr(PullRequestReviewDecision)),
484 checksState: Schema.optional(Schema.NullOr(PullRequestChecksState)),
485 mergeability: Schema.optional(PullRequestMergeability),
486 /** Null when the host says the pull request is in no stack; absent when the read did not ask. */
487 stack: Schema.optional(Schema.NullOr(PullRequestStackMembership)),
488});
489export type PullRequestSummary = typeof PullRequestSummary.Type;
490
491export const PullRequestStack = Schema.Struct({
492 id: TrimmedNonEmptyString,
493 number: PositiveInt,
494 url: TrimmedNonEmptyString,
495 base: TrimmedNonEmptyString,
496 layers: Schema.Array(
497 Schema.Struct({
498 number: PositiveInt,
499 title: Schema.optional(Schema.String),
500 isDraft: Schema.optional(Schema.Boolean),
501 headSha: Schema.optional(TrimmedNonEmptyString),
502 headBranch: TrimmedNonEmptyString,
503 state: PullRequestState,
504 }),
505 ),
506});
507export type PullRequestStack = typeof PullRequestStack.Type;
508
509export const PullRequestDiffStat = Schema.Struct({
510 projectId: ProjectId,
511 repository: TrimmedNonEmptyString,
512 number: PositiveInt,
513 additions: NonNegativeInt,
514 deletions: NonNegativeInt,
515});
516export type PullRequestDiffStat = typeof PullRequestDiffStat.Type;
517
518export const PullRequestListStatsInput = Schema.Struct({
519 refs: Schema.Array(PullRequestRef).check(Schema.isMaxLength(500)),
520});
521export type PullRequestListStatsInput = typeof PullRequestListStatsInput.Type;
522
523export const PullRequestListStatsResult = Schema.Struct({
524 stats: Schema.Array(PullRequestDiffStat),
525});
526export type PullRequestListStatsResult = typeof PullRequestListStatsResult.Type;
527
528/**
529 * A separate request rather than a flag on the reads, so an explicit "refresh" one person
530 * presses is the only thing that spends host requests — every ordinary read shares.
531 */
532export const PullRequestInvalidateInput = Schema.Struct({
533 reference: Schema.optional(PullRequestRef),
534 filesViewedOnly: Schema.optional(Schema.Boolean),
535});
536export type PullRequestInvalidateInput = typeof PullRequestInvalidateInput.Type;
537
538export const PullRequestReportStateInput = Schema.Struct({
539 reference: PullRequestRef,
540 state: PullRequestState,
541 isDraft: Schema.optional(Schema.Boolean),
542});
543export type PullRequestReportStateInput = typeof PullRequestReportStateInput.Type;
544
545export const PullRequestDetail = Schema.Struct({
546 provider: SourceControlProviderKind,
547 capabilities: PullRequestCapabilities,
548 viewerPermissions: PullRequestViewerPermissions,
549 projectId: ProjectId,
550 projectTitle: TrimmedNonEmptyString,
551 workspaceRoot: TrimmedNonEmptyString,
552 repository: TrimmedNonEmptyString,
553 number: PositiveInt,
554 title: TrimmedNonEmptyString,
555 body: Schema.String,
556 url: TrimmedNonEmptyString,
557 author: Schema.NullOr(PullRequestActor),
558 state: PullRequestState,
559 isDraft: Schema.Boolean,
560 mergeability: PullRequestMergeability,
561 additions: NonNegativeInt,
562 deletions: NonNegativeInt,
563 changedFiles: NonNegativeInt,
564 headBranch: TrimmedNonEmptyString,
565 headRepositoryNameWithOwner: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
566 headSha: Schema.optional(TrimmedNonEmptyString),
567 baseBranch: TrimmedNonEmptyString,
568 createdAt: IsoDateTime,
569 updatedAt: IsoDateTime,
570 observedAt: Schema.optional(Schema.Finite),
571 mergedAt: Schema.NullOr(IsoDateTime),
572 closedAt: Schema.NullOr(IsoDateTime),
573 reviewers: Schema.Array(PullRequestActor),
574 labels: Schema.Array(PullRequestLabel),
575 checks: Schema.Array(PullRequestCheck),
576 mergeCapabilities: PullRequestMergeCapabilities,
577 viewer: Schema.optional(TrimmedNonEmptyString),
578 baseComparison: Schema.optional(PullRequestBaseComparison),
579 behindBy: Schema.optional(NonNegativeInt),
580 /**
581 * Whether the host is already armed to merge this on its own. Absent where the host does not
582 * report it, which is not the same as off: a page that reads silence as "not armed" offers to
583 * arm something that is already armed, and a second arming is a write nobody asked for.
584 */
585 autoMergeEnabled: Schema.optional(Schema.Boolean),
586 autoMergeMethod: Schema.optional(PullRequestMergeMethod),
587 workflowApprovalsRequired: Schema.optional(NonNegativeInt),
588});
589export type PullRequestDetail = typeof PullRequestDetail.Type;
590
591export const PullRequestChecks = Schema.Struct({
592 state: PullRequestState,
593 checks: Schema.Array(PullRequestCheck),
594});
595export type PullRequestChecks = typeof PullRequestChecks.Type;
596
597export const PullRequestActivity = Schema.Struct({
598 author: Schema.optional(Schema.NullOr(PullRequestActor)),
599 reviewers: Schema.optional(Schema.Array(PullRequestActor)),
600 comments: Schema.Array(PullRequestComment),
601 commentCount: NonNegativeInt,
602 commentsTruncated: Schema.Boolean,
603 reviewThreadsTruncated: Schema.optional(Schema.Boolean),
604 reviewThreads: Schema.Array(PullRequestReviewThread),
605 commits: Schema.Array(PullRequestCommit),
606 reactions: Schema.optional(Schema.Array(PullRequestReaction)),
607});
608export type PullRequestActivity = typeof PullRequestActivity.Type;
609
610export const PullRequestDetailView = Schema.Struct({
611 ...PullRequestDetail.fields,
612 ...PullRequestActivity.fields,
613 author: Schema.NullOr(PullRequestActor),
614 reviewers: Schema.Array(PullRequestActor),
615});
616export type PullRequestDetailView = typeof PullRequestDetailView.Type;
617
618/**
619 * A diff arrives a slice at a time, because a large change is more than any host will hand over
620 * at once — GitHub refuses outright past 300 files — and more than a viewer can lay out in one
621 * go.
622 */
623export const PullRequestDiffInput = Schema.Struct({
624 ...PullRequestRef.fields,
625 cursor: Schema.optional(TrimmedNonEmptyString),
626 commit: Schema.optional(TrimmedNonEmptyString),
627});
628export type PullRequestDiffInput = typeof PullRequestDiffInput.Type;
629
630export const PullRequestOmittedFileStat = Schema.Struct({
631 path: TrimmedNonEmptyString,
632 additions: Schema.Number,
633 deletions: Schema.Number,
634});
635export type PullRequestOmittedFileStat = typeof PullRequestOmittedFileStat.Type;
636
637export const PullRequestDiffResult = Schema.Struct({
638 patch: Schema.String,
639 /**
640 * Something inside this slice could not be shown — a binary file, or a hunk the host declined
641 * to inline. Not the same as there being more slices, which `nextCursor` answers.
642 */
643 truncated: Schema.Boolean,
644 nextCursor: Schema.NullOr(TrimmedNonEmptyString),
645 omittedFileStats: Schema.optional(Schema.Array(PullRequestOmittedFileStat)),
646});
647export type PullRequestDiffResult = typeof PullRequestDiffResult.Type;
648
649export const PullRequestDiffFileContentsInput = Schema.Struct({
650 ...PullRequestRef.fields,
651 commit: Schema.optional(TrimmedNonEmptyString),
652 changeType: Schema.Literals(["change", "rename-pure", "rename-changed", "new", "deleted"]),
653 oldPath: TrimmedNonEmptyString,
654 newPath: TrimmedNonEmptyString,
655});
656export type PullRequestDiffFileContentsInput = typeof PullRequestDiffFileContentsInput.Type;
657
658export const PullRequestDiffFileContentsResult = Schema.Struct({
659 oldContents: Schema.String,
660 newContents: Schema.String,
661});
662export type PullRequestDiffFileContentsResult = typeof PullRequestDiffFileContentsResult.Type;
663
664const MAX_FILE_PATH_LENGTH = 4096;
665/**
666 * Not trimmed: a leading or trailing space is a legal part of a file's name, and the mark is
667 * keyed by the name the host gave, so trimming files it under a name nothing else uses.
668 */
669const FilePath = Schema.String.check(Schema.isNonEmpty(), Schema.isMaxLength(MAX_FILE_PATH_LENGTH));
670
671export const PullRequestFileViewedState = Schema.Literals(["unviewed", "viewed", "dismissed"]);
672export type PullRequestFileViewedState = typeof PullRequestFileViewedState.Type;
673
674export const PullRequestFileViewed = Schema.Struct({
675 path: FilePath,
676 state: PullRequestFileViewedState,
677});
678export type PullRequestFileViewed = typeof PullRequestFileViewed.Type;
679
680export const PullRequestFilesViewedResult = Schema.Struct({
681 /** Only the files the host reported a state for. A file missing from this list is unviewed. */
682 files: Schema.Array(PullRequestFileViewed),
683 truncated: Schema.Boolean,
684});
685export type PullRequestFilesViewedResult = typeof PullRequestFilesViewedResult.Type;
686
687const PullRequestLineCount = Schema.Struct({
688 additions: NonNegativeInt,
689 deletions: NonNegativeInt,
690});
691
692/**
693 * A change's line counts split by what the changed files are, judged from their paths. Null when
694 * the host gives no per-file counts, or has more files than one read covers: a split of part of
695 * the change would not add up to the totals shown beside it.
696 */
697export const PullRequestChangeBreakdown = Schema.NullOr(
698 Schema.Struct({
699 code: PullRequestLineCount,
700 tests: PullRequestLineCount,
701 mocks: PullRequestLineCount,
702 generated: PullRequestLineCount,
703 // Servers from before the docs split send no key; their docs lines sit in `code`.
704 docs: PullRequestLineCount.pipe(
705 Schema.withDecodingDefault(Effect.succeed({ additions: 0, deletions: 0 })),
706 ),
707 }),
708);
709export type PullRequestChangeBreakdown = typeof PullRequestChangeBreakdown.Type;
710
711const MAX_FILES_VIEWED_PRESSES = 500;
712
713export const PullRequestSetFilesViewedInput = Schema.Struct({
714 ...PullRequestRef.fields,
715 files: Schema.Array(
716 Schema.Struct({
717 path: FilePath,
718 viewed: Schema.Boolean,
719 }),
720 ).check(Schema.isMaxLength(MAX_FILES_VIEWED_PRESSES)),
721});
722export type PullRequestSetFilesViewedInput = typeof PullRequestSetFilesViewedInput.Type;
723
724export const PullRequestStackHead = Schema.Struct({
725 number: PositiveInt,
726 headSha: TrimmedNonEmptyString,
727});
728export type PullRequestStackHead = typeof PullRequestStackHead.Type;
729
730export const PullRequestActionInput = Schema.Struct({
731 /** Native stack scope; only send to environments advertising pullRequestStackActions. */
732 stackNumber: Schema.optional(PositiveInt),
733 expectedStackHeads: Schema.optional(Schema.Array(PullRequestStackHead)),
734 ...PullRequestRef.fields,
735 action: PullRequestAction,
736 mergeMethod: Schema.optional(PullRequestMergeMethod),
737 updateMethod: Schema.optional(PullRequestUpdateMethod),
738});
739export type PullRequestActionInput = typeof PullRequestActionInput.Type;
740
741// Not trimmed: the body is markdown, where leading spaces open a code block and two trailing
742// spaces are a line break.
743const CommentBody = Schema.String.check(Schema.isNonEmpty()).check(Schema.isMaxLength(65_536));
744
745export const PullRequestCommentInput = Schema.Struct({
746 ...PullRequestRef.fields,
747 body: CommentBody,
748});
749export type PullRequestCommentInput = typeof PullRequestCommentInput.Type;
750
751export const PullRequestUpdateInput = Schema.Struct({
752 ...PullRequestRef.fields,
753 title: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(1024))),
754 body: Schema.optional(Schema.String.check(Schema.isMaxLength(65_536))),
755});
756export type PullRequestUpdateInput = typeof PullRequestUpdateInput.Type;
757
758/**
759 * The kind travels beside the id because an id alone does not say which endpoint
760 * addresses it: GitHub rewrites a comment on the conversation and a comment on a line
761 * through two different mutations.
762 */
763export const PullRequestCommentUpdateInput = Schema.Struct({
764 ...PullRequestRef.fields,
765 commentId: TrimmedNonEmptyString,
766 kind: Schema.Literals(["issue-comment", "review-comment"]),
767 body: CommentBody,
768});
769export type PullRequestCommentUpdateInput = typeof PullRequestCommentUpdateInput.Type;
770
771export const PullRequestReviewPosition = Schema.Union([
772 Schema.Struct({
773 kind: Schema.Literal("added"),
774 newLine: PositiveInt,
775 }),
776 Schema.Struct({
777 kind: Schema.Literal("deleted"),
778 oldLine: PositiveInt,
779 }),
780 Schema.Struct({
781 kind: Schema.Literal("context"),
782 oldLine: PositiveInt,
783 newLine: PositiveInt,
784 side: PullRequestDiffSide,
785 }),
786]);
787export type PullRequestReviewPosition = typeof PullRequestReviewPosition.Type;
788
789export const PullRequestReviewCommentDraft = Schema.Struct({
790 path: TrimmedNonEmptyString,
791 /**
792 * GitLab resolves a position against both sides of the diff, so a comment on a renamed file
793 * needs both names; the hosts that address a comment by one path ignore this.
794 */
795 oldPath: Schema.optional(TrimmedNonEmptyString),
796 position: PullRequestReviewPosition,
797 body: CommentBody,
798});
799export type PullRequestReviewCommentDraft = typeof PullRequestReviewCommentDraft.Type;
800
801export const PullRequestSubmitReviewInput = Schema.Struct({
802 ...PullRequestRef.fields,
803 verdict: PullRequestReviewVerdict,
804 body: Schema.String.check(Schema.isMaxLength(65_536)),
805 comments: Schema.Array(PullRequestReviewCommentDraft),
806});
807export type PullRequestSubmitReviewInput = typeof PullRequestSubmitReviewInput.Type;
808
809export const PullRequestThreadCommentsInput = Schema.Struct({
810 ...PullRequestRef.fields,
811 threadId: TrimmedNonEmptyString,
812 cursor: TrimmedNonEmptyString,
813});
814export type PullRequestThreadCommentsInput = typeof PullRequestThreadCommentsInput.Type;
815
816export const PullRequestThreadCommentsResult = Schema.Struct({
817 comments: Schema.Array(PullRequestThreadComment),
818 nextCursor: Schema.NullOr(TrimmedNonEmptyString),
819});
820export type PullRequestThreadCommentsResult = typeof PullRequestThreadCommentsResult.Type;
821
822export const PullRequestThreadReplyInput = Schema.Struct({
823 ...PullRequestRef.fields,
824 threadId: TrimmedNonEmptyString,
825 body: CommentBody,
826});
827export type PullRequestThreadReplyInput = typeof PullRequestThreadReplyInput.Type;
828
829export const PullRequestThreadResolutionInput = Schema.Struct({
830 ...PullRequestRef.fields,
831 threadId: TrimmedNonEmptyString,
832 resolved: Schema.Boolean,
833});
834export type PullRequestThreadResolutionInput = typeof PullRequestThreadResolutionInput.Type;
835
836export const PullRequestReactionInput = Schema.Struct({
837 ...PullRequestRef.fields,
838 subjectId: Schema.optional(TrimmedNonEmptyString),
839 content: PullRequestReactionContent,
840 reacted: Schema.Boolean,
841});
842export type PullRequestReactionInput = typeof PullRequestReactionInput.Type;
843
844export const PullRequestReviewerRequestInput = Schema.Struct({
845 ...PullRequestRef.fields,
846 reviewers: Schema.Array(
847 Schema.Struct({ id: TrimmedNonEmptyString, kind: PullRequestReviewerKind }),
848 ).check(Schema.isMinLength(1), Schema.isMaxLength(25)),
849 requested: Schema.Boolean,
850});
851export type PullRequestReviewerRequestInput = typeof PullRequestReviewerRequestInput.Type;
852
853export const PullRequestLabelChangeInput = Schema.Struct({
854 ...PullRequestRef.fields,
855 labels: Schema.Array(TrimmedNonEmptyString).check(Schema.isMinLength(1), Schema.isMaxLength(25)),
856 applied: Schema.Boolean,
857});
858export type PullRequestLabelChangeInput = typeof PullRequestLabelChangeInput.Type;
859
860export const PullRequestUnavailableReason = Schema.Literals([
861 "cli-missing",
862 "cli-unauthenticated",
863 "provider-unsupported",
864]);
865export type PullRequestUnavailableReason = typeof PullRequestUnavailableReason.Type;
866
867/**
868 * The reason names keep their `cli-` prefix for wire compatibility; for GitHub and
869 * Bitbucket they mean "no credential" and "a refused credential", not a missing tool.
870 */
871const PROVIDER_REQUIREMENT = new Map<
872 string,
873 { readonly missing: string; readonly unauthenticated: string }
874>([
875 [
876 "github",
877 {
878 missing:
879 "No GitHub credential on the server. Set GH_TOKEN, or install the GitHub CLI (https://cli.github.com/) and run `gh auth login`.",
880 unauthenticated:
881 "GitHub has no working credential for this host. Run `gh auth login`, or check the account and hosts in Settings → Source Control.",
882 },
883 ],
884 [
885 "forgejo",
886 {
887 missing:
888 "Install Forgejo CLI (`fj` 0.6 or later) from https://codeberg.org/forgejo-contrib/forgejo-cli or Gitea CLI (`tea` 0.16 or later) from https://gitea.com/gitea/tea to browse Forgejo pull requests.",
889 unauthenticated:
890 "Authenticate your Forgejo or Gitea server with `fj --host <server-url> auth add-token` on the T3 Code server. If fj is missing or unconfigured for that server, use `tea login add`. A configured fj account must be repaired with fj.",
891 },
892 ],
893 [
894 "gitlab",
895 {
896 missing:
897 "GitLab CLI (`glab`) is required to browse change requests on this host. Install it from https://gitlab.com/gitlab-org/cli and reload.",
898 unauthenticated: "GitLab CLI is not authenticated. Run `glab auth login` and retry.",
899 },
900 ],
901 [
902 "azure-devops",
903 {
904 missing:
905 "Azure CLI (`az`) with the Azure DevOps extension is required. Install `az`, then run `az extension add --name azure-devops`.",
906 unauthenticated: "Azure CLI is not signed in. Run `az login` and retry.",
907 },
908 ],
909 [
910 "bitbucket",
911 {
912 missing:
913 "Bitbucket needs API credentials on the server. Add them in Settings → Source Control.",
914 unauthenticated:
915 "Bitbucket rejected the configured credentials. Check them in Settings → Source Control.",
916 },
917 ],
918]);
919
920export function pullRequestHostOf(
921 identity:
922 | {
923 readonly canonicalKey?: string | undefined;
924 readonly locator?: { readonly remoteUrl: string } | undefined;
925 }
926 | null
927 | undefined,
928 kind: SourceControlProviderKind,
929): string {
930 if (kind === "forgejo") {
931 try {
932 const remote = new URL(identity?.locator?.remoteUrl ?? "");
933 if (remote.protocol === "http:" || remote.protocol === "https:")
934 return remote.host.toLowerCase();
935 } catch {}
936 }
937 const host = identity?.canonicalKey?.split("/")[0]?.trim();
938 return host === undefined || host.length === 0 ? kind : host.toLowerCase();
939}
940
941/**
942 * `author:me` names whoever is signed in to the host being read, GitHub's `@me` spelled either
943 * way. A host's own search would answer it, but the page and the server both re-check an author
944 * against the row's login, so the name is resolved before either comparison rather than being
945 * matched as the literal login "me".
946 */
947export function resolvePullRequestAuthorFilter(
948 author: string,
949 viewer: string | null | undefined,
950): string {
951 const trimmed = author.trim();
952 if (!/^@?me$/i.test(trimmed)) return trimmed;
953 return viewer === null || viewer === undefined || viewer.trim().length === 0 ? trimmed : viewer;
954}
955
956export function pullRequestProviderRequirement(
957 provider: SourceControlProviderKind,
958 reason: PullRequestUnavailableReason,
959): string | null {
960 const requirement = PROVIDER_REQUIREMENT.get(provider);
961 if (requirement === undefined) return null;
962 switch (reason) {
963 case "cli-missing":
964 return requirement.missing;
965 case "cli-unauthenticated":
966 return requirement.unauthenticated;
967 case "provider-unsupported":
968 return null;
969 }
970}
971
972export class PullRequestUnavailableError extends Schema.TaggedError<PullRequestUnavailableError>()(
973 "PullRequestUnavailableError",
974 {
975 reason: PullRequestUnavailableReason,
976 provider: Schema.optional(SourceControlProviderKind),
977 cause: Schema.optional(Schema.Defect()),
978 },
979 { httpApiStatus: 503 },
980) {
981 [HttpServerRespondable.symbol]() {
982 return HttpServerResponse.schemaJson(PullRequestUnavailableError)(this, { status: 503 });
983 }
984
985 override get message(): string {
986 const requirement =
987 this.provider === undefined ? undefined : PROVIDER_REQUIREMENT.get(this.provider);
988 switch (this.reason) {
989 case "cli-missing":
990 return (
991 requirement?.missing ?? "The tool this host is read through is not installed or set up."
992 );
993 case "cli-unauthenticated":
994 return requirement?.unauthenticated ?? "This host has no working credentials.";
995 case "provider-unsupported":
996 return "Change requests cannot be browsed for this project's host yet.";
997 }
998 }
999}
1000
1001export class PullRequestOperationError extends Schema.TaggedError<PullRequestOperationError>()(
1002 "PullRequestOperationError",
1003 {
1004 operation: Schema.String,
1005 detail: TrimmedNonEmptyString,
1006 reason: Schema.optional(Schema.Literal("not-found")),
1007 cause: Schema.optional(Schema.Defect()),
1008 },
1009 { httpApiStatus: 502 },
1010) {
1011 [HttpServerRespondable.symbol]() {
1012 return HttpServerResponse.schemaJson(PullRequestOperationError)(this, { status: 502 });
1013 }
1014
1015 override get message(): string {
1016 return `Pull request operation ${this.operation} failed: ${this.detail}`;
1017 }
1018}