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