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

pullRequest.ts

1,019 lines37.3 KB

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