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