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});
542export type PullRequestReportStateInput = typeof PullRequestReportStateInput.Type;
543
544export const PullRequestDetail = Schema.Struct({
545 provider: SourceControlProviderKind,
546 capabilities: PullRequestCapabilities,
547 viewerPermissions: PullRequestViewerPermissions,
548 projectId: ProjectId,
549 projectTitle: TrimmedNonEmptyString,
550 workspaceRoot: TrimmedNonEmptyString,
551 repository: TrimmedNonEmptyString,
552 number: PositiveInt,
553 title: TrimmedNonEmptyString,
554 body: Schema.String,
555 url: TrimmedNonEmptyString,
556 author: Schema.NullOr(PullRequestActor),
557 state: PullRequestState,
558 isDraft: Schema.Boolean,
559 mergeability: PullRequestMergeability,
560 additions: NonNegativeInt,
561 deletions: NonNegativeInt,
562 changedFiles: NonNegativeInt,
563 headBranch: TrimmedNonEmptyString,
564 headRepositoryNameWithOwner: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
565 headSha: Schema.optional(TrimmedNonEmptyString),
566 baseBranch: TrimmedNonEmptyString,
567 createdAt: IsoDateTime,
568 updatedAt: IsoDateTime,
569 observedAt: Schema.optional(Schema.Finite),
570 mergedAt: Schema.NullOr(IsoDateTime),
571 closedAt: Schema.NullOr(IsoDateTime),
572 reviewers: Schema.Array(PullRequestActor),
573 labels: Schema.Array(PullRequestLabel),
574 checks: Schema.Array(PullRequestCheck),
575 mergeCapabilities: PullRequestMergeCapabilities,
576 viewer: Schema.optional(TrimmedNonEmptyString),
577 baseComparison: Schema.optional(PullRequestBaseComparison),
578 behindBy: Schema.optional(NonNegativeInt),
579 /**
580 * Whether the host is already armed to merge this on its own. Absent where the host does not
581 * report it, which is not the same as off: a page that reads silence as "not armed" offers to
582 * arm something that is already armed, and a second arming is a write nobody asked for.
583 */
584 autoMergeEnabled: Schema.optional(Schema.Boolean),
585 autoMergeMethod: Schema.optional(PullRequestMergeMethod),
586 workflowApprovalsRequired: Schema.optional(NonNegativeInt),
587});
588export type PullRequestDetail = typeof PullRequestDetail.Type;
589
590export const PullRequestChecks = Schema.Struct({
591 state: PullRequestState,
592 checks: Schema.Array(PullRequestCheck),
593});
594export type PullRequestChecks = typeof PullRequestChecks.Type;
595
596export const PullRequestActivity = Schema.Struct({
597 author: Schema.optional(Schema.NullOr(PullRequestActor)),
598 reviewers: Schema.optional(Schema.Array(PullRequestActor)),
599 comments: Schema.Array(PullRequestComment),
600 commentCount: NonNegativeInt,
601 commentsTruncated: Schema.Boolean,
602 reviewThreadsTruncated: Schema.optional(Schema.Boolean),
603 reviewThreads: Schema.Array(PullRequestReviewThread),
604 commits: Schema.Array(PullRequestCommit),
605 reactions: Schema.optional(Schema.Array(PullRequestReaction)),
606});
607export type PullRequestActivity = typeof PullRequestActivity.Type;
608
609export const PullRequestDetailView = Schema.Struct({
610 ...PullRequestDetail.fields,
611 ...PullRequestActivity.fields,
612 author: Schema.NullOr(PullRequestActor),
613 reviewers: Schema.Array(PullRequestActor),
614});
615export type PullRequestDetailView = typeof PullRequestDetailView.Type;
616
617/**
618 * A diff arrives a slice at a time, because a large change is more than any host will hand over
619 * at once — GitHub refuses outright past 300 files — and more than a viewer can lay out in one
620 * go.
621 */
622export const PullRequestDiffInput = Schema.Struct({
623 ...PullRequestRef.fields,
624 cursor: Schema.optional(TrimmedNonEmptyString),
625 commit: Schema.optional(TrimmedNonEmptyString),
626});
627export type PullRequestDiffInput = typeof PullRequestDiffInput.Type;
628
629export const PullRequestOmittedFileStat = Schema.Struct({
630 path: TrimmedNonEmptyString,
631 additions: Schema.Number,
632 deletions: Schema.Number,
633});
634export type PullRequestOmittedFileStat = typeof PullRequestOmittedFileStat.Type;
635
636export const PullRequestDiffResult = Schema.Struct({
637 patch: Schema.String,
638 /**
639 * Something inside this slice could not be shown — a binary file, or a hunk the host declined
640 * to inline. Not the same as there being more slices, which `nextCursor` answers.
641 */
642 truncated: Schema.Boolean,
643 nextCursor: Schema.NullOr(TrimmedNonEmptyString),
644 omittedFileStats: Schema.optional(Schema.Array(PullRequestOmittedFileStat)),
645});
646export type PullRequestDiffResult = typeof PullRequestDiffResult.Type;
647
648export const PullRequestDiffFileContentsInput = Schema.Struct({
649 ...PullRequestRef.fields,
650 commit: Schema.optional(TrimmedNonEmptyString),
651 changeType: Schema.Literals(["change", "rename-pure", "rename-changed", "new", "deleted"]),
652 oldPath: TrimmedNonEmptyString,
653 newPath: TrimmedNonEmptyString,
654});
655export type PullRequestDiffFileContentsInput = typeof PullRequestDiffFileContentsInput.Type;
656
657export const PullRequestDiffFileContentsResult = Schema.Struct({
658 oldContents: Schema.String,
659 newContents: Schema.String,
660});
661export type PullRequestDiffFileContentsResult = typeof PullRequestDiffFileContentsResult.Type;
662
663const MAX_FILE_PATH_LENGTH = 4096;
664/**
665 * Not trimmed: a leading or trailing space is a legal part of a file's name, and the mark is
666 * keyed by the name the host gave, so trimming files it under a name nothing else uses.
667 */
668const FilePath = Schema.String.check(Schema.isNonEmpty(), Schema.isMaxLength(MAX_FILE_PATH_LENGTH));
669
670export const PullRequestFileViewedState = Schema.Literals(["unviewed", "viewed", "dismissed"]);
671export type PullRequestFileViewedState = typeof PullRequestFileViewedState.Type;
672
673export const PullRequestFileViewed = Schema.Struct({
674 path: FilePath,
675 state: PullRequestFileViewedState,
676});
677export type PullRequestFileViewed = typeof PullRequestFileViewed.Type;
678
679export const PullRequestFilesViewedResult = Schema.Struct({
680 /** Only the files the host reported a state for. A file missing from this list is unviewed. */
681 files: Schema.Array(PullRequestFileViewed),
682 truncated: Schema.Boolean,
683});
684export type PullRequestFilesViewedResult = typeof PullRequestFilesViewedResult.Type;
685
686const PullRequestLineCount = Schema.Struct({
687 additions: NonNegativeInt,
688 deletions: NonNegativeInt,
689});
690
691/**
692 * A change's line counts split by what the changed files are, judged from their paths. Null when
693 * the host gives no per-file counts, or has more files than one read covers: a split of part of
694 * the change would not add up to the totals shown beside it.
695 */
696export const PullRequestChangeBreakdown = Schema.NullOr(
697 Schema.Struct({
698 code: PullRequestLineCount,
699 tests: PullRequestLineCount,
700 mocks: PullRequestLineCount,
701 generated: PullRequestLineCount,
702 // Servers from before the docs split send no key; their docs lines sit in `code`.
703 docs: PullRequestLineCount.pipe(
704 Schema.withDecodingDefault(Effect.succeed({ additions: 0, deletions: 0 })),
705 ),
706 }),
707);
708export type PullRequestChangeBreakdown = typeof PullRequestChangeBreakdown.Type;
709
710const MAX_FILES_VIEWED_PRESSES = 500;
711
712export const PullRequestSetFilesViewedInput = Schema.Struct({
713 ...PullRequestRef.fields,
714 files: Schema.Array(
715 Schema.Struct({
716 path: FilePath,
717 viewed: Schema.Boolean,
718 }),
719 ).check(Schema.isMaxLength(MAX_FILES_VIEWED_PRESSES)),
720});
721export type PullRequestSetFilesViewedInput = typeof PullRequestSetFilesViewedInput.Type;
722
723export const PullRequestStackHead = Schema.Struct({
724 number: PositiveInt,
725 headSha: TrimmedNonEmptyString,
726});
727export type PullRequestStackHead = typeof PullRequestStackHead.Type;
728
729export const PullRequestActionInput = Schema.Struct({
730 /** Native stack scope; only send to environments advertising pullRequestStackActions. */
731 stackNumber: Schema.optional(PositiveInt),
732 expectedStackHeads: Schema.optional(Schema.Array(PullRequestStackHead)),
733 ...PullRequestRef.fields,
734 action: PullRequestAction,
735 mergeMethod: Schema.optional(PullRequestMergeMethod),
736 updateMethod: Schema.optional(PullRequestUpdateMethod),
737});
738export type PullRequestActionInput = typeof PullRequestActionInput.Type;
739
740// Not trimmed: the body is markdown, where leading spaces open a code block and two trailing
741// spaces are a line break.
742const CommentBody = Schema.String.check(Schema.isNonEmpty()).check(Schema.isMaxLength(65_536));
743
744export const PullRequestCommentInput = Schema.Struct({
745 ...PullRequestRef.fields,
746 body: CommentBody,
747});
748export type PullRequestCommentInput = typeof PullRequestCommentInput.Type;
749
750export const PullRequestUpdateInput = Schema.Struct({
751 ...PullRequestRef.fields,
752 title: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(1024))),
753 body: Schema.optional(Schema.String.check(Schema.isMaxLength(65_536))),
754});
755export type PullRequestUpdateInput = typeof PullRequestUpdateInput.Type;
756
757/**
758 * The kind travels beside the id because an id alone does not say which endpoint
759 * addresses it: GitHub rewrites a comment on the conversation and a comment on a line
760 * through two different mutations.
761 */
762export const PullRequestCommentUpdateInput = Schema.Struct({
763 ...PullRequestRef.fields,
764 commentId: TrimmedNonEmptyString,
765 kind: Schema.Literals(["issue-comment", "review-comment"]),
766 body: CommentBody,
767});
768export type PullRequestCommentUpdateInput = typeof PullRequestCommentUpdateInput.Type;
769
770export const PullRequestReviewPosition = Schema.Union([
771 Schema.Struct({
772 kind: Schema.Literal("added"),
773 newLine: PositiveInt,
774 }),
775 Schema.Struct({
776 kind: Schema.Literal("deleted"),
777 oldLine: PositiveInt,
778 }),
779 Schema.Struct({
780 kind: Schema.Literal("context"),
781 oldLine: PositiveInt,
782 newLine: PositiveInt,
783 side: PullRequestDiffSide,
784 }),
785]);
786export type PullRequestReviewPosition = typeof PullRequestReviewPosition.Type;
787
788export const PullRequestReviewCommentDraft = Schema.Struct({
789 path: TrimmedNonEmptyString,
790 /**
791 * GitLab resolves a position against both sides of the diff, so a comment on a renamed file
792 * needs both names; the hosts that address a comment by one path ignore this.
793 */
794 oldPath: Schema.optional(TrimmedNonEmptyString),
795 position: PullRequestReviewPosition,
796 body: CommentBody,
797});
798export type PullRequestReviewCommentDraft = typeof PullRequestReviewCommentDraft.Type;
799
800export const PullRequestSubmitReviewInput = Schema.Struct({
801 ...PullRequestRef.fields,
802 verdict: PullRequestReviewVerdict,
803 body: Schema.String.check(Schema.isMaxLength(65_536)),
804 comments: Schema.Array(PullRequestReviewCommentDraft),
805});
806export type PullRequestSubmitReviewInput = typeof PullRequestSubmitReviewInput.Type;
807
808export const PullRequestThreadCommentsInput = Schema.Struct({
809 ...PullRequestRef.fields,
810 threadId: TrimmedNonEmptyString,
811 cursor: TrimmedNonEmptyString,
812});
813export type PullRequestThreadCommentsInput = typeof PullRequestThreadCommentsInput.Type;
814
815export const PullRequestThreadCommentsResult = Schema.Struct({
816 comments: Schema.Array(PullRequestThreadComment),
817 nextCursor: Schema.NullOr(TrimmedNonEmptyString),
818});
819export type PullRequestThreadCommentsResult = typeof PullRequestThreadCommentsResult.Type;
820
821export const PullRequestThreadReplyInput = Schema.Struct({
822 ...PullRequestRef.fields,
823 threadId: TrimmedNonEmptyString,
824 body: CommentBody,
825});
826export type PullRequestThreadReplyInput = typeof PullRequestThreadReplyInput.Type;
827
828export const PullRequestThreadResolutionInput = Schema.Struct({
829 ...PullRequestRef.fields,
830 threadId: TrimmedNonEmptyString,
831 resolved: Schema.Boolean,
832});
833export type PullRequestThreadResolutionInput = typeof PullRequestThreadResolutionInput.Type;
834
835export const PullRequestReactionInput = Schema.Struct({
836 ...PullRequestRef.fields,
837 subjectId: Schema.optional(TrimmedNonEmptyString),
838 content: PullRequestReactionContent,
839 reacted: Schema.Boolean,
840});
841export type PullRequestReactionInput = typeof PullRequestReactionInput.Type;
842
843export const PullRequestReviewerRequestInput = Schema.Struct({
844 ...PullRequestRef.fields,
845 reviewers: Schema.Array(
846 Schema.Struct({ id: TrimmedNonEmptyString, kind: PullRequestReviewerKind }),
847 ).check(Schema.isMinLength(1), Schema.isMaxLength(25)),
848 requested: Schema.Boolean,
849});
850export type PullRequestReviewerRequestInput = typeof PullRequestReviewerRequestInput.Type;
851
852export const PullRequestLabelChangeInput = Schema.Struct({
853 ...PullRequestRef.fields,
854 labels: Schema.Array(TrimmedNonEmptyString).check(Schema.isMinLength(1), Schema.isMaxLength(25)),
855 applied: Schema.Boolean,
856});
857export type PullRequestLabelChangeInput = typeof PullRequestLabelChangeInput.Type;
858
859export const PullRequestUnavailableReason = Schema.Literals([
860 "cli-missing",
861 "cli-unauthenticated",
862 "provider-unsupported",
863]);
864export type PullRequestUnavailableReason = typeof PullRequestUnavailableReason.Type;
865
866/**
867 * The reason names keep their `cli-` prefix for wire compatibility; for GitHub and
868 * Bitbucket they mean "no credential" and "a refused credential", not a missing tool.
869 */
870const PROVIDER_REQUIREMENT = new Map<
871 string,
872 { readonly missing: string; readonly unauthenticated: string }
873>([
874 [
875 "github",
876 {
877 missing:
878 "No GitHub credential on the server. Set GH_TOKEN, or install the GitHub CLI (https://cli.github.com/) and run `gh auth login`.",
879 unauthenticated:
880 "GitHub has no working credential for this host. Run `gh auth login`, or check the account and hosts in Settings → Source Control.",
881 },
882 ],
883 [
884 "forgejo",
885 {
886 missing:
887 "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.",
888 unauthenticated:
889 "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.",
890 },
891 ],
892 [
893 "gitlab",
894 {
895 missing:
896 "GitLab CLI (`glab`) is required to browse change requests on this host. Install it from https://gitlab.com/gitlab-org/cli and reload.",
897 unauthenticated: "GitLab CLI is not authenticated. Run `glab auth login` and retry.",
898 },
899 ],
900 [
901 "azure-devops",
902 {
903 missing:
904 "Azure CLI (`az`) with the Azure DevOps extension is required. Install `az`, then run `az extension add --name azure-devops`.",
905 unauthenticated: "Azure CLI is not signed in. Run `az login` and retry.",
906 },
907 ],
908 [
909 "bitbucket",
910 {
911 missing:
912 "Bitbucket needs API credentials on the server. Add them in Settings → Source Control.",
913 unauthenticated:
914 "Bitbucket rejected the configured credentials. Check them in Settings → Source Control.",
915 },
916 ],
917]);
918
919export function pullRequestHostOf(
920 identity:
921 | {
922 readonly canonicalKey?: string | undefined;
923 readonly locator?: { readonly remoteUrl: string } | undefined;
924 }
925 | null
926 | undefined,
927 kind: SourceControlProviderKind,
928): string {
929 if (kind === "forgejo") {
930 try {
931 const remote = new URL(identity?.locator?.remoteUrl ?? "");
932 if (remote.protocol === "http:" || remote.protocol === "https:")
933 return remote.host.toLowerCase();
934 } catch {}
935 }
936 const host = identity?.canonicalKey?.split("/")[0]?.trim();
937 return host === undefined || host.length === 0 ? kind : host.toLowerCase();
938}
939
940/**
941 * `author:me` names whoever is signed in to the host being read, GitHub's `@me` spelled either
942 * way. A host's own search would answer it, but the page and the server both re-check an author
943 * against the row's login, so the name is resolved before either comparison rather than being
944 * matched as the literal login "me".
945 */
946export function resolvePullRequestAuthorFilter(
947 author: string,
948 viewer: string | null | undefined,
949): string {
950 const trimmed = author.trim();
951 if (!/^@?me$/i.test(trimmed)) return trimmed;
952 return viewer === null || viewer === undefined || viewer.trim().length === 0 ? trimmed : viewer;
953}
954
955export function pullRequestProviderRequirement(
956 provider: SourceControlProviderKind,
957 reason: PullRequestUnavailableReason,
958): string | null {
959 const requirement = PROVIDER_REQUIREMENT.get(provider);
960 if (requirement === undefined) return null;
961 switch (reason) {
962 case "cli-missing":
963 return requirement.missing;
964 case "cli-unauthenticated":
965 return requirement.unauthenticated;
966 case "provider-unsupported":
967 return null;
968 }
969}
970
971export class PullRequestUnavailableError extends Schema.TaggedError<PullRequestUnavailableError>()(
972 "PullRequestUnavailableError",
973 {
974 reason: PullRequestUnavailableReason,
975 provider: Schema.optional(SourceControlProviderKind),
976 cause: Schema.optional(Schema.Defect()),
977 },
978 { httpApiStatus: 503 },
979) {
980 [HttpServerRespondable.symbol]() {
981 return HttpServerResponse.schemaJson(PullRequestUnavailableError)(this, { status: 503 });
982 }
983
984 override get message(): string {
985 const requirement =
986 this.provider === undefined ? undefined : PROVIDER_REQUIREMENT.get(this.provider);
987 switch (this.reason) {
988 case "cli-missing":
989 return (
990 requirement?.missing ?? "The tool this host is read through is not installed or set up."
991 );
992 case "cli-unauthenticated":
993 return requirement?.unauthenticated ?? "This host has no working credentials.";
994 case "provider-unsupported":
995 return "Change requests cannot be browsed for this project's host yet.";
996 }
997 }
998}
999
1000export class PullRequestOperationError extends Schema.TaggedError<PullRequestOperationError>()(
1001 "PullRequestOperationError",
1002 {
1003 operation: Schema.String,
1004 detail: TrimmedNonEmptyString,
1005 reason: Schema.optional(Schema.Literal("not-found")),
1006 cause: Schema.optional(Schema.Defect()),
1007 },
1008 { httpApiStatus: 502 },
1009) {
1010 [HttpServerRespondable.symbol]() {
1011 return HttpServerResponse.schemaJson(PullRequestOperationError)(this, { status: 502 });
1012 }
1013
1014 override get message(): string {
1015 return `Pull request operation ${this.operation} failed: ${this.detail}`;
1016 }
1017}