Open History to see the last commit
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 GitRefName,
8 ForwardCompatibleArray,
9 IsoDateTime,
10 NonNegativeInt,
11 PositiveInt,
12 ProjectId,
13 ThreadId,
14 TrimmedNonEmptyString,
15} from "./baseSchemas.ts";
16import { SourceControlProviderKind } from "./sourceControl.ts";
17
18export const PullRequestInvolvement = Schema.Literals(["all", "reviewing", "authored"]);
19export type PullRequestInvolvement = typeof PullRequestInvolvement.Type;
20
21export const PullRequestState = Schema.Literals(["open", "closed", "merged"]);
22export type PullRequestState = typeof PullRequestState.Type;
23
24export const PullRequestListState = Schema.Literals(["all", "open", "closed", "merged"]);
25export type PullRequestListState = typeof PullRequestListState.Type;
26
27export const PullRequestReviewDecision = Schema.Literals([
28 "approved",
29 "changes-requested",
30 "review-required",
31]);
32export type PullRequestReviewDecision = typeof PullRequestReviewDecision.Type;
33
34const PullRequestQualifierValue = TrimmedNonEmptyString.check(Schema.isMaxLength(200));
35const PullRequestQualifierValues = Schema.Array(PullRequestQualifierValue).check(
36 Schema.isMaxLength(10),
37);
38
39export const PullRequestListFilters = Schema.Struct({
40 draft: Schema.optional(Schema.Literals(["only", "hide"])),
41 review: Schema.optional(
42 Schema.Literals(["approved", "changes-requested", "review-required", "none"]),
43 ),
44 checks: Schema.optional(Schema.Literals(["passing", "failing"])),
45 /** GitHub label qualifiers: every group must match; any name within a group suffices. */
46 labels: Schema.optional(Schema.Array(PullRequestQualifierValues).check(Schema.isMaxLength(10))),
47 excludedLabels: Schema.optional(PullRequestQualifierValues),
48 author: Schema.optional(PullRequestQualifierValue),
49});
50export type PullRequestListFilters = typeof PullRequestListFilters.Type;
51
52export const PullRequestChecksState = Schema.Literals(["passing", "failing", "pending"]);
53export type PullRequestChecksState = typeof PullRequestChecksState.Type;
54
55export const PullRequestMergeability = Schema.Literals(["mergeable", "conflicting", "unknown"]);
56export type PullRequestMergeability = typeof PullRequestMergeability.Type;
57
58/** `fast-forward` refuses unless the head contains the base, where `rebase` rewrites the branch onto it. */
59export const PullRequestMergeMethod = Schema.Literals([
60 "merge",
61 "squash",
62 "rebase",
63 "fast-forward",
64]);
65export type PullRequestMergeMethod = typeof PullRequestMergeMethod.Type;
66
67export const PullRequestAction = Schema.Literals([
68 "merge",
69 "ready",
70 "draft",
71 "close",
72 "reopen",
73 "delete-branch",
74 "update-branch",
75 "enable-auto-merge",
76 "disable-auto-merge",
77 "revert",
78 "approve-workflows",
79]);
80export type PullRequestAction = typeof PullRequestAction.Type;
81
82export const PullRequestUpdateMethod = Schema.Literals(["merge", "rebase"]);
83export type PullRequestUpdateMethod = typeof PullRequestUpdateMethod.Type;
84
85export const PullRequestBaseComparison = Schema.Literals(["up-to-date", "behind", "unknown"]);
86export type PullRequestBaseComparison = typeof PullRequestBaseComparison.Type;
87
88export const PullRequestActor = Schema.Struct({
89 isBot: Schema.optional(Schema.Boolean),
90 login: TrimmedNonEmptyString,
91 name: Schema.NullOr(Schema.String),
92 avatarUrl: Schema.NullOr(Schema.String),
93});
94export type PullRequestActor = typeof PullRequestActor.Type;
95
96export const PullRequestLabel = Schema.Struct({
97 name: TrimmedNonEmptyString,
98 color: Schema.NullOr(Schema.String),
99 description: Schema.optional(Schema.NullOr(Schema.String)),
100});
101export type PullRequestLabel = typeof PullRequestLabel.Type;
102
103export const PullRequestCheckStatus = Schema.Literals([
104 "pending",
105 "action-required",
106 "success",
107 "failure",
108 "skipped",
109 "neutral",
110 "cancelled",
111]);
112export type PullRequestCheckStatus = typeof PullRequestCheckStatus.Type;
113
114export const PullRequestCheck = Schema.Struct({
115 name: TrimmedNonEmptyString,
116 status: PullRequestCheckStatus,
117 description: Schema.NullOr(Schema.String),
118 url: Schema.NullOr(Schema.String),
119 required: Schema.optional(Schema.Boolean),
120 /**
121 * Not a check: it stands for ones the host could not list, such as workflow runs awaiting
122 * approval when that read fails, so nothing waits on it or counts it as passing.
123 */
124 placeholder: Schema.optional(Schema.Boolean),
125});
126export type PullRequestCheck = typeof PullRequestCheck.Type;
127
128export const PullRequestReactionContent = Schema.Literals([
129 "thumbs-up",
130 "thumbs-down",
131 "laugh",
132 "hooray",
133 "confused",
134 "heart",
135 "rocket",
136 "eyes",
137]);
138export type PullRequestReactionContent = typeof PullRequestReactionContent.Type;
139
140export const PullRequestReaction = Schema.Struct({
141 content: PullRequestReactionContent,
142 count: PositiveInt,
143 /** Excludes the viewer's login when viewerHasReacted; count still includes them. */
144 actors: Schema.Array(TrimmedNonEmptyString),
145 viewerHasReacted: Schema.Boolean,
146});
147export type PullRequestReaction = typeof PullRequestReaction.Type;
148
149export const PullRequestCommentKind = Schema.Literals([
150 "issue-comment",
151 "review-comment",
152 "review",
153]);
154export type PullRequestCommentKind = typeof PullRequestCommentKind.Type;
155
156export const PullRequestComment = Schema.Struct({
157 id: TrimmedNonEmptyString,
158 kind: PullRequestCommentKind,
159 author: Schema.NullOr(PullRequestActor),
160 body: Schema.String,
161 createdAt: IsoDateTime,
162 editedAt: Schema.optional(Schema.NullOr(IsoDateTime)),
163 url: Schema.NullOr(Schema.String),
164 path: Schema.NullOr(Schema.String),
165 reviewState: Schema.NullOr(Schema.String),
166 reactions: Schema.optional(Schema.Array(PullRequestReaction)),
167 canEdit: Schema.optional(Schema.Boolean),
168});
169export type PullRequestComment = typeof PullRequestComment.Type;
170
171export const PullRequestDiffSide = Schema.Literals(["left", "right"]);
172export type PullRequestDiffSide = typeof PullRequestDiffSide.Type;
173
174export const PullRequestReviewVerdict = Schema.Literals(["comment", "approve", "request-changes"]);
175export type PullRequestReviewVerdict = typeof PullRequestReviewVerdict.Type;
176
177export const PullRequestThreadComment = Schema.Struct({
178 id: TrimmedNonEmptyString,
179 author: Schema.NullOr(PullRequestActor),
180 body: Schema.String,
181 createdAt: IsoDateTime,
182 editedAt: Schema.optional(Schema.NullOr(IsoDateTime)),
183 url: Schema.NullOr(Schema.String),
184 /** The review that posted the comment, so a link to a review with no text finds its comments. */
185 reviewUrl: Schema.optional(Schema.NullOr(Schema.String)),
186 reactions: Schema.optional(Schema.Array(PullRequestReaction)),
187 canEdit: Schema.optional(Schema.Boolean),
188});
189export type PullRequestThreadComment = typeof PullRequestThreadComment.Type;
190
191export const PullRequestReviewThread = Schema.Struct({
192 id: TrimmedNonEmptyString,
193 path: TrimmedNonEmptyString,
194 line: Schema.NullOr(PositiveInt),
195 side: PullRequestDiffSide,
196 isResolved: Schema.Boolean,
197 isOutdated: Schema.Boolean,
198 comments: Schema.Array(PullRequestThreadComment),
199 commentCount: Schema.optional(NonNegativeInt),
200 nextCommentsCursor: Schema.optional(TrimmedNonEmptyString),
201 /** Can only narrow repository-wide resolve permission. Absent when unreported. */
202 canResolve: Schema.optional(Schema.Boolean),
203});
204export type PullRequestReviewThread = typeof PullRequestReviewThread.Type;
205
206/** A thread on the change request as a whole, such as a GitLab overview thread. */
207export const PullRequestOverviewThread = Schema.Struct({
208 ...PullRequestReviewThread.fields,
209 path: Schema.Null,
210 line: Schema.Null,
211});
212export type PullRequestOverviewThread = typeof PullRequestOverviewThread.Type;
213
214export type PullRequestConversationThread = PullRequestReviewThread | PullRequestOverviewThread;
215
216export const PullRequestReviewerKind = Schema.Literals(["user", "team"]);
217export type PullRequestReviewerKind = typeof PullRequestReviewerKind.Type;
218
219export const PullRequestReviewerCandidate = Schema.Struct({
220 ...PullRequestActor.fields,
221 id: TrimmedNonEmptyString,
222 kind: PullRequestReviewerKind,
223 isRequested: Schema.Boolean,
224});
225export type PullRequestReviewerCandidate = typeof PullRequestReviewerCandidate.Type;
226
227export const PullRequestReviewerCandidateList = Schema.Struct({
228 candidates: Schema.Array(PullRequestReviewerCandidate),
229 truncated: Schema.Boolean,
230});
231export type PullRequestReviewerCandidateList = typeof PullRequestReviewerCandidateList.Type;
232
233export const PullRequestLabelCandidate = Schema.Struct({
234 ...PullRequestLabel.fields,
235 description: Schema.NullOr(Schema.String),
236 isApplied: Schema.Boolean,
237});
238export type PullRequestLabelCandidate = typeof PullRequestLabelCandidate.Type;
239
240export const PullRequestLabelCandidateList = Schema.Struct({
241 candidates: Schema.Array(PullRequestLabelCandidate),
242 truncated: Schema.Boolean,
243});
244export type PullRequestLabelCandidateList = typeof PullRequestLabelCandidateList.Type;
245
246export const PullRequestCommit = Schema.Struct({
247 oid: TrimmedNonEmptyString,
248 messageHeadline: Schema.String,
249 committedDate: IsoDateTime,
250 additions: Schema.optional(NonNegativeInt),
251 deletions: Schema.optional(NonNegativeInt),
252 authors: Schema.optional(Schema.Array(PullRequestActor)),
253});
254export type PullRequestCommit = typeof PullRequestCommit.Type;
255
256export const PullRequestReviewCapabilities = Schema.Struct({
257 inlineComment: Schema.Boolean,
258 /**
259 * An inline comment can cover a range of lines rather than its last one. Absent, as from a
260 * server that predates it, means one line.
261 */
262 rangeComment: Schema.optional(Schema.Boolean),
263 reply: Schema.Boolean,
264 resolve: Schema.Boolean,
265 verdicts: ForwardCompatibleArray(PullRequestReviewVerdict),
266});
267export type PullRequestReviewCapabilities = typeof PullRequestReviewCapabilities.Type;
268
269export const PullRequestEditCapabilities = Schema.Struct({
270 changeRequest: Schema.Boolean,
271 comment: Schema.Boolean,
272 maintainerEdits: Schema.optional(Schema.Boolean),
273});
274export type PullRequestEditCapabilities = typeof PullRequestEditCapabilities.Type;
275
276export const PullRequestReviewerCapabilities = Schema.Struct({
277 request: Schema.Boolean,
278 listCandidates: Schema.Boolean,
279 /**
280 * Reviewer candidates take a `query` the host matches past the first page. Absent, as from a
281 * server that predates it, means the client narrows the loaded page itself.
282 */
283 searchCandidates: Schema.optional(Schema.Boolean),
284});
285export type PullRequestReviewerCapabilities = typeof PullRequestReviewerCapabilities.Type;
286
287export const PullRequestViewedFilesStore = Schema.Literals(["host", "environment"]);
288export type PullRequestViewedFilesStore = typeof PullRequestViewedFilesStore.Type;
289
290/** `#` references issues (and GitHub pull requests); `!` references GitLab merge requests. */
291export const PullRequestReferenceSigil = Schema.Literals(["#", "!"]);
292export type PullRequestReferenceSigil = typeof PullRequestReferenceSigil.Type;
293
294export const PullRequestCapabilities = Schema.Struct({
295 diff: Schema.Boolean,
296 comment: Schema.Boolean,
297 actions: ForwardCompatibleArray(PullRequestAction),
298 mergeMethods: ForwardCompatibleArray(PullRequestMergeMethod),
299 deleteBranch: Schema.optional(Schema.Boolean),
300 /**
301 * Absent where it cannot at all, which is every host that has not said otherwise — so a
302 * provider that says nothing offers nothing.
303 */
304 updateMethods: Schema.optional(ForwardCompatibleArray(PullRequestUpdateMethod)),
305 search: Schema.Boolean,
306 reactions: Schema.optional(Schema.Boolean),
307 viewedFiles: Schema.optional(PullRequestViewedFilesStore),
308 review: PullRequestReviewCapabilities,
309 reviewers: PullRequestReviewerCapabilities,
310 edit: Schema.optional(PullRequestEditCapabilities),
311 stacks: Schema.optional(Schema.Boolean),
312 stackActions: Schema.optional(Schema.Boolean),
313 /** Stack actions also arm and cancel auto-merge for a layer and the open layers below it. */
314 stackAutoMerge: Schema.optional(Schema.Boolean),
315 labels: Schema.optional(Schema.Boolean),
316 /** The host searches the repository's users, so composers can complete `@login`. */
317 mentions: Schema.optional(Schema.Boolean),
318 /** The sigils whose issue and change request references composers can complete. */
319 referenceSigils: Schema.optional(ForwardCompatibleArray(PullRequestReferenceSigil)),
320 /**
321 * Every head gets at least one check, so a head that has gone a while without any means none
322 * reported rather than that the repository runs none. Absent means an empty list says nothing.
323 */
324 checksAlwaysReport: Schema.optional(Schema.Boolean),
325 /** The diff can read one file the paged diff withheld as too large (`PullRequestDiffInput.path`). */
326 withheldFileDiff: Schema.optional(Schema.Boolean),
327});
328export type PullRequestCapabilities = typeof PullRequestCapabilities.Type;
329
330/** Unreported host permissions are granted. */
331export const PullRequestViewerPermissions = Schema.Struct({
332 stackRebase: Schema.optional(Schema.Boolean),
333 actions: ForwardCompatibleArray(PullRequestAction),
334 comment: Schema.Boolean,
335 resolve: Schema.Boolean,
336 verdicts: ForwardCompatibleArray(PullRequestReviewVerdict),
337 requestReviewers: Schema.Boolean,
338 updateMethods: Schema.optional(ForwardCompatibleArray(PullRequestUpdateMethod)),
339 labels: Schema.optional(Schema.Boolean),
340 editChangeRequest: Schema.optional(Schema.Boolean),
341 maintainerEdits: Schema.optional(Schema.Boolean),
342});
343export type PullRequestViewerPermissions = typeof PullRequestViewerPermissions.Type;
344
345export const PullRequestMergeCapabilities = Schema.Struct({
346 merge: Schema.Boolean,
347 squash: Schema.Boolean,
348 rebase: Schema.Boolean,
349 /** Optional so servers that predate it still decode; absent means not allowed. */
350 "fast-forward": Schema.optional(Schema.Boolean),
351});
352export type PullRequestMergeCapabilities = typeof PullRequestMergeCapabilities.Type;
353
354export const PullRequestStackMembership = Schema.Struct({
355 number: PositiveInt,
356 position: PositiveInt,
357 size: PositiveInt,
358 base: GitRefName,
359});
360export type PullRequestStackMembership = typeof PullRequestStackMembership.Type;
361
362/** Who can read a pull request on a host that can hide one in a public repository. */
363export const PullRequestVisibility = Schema.Literals(["public", "private"]);
364export type PullRequestVisibility = typeof PullRequestVisibility.Type;
365
366export const PullRequestListEntry = Schema.Struct({
367 stack: Schema.optional(PullRequestStackMembership),
368 provider: SourceControlProviderKind,
369 host: TrimmedNonEmptyString,
370 projectId: ProjectId,
371 projectTitle: TrimmedNonEmptyString,
372 repository: TrimmedNonEmptyString,
373 number: PositiveInt,
374 title: TrimmedNonEmptyString,
375 url: TrimmedNonEmptyString,
376 author: Schema.NullOr(PullRequestActor),
377 headBranch: GitRefName,
378 baseBranch: GitRefName,
379 state: PullRequestState,
380 isDraft: Schema.Boolean,
381 mergeability: PullRequestMergeability,
382 rebaseRequired: Schema.optional(Schema.Boolean),
383 /** Zero until fetched through `PullRequestListStatsInput`; GitHub listings omit counts. */
384 additions: NonNegativeInt,
385 deletions: NonNegativeInt,
386 createdAt: IsoDateTime,
387 updatedAt: IsoDateTime,
388 observedAt: Schema.optional(Schema.Finite),
389 viewerReviewRequested: Schema.Boolean,
390 labels: Schema.Array(PullRequestLabel),
391 reviewDecision: Schema.optional(PullRequestReviewDecision),
392 checksState: Schema.optional(PullRequestChecksState),
393 /** Absent where the host has no private pull requests or names no visibility. */
394 visibility: Schema.optional(PullRequestVisibility),
395});
396export type PullRequestListEntry = typeof PullRequestListEntry.Type;
397
398export const PullRequestListCursors = Schema.Record(
399 TrimmedNonEmptyString,
400 TrimmedNonEmptyString.check(Schema.isMaxLength(4096)),
401);
402export type PullRequestListCursors = typeof PullRequestListCursors.Type;
403
404export const PULL_REQUEST_LIST_PROJECT_LIMIT = 100;
405
406export const PullRequestListInput = Schema.Struct({
407 state: PullRequestListState,
408 involvement: Schema.optional(PullRequestInvolvement),
409 filters: Schema.optional(PullRequestListFilters),
410 projectId: Schema.optional(ProjectId),
411 projectIds: Schema.optional(
412 Schema.Array(ProjectId).check(Schema.isMaxLength(PULL_REQUEST_LIST_PROJECT_LIMIT)),
413 ),
414 host: Schema.optional(TrimmedNonEmptyString),
415 limit: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 500 }))),
416 cursors: Schema.optional(PullRequestListCursors),
417 query: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(200))),
418});
419export type PullRequestListInput = typeof PullRequestListInput.Type;
420
421export const PullRequestProviderSummary = Schema.Struct({
422 host: TrimmedNonEmptyString,
423 kind: SourceControlProviderKind,
424 searchesOnHost: Schema.Boolean,
425 actions: Schema.optional(ForwardCompatibleArray(PullRequestAction)),
426 projectCount: PositiveInt,
427 /** SSH checkouts can resolve to an HTTP authority different from their stored identity. */
428 projectIds: Schema.optional(Schema.Array(ProjectId)),
429 configured: Schema.Boolean,
430 detail: Schema.NullOr(TrimmedNonEmptyString),
431});
432export type PullRequestProviderSummary = typeof PullRequestProviderSummary.Type;
433
434export const PullRequestListProjectError = Schema.Struct({
435 projectId: ProjectId,
436 projectTitle: TrimmedNonEmptyString,
437 message: TrimmedNonEmptyString,
438});
439export type PullRequestListProjectError = typeof PullRequestListProjectError.Type;
440
441export const PullRequestListResult = Schema.Struct({
442 /** Unreadable hosts are absent; JSON codecs reject optional record values. */
443 viewers: Schema.Record(TrimmedNonEmptyString, TrimmedNonEmptyString),
444 providers: Schema.Array(PullRequestProviderSummary),
445 /** Re-sort after appending: one repository's older slice may outrank another's existing rows. */
446 entries: Schema.Array(PullRequestListEntry),
447 errors: Schema.Array(PullRequestListProjectError),
448 truncated: Schema.Boolean,
449 nextCursors: PullRequestListCursors,
450});
451export type PullRequestListResult = typeof PullRequestListResult.Type;
452
453export const PullRequestRef = Schema.Struct({
454 projectId: ProjectId,
455 host: Schema.optional(TrimmedNonEmptyString),
456 expectedAccountId: Schema.optional(TrimmedNonEmptyString),
457 allowStale: Schema.optional(Schema.Boolean),
458 repository: TrimmedNonEmptyString,
459 number: PositiveInt,
460});
461export type PullRequestRef = typeof PullRequestRef.Type;
462
463/** `query` asks the host for matches past the first page of candidates. */
464export const PullRequestReviewerCandidatesInput = Schema.Struct({
465 ...PullRequestRef.fields,
466 query: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(100))),
467});
468export type PullRequestReviewerCandidatesInput = typeof PullRequestReviewerCandidatesInput.Type;
469
470export const PullRequestRoutingIdentityInput = Schema.Struct({
471 host: TrimmedNonEmptyString,
472});
473export type PullRequestRoutingIdentityInput = typeof PullRequestRoutingIdentityInput.Type;
474
475export const PullRequestRoutingIdentityResult = Schema.Struct({
476 accountId: TrimmedNonEmptyString,
477 host: TrimmedNonEmptyString,
478 provider: SourceControlProviderKind,
479 viewer: TrimmedNonEmptyString,
480});
481export type PullRequestRoutingIdentityResult = typeof PullRequestRoutingIdentityResult.Type;
482
483export const PullRequestRoutingResult = Schema.Struct({
484 accountId: TrimmedNonEmptyString,
485 host: TrimmedNonEmptyString,
486 provider: SourceControlProviderKind,
487 viewer: TrimmedNonEmptyString,
488 projectTitle: TrimmedNonEmptyString,
489 workspaceRoot: TrimmedNonEmptyString,
490});
491export type PullRequestRoutingResult = typeof PullRequestRoutingResult.Type;
492
493export const PullRequestLinkedThreadsResult = Schema.Struct({
494 threads: Schema.Array(
495 Schema.Struct({
496 id: ThreadId,
497 projectId: ProjectId,
498 title: Schema.String,
499 archivedAt: Schema.NullOr(IsoDateTime),
500 settled: Schema.optionalKey(Schema.Boolean),
501 }),
502 ),
503});
504export type PullRequestLinkedThreadsResult = typeof PullRequestLinkedThreadsResult.Type;
505
506export const PullRequestPreview = Schema.Struct({
507 projectId: ProjectId,
508 repository: TrimmedNonEmptyString,
509 number: PositiveInt,
510 title: TrimmedNonEmptyString,
511 url: TrimmedNonEmptyString,
512 author: Schema.NullOr(PullRequestActor),
513 state: PullRequestState,
514 isDraft: Schema.Boolean,
515 createdAt: IsoDateTime,
516 updatedAt: Schema.optional(IsoDateTime),
517 observedAt: Schema.optional(Schema.Finite),
518});
519export type PullRequestPreview = typeof PullRequestPreview.Type;
520
521export const PullRequestSummary = Schema.Struct({
522 provider: SourceControlProviderKind,
523 projectId: ProjectId,
524 repository: TrimmedNonEmptyString,
525 number: PositiveInt,
526 title: TrimmedNonEmptyString,
527 url: TrimmedNonEmptyString,
528 state: PullRequestState,
529 isDraft: Schema.optional(Schema.Boolean),
530 headBranch: GitRefName,
531 baseBranch: GitRefName,
532 closedAt: Schema.optional(Schema.NullOr(Schema.String)),
533 mergedAt: Schema.optional(Schema.NullOr(Schema.String)),
534 updatedAt: IsoDateTime,
535 observedAt: Schema.optional(Schema.Finite),
536 author: Schema.optional(Schema.NullOr(PullRequestActor)),
537 additions: Schema.optional(NonNegativeInt),
538 deletions: Schema.optional(NonNegativeInt),
539 changedFiles: Schema.optional(NonNegativeInt),
540 reviewDecision: Schema.optional(Schema.NullOr(PullRequestReviewDecision)),
541 checksState: Schema.optional(Schema.NullOr(PullRequestChecksState)),
542 mergeability: Schema.optional(PullRequestMergeability),
543 rebaseRequired: Schema.optional(Schema.Boolean),
544 /** Null when the host says the pull request is in no stack; absent when the read did not ask. */
545 stack: Schema.optional(Schema.NullOr(PullRequestStackMembership)),
546 headSha: Schema.optional(TrimmedNonEmptyString),
547 /** Absent means the read did not report auto-merge state, not that it is off. */
548 autoMergeEnabled: Schema.optional(Schema.Boolean),
549 autoMergeMethod: Schema.optional(PullRequestMergeMethod),
550});
551export type PullRequestSummary = typeof PullRequestSummary.Type;
552
553export const PullRequestStack = Schema.Struct({
554 id: TrimmedNonEmptyString,
555 number: PositiveInt,
556 url: TrimmedNonEmptyString,
557 base: GitRefName,
558 layers: Schema.Array(
559 Schema.Struct({
560 number: PositiveInt,
561 title: Schema.optional(Schema.String),
562 isDraft: Schema.optional(Schema.Boolean),
563 headSha: Schema.optional(TrimmedNonEmptyString),
564 headBranch: GitRefName,
565 state: PullRequestState,
566 }),
567 ),
568});
569export type PullRequestStack = typeof PullRequestStack.Type;
570
571export const PullRequestDiffStat = Schema.Struct({
572 projectId: ProjectId,
573 repository: TrimmedNonEmptyString,
574 number: PositiveInt,
575 additions: NonNegativeInt,
576 deletions: NonNegativeInt,
577});
578export type PullRequestDiffStat = typeof PullRequestDiffStat.Type;
579
580export const PullRequestListStatsInput = Schema.Struct({
581 refs: Schema.Array(PullRequestRef).check(Schema.isMaxLength(500)),
582});
583export type PullRequestListStatsInput = typeof PullRequestListStatsInput.Type;
584
585export const PullRequestListStatsResult = Schema.Struct({
586 stats: Schema.Array(PullRequestDiffStat),
587});
588export type PullRequestListStatsResult = typeof PullRequestListStatsResult.Type;
589
590export const PullRequestFilterOptionKind = Schema.Literals(["labels", "authors", "references"]);
591export type PullRequestFilterOptionKind = typeof PullRequestFilterOptionKind.Type;
592
593/** Scoped like `PullRequestListInput`, so the options come from the repositories it lists. */
594export const PullRequestFilterOptionsInput = Schema.Struct({
595 kind: PullRequestFilterOptionKind,
596 projectId: Schema.optional(ProjectId),
597 projectIds: Schema.optional(Schema.Array(ProjectId).check(Schema.isMaxLength(100))),
598 host: Schema.optional(TrimmedNonEmptyString),
599 query: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(100))),
600 /** For `references`, the sigil typed, so a host can search only what it links. */
601 sigil: Schema.optional(PullRequestReferenceSigil),
602});
603export type PullRequestFilterOptionsInput = typeof PullRequestFilterOptionsInput.Type;
604
605/** An issue or change request a composer can reference, with the text the host links. */
606export const PullRequestReferenceCandidate = Schema.Struct({
607 reference: TrimmedNonEmptyString,
608 kind: Schema.Literals(["issue", "change-request"]),
609 number: PositiveInt,
610 title: Schema.String,
611 state: Schema.Literals(["open", "closed", "merged"]),
612});
613export type PullRequestReferenceCandidate = typeof PullRequestReferenceCandidate.Type;
614
615export const PullRequestFilterOptions = Schema.Struct({
616 labels: Schema.Array(PullRequestLabel),
617 authors: Schema.Array(PullRequestActor),
618 // Servers from before reference completion send no key.
619 references: Schema.Array(PullRequestReferenceCandidate).pipe(
620 Schema.withDecodingDefault(Effect.succeed([])),
621 ),
622 truncated: Schema.Boolean,
623});
624export type PullRequestFilterOptions = typeof PullRequestFilterOptions.Type;
625
626/**
627 * A separate request rather than a flag on the reads, so an explicit "refresh" one person
628 * presses is the only thing that spends host requests — every ordinary read shares.
629 */
630export const PullRequestInvalidateInput = Schema.Struct({
631 reference: Schema.optional(PullRequestRef),
632 filesViewedOnly: Schema.optional(Schema.Boolean),
633});
634export type PullRequestInvalidateInput = typeof PullRequestInvalidateInput.Type;
635
636export const PullRequestReportStateInput = Schema.Struct({
637 reference: PullRequestRef,
638 state: PullRequestState,
639 isDraft: Schema.optional(Schema.Boolean),
640});
641export type PullRequestReportStateInput = typeof PullRequestReportStateInput.Type;
642
643/** The host's merge queue letting go of a pull request without merging it, newest first. */
644export const PullRequestMergeQueueRemoval = Schema.Struct({
645 id: TrimmedNonEmptyString,
646 removedAt: IsoDateTime,
647 reason: Schema.NullOr(Schema.String),
648});
649export type PullRequestMergeQueueRemoval = typeof PullRequestMergeQueueRemoval.Type;
650
651export const PullRequestDetail = Schema.Struct({
652 provider: SourceControlProviderKind,
653 capabilities: PullRequestCapabilities,
654 viewerPermissions: PullRequestViewerPermissions,
655 projectId: ProjectId,
656 projectTitle: TrimmedNonEmptyString,
657 workspaceRoot: TrimmedNonEmptyString,
658 repository: TrimmedNonEmptyString,
659 number: PositiveInt,
660 title: TrimmedNonEmptyString,
661 body: Schema.String,
662 url: TrimmedNonEmptyString,
663 author: Schema.NullOr(PullRequestActor),
664 state: PullRequestState,
665 isDraft: Schema.Boolean,
666 mergeability: PullRequestMergeability,
667 rebaseRequired: Schema.optional(Schema.Boolean),
668 additions: NonNegativeInt,
669 deletions: NonNegativeInt,
670 changedFiles: NonNegativeInt,
671 headBranch: GitRefName,
672 headRepositoryNameWithOwner: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
673 headSha: Schema.optional(TrimmedNonEmptyString),
674 baseBranch: GitRefName,
675 createdAt: IsoDateTime,
676 updatedAt: IsoDateTime,
677 observedAt: Schema.optional(Schema.Finite),
678 mergedAt: Schema.NullOr(IsoDateTime),
679 mergeCommitSha: Schema.optional(TrimmedNonEmptyString),
680 mergedBy: Schema.optional(PullRequestActor),
681 closedAt: Schema.NullOr(IsoDateTime),
682 reviewers: Schema.Array(PullRequestActor),
683 labels: Schema.Array(PullRequestLabel),
684 checks: Schema.Array(PullRequestCheck),
685 mergeCapabilities: PullRequestMergeCapabilities,
686 viewer: Schema.optional(TrimmedNonEmptyString),
687 baseComparison: Schema.optional(PullRequestBaseComparison),
688 behindBy: Schema.optional(NonNegativeInt),
689 /** Absent means the host does not report auto-merge state, not that it is off. */
690 autoMergeEnabled: Schema.optional(Schema.Boolean),
691 autoMergeMethod: Schema.optional(PullRequestMergeMethod),
692 workflowApprovalsRequired: Schema.optional(NonNegativeInt),
693 /** Absent where the host has no merge queue or does not report one. */
694 mergeQueueRemoval: Schema.optional(PullRequestMergeQueueRemoval),
695 maintainerCanModify: Schema.optional(Schema.Boolean),
696 /** Absent where the host has no private pull requests or names no visibility. */
697 visibility: Schema.optional(PullRequestVisibility),
698});
699export type PullRequestDetail = typeof PullRequestDetail.Type;
700
701export const PullRequestChecks = Schema.Struct({
702 observedAt: Schema.optional(Schema.Finite),
703 state: PullRequestState,
704 checks: Schema.Array(PullRequestCheck),
705});
706export type PullRequestChecks = typeof PullRequestChecks.Type;
707
708export const PullRequestActivity = Schema.Struct({
709 author: Schema.optional(Schema.NullOr(PullRequestActor)),
710 reviewers: Schema.optional(Schema.Array(PullRequestActor)),
711 comments: Schema.Array(PullRequestComment),
712 commentCount: NonNegativeInt,
713 commentsTruncated: Schema.Boolean,
714 reviewThreadsTruncated: Schema.optional(Schema.Boolean),
715 reviewThreads: Schema.Array(PullRequestReviewThread),
716 // Apart from reviewThreads so clients that require a thread path still decode the activity.
717 overviewThreads: Schema.optional(Schema.Array(PullRequestOverviewThread)),
718 commits: Schema.Array(PullRequestCommit),
719 reactions: Schema.optional(Schema.Array(PullRequestReaction)),
720});
721export type PullRequestActivity = typeof PullRequestActivity.Type;
722
723export const pullRequestConversationThreads = (
724 activity: Pick<PullRequestActivity, "reviewThreads" | "overviewThreads">,
725): ReadonlyArray<PullRequestConversationThread> =>
726 activity.overviewThreads === undefined || activity.overviewThreads.length === 0
727 ? activity.reviewThreads
728 : [...activity.reviewThreads, ...activity.overviewThreads];
729
730export const PullRequestDetailView = Schema.Struct({
731 ...PullRequestDetail.fields,
732 ...PullRequestActivity.fields,
733 author: Schema.NullOr(PullRequestActor),
734 reviewers: Schema.Array(PullRequestActor),
735});
736export type PullRequestDetailView = typeof PullRequestDetailView.Type;
737
738/** GitHub refuses diffs past 300 files, so callers must page large changes. */
739export const PullRequestDiffInput = Schema.Struct({
740 ...PullRequestRef.fields,
741 cursor: Schema.optional(TrimmedNonEmptyString),
742 commit: Schema.optional(TrimmedNonEmptyString),
743 /** Reads one file the paged diff withheld as too large; ignores `cursor`. */
744 path: Schema.optional(TrimmedNonEmptyString),
745});
746export type PullRequestDiffInput = typeof PullRequestDiffInput.Type;
747
748export const PullRequestOmittedFileStat = Schema.Struct({
749 path: TrimmedNonEmptyString,
750 additions: Schema.Number,
751 deletions: Schema.Number,
752});
753export type PullRequestOmittedFileStat = typeof PullRequestOmittedFileStat.Type;
754
755export const PullRequestDiffResult = Schema.Struct({
756 patch: Schema.String,
757 /** Content omitted within this slice; nextCursor separately indicates more slices. */
758 truncated: Schema.Boolean,
759 nextCursor: Schema.NullOr(TrimmedNonEmptyString),
760 omittedFileStats: Schema.optional(Schema.Array(PullRequestOmittedFileStat)),
761 /** On the first page: files this diff withholds can be read one at a time with `path`. */
762 withheldFilesReadable: Schema.optional(Schema.Boolean),
763});
764export type PullRequestDiffResult = typeof PullRequestDiffResult.Type;
765
766export const PullRequestDiffFileContentsInput = Schema.Struct({
767 ...PullRequestRef.fields,
768 commit: Schema.optional(TrimmedNonEmptyString),
769 changeType: Schema.Literals(["change", "rename-pure", "rename-changed", "new", "deleted"]),
770 oldPath: Schema.NonEmptyString,
771 newPath: Schema.NonEmptyString,
772});
773export type PullRequestDiffFileContentsInput = typeof PullRequestDiffFileContentsInput.Type;
774
775export const PullRequestDiffFileContentsResult = Schema.Struct({
776 oldContents: Schema.String,
777 newContents: Schema.String,
778});
779export type PullRequestDiffFileContentsResult = typeof PullRequestDiffFileContentsResult.Type;
780
781const MAX_FILE_PATH_LENGTH = 4096;
782// Preserve filename whitespace so viewed marks match host paths.
783const FilePath = Schema.String.check(Schema.isNonEmpty(), Schema.isMaxLength(MAX_FILE_PATH_LENGTH));
784
785export const PullRequestFileViewedState = Schema.Literals(["unviewed", "viewed", "dismissed"]);
786export type PullRequestFileViewedState = typeof PullRequestFileViewedState.Type;
787
788export const PullRequestFileViewed = Schema.Struct({
789 path: FilePath,
790 state: PullRequestFileViewedState,
791});
792export type PullRequestFileViewed = typeof PullRequestFileViewed.Type;
793
794export const PullRequestFilesViewedResult = Schema.Struct({
795 /** Only the files the host reported a state for. A file missing from this list is unviewed. */
796 files: Schema.Array(PullRequestFileViewed),
797 truncated: Schema.Boolean,
798});
799export type PullRequestFilesViewedResult = typeof PullRequestFilesViewedResult.Type;
800
801const PullRequestLineCount = Schema.Struct({
802 additions: NonNegativeInt,
803 deletions: NonNegativeInt,
804});
805
806/**
807 * A change's line counts split by what the changed files are, judged from their paths. Null when
808 * the host gives no per-file counts, or has more files than one read covers: a split of part of
809 * the change would not add up to the totals shown beside it.
810 */
811export const PullRequestChangeBreakdown = Schema.NullOr(
812 Schema.Struct({
813 code: PullRequestLineCount,
814 tests: PullRequestLineCount,
815 mocks: PullRequestLineCount,
816 generated: PullRequestLineCount,
817 // Servers from before the docs split send no key; their docs lines sit in `code`.
818 docs: PullRequestLineCount.pipe(
819 Schema.withDecodingDefault(Effect.succeed({ additions: 0, deletions: 0 })),
820 ),
821 }),
822);
823export type PullRequestChangeBreakdown = typeof PullRequestChangeBreakdown.Type;
824
825const MAX_FILES_VIEWED_PRESSES = 500;
826
827export const PullRequestSetFilesViewedInput = Schema.Struct({
828 ...PullRequestRef.fields,
829 files: Schema.Array(
830 Schema.Struct({
831 path: FilePath,
832 viewed: Schema.Boolean,
833 /** The file's version in the diff the reader was shown; without it the head's is recorded. */
834 revision: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(256))),
835 }),
836 ).check(Schema.isMaxLength(MAX_FILES_VIEWED_PRESSES)),
837});
838export type PullRequestSetFilesViewedInput = typeof PullRequestSetFilesViewedInput.Type;
839
840export const PullRequestStackHead = Schema.Struct({
841 number: PositiveInt,
842 headSha: TrimmedNonEmptyString,
843});
844export type PullRequestStackHead = typeof PullRequestStackHead.Type;
845
846export const PullRequestActionInput = Schema.Struct({
847 /** Native stack scope; only send to environments advertising pullRequestStackActions. */
848 stackNumber: Schema.optional(PositiveInt),
849 expectedStackHeads: Schema.optional(Schema.Array(PullRequestStackHead)),
850 /** Merges refuse when the head commit is no longer this one. */
851 expectedHeadSha: Schema.optional(TrimmedNonEmptyString),
852 ...PullRequestRef.fields,
853 action: PullRequestAction,
854 mergeMethod: Schema.optional(PullRequestMergeMethod),
855 updateMethod: Schema.optional(PullRequestUpdateMethod),
856 /** Opt-in, because older clients decode the action's success as void and reject a summary. */
857 replyWithSummary: Schema.optional(Schema.Literal(true)),
858});
859export type PullRequestActionInput = typeof PullRequestActionInput.Type;
860
861// Preserve whitespace because it affects Markdown formatting.
862const CommentBody = Schema.String.check(Schema.isNonEmpty()).check(Schema.isMaxLength(65_536));
863
864export const PullRequestCommentInput = Schema.Struct({
865 ...PullRequestRef.fields,
866 body: CommentBody,
867});
868export type PullRequestCommentInput = typeof PullRequestCommentInput.Type;
869
870export const PullRequestUpdateInput = Schema.Struct({
871 ...PullRequestRef.fields,
872 title: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(1024))),
873 body: Schema.optional(Schema.String.check(Schema.isMaxLength(65_536))),
874 maintainerCanModify: Schema.optional(Schema.Boolean),
875});
876export type PullRequestUpdateInput = typeof PullRequestUpdateInput.Type;
877
878/** Comment kind selects the endpoint; GitHub uses separate mutations for conversation and line comments. */
879export const PullRequestCommentUpdateInput = Schema.Struct({
880 ...PullRequestRef.fields,
881 commentId: TrimmedNonEmptyString,
882 kind: Schema.Literals(["issue-comment", "review-comment"]),
883 body: CommentBody,
884});
885export type PullRequestCommentUpdateInput = typeof PullRequestCommentUpdateInput.Type;
886
887export const PullRequestReviewPosition = Schema.Union([
888 Schema.Struct({
889 kind: Schema.Literal("added"),
890 newLine: PositiveInt,
891 }),
892 Schema.Struct({
893 kind: Schema.Literal("deleted"),
894 oldLine: PositiveInt,
895 }),
896 Schema.Struct({
897 kind: Schema.Literal("context"),
898 oldLine: PositiveInt,
899 newLine: PositiveInt,
900 side: PullRequestDiffSide,
901 }),
902]);
903export type PullRequestReviewPosition = typeof PullRequestReviewPosition.Type;
904
905export const PullRequestReviewCommentDraft = Schema.Struct({
906 path: TrimmedNonEmptyString,
907 /** GitLab needs both names for renamed-file comments; hosts using one path ignore this. */
908 oldPath: Schema.optional(TrimmedNonEmptyString),
909 position: PullRequestReviewPosition,
910 /**
911 * The first line of a comment on a range, which ends at `position`. Sent only to a host whose
912 * capabilities include `review.rangeComment`.
913 */
914 startPosition: Schema.optional(PullRequestReviewPosition),
915 body: CommentBody,
916});
917export type PullRequestReviewCommentDraft = typeof PullRequestReviewCommentDraft.Type;
918
919export const PullRequestSubmitReviewInput = Schema.Struct({
920 ...PullRequestRef.fields,
921 /** Reviews refuse when the head commit is no longer this one. */
922 expectedHeadSha: Schema.optional(TrimmedNonEmptyString),
923 verdict: PullRequestReviewVerdict,
924 body: Schema.String.check(Schema.isMaxLength(65_536)),
925 comments: Schema.Array(PullRequestReviewCommentDraft),
926});
927export type PullRequestSubmitReviewInput = typeof PullRequestSubmitReviewInput.Type;
928
929export const PullRequestThreadCommentsInput = Schema.Struct({
930 ...PullRequestRef.fields,
931 threadId: TrimmedNonEmptyString,
932 cursor: TrimmedNonEmptyString,
933});
934export type PullRequestThreadCommentsInput = typeof PullRequestThreadCommentsInput.Type;
935
936export const PullRequestThreadCommentsResult = Schema.Struct({
937 comments: Schema.Array(PullRequestThreadComment),
938 nextCursor: Schema.NullOr(TrimmedNonEmptyString),
939 // Comments the host hid that this page left out; the thread's `commentCount` still counts them.
940 hiddenCount: Schema.optional(NonNegativeInt),
941});
942export type PullRequestThreadCommentsResult = typeof PullRequestThreadCommentsResult.Type;
943
944export const PullRequestThreadReplyInput = Schema.Struct({
945 ...PullRequestRef.fields,
946 threadId: TrimmedNonEmptyString,
947 body: CommentBody,
948});
949export type PullRequestThreadReplyInput = typeof PullRequestThreadReplyInput.Type;
950
951export const PullRequestThreadResolutionInput = Schema.Struct({
952 ...PullRequestRef.fields,
953 threadId: TrimmedNonEmptyString,
954 resolved: Schema.Boolean,
955});
956export type PullRequestThreadResolutionInput = typeof PullRequestThreadResolutionInput.Type;
957
958export const PullRequestReactionInput = Schema.Struct({
959 ...PullRequestRef.fields,
960 subjectId: Schema.optional(TrimmedNonEmptyString),
961 content: PullRequestReactionContent,
962 reacted: Schema.Boolean,
963});
964export type PullRequestReactionInput = typeof PullRequestReactionInput.Type;
965
966/** The most labels or reviewers one write may change. */
967export const PULL_REQUEST_CHANGE_BATCH_LIMIT = 25;
968
969export const PullRequestReviewerRequestInput = Schema.Struct({
970 ...PullRequestRef.fields,
971 reviewers: Schema.Array(
972 Schema.Struct({ id: TrimmedNonEmptyString, kind: PullRequestReviewerKind }),
973 ).check(Schema.isMinLength(1), Schema.isMaxLength(PULL_REQUEST_CHANGE_BATCH_LIMIT)),
974 requested: Schema.Boolean,
975});
976export type PullRequestReviewerRequestInput = typeof PullRequestReviewerRequestInput.Type;
977
978export const PullRequestLabelChangeInput = Schema.Struct({
979 ...PullRequestRef.fields,
980 labels: Schema.Array(TrimmedNonEmptyString).check(
981 Schema.isMinLength(1),
982 Schema.isMaxLength(PULL_REQUEST_CHANGE_BATCH_LIMIT),
983 ),
984 applied: Schema.Boolean,
985});
986export type PullRequestLabelChangeInput = typeof PullRequestLabelChangeInput.Type;
987
988/**
989 * The names keep their `cli-` prefix for wire compatibility; for a host read with a credential
990 * rather than a tool they mean "no credential" and "a refused credential".
991 */
992export const PullRequestUnavailableReason = Schema.Literals([
993 "cli-missing",
994 "cli-unauthenticated",
995 "provider-unsupported",
996 "repository-without-project",
997]);
998export type PullRequestUnavailableReason = typeof PullRequestUnavailableReason.Type;
999
1000/**
1001 * `author:me` names whoever is signed in to the host being read, GitHub's `@me` spelled either
1002 * way. A host's own search would answer it, but the page and the server both re-check an author
1003 * against the row's login, so the name is resolved before either comparison rather than being
1004 * matched as the literal login "me".
1005 */
1006export function resolvePullRequestAuthorFilter(
1007 author: string,
1008 viewer: string | null | undefined,
1009): string {
1010 const trimmed = author.trim();
1011 if (!/^@?me$/i.test(trimmed)) return trimmed;
1012 return viewer === null || viewer === undefined || viewer.trim().length === 0 ? trimmed : viewer;
1013}
1014
1015export class PullRequestUnavailableError extends Schema.TaggedError<PullRequestUnavailableError>()(
1016 "PullRequestUnavailableError",
1017 {
1018 reason: PullRequestUnavailableReason,
1019 provider: Schema.optional(SourceControlProviderKind),
1020 listResult: Schema.optional(PullRequestListResult),
1021 /**
1022 * What the host needs on the server, in its own words. Absent from older servers, which
1023 * shipped no such field, and for a host that names nothing.
1024 */
1025 requirement: Schema.optional(TrimmedNonEmptyString),
1026 /** The repository no project has, for `repository-without-project`. */
1027 repository: Schema.optional(TrimmedNonEmptyString),
1028 /**
1029 * For `repository-without-project` on a host read through any checkout on it: the host no
1030 * project is on.
1031 */
1032 host: Schema.optional(TrimmedNonEmptyString),
1033 cause: Schema.optional(Schema.Defect()),
1034 },
1035 { httpApiStatus: 503 },
1036) {
1037 [HttpServerRespondable.symbol]() {
1038 return HttpServerResponse.schemaJson(PullRequestUnavailableError)(this, { status: 503 });
1039 }
1040
1041 override get message(): string {
1042 switch (this.reason) {
1043 case "cli-missing":
1044 return this.requirement ?? "The tool this host is read through is not installed or set up.";
1045 case "cli-unauthenticated":
1046 return this.requirement ?? "This host has no working credentials.";
1047 case "provider-unsupported":
1048 return "Change requests cannot be browsed for this project's host yet.";
1049 case "repository-without-project":
1050 if (this.host !== undefined) {
1051 return `No project in T3 Code is on ${this.host}. Add any ${this.host} project to read its change requests.`;
1052 }
1053 return `${this.repository ?? "This repository"} is not a project in T3 Code. Add it as a project to read its change requests.`;
1054 }
1055 }
1056}
1057
1058export class PullRequestOperationError extends Schema.TaggedError<PullRequestOperationError>()(
1059 "PullRequestOperationError",
1060 {
1061 operation: Schema.String,
1062 detail: TrimmedNonEmptyString,
1063 reason: Schema.optional(Schema.Literal("not-found")),
1064 listResult: Schema.optional(PullRequestListResult),
1065 cause: Schema.optional(Schema.Defect()),
1066 },
1067 { httpApiStatus: 502 },
1068) {
1069 [HttpServerRespondable.symbol]() {
1070 return HttpServerResponse.schemaJson(PullRequestOperationError)(this, { status: 502 });
1071 }
1072
1073 override get message(): string {
1074 return `Pull request operation ${this.operation} failed: ${this.detail}`;
1075 }
1076}