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

pullRequest.ts

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