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 workflowApprovalsRequired: Schema.optional(NonNegativeInt),
706});
707export type PullRequestChecks = typeof PullRequestChecks.Type;
708
709export const PullRequestActivity = Schema.Struct({
710 author: Schema.optional(Schema.NullOr(PullRequestActor)),
711 reviewers: Schema.optional(Schema.Array(PullRequestActor)),
712 comments: Schema.Array(PullRequestComment),
713 commentCount: NonNegativeInt,
714 commentsTruncated: Schema.Boolean,
715 reviewThreadsTruncated: Schema.optional(Schema.Boolean),
716 reviewThreads: Schema.Array(PullRequestReviewThread),
717 // Apart from reviewThreads so clients that require a thread path still decode the activity.
718 overviewThreads: Schema.optional(Schema.Array(PullRequestOverviewThread)),
719 commits: Schema.Array(PullRequestCommit),
720 reactions: Schema.optional(Schema.Array(PullRequestReaction)),
721});
722export type PullRequestActivity = typeof PullRequestActivity.Type;
723
724export const pullRequestConversationThreads = (
725 activity: Pick<PullRequestActivity, "reviewThreads" | "overviewThreads">,
726): ReadonlyArray<PullRequestConversationThread> =>
727 activity.overviewThreads === undefined || activity.overviewThreads.length === 0
728 ? activity.reviewThreads
729 : [...activity.reviewThreads, ...activity.overviewThreads];
730
731export const PullRequestDetailView = Schema.Struct({
732 ...PullRequestDetail.fields,
733 ...PullRequestActivity.fields,
734 author: Schema.NullOr(PullRequestActor),
735 reviewers: Schema.Array(PullRequestActor),
736});
737export type PullRequestDetailView = typeof PullRequestDetailView.Type;
738
739/** GitHub refuses diffs past 300 files, so callers must page large changes. */
740export const PullRequestDiffInput = Schema.Struct({
741 ...PullRequestRef.fields,
742 cursor: Schema.optional(TrimmedNonEmptyString),
743 commit: Schema.optional(TrimmedNonEmptyString),
744 /** Reads one file the paged diff withheld as too large; ignores `cursor`. */
745 path: Schema.optional(TrimmedNonEmptyString),
746});
747export type PullRequestDiffInput = typeof PullRequestDiffInput.Type;
748
749export const PullRequestOmittedFileStat = Schema.Struct({
750 path: TrimmedNonEmptyString,
751 additions: Schema.Number,
752 deletions: Schema.Number,
753});
754export type PullRequestOmittedFileStat = typeof PullRequestOmittedFileStat.Type;
755
756export const PullRequestDiffResult = Schema.Struct({
757 patch: Schema.String,
758 /** Content omitted within this slice; nextCursor separately indicates more slices. */
759 truncated: Schema.Boolean,
760 nextCursor: Schema.NullOr(TrimmedNonEmptyString),
761 omittedFileStats: Schema.optional(Schema.Array(PullRequestOmittedFileStat)),
762 /** On the first page: files this diff withholds can be read one at a time with `path`. */
763 withheldFilesReadable: Schema.optional(Schema.Boolean),
764});
765export type PullRequestDiffResult = typeof PullRequestDiffResult.Type;
766
767export const PullRequestDiffFileContentsInput = Schema.Struct({
768 ...PullRequestRef.fields,
769 commit: Schema.optional(TrimmedNonEmptyString),
770 changeType: Schema.Literals(["change", "rename-pure", "rename-changed", "new", "deleted"]),
771 oldPath: Schema.NonEmptyString,
772 newPath: Schema.NonEmptyString,
773});
774export type PullRequestDiffFileContentsInput = typeof PullRequestDiffFileContentsInput.Type;
775
776export const PullRequestDiffFileContentsResult = Schema.Struct({
777 oldContents: Schema.String,
778 newContents: Schema.String,
779});
780export type PullRequestDiffFileContentsResult = typeof PullRequestDiffFileContentsResult.Type;
781
782const MAX_FILE_PATH_LENGTH = 4096;
783// Preserve filename whitespace so viewed marks match host paths.
784const FilePath = Schema.String.check(Schema.isNonEmpty(), Schema.isMaxLength(MAX_FILE_PATH_LENGTH));
785
786export const PullRequestFileViewedState = Schema.Literals(["unviewed", "viewed", "dismissed"]);
787export type PullRequestFileViewedState = typeof PullRequestFileViewedState.Type;
788
789export const PullRequestFileViewed = Schema.Struct({
790 path: FilePath,
791 state: PullRequestFileViewedState,
792});
793export type PullRequestFileViewed = typeof PullRequestFileViewed.Type;
794
795export const PullRequestFilesViewedResult = Schema.Struct({
796 /** Only the files the host reported a state for. A file missing from this list is unviewed. */
797 files: Schema.Array(PullRequestFileViewed),
798 truncated: Schema.Boolean,
799});
800export type PullRequestFilesViewedResult = typeof PullRequestFilesViewedResult.Type;
801
802const PullRequestLineCount = Schema.Struct({
803 additions: NonNegativeInt,
804 deletions: NonNegativeInt,
805});
806
807/**
808 * A change's line counts split by what the changed files are, judged from their paths. Null when
809 * the host gives no per-file counts, or has more files than one read covers: a split of part of
810 * the change would not add up to the totals shown beside it.
811 */
812export const PullRequestChangeBreakdown = Schema.NullOr(
813 Schema.Struct({
814 code: PullRequestLineCount,
815 tests: PullRequestLineCount,
816 mocks: PullRequestLineCount,
817 generated: PullRequestLineCount,
818 // Servers from before the docs split send no key; their docs lines sit in `code`.
819 docs: PullRequestLineCount.pipe(
820 Schema.withDecodingDefault(Effect.succeed({ additions: 0, deletions: 0 })),
821 ),
822 }),
823);
824export type PullRequestChangeBreakdown = typeof PullRequestChangeBreakdown.Type;
825
826const MAX_FILES_VIEWED_PRESSES = 500;
827
828export const PullRequestSetFilesViewedInput = Schema.Struct({
829 ...PullRequestRef.fields,
830 files: Schema.Array(
831 Schema.Struct({
832 path: FilePath,
833 viewed: Schema.Boolean,
834 /** The file's version in the diff the reader was shown; without it the head's is recorded. */
835 revision: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(256))),
836 }),
837 ).check(Schema.isMaxLength(MAX_FILES_VIEWED_PRESSES)),
838});
839export type PullRequestSetFilesViewedInput = typeof PullRequestSetFilesViewedInput.Type;
840
841export const PullRequestStackHead = Schema.Struct({
842 number: PositiveInt,
843 headSha: TrimmedNonEmptyString,
844});
845export type PullRequestStackHead = typeof PullRequestStackHead.Type;
846
847export const PullRequestActionInput = Schema.Struct({
848 /** Native stack scope; only send to environments advertising pullRequestStackActions. */
849 stackNumber: Schema.optional(PositiveInt),
850 expectedStackHeads: Schema.optional(Schema.Array(PullRequestStackHead)),
851 /** Merges refuse when the head commit is no longer this one. */
852 expectedHeadSha: Schema.optional(TrimmedNonEmptyString),
853 ...PullRequestRef.fields,
854 action: PullRequestAction,
855 mergeMethod: Schema.optional(PullRequestMergeMethod),
856 updateMethod: Schema.optional(PullRequestUpdateMethod),
857 /** Opt-in, because older clients decode the action's success as void and reject a summary. */
858 replyWithSummary: Schema.optional(Schema.Literal(true)),
859});
860export type PullRequestActionInput = typeof PullRequestActionInput.Type;
861
862// Preserve whitespace because it affects Markdown formatting.
863const CommentBody = Schema.String.check(Schema.isNonEmpty()).check(Schema.isMaxLength(65_536));
864
865export const PullRequestCommentInput = Schema.Struct({
866 ...PullRequestRef.fields,
867 body: CommentBody,
868});
869export type PullRequestCommentInput = typeof PullRequestCommentInput.Type;
870
871export const PullRequestUpdateInput = Schema.Struct({
872 ...PullRequestRef.fields,
873 title: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(1024))),
874 body: Schema.optional(Schema.String.check(Schema.isMaxLength(65_536))),
875 maintainerCanModify: Schema.optional(Schema.Boolean),
876});
877export type PullRequestUpdateInput = typeof PullRequestUpdateInput.Type;
878
879/** Comment kind selects the endpoint; GitHub uses separate mutations for conversation and line comments. */
880export const PullRequestCommentUpdateInput = Schema.Struct({
881 ...PullRequestRef.fields,
882 commentId: TrimmedNonEmptyString,
883 kind: Schema.Literals(["issue-comment", "review-comment"]),
884 body: CommentBody,
885});
886export type PullRequestCommentUpdateInput = typeof PullRequestCommentUpdateInput.Type;
887
888export const PullRequestReviewPosition = Schema.Union([
889 Schema.Struct({
890 kind: Schema.Literal("added"),
891 newLine: PositiveInt,
892 }),
893 Schema.Struct({
894 kind: Schema.Literal("deleted"),
895 oldLine: PositiveInt,
896 }),
897 Schema.Struct({
898 kind: Schema.Literal("context"),
899 oldLine: PositiveInt,
900 newLine: PositiveInt,
901 side: PullRequestDiffSide,
902 }),
903]);
904export type PullRequestReviewPosition = typeof PullRequestReviewPosition.Type;
905
906export const PullRequestReviewCommentDraft = Schema.Struct({
907 path: TrimmedNonEmptyString,
908 /** GitLab needs both names for renamed-file comments; hosts using one path ignore this. */
909 oldPath: Schema.optional(TrimmedNonEmptyString),
910 position: PullRequestReviewPosition,
911 /**
912 * The first line of a comment on a range, which ends at `position`. Sent only to a host whose
913 * capabilities include `review.rangeComment`.
914 */
915 startPosition: Schema.optional(PullRequestReviewPosition),
916 body: CommentBody,
917});
918export type PullRequestReviewCommentDraft = typeof PullRequestReviewCommentDraft.Type;
919
920export const PullRequestSubmitReviewInput = Schema.Struct({
921 ...PullRequestRef.fields,
922 /** Reviews refuse when the head commit is no longer this one. */
923 expectedHeadSha: Schema.optional(TrimmedNonEmptyString),
924 verdict: PullRequestReviewVerdict,
925 body: Schema.String.check(Schema.isMaxLength(65_536)),
926 comments: Schema.Array(PullRequestReviewCommentDraft),
927});
928export type PullRequestSubmitReviewInput = typeof PullRequestSubmitReviewInput.Type;
929
930export const PullRequestThreadCommentsInput = Schema.Struct({
931 ...PullRequestRef.fields,
932 threadId: TrimmedNonEmptyString,
933 cursor: TrimmedNonEmptyString,
934});
935export type PullRequestThreadCommentsInput = typeof PullRequestThreadCommentsInput.Type;
936
937export const PullRequestThreadCommentsResult = Schema.Struct({
938 comments: Schema.Array(PullRequestThreadComment),
939 nextCursor: Schema.NullOr(TrimmedNonEmptyString),
940 // Comments the host hid that this page left out; the thread's `commentCount` still counts them.
941 hiddenCount: Schema.optional(NonNegativeInt),
942});
943export type PullRequestThreadCommentsResult = typeof PullRequestThreadCommentsResult.Type;
944
945export const PullRequestThreadReplyInput = Schema.Struct({
946 ...PullRequestRef.fields,
947 threadId: TrimmedNonEmptyString,
948 body: CommentBody,
949});
950export type PullRequestThreadReplyInput = typeof PullRequestThreadReplyInput.Type;
951
952export const PullRequestThreadResolutionInput = Schema.Struct({
953 ...PullRequestRef.fields,
954 threadId: TrimmedNonEmptyString,
955 resolved: Schema.Boolean,
956});
957export type PullRequestThreadResolutionInput = typeof PullRequestThreadResolutionInput.Type;
958
959export const PullRequestReactionInput = Schema.Struct({
960 ...PullRequestRef.fields,
961 subjectId: Schema.optional(TrimmedNonEmptyString),
962 content: PullRequestReactionContent,
963 reacted: Schema.Boolean,
964});
965export type PullRequestReactionInput = typeof PullRequestReactionInput.Type;
966
967/** The most labels or reviewers one write may change. */
968export const PULL_REQUEST_CHANGE_BATCH_LIMIT = 25;
969
970export const PullRequestReviewerRequestInput = Schema.Struct({
971 ...PullRequestRef.fields,
972 reviewers: Schema.Array(
973 Schema.Struct({ id: TrimmedNonEmptyString, kind: PullRequestReviewerKind }),
974 ).check(Schema.isMinLength(1), Schema.isMaxLength(PULL_REQUEST_CHANGE_BATCH_LIMIT)),
975 requested: Schema.Boolean,
976});
977export type PullRequestReviewerRequestInput = typeof PullRequestReviewerRequestInput.Type;
978
979export const PullRequestLabelChangeInput = Schema.Struct({
980 ...PullRequestRef.fields,
981 labels: Schema.Array(TrimmedNonEmptyString).check(
982 Schema.isMinLength(1),
983 Schema.isMaxLength(PULL_REQUEST_CHANGE_BATCH_LIMIT),
984 ),
985 applied: Schema.Boolean,
986});
987export type PullRequestLabelChangeInput = typeof PullRequestLabelChangeInput.Type;
988
989/**
990 * The names keep their `cli-` prefix for wire compatibility; for a host read with a credential
991 * rather than a tool they mean "no credential" and "a refused credential".
992 */
993export const PullRequestUnavailableReason = Schema.Literals([
994 "cli-missing",
995 "cli-unauthenticated",
996 "provider-unsupported",
997 "repository-without-project",
998]);
999export type PullRequestUnavailableReason = typeof PullRequestUnavailableReason.Type;
1000
1001/**
1002 * `author:me` names whoever is signed in to the host being read, GitHub's `@me` spelled either
1003 * way. A host's own search would answer it, but the page and the server both re-check an author
1004 * against the row's login, so the name is resolved before either comparison rather than being
1005 * matched as the literal login "me".
1006 */
1007export function resolvePullRequestAuthorFilter(
1008 author: string,
1009 viewer: string | null | undefined,
1010): string {
1011 const trimmed = author.trim();
1012 if (!/^@?me$/i.test(trimmed)) return trimmed;
1013 return viewer === null || viewer === undefined || viewer.trim().length === 0 ? trimmed : viewer;
1014}
1015
1016export class PullRequestUnavailableError extends Schema.TaggedError<PullRequestUnavailableError>()(
1017 "PullRequestUnavailableError",
1018 {
1019 reason: PullRequestUnavailableReason,
1020 provider: Schema.optional(SourceControlProviderKind),
1021 listResult: Schema.optional(PullRequestListResult),
1022 /**
1023 * What the host needs on the server, in its own words. Absent from older servers, which
1024 * shipped no such field, and for a host that names nothing.
1025 */
1026 requirement: Schema.optional(TrimmedNonEmptyString),
1027 /** The repository no project has, for `repository-without-project`. */
1028 repository: Schema.optional(TrimmedNonEmptyString),
1029 /**
1030 * For `repository-without-project` on a host read through any checkout on it: the host no
1031 * project is on.
1032 */
1033 host: Schema.optional(TrimmedNonEmptyString),
1034 cause: Schema.optional(Schema.Defect()),
1035 },
1036 { httpApiStatus: 503 },
1037) {
1038 [HttpServerRespondable.symbol]() {
1039 return HttpServerResponse.schemaJson(PullRequestUnavailableError)(this, { status: 503 });
1040 }
1041
1042 override get message(): string {
1043 switch (this.reason) {
1044 case "cli-missing":
1045 return this.requirement ?? "The tool this host is read through is not installed or set up.";
1046 case "cli-unauthenticated":
1047 return this.requirement ?? "This host has no working credentials.";
1048 case "provider-unsupported":
1049 return "Change requests cannot be browsed for this project's host yet.";
1050 case "repository-without-project":
1051 if (this.host !== undefined) {
1052 return `No project in T3 Code is on ${this.host}. Add any ${this.host} project to read its change requests.`;
1053 }
1054 return `${this.repository ?? "This repository"} is not a project in T3 Code. Add it as a project to read its change requests.`;
1055 }
1056 }
1057}
1058
1059export class PullRequestOperationError extends Schema.TaggedError<PullRequestOperationError>()(
1060 "PullRequestOperationError",
1061 {
1062 operation: Schema.String,
1063 detail: TrimmedNonEmptyString,
1064 reason: Schema.optional(Schema.Literal("not-found")),
1065 listResult: Schema.optional(PullRequestListResult),
1066 cause: Schema.optional(Schema.Defect()),
1067 },
1068 { httpApiStatus: 502 },
1069) {
1070 [HttpServerRespondable.symbol]() {
1071 return HttpServerResponse.schemaJson(PullRequestOperationError)(this, { status: 502 });
1072 }
1073
1074 override get message(): string {
1075 return `Pull request operation ${this.operation} failed: ${this.detail}`;
1076 }
1077}