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

pullRequest.ts

991 lines36.2 KB

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