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

pullRequest.ts

992 lines36.3 KB

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