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

pullRequest.ts

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