Open History to see the last commit
1import * as Schema from "effect/Schema";
2import * as HttpServerRespondable from "effect/unstable/http/HttpServerRespondable";
3import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";
4
5import {
6 IsoDateTime,
7 NonNegativeInt,
8 PositiveInt,
9 ProjectId,
10 ThreadId,
11 TrimmedNonEmptyString,
12} from "./baseSchemas.ts";
13import { SourceControlProviderKind } from "./sourceControl.ts";
14
15export const PullRequestInvolvement = Schema.Literals(["all", "reviewing", "authored"]);
16export type PullRequestInvolvement = typeof PullRequestInvolvement.Type;
17
18export const PullRequestState = Schema.Literals(["open", "closed", "merged"]);
19export type PullRequestState = typeof PullRequestState.Type;
20
21/**
22 * What a listing asks for, which is the three states a change request can be in plus the option
23 * to span them. Separate from `PullRequestState` because a change request is never "all" — only
24 * a request for one can be.
25 */
26export const PullRequestListState = Schema.Literals(["all", "open", "closed", "merged"]);
27export type PullRequestListState = typeof PullRequestListState.Type;
28
29/** Where a review stands overall, as a host that summarises its reviews reports it. */
30export const PullRequestReviewDecision = Schema.Literals([
31 "approved",
32 "changes-requested",
33 "review-required",
34]);
35export type PullRequestReviewDecision = typeof PullRequestReviewDecision.Type;
36
37/** One qualifier's value, bounded because it is written into a host's own search query. */
38const PullRequestQualifierValue = TrimmedNonEmptyString.check(Schema.isMaxLength(200));
39const PullRequestQualifierValues = Schema.Array(PullRequestQualifierValue).check(
40 Schema.isMaxLength(10),
41);
42
43/**
44 * Narrowings beyond state and involvement, each absent by default — an absent field filters
45 * nothing, which is what every listing did before there were any. Optional as a whole so a page
46 * and a server of different ages still speak to each other.
47 *
48 * `checks` is host-side only: no row carries its own check state, so a host that cannot match it
49 * answers unnarrowed rather than the page pretending to know.
50 */
51export const PullRequestListFilters = Schema.Struct({
52 draft: Schema.optional(Schema.Literals(["only", "hide"])),
53 review: Schema.optional(
54 Schema.Literals(["approved", "changes-requested", "review-required", "none"]),
55 ),
56 checks: Schema.optional(Schema.Literals(["passing", "failing"])),
57 /**
58 * Labels as GitHub's own search reads them: each group is one `label:` qualifier, a row must
59 * satisfy every group, and a group holding several names is satisfied by any one of them —
60 * `label:size:S,size:XS` finds either size. Typed rather than picked, since a project's labels
61 * are its own and no menu can list them. Bounded because each becomes a host search qualifier.
62 */
63 labels: Schema.optional(Schema.Array(PullRequestQualifierValues).check(Schema.isMaxLength(10))),
64 excludedLabels: Schema.optional(PullRequestQualifierValues),
65 /** One login, as `author:` names it. */
66 author: Schema.optional(PullRequestQualifierValue),
67});
68export type PullRequestListFilters = typeof PullRequestListFilters.Type;
69
70/** The one-glyph summary of a change request's checks, as its list row wears it. */
71export const PullRequestChecksState = Schema.Literals(["passing", "failing", "pending"]);
72export type PullRequestChecksState = typeof PullRequestChecksState.Type;
73
74export const PullRequestMergeability = Schema.Literals(["mergeable", "conflicting", "unknown"]);
75export type PullRequestMergeability = typeof PullRequestMergeability.Type;
76
77export const PullRequestMergeMethod = Schema.Literals(["merge", "squash", "rebase"]);
78export type PullRequestMergeMethod = typeof PullRequestMergeMethod.Type;
79
80export const PullRequestAction = Schema.Literals([
81 "merge",
82 "ready",
83 "draft",
84 "close",
85 "reopen",
86 /** Bring the base branch's commits into this one, which is what unblocks a stale branch. */
87 "update-branch",
88 /**
89 * Hand the merge to the host: it waits for whatever it requires — a green pipeline, the
90 * approvals, an unblocked branch — and merges without anybody coming back. Separate from
91 * `merge` because the two answer different questions. `merge` is refused outright by a host
92 * that is not ready yet, and this one is for exactly that moment.
93 */
94 "enable-auto-merge",
95 /** Take the standing instruction back, which leaves the change request where it was. */
96 "disable-auto-merge",
97 /** Open a new change request that reverses a merged one. */
98 "revert",
99 /** Allow Actions workflows from a fork pull request to begin running. */
100 "approve-workflows",
101]);
102export type PullRequestAction = typeof PullRequestAction.Type;
103
104/**
105 * How a stale branch catches up with its base: a merge commit, or a rebase onto it. The two are
106 * the host's own choices, not this page's — GitHub offers both and refuses a rebase it cannot
107 * replay, so what is offered comes from the host and what is allowed comes from the viewer.
108 */
109export const PullRequestUpdateMethod = Schema.Literals(["merge", "rebase"]);
110export type PullRequestUpdateMethod = typeof PullRequestUpdateMethod.Type;
111
112/**
113 * Where the branch stands against the base it would merge into. Separate from `mergeability`,
114 * which answers a different question: a branch can be behind and still merge cleanly, and that
115 * pairing — out of date, no conflicts — is the one an update button exists for.
116 *
117 * "unknown" where the host was not asked or could not say, which is every host but GitHub and
118 * every pull request whose head repository could not be compared.
119 */
120export const PullRequestBaseComparison = Schema.Literals(["up-to-date", "behind", "unknown"]);
121export type PullRequestBaseComparison = typeof PullRequestBaseComparison.Type;
122
123export const PullRequestActor = Schema.Struct({
124 /** Present when the host identifies an automated account. */
125 isBot: Schema.optional(Schema.Boolean),
126 login: TrimmedNonEmptyString,
127 name: Schema.NullOr(Schema.String),
128 /** Null where a host does not report one, which is what the initials fall back to. */
129 avatarUrl: Schema.NullOr(Schema.String),
130});
131export type PullRequestActor = typeof PullRequestActor.Type;
132
133export const PullRequestLabel = Schema.Struct({
134 name: TrimmedNonEmptyString,
135 color: Schema.NullOr(Schema.String),
136});
137export type PullRequestLabel = typeof PullRequestLabel.Type;
138
139export const PullRequestCheckStatus = Schema.Literals([
140 "pending",
141 "action-required",
142 "success",
143 "failure",
144 "skipped",
145 "neutral",
146 "cancelled",
147]);
148export type PullRequestCheckStatus = typeof PullRequestCheckStatus.Type;
149
150export const PullRequestCheck = Schema.Struct({
151 name: TrimmedNonEmptyString,
152 status: PullRequestCheckStatus,
153 description: Schema.NullOr(Schema.String),
154 url: Schema.NullOr(Schema.String),
155 /** The base branch requires this check to merge. Absent where the host does not say. */
156 required: Schema.optional(Schema.Boolean),
157});
158export type PullRequestCheck = typeof PullRequestCheck.Type;
159
160/**
161 * The reactions a remark can carry. GitHub's eight, which is also what the picker offers: GitLab
162 * accepts any emoji as an award, and the ones outside this set are read as nothing rather than
163 * shown under a name no other host would recognise.
164 */
165export const PullRequestReactionContent = Schema.Literals([
166 "thumbs-up",
167 "thumbs-down",
168 "laugh",
169 "hooray",
170 "confused",
171 "heart",
172 "rocket",
173 "eyes",
174]);
175export type PullRequestReactionContent = typeof PullRequestReactionContent.Type;
176
177/** One reaction and everyone behind it, which is what the hover on a reaction pill says. */
178export const PullRequestReaction = Schema.Struct({
179 content: PullRequestReactionContent,
180 count: PositiveInt,
181 /**
182 * Who reacted, as far as the host named them. A host that reports fewer names than it counts
183 * leaves the rest out, so this is never longer than `count` and may be shorter. When
184 * `viewerHasReacted` is true, the viewer's own login is left out of this list — the page names
185 * them "You" instead — but `count` still counts them along with everyone else.
186 */
187 actors: Schema.Array(TrimmedNonEmptyString),
188 /** The signed-in account is one of them, so pressing the pill takes the reaction back. */
189 viewerHasReacted: Schema.Boolean,
190});
191export type PullRequestReaction = typeof PullRequestReaction.Type;
192
193export const PullRequestCommentKind = Schema.Literals([
194 "issue-comment",
195 "review-comment",
196 "review",
197]);
198export type PullRequestCommentKind = typeof PullRequestCommentKind.Type;
199
200export const PullRequestComment = Schema.Struct({
201 id: TrimmedNonEmptyString,
202 kind: PullRequestCommentKind,
203 author: Schema.NullOr(PullRequestActor),
204 body: Schema.String,
205 createdAt: IsoDateTime,
206 url: Schema.NullOr(Schema.String),
207 path: Schema.NullOr(Schema.String),
208 reviewState: Schema.NullOr(Schema.String),
209 /** Absent from a host with no reactions at all, which is a different thing from none on this. */
210 reactions: Schema.optional(Schema.Array(PullRequestReaction)),
211});
212export type PullRequestComment = typeof PullRequestComment.Type;
213
214/**
215 * Which file a diff line belongs to: `left` is the version before the change, `right` the
216 * version after. A comment has to name one, because a unified diff shows both at once and the
217 * same line number means two different lines.
218 */
219export const PullRequestDiffSide = Schema.Literals(["left", "right"]);
220export type PullRequestDiffSide = typeof PullRequestDiffSide.Type;
221
222/** What submitting a review says about the change, beyond the words in it. */
223export const PullRequestReviewVerdict = Schema.Literals(["comment", "approve", "request-changes"]);
224export type PullRequestReviewVerdict = typeof PullRequestReviewVerdict.Type;
225
226export const PullRequestThreadComment = Schema.Struct({
227 id: TrimmedNonEmptyString,
228 author: Schema.NullOr(PullRequestActor),
229 body: Schema.String,
230 createdAt: IsoDateTime,
231 url: Schema.NullOr(Schema.String),
232 reactions: Schema.optional(Schema.Array(PullRequestReaction)),
233});
234export type PullRequestThreadComment = typeof PullRequestThreadComment.Type;
235
236/**
237 * A conversation anchored to a line of the diff. The detail carries these alongside `comments`
238 * rather than instead of them: the timeline wants one flat, chronological list, and the diff
239 * wants whole threads pinned to their line — the same remarks, read two different ways.
240 */
241export const PullRequestReviewThread = Schema.Struct({
242 id: TrimmedNonEmptyString,
243 path: TrimmedNonEmptyString,
244 /** Null when the host anchors the thread to a file rather than to a line. */
245 line: Schema.NullOr(PositiveInt),
246 side: PullRequestDiffSide,
247 isResolved: Schema.Boolean,
248 /**
249 * The line the thread was written against is no longer in the diff, so it cannot be shown
250 * against the code. Such a thread is listed separately rather than pinned to the wrong line.
251 */
252 isOutdated: Schema.Boolean,
253 comments: Schema.Array(PullRequestThreadComment),
254 /** Host-reported total, when this thread was read in pages. */
255 commentCount: Schema.optional(NonNegativeInt),
256 /** Opaque cursor for the next comment page. Absent once this thread is whole. */
257 nextCommentsCursor: Schema.optional(TrimmedNonEmptyString),
258});
259export type PullRequestReviewThread = typeof PullRequestReviewThread.Type;
260
261/**
262 * Whether a reviewer is a person or a group of them the host addresses as one. GitHub is the only
263 * host here that takes a review request for a team; the others name individuals only, so nothing
264 * they report is ever anything but a user.
265 */
266export const PullRequestReviewerKind = Schema.Literals(["user", "team"]);
267export type PullRequestReviewerKind = typeof PullRequestReviewerKind.Type;
268
269/**
270 * Somebody a review may be asked of. Carries the actor fields so the menu shows the same face and
271 * handle the rest of the page does, and a team wears them too — its slug stands in for a login.
272 */
273export const PullRequestReviewerCandidate = Schema.Struct({
274 ...PullRequestActor.fields,
275 /**
276 * How the host addresses this reviewer when a review is requested, which is not always the
277 * handle it shows: GitHub takes a login or a team slug, GitLab a numeric user id, Bitbucket an
278 * account uuid. Opaque to the page, which sends back whatever the candidate arrived with.
279 */
280 id: TrimmedNonEmptyString,
281 kind: PullRequestReviewerKind,
282 /** A review has already been asked of them, so pressing them takes the request back. */
283 isRequested: Schema.Boolean,
284});
285export type PullRequestReviewerCandidate = typeof PullRequestReviewerCandidate.Type;
286
287export const PullRequestReviewerCandidateList = Schema.Struct({
288 /** Never includes the author: nobody is asked to review their own change request. */
289 candidates: Schema.Array(PullRequestReviewerCandidate),
290 /**
291 * The host has more people with access than the read asked for, so somebody missing from the
292 * menu may still be somebody this change request can be sent to. Nothing is wrong with the
293 * list; it is simply not all of it.
294 */
295 truncated: Schema.Boolean,
296});
297export type PullRequestReviewerCandidateList = typeof PullRequestReviewerCandidateList.Type;
298
299/** A label the repository defines, with whether this change request already wears it. */
300export const PullRequestLabelCandidate = Schema.Struct({
301 ...PullRequestLabel.fields,
302 description: Schema.NullOr(Schema.String),
303 isApplied: Schema.Boolean,
304});
305export type PullRequestLabelCandidate = typeof PullRequestLabelCandidate.Type;
306
307export const PullRequestLabelCandidateList = Schema.Struct({
308 candidates: Schema.Array(PullRequestLabelCandidate),
309 /** The repository defines more labels than the read asked for; the list is not all of them. */
310 truncated: Schema.Boolean,
311});
312export type PullRequestLabelCandidateList = typeof PullRequestLabelCandidateList.Type;
313
314export const PullRequestCommit = Schema.Struct({
315 oid: TrimmedNonEmptyString,
316 messageHeadline: Schema.String,
317 committedDate: IsoDateTime,
318 /** Per-commit line counts where the host can return them without a request per commit. */
319 additions: Schema.optional(NonNegativeInt),
320 deletions: Schema.optional(NonNegativeInt),
321 /**
322 * Everyone the host attributes the commit to. Optional because older servers and hosts that
323 * do not expose commit authors still produce a useful timeline entry.
324 */
325 authors: Schema.optional(Schema.Array(PullRequestActor)),
326});
327export type PullRequestCommit = typeof PullRequestCommit.Type;
328
329/**
330 * What a host can do with a review, which is where the four differ most. GitLab has no way to
331 * say "changes requested", and Azure DevOps exposes no diff through its CLI, so it has no lines
332 * to write against at all.
333 */
334export const PullRequestReviewCapabilities = Schema.Struct({
335 /** A new comment can be anchored to a line of the diff. */
336 inlineComment: Schema.Boolean,
337 /** An existing thread can be replied to. */
338 reply: Schema.Boolean,
339 /** A thread can be marked resolved, and unresolved again. */
340 resolve: Schema.Boolean,
341 /** The verdicts a submitted review can carry. Empty means reviews cannot be submitted. */
342 verdicts: Schema.Array(PullRequestReviewVerdict),
343});
344export type PullRequestReviewCapabilities = typeof PullRequestReviewCapabilities.Type;
345
346/**
347 * What a host lets be rewritten after it has been posted. The two are separate because a host can
348 * take one without the other: Azure DevOps takes a new title and description through the same
349 * command that closes a pull request, and has no way to post a remark here at all — so nothing it
350 * shows in a conversation can be rewritten either.
351 */
352export const PullRequestEditCapabilities = Schema.Struct({
353 /** The change request's own title and description can be rewritten. */
354 changeRequest: Schema.Boolean,
355 /** A remark can be rewritten by whoever wrote it. */
356 comment: Schema.Boolean,
357});
358export type PullRequestEditCapabilities = typeof PullRequestEditCapabilities.Type;
359
360/**
361 * What a host can do about who reviews. The two are independent: a host can take a request without
362 * publishing who may receive one, which is Azure DevOps.
363 */
364export const PullRequestReviewerCapabilities = Schema.Struct({
365 /** A review can be asked of somebody, and the request taken back. */
366 request: Schema.Boolean,
367 /**
368 * The people who may be asked can be listed. False where the host has no such list to give,
369 * which leaves the page to take a name rather than offer a menu — a guessed list would be worse,
370 * because a name missing from it reads as a name that cannot be asked.
371 */
372 listCandidates: Schema.Boolean,
373});
374export type PullRequestReviewerCapabilities = typeof PullRequestReviewerCapabilities.Type;
375
376/**
377 * Who remembers which files a reader has cleared. `host` is the host's own record, so the marks
378 * are the ones its web UI shows and a review can be carried on from either side. `environment` is
379 * this server's record, for a host that keeps none anything outside one browser can read.
380 */
381export const PullRequestViewedFilesStore = Schema.Literals(["host", "environment"]);
382export type PullRequestViewedFilesStore = typeof PullRequestViewedFilesStore.Type;
383
384/**
385 * What a provider can actually do, so a surface can hide what is missing rather than offer an
386 * action that would fail. Every provider fills this in for itself; nothing is assumed.
387 *
388 * Hosts differ more than they look: Azure DevOps exposes no patch through its CLI, and
389 * Bitbucket has no endpoint that reopens a declined pull request. Both would otherwise be dead
390 * buttons.
391 */
392export const PullRequestCapabilities = Schema.Struct({
393 /** A unified patch can be fetched for the change request. */
394 diff: Schema.Boolean,
395 /** A comment can be posted, and the conversation read back. */
396 comment: Schema.Boolean,
397 /** The actions this host can carry out; anything absent is never offered. */
398 actions: Schema.Array(PullRequestAction),
399 /** Merge strategies the provider itself offers, before repository settings narrow them. */
400 mergeMethods: Schema.Array(PullRequestMergeMethod),
401 /**
402 * How this host can bring a stale branch up to date. Absent where it cannot at all, which is
403 * every host that has not said otherwise — so a provider that says nothing offers nothing.
404 */
405 updateMethods: Schema.optional(Schema.Array(PullRequestUpdateMethod)),
406 /**
407 * The host can narrow a listing by free text. False means it answers unnarrowed and whoever
408 * asked has to do the narrowing — which is a different promise, so the page is told rather
409 * than left to show every change request on that host as a search result.
410 */
411 search: Schema.Boolean,
412 /**
413 * Reactions can be read from a remark, and added to one or taken back. One flag for both:
414 * neither host here reports reactions it will not also take, and a surface that could show a
415 * pill it may never press is a surface offering nothing. Optional for the same reason as
416 * `updateMethods` and `edit`: a server that says nothing about reactions has none, which is
417 * what every server before this field was.
418 */
419 reactions: Schema.optional(Schema.Boolean),
420 /**
421 * Where the reader's own marks are kept, or absent where they are kept nowhere and the
422 * checkbox is not offered at all. Optional for the same reason as `reactions`.
423 */
424 viewedFiles: Schema.optional(PullRequestViewedFilesStore),
425 review: PullRequestReviewCapabilities,
426 reviewers: PullRequestReviewerCapabilities,
427 /**
428 * What can be rewritten after the fact. Optional so a page and a server of different ages still
429 * speak to each other: a server that says nothing about rewriting is one that cannot, which is
430 * what every server before this one was.
431 */
432 edit: Schema.optional(PullRequestEditCapabilities),
433 /**
434 * The host keeps stacks of change requests as objects of its own, so a linked thread can show
435 * the stack the host shows. Absent means chains are only ever inferred from base branches.
436 */
437 stacks: Schema.optional(Schema.Boolean),
438 stackActions: Schema.optional(Schema.Boolean),
439 /**
440 * The repository's labels can be listed, and one put on a change request or taken off it.
441 * Optional for the same reason `edit` is: a server that says nothing about labels has no way
442 * to change them, which is what every server before this field was.
443 */
444 labels: Schema.optional(Schema.Boolean),
445});
446export type PullRequestCapabilities = typeof PullRequestCapabilities.Type;
447
448/**
449 * What the signed-in account may do with this change request, which is a different question from
450 * `capabilities`: that says what the host is able to do at all, and this says whether this viewer
451 * is allowed to ask for it. A host that merges pull requests still will not let a stranger merge
452 * one, so a control belongs on the page only where the two agree.
453 *
454 * A permission the host reports nothing about is granted rather than withheld. Hiding a control
455 * from someone who may in fact use it leaves them no way through and no reason given, while
456 * offering one they may not use ends in the host's own refusal — which at least says why.
457 */
458export const PullRequestViewerPermissions = Schema.Struct({
459 /** May request remote stack rebases, including when this layer is already current. */
460 stackRebase: Schema.optional(Schema.Boolean),
461 /** Which of the actions this viewer may take; anything absent is theirs to look at only. */
462 actions: Schema.Array(PullRequestAction),
463 /** This viewer may write a remark: a comment, a reply, or a note against a line. */
464 comment: Schema.Boolean,
465 /** This viewer may mark a review conversation resolved, and unresolved again. */
466 resolve: Schema.Boolean,
467 /** The verdicts this viewer may submit a review with. Empty means they may not review. */
468 verdicts: Schema.Array(PullRequestReviewVerdict),
469 /** This viewer may ask somebody for a review, and take the request back again. */
470 requestReviewers: Schema.Boolean,
471 /**
472 * The ways this viewer may bring the branch up to date, narrowed from what the host offers.
473 * Absent or empty means they may not, which is also what a host with no such action says.
474 */
475 updateMethods: Schema.optional(Schema.Array(PullRequestUpdateMethod)),
476 /**
477 * This viewer may put a label on the change request, and take one off. Absent is granted, like
478 * every permission here; the capability beside it is what decides whether a label can be
479 * changed on this host at all.
480 */
481 labels: Schema.optional(Schema.Boolean),
482});
483export type PullRequestViewerPermissions = typeof PullRequestViewerPermissions.Type;
484
485export const PullRequestMergeCapabilities = Schema.Struct({
486 merge: Schema.Boolean,
487 squash: Schema.Boolean,
488 rebase: Schema.Boolean,
489});
490export type PullRequestMergeCapabilities = typeof PullRequestMergeCapabilities.Type;
491
492export const PullRequestStackMembership = Schema.Struct({
493 number: PositiveInt,
494 position: PositiveInt,
495 size: PositiveInt,
496 base: TrimmedNonEmptyString,
497});
498export type PullRequestStackMembership = typeof PullRequestStackMembership.Type;
499
500export const PullRequestListEntry = Schema.Struct({
501 stack: Schema.optional(PullRequestStackMembership),
502 provider: SourceControlProviderKind,
503 /**
504 * The host below which `repository` is addressed, so the same provider kind can serve more
505 * than one account — github.com and a GitHub Enterprise install are different identities.
506 */
507 host: TrimmedNonEmptyString,
508 projectId: ProjectId,
509 projectTitle: TrimmedNonEmptyString,
510 repository: TrimmedNonEmptyString,
511 number: PositiveInt,
512 title: TrimmedNonEmptyString,
513 url: TrimmedNonEmptyString,
514 author: Schema.NullOr(PullRequestActor),
515 headBranch: TrimmedNonEmptyString,
516 baseBranch: TrimmedNonEmptyString,
517 state: PullRequestState,
518 isDraft: Schema.Boolean,
519 mergeability: PullRequestMergeability,
520 /**
521 * Zero where the host has not been asked for the counts yet, which on GitHub is every row a
522 * listing hands over: they are read afterwards, through `PullRequestListStatsInput`. Zero is
523 * what a host that reports no counts at all has always sent, and what the page already draws
524 * as no stat rather than as an empty change.
525 */
526 additions: NonNegativeInt,
527 deletions: NonNegativeInt,
528 createdAt: IsoDateTime,
529 updatedAt: IsoDateTime,
530 /** Server epoch milliseconds when the provider read started; preserved on cache hits. */
531 observedAt: Schema.optional(Schema.Finite),
532 viewerReviewRequested: Schema.Boolean,
533 labels: Schema.Array(PullRequestLabel),
534 /** Absent where the host does not summarise its reviews, which is every host but GitHub. */
535 reviewDecision: Schema.optional(PullRequestReviewDecision),
536 /** Absent where the host reports no check rollup, or the change request has no checks. */
537 checksState: Schema.optional(PullRequestChecksState),
538});
539export type PullRequestListEntry = typeof PullRequestListEntry.Type;
540
541/**
542 * Where each repository a listing already reached carries on from, keyed `"<host> <repository>"`
543 * — which is how a listing tells two repositories apart, since the same `owner/repo` exists on
544 * github.com and on an Enterprise install at once.
545 *
546 * Each value is opaque: only the provider that issued one knows what it means, and the page hands
547 * back exactly what it was given rather than composing one.
548 */
549export const PullRequestListCursors = Schema.Record(
550 TrimmedNonEmptyString,
551 // Bounded because it arrives from the page and is unfolded into a host's own filter.
552 TrimmedNonEmptyString.check(Schema.isMaxLength(4096)),
553);
554export type PullRequestListCursors = typeof PullRequestListCursors.Type;
555
556export const PullRequestListInput = Schema.Struct({
557 state: PullRequestListState,
558 involvement: Schema.optional(PullRequestInvolvement),
559 filters: Schema.optional(PullRequestListFilters),
560 projectId: Schema.optional(ProjectId),
561 /**
562 * Only these projects, for a client that assigns each shared repository to one of its
563 * connections and asks the others to stay quiet about it. Absent means every project, which
564 * is what every listing asked for before there were several connections to spread across.
565 */
566 projectIds: Schema.optional(Schema.Array(ProjectId).check(Schema.isMaxLength(100))),
567 /**
568 * Narrows the listing to one host, named as the host itself rather than as its provider kind:
569 * github.com and a GitHub Enterprise install are two accounts, and a kind cannot tell them
570 * apart. Absent means every host the workspace has.
571 */
572 host: Schema.optional(TrimmedNonEmptyString),
573 /** Rows to return per repository, which with a continuation is rows per slice. */
574 limit: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 500 }))),
575 /**
576 * Carry on from an answer already on the page rather than read the listing again. Only the
577 * repositories named here are read, one slice each — which is what makes a further page cost
578 * one slice rather than every repository over again at a larger size. A repository the page has
579 * enough of is simply left out.
580 *
581 * Absent asks for the listing from the top, which is what raising `limit` alone has always
582 * done and still does.
583 */
584 cursors: Schema.optional(PullRequestListCursors),
585 /**
586 * Free text the hosts themselves are asked to match, rather than a filter over the rows that
587 * have already arrived: a listing only ever holds a page per repository, so a search that
588 * never leaves the client can only find what happened to be loaded. What a match means is the
589 * host's own business — GitHub reads a body, GitLab a description — and a host with no text
590 * filter answers unnarrowed rather than pretending.
591 *
592 * Bounded because it travels into a CLI argument and a query string, and no host makes
593 * anything of a search term this long.
594 */
595 query: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(200))),
596});
597export type PullRequestListInput = typeof PullRequestListInput.Type;
598
599/**
600 * A host the workspace has projects on, and whether it can be read right now. Drives the host
601 * switcher and explains projects the list leaves out.
602 *
603 * One per host rather than per provider kind, because signing in is a question about the host:
604 * a workspace can hold repositories on github.com and on a GitHub Enterprise install at once,
605 * and the two are separate accounts that succeed and fail independently.
606 */
607export const PullRequestProviderSummary = Schema.Struct({
608 host: TrimmedNonEmptyString,
609 kind: SourceControlProviderKind,
610 /** False where a search has to be applied to the rows after they arrive. */
611 searchesOnHost: Schema.Boolean,
612 projectCount: PositiveInt,
613 /** False when the provider's CLI or credentials are missing, with `detail` saying which. */
614 configured: Schema.Boolean,
615 detail: Schema.NullOr(TrimmedNonEmptyString),
616});
617export type PullRequestProviderSummary = typeof PullRequestProviderSummary.Type;
618
619/** One project whose repository could not be read; healthy projects still return entries. */
620export const PullRequestListProjectError = Schema.Struct({
621 projectId: ProjectId,
622 projectTitle: TrimmedNonEmptyString,
623 message: TrimmedNonEmptyString,
624});
625export type PullRequestListProjectError = typeof PullRequestListProjectError.Type;
626
627export const PullRequestListResult = Schema.Struct({
628 /**
629 * The signed-in account per host, which is what involvement filtering compares. Keyed by
630 * host rather than by provider kind: two GitHub hosts are two accounts. A host that could
631 * not be read is absent rather than present-and-undefined, because an open-keyed record
632 * cannot carry an optional value through the JSON codec.
633 */
634 viewers: Schema.Record(TrimmedNonEmptyString, TrimmedNonEmptyString),
635 providers: Schema.Array(PullRequestProviderSummary),
636 /**
637 * By update, newest first, across every repository this answer covers. A page that appends a
638 * continuation sorts what it then holds the same way: a repository's next slice is older than
639 * that repository's last row, but can still be newer than another repository's.
640 */
641 entries: Schema.Array(PullRequestListEntry),
642 errors: Schema.Array(PullRequestListProjectError),
643 /** At least one repository hit the per-repository listing cap. */
644 truncated: Schema.Boolean,
645 /**
646 * Where each repository carries on, to be sent straight back as `cursors`. A repository is
647 * absent from this once it has nothing more to give, and also where its host could not be asked
648 * in an order a continuation means anything in — so an empty record beside `truncated` means
649 * more rows are only reachable by raising `limit`, the way they always were.
650 */
651 nextCursors: PullRequestListCursors,
652});
653export type PullRequestListResult = typeof PullRequestListResult.Type;
654
655/**
656 * Addresses one pull request for reads and writes. `projectId` picks the checkout the host
657 * CLI runs in and, when its repository matches, the credentials; `host` lets the server
658 * route a pull request from another repository through any project on the same host
659 * (a frontend project's thread linking a backend PR). Absent `host` means "the project's
660 * own host", which is every reference from before thread links became host-level.
661 */
662export const PullRequestRef = Schema.Struct({
663 projectId: ProjectId,
664 host: Schema.optional(TrimmedNonEmptyString),
665 /** Refuse a routed operation unless this GitHub account still owns the active credential. */
666 expectedAccountId: Schema.optional(TrimmedNonEmptyString),
667 /** Let another environment answer when this one's cached response has expired. */
668 allowStale: Schema.optional(Schema.Boolean),
669 repository: TrimmedNonEmptyString,
670 number: PositiveInt,
671});
672export type PullRequestRef = typeof PullRequestRef.Type;
673
674/** Account discovery never needs the originating project's private repository metadata. */
675export const PullRequestRoutingIdentityInput = Schema.Struct({
676 host: TrimmedNonEmptyString,
677});
678export type PullRequestRoutingIdentityInput = typeof PullRequestRoutingIdentityInput.Type;
679
680export const PullRequestRoutingIdentityResult = Schema.Struct({
681 accountId: TrimmedNonEmptyString,
682 host: TrimmedNonEmptyString,
683 provider: Schema.Literal("github"),
684 viewer: TrimmedNonEmptyString,
685});
686export type PullRequestRoutingIdentityResult = typeof PullRequestRoutingIdentityResult.Type;
687
688export const PullRequestRoutingResult = Schema.Struct({
689 accountId: TrimmedNonEmptyString,
690 host: TrimmedNonEmptyString,
691 provider: SourceControlProviderKind,
692 viewer: TrimmedNonEmptyString,
693 projectTitle: TrimmedNonEmptyString,
694 workspaceRoot: TrimmedNonEmptyString,
695});
696export type PullRequestRoutingResult = typeof PullRequestRoutingResult.Type;
697
698export const PullRequestLinkedThreadsResult = Schema.Struct({
699 threads: Schema.Array(
700 Schema.Struct({
701 id: ThreadId,
702 projectId: ProjectId,
703 title: Schema.String,
704 archivedAt: Schema.NullOr(IsoDateTime),
705 }),
706 ),
707});
708export type PullRequestLinkedThreadsResult = typeof PullRequestLinkedThreadsResult.Type;
709
710/** The complete hover card, without checks, permissions, or branch comparisons. */
711export const PullRequestPreview = Schema.Struct({
712 projectId: ProjectId,
713 repository: TrimmedNonEmptyString,
714 number: PositiveInt,
715 title: TrimmedNonEmptyString,
716 url: TrimmedNonEmptyString,
717 author: Schema.NullOr(PullRequestActor),
718 state: PullRequestState,
719 isDraft: Schema.Boolean,
720 createdAt: IsoDateTime,
721});
722export type PullRequestPreview = typeof PullRequestPreview.Type;
723
724/**
725 * The small live shape a linked thread needs. Keeping it separate from detail means a sidebar
726 * status check never loads permissions, repository settings, checks, or base comparison data.
727 */
728export const PullRequestSummary = Schema.Struct({
729 provider: SourceControlProviderKind,
730 projectId: ProjectId,
731 repository: TrimmedNonEmptyString,
732 number: PositiveInt,
733 title: TrimmedNonEmptyString,
734 url: TrimmedNonEmptyString,
735 state: PullRequestState,
736 /** Present when the host says the open pull request is still a draft. */
737 isDraft: Schema.optional(Schema.Boolean),
738 headBranch: TrimmedNonEmptyString,
739 baseBranch: TrimmedNonEmptyString,
740 closedAt: Schema.optional(Schema.NullOr(Schema.String)),
741 mergedAt: Schema.optional(Schema.NullOr(Schema.String)),
742 updatedAt: IsoDateTime,
743 /** Server epoch milliseconds when the provider read started; preserved on cache hits. */
744 observedAt: Schema.optional(Schema.Finite),
745 author: Schema.optional(Schema.NullOr(PullRequestActor)),
746 additions: Schema.optional(NonNegativeInt),
747 deletions: Schema.optional(NonNegativeInt),
748 changedFiles: Schema.optional(NonNegativeInt),
749 reviewDecision: Schema.optional(Schema.NullOr(PullRequestReviewDecision)),
750 checksState: Schema.optional(Schema.NullOr(PullRequestChecksState)),
751 mergeability: Schema.optional(PullRequestMergeability),
752 /** Null when the host says the pull request is in no stack; absent when the read did not ask. */
753 stack: Schema.optional(Schema.NullOr(PullRequestStackMembership)),
754});
755export type PullRequestSummary = typeof PullRequestSummary.Type;
756
757/** The host-native stack a pull request belongs to, in the thread link's shape. */
758export const PullRequestStack = Schema.Struct({
759 id: TrimmedNonEmptyString,
760 number: PositiveInt,
761 url: TrimmedNonEmptyString,
762 base: TrimmedNonEmptyString,
763 layers: Schema.Array(
764 Schema.Struct({
765 number: PositiveInt,
766 title: Schema.optional(Schema.String),
767 isDraft: Schema.optional(Schema.Boolean),
768 headSha: Schema.optional(TrimmedNonEmptyString),
769 headBranch: TrimmedNonEmptyString,
770 state: PullRequestState,
771 }),
772 ),
773});
774export type PullRequestStack = typeof PullRequestStack.Type;
775
776/**
777 * One row's line counts, read after the listing rather than inside it. On GitHub the pair is
778 * 40-60% of the wall clock of the search that answers the whole page — measured over twelve
779 * repositories, 7.1s with it and 4.0s without — for two small numbers at the end of a row.
780 *
781 * So the listing answers without them and the page asks for them next, which is what puts the
782 * rows on screen at the speed of everything else on them.
783 */
784export const PullRequestDiffStat = Schema.Struct({
785 projectId: ProjectId,
786 repository: TrimmedNonEmptyString,
787 number: PositiveInt,
788 additions: NonNegativeInt,
789 deletions: NonNegativeInt,
790});
791export type PullRequestDiffStat = typeof PullRequestDiffStat.Type;
792
793/**
794 * The rows whose line counts are wanted, which is the rows the page is showing.
795 *
796 * Bounded at the largest listing a page can hold, since each ref becomes a lookup of its own on
797 * the host.
798 */
799export const PullRequestListStatsInput = Schema.Struct({
800 refs: Schema.Array(PullRequestRef).check(Schema.isMaxLength(500)),
801});
802export type PullRequestListStatsInput = typeof PullRequestListStatsInput.Type;
803
804/**
805 * Only the rows that could be answered for. A host with no batched stat read of its own — every
806 * one but GitHub, which all report the counts in the listing itself — is simply absent here, and
807 * the numbers its listing already carried stand.
808 */
809export const PullRequestListStatsResult = Schema.Struct({
810 stats: Schema.Array(PullRequestDiffStat),
811});
812export type PullRequestListStatsResult = typeof PullRequestListStatsResult.Type;
813
814/**
815 * Forget what the server has cached, so the next read asks the host. With a reference it
816 * forgets that one change request's detail and diff; without one it forgets the listings.
817 * A separate request rather than a flag on the reads, so an explicit "refresh" one person
818 * presses is the only thing that spends host requests — every ordinary read shares.
819 */
820export const PullRequestInvalidateInput = Schema.Struct({
821 reference: Schema.optional(PullRequestRef),
822 /** Refresh review progress across routed environments without discarding the patch. */
823 filesViewedOnly: Schema.optional(Schema.Boolean),
824});
825export type PullRequestInvalidateInput = typeof PullRequestInvalidateInput.Type;
826
827export const PullRequestDetail = Schema.Struct({
828 provider: SourceControlProviderKind,
829 capabilities: PullRequestCapabilities,
830 /** What this viewer may do, which `capabilities` says nothing about. Both narrow the page. */
831 viewerPermissions: PullRequestViewerPermissions,
832 projectId: ProjectId,
833 projectTitle: TrimmedNonEmptyString,
834 workspaceRoot: TrimmedNonEmptyString,
835 repository: TrimmedNonEmptyString,
836 number: PositiveInt,
837 title: TrimmedNonEmptyString,
838 body: Schema.String,
839 url: TrimmedNonEmptyString,
840 author: Schema.NullOr(PullRequestActor),
841 state: PullRequestState,
842 isDraft: Schema.Boolean,
843 mergeability: PullRequestMergeability,
844 additions: NonNegativeInt,
845 deletions: NonNegativeInt,
846 changedFiles: NonNegativeInt,
847 headBranch: TrimmedNonEmptyString,
848 headRepositoryNameWithOwner: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
849 /** The head commit, where the host reports it with the detail. */
850 headSha: Schema.optional(TrimmedNonEmptyString),
851 baseBranch: TrimmedNonEmptyString,
852 createdAt: IsoDateTime,
853 updatedAt: IsoDateTime,
854 /** Server epoch milliseconds when the provider read started; preserved on cache hits. */
855 observedAt: Schema.optional(Schema.Finite),
856 mergedAt: Schema.NullOr(IsoDateTime),
857 closedAt: Schema.NullOr(IsoDateTime),
858 reviewers: Schema.Array(PullRequestActor),
859 labels: Schema.Array(PullRequestLabel),
860 checks: Schema.Array(PullRequestCheck),
861 mergeCapabilities: PullRequestMergeCapabilities,
862 /**
863 * Who the host says the reader is, which is the one thing a conversation cannot be read without
864 * to tell the reader's own remarks from everybody else's — and rewriting a remark is offered
865 * only where the two names agree. Absent where the host could not say, which offers nothing
866 * rather than everything.
867 */
868 viewer: Schema.optional(TrimmedNonEmptyString),
869 /**
870 * Where the branch stands against its base. Optional so a host that cannot compare says
871 * nothing rather than claiming the branch is current — the page shows a banner only where the
872 * answer is "behind", and silence is not that answer.
873 */
874 baseComparison: Schema.optional(PullRequestBaseComparison),
875 /** How many commits the base is ahead by, where the host counted them. */
876 behindBy: Schema.optional(NonNegativeInt),
877 /**
878 * Whether the host is already armed to merge this on its own. Absent where the host does not
879 * report it, which is not the same as off: a page that reads silence as "not armed" offers to
880 * arm something that is already armed, and a second arming is a write nobody asked for.
881 */
882 autoMergeEnabled: Schema.optional(Schema.Boolean),
883 /** The strategy the host will use for an armed auto-merge, where it reports one. */
884 autoMergeMethod: Schema.optional(PullRequestMergeMethod),
885 /** GitHub Actions runs on this head commit that are waiting for a maintainer's approval. */
886 workflowApprovalsRequired: Schema.optional(NonNegativeInt),
887});
888export type PullRequestDetail = typeof PullRequestDetail.Type;
889
890export const PullRequestChecks = Schema.Struct({
891 state: PullRequestState,
892 checks: Schema.Array(PullRequestCheck),
893});
894export type PullRequestChecks = typeof PullRequestChecks.Type;
895
896/**
897 * The slower, conversation-shaped half of a change request. It is read independently from the
898 * core detail so a host with a deeply paginated review history cannot hold the title, body,
899 * checks, and actions off screen. `author` and `reviewers` are optional enrichments: GitHub's
900 * conversation query carries avatars and completed reviewers that its basic detail does not.
901 */
902export const PullRequestActivity = Schema.Struct({
903 author: Schema.optional(Schema.NullOr(PullRequestActor)),
904 reviewers: Schema.optional(Schema.Array(PullRequestActor)),
905 comments: Schema.Array(PullRequestComment),
906 /**
907 * How many remarks the host itself counts in the conversation, which is the number a surface
908 * showing a count has to show: `comments` carries what was read, and a reader who can see 225
909 * on the host is not reassured by a page that says 10. Never less than `comments` holds, and
910 * equal to it wherever the host was read to the end.
911 */
912 commentCount: NonNegativeInt,
913 /**
914 * The read stopped at a bound of its own before the host ran out, so `comments` holds less
915 * than `commentCount` counts. Nothing else: a conversation read whole is never truncated,
916 * however long it is.
917 */
918 commentsTruncated: Schema.Boolean,
919 reviewThreads: Schema.Array(PullRequestReviewThread),
920 commits: Schema.Array(PullRequestCommit),
921 /**
922 * The change request's own reactions — the ones on its description, which every host counts
923 * against the change request itself rather than against a remark in the conversation.
924 */
925 reactions: Schema.optional(Schema.Array(PullRequestReaction)),
926});
927export type PullRequestActivity = typeof PullRequestActivity.Type;
928
929/** The complete detail shape after the independently loaded activity has been applied. */
930export const PullRequestDetailView = Schema.Struct({
931 ...PullRequestDetail.fields,
932 ...PullRequestActivity.fields,
933 // A composed view always has the core identity fields, even when the activity did not enrich
934 // them. Re-declare them as required after the activity's optional overrides.
935 author: Schema.NullOr(PullRequestActor),
936 reviewers: Schema.Array(PullRequestActor),
937});
938export type PullRequestDetailView = typeof PullRequestDetailView.Type;
939
940/**
941 * A diff arrives a slice at a time, because a large change is more than any host will hand over
942 * at once — GitHub refuses outright past 300 files — and more than a viewer can lay out in one
943 * go. A slice is a whole number of files, never a file cut in half, so each one parses on its
944 * own and the reader can start on the first while the rest is still coming.
945 */
946export const PullRequestDiffInput = Schema.Struct({
947 ...PullRequestRef.fields,
948 /**
949 * Where to carry on from. Absent asks for the first slice. Opaque to the reader: each host
950 * counts its files its own way, and only the provider that issued one knows what it means.
951 */
952 cursor: Schema.optional(TrimmedNonEmptyString),
953 /**
954 * One commit of the change rather than the whole of it. A large pull request is easier to
955 * read a commit at a time, and the author's own commits are the boundaries they chose.
956 */
957 commit: Schema.optional(TrimmedNonEmptyString),
958});
959export type PullRequestDiffInput = typeof PullRequestDiffInput.Type;
960
961/** Real line counts for a file whose hunks the host withheld from the patch. */
962export const PullRequestOmittedFileStat = Schema.Struct({
963 path: TrimmedNonEmptyString,
964 additions: Schema.Number,
965 deletions: Schema.Number,
966});
967export type PullRequestOmittedFileStat = typeof PullRequestOmittedFileStat.Type;
968
969export const PullRequestDiffResult = Schema.Struct({
970 patch: Schema.String,
971 /**
972 * Something inside this slice could not be shown — a binary file, or a hunk the host declined
973 * to inline. Not the same as there being more slices, which `nextCursor` answers.
974 */
975 truncated: Schema.Boolean,
976 /** Where the next slice starts, or null once the diff is whole. */
977 nextCursor: Schema.NullOr(TrimmedNonEmptyString),
978 /**
979 * The host's own counts for the files whose hunks it withheld, so a file the patch cannot
980 * show still reports what changed instead of a zero the diff never had.
981 */
982 omittedFileStats: Schema.optional(Schema.Array(PullRequestOmittedFileStat)),
983});
984export type PullRequestDiffResult = typeof PullRequestDiffResult.Type;
985
986/** The complete old and new files Pierre needs to open omitted context in a host-backed patch. */
987export const PullRequestDiffFileContentsInput = Schema.Struct({
988 ...PullRequestRef.fields,
989 /** One commit's own comparison; absent means the whole change request. */
990 commit: Schema.optional(TrimmedNonEmptyString),
991 changeType: Schema.Literals(["change", "rename-pure", "rename-changed", "new", "deleted"]),
992 oldPath: TrimmedNonEmptyString,
993 newPath: TrimmedNonEmptyString,
994});
995export type PullRequestDiffFileContentsInput = typeof PullRequestDiffFileContentsInput.Type;
996
997export const PullRequestDiffFileContentsResult = Schema.Struct({
998 oldContents: Schema.String,
999 newContents: Schema.String,
1000});
1001export type PullRequestDiffFileContentsResult = typeof PullRequestDiffFileContentsResult.Type;
1002
1003/**
1004 * Bounded because a path arrives from a client rather than from the host: one element of a write
1005 * batch could otherwise carry a megabyte into a SQL statement or a GraphQL field.
1006 */
1007const MAX_FILE_PATH_LENGTH = 4096;
1008/**
1009 * Not trimmed: a leading or trailing space is a legal part of a file's name, and the mark is
1010 * keyed by the name the host gave, so trimming files it under a name nothing else uses.
1011 */
1012const FilePath = Schema.String.check(Schema.isNonEmpty(), Schema.isMaxLength(MAX_FILE_PATH_LENGTH));
1013
1014/**
1015 * Where one file of a change request stands with the person reading it. `dismissed` is the file
1016 * that was cleared and has since been pushed to, which is worth telling a reader apart from
1017 * `unviewed`: this file and not the other forty is the one that moved.
1018 */
1019export const PullRequestFileViewedState = Schema.Literals(["unviewed", "viewed", "dismissed"]);
1020export type PullRequestFileViewedState = typeof PullRequestFileViewedState.Type;
1021
1022export const PullRequestFileViewed = Schema.Struct({
1023 path: FilePath,
1024 state: PullRequestFileViewedState,
1025});
1026export type PullRequestFileViewed = typeof PullRequestFileViewed.Type;
1027
1028/**
1029 * Which files of a change request the reader has cleared. Its own read rather than a field on the
1030 * patch: a patch changes when somebody pushes and is cached by the minute, this changes on every
1031 * press, so one read would have to be wrong for the other to be right.
1032 */
1033export const PullRequestFilesViewedResult = Schema.Struct({
1034 /** Only the files the host reported a state for. A file missing from this list is unviewed. */
1035 files: Schema.Array(PullRequestFileViewed),
1036 /** The host had more files than were read, so the count is short and says so. */
1037 truncated: Schema.Boolean,
1038});
1039export type PullRequestFilesViewedResult = typeof PullRequestFilesViewedResult.Type;
1040
1041/**
1042 * How many presses one write carries. Every element is a statement of its own inside one
1043 * transaction here, or a field of its own in one GraphQL document on GitHub. Matched to what a
1044 * read of the marks carries, so a client cannot write more of them than it can ever read back.
1045 */
1046const MAX_FILES_VIEWED_PRESSES = 500;
1047
1048/**
1049 * Files to clear, or to put back. Several at once because a reader working down a diff ticks
1050 * boxes far faster than a host answers, so a burst is gathered into one request.
1051 */
1052export const PullRequestSetFilesViewedInput = Schema.Struct({
1053 ...PullRequestRef.fields,
1054 files: Schema.Array(
1055 Schema.Struct({
1056 path: FilePath,
1057 viewed: Schema.Boolean,
1058 }),
1059 ).check(Schema.isMaxLength(MAX_FILES_VIEWED_PRESSES)),
1060});
1061export type PullRequestSetFilesViewedInput = typeof PullRequestSetFilesViewedInput.Type;
1062
1063export const PullRequestStackHead = Schema.Struct({
1064 number: PositiveInt,
1065 headSha: TrimmedNonEmptyString,
1066});
1067export type PullRequestStackHead = typeof PullRequestStackHead.Type;
1068
1069export const PullRequestActionInput = Schema.Struct({
1070 /** Native stack scope; only send to environments advertising pullRequestStackActions. */
1071 stackNumber: Schema.optional(PositiveInt),
1072 expectedStackHeads: Schema.optional(Schema.Array(PullRequestStackHead)),
1073 ...PullRequestRef.fields,
1074 action: PullRequestAction,
1075 /**
1076 * Which strategy the merge uses, read for `merge` and for `enable-auto-merge` alike — a merge
1077 * the host performs later is still a merge, and the strategy is chosen when it is armed rather
1078 * than at the moment it happens. Absent means the host's own default.
1079 */
1080 mergeMethod: Schema.optional(PullRequestMergeMethod),
1081 /** Only read for `update-branch`, where absent means the host's own default. */
1082 updateMethod: Schema.optional(PullRequestUpdateMethod),
1083});
1084export type PullRequestActionInput = typeof PullRequestActionInput.Type;
1085
1086// Not trimmed: the body is markdown, where leading spaces open a code block and two trailing
1087// spaces are a line break. GitHub rejects bodies past 65536 characters, so that bound is
1088// enforced here to keep oversized payloads off the wire and out of subprocess plumbing; the
1089// service rejects a body that is only whitespace.
1090const CommentBody = Schema.String.check(Schema.isNonEmpty()).check(Schema.isMaxLength(65_536));
1091
1092export const PullRequestCommentInput = Schema.Struct({
1093 ...PullRequestRef.fields,
1094 body: CommentBody,
1095});
1096export type PullRequestCommentInput = typeof PullRequestCommentInput.Type;
1097
1098/**
1099 * A change request's own words rewritten: its title, its description, or both. Each is optional
1100 * because the page rewrites them apart — a title corrected on its own must not carry a description
1101 * nobody opened — and the service refuses a request that carries neither.
1102 *
1103 * The title is bounded far past what any host takes, GitHub refusing one past 256 characters, so
1104 * an oversized one is turned away before it reaches a subprocess. The body is not trimmed for the
1105 * same reason a comment is not: it is markdown, where leading spaces open a code block. An empty
1106 * one is allowed, which is how a description is cleared.
1107 */
1108export const PullRequestUpdateInput = Schema.Struct({
1109 ...PullRequestRef.fields,
1110 title: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(1024))),
1111 body: Schema.optional(Schema.String.check(Schema.isMaxLength(65_536))),
1112});
1113export type PullRequestUpdateInput = typeof PullRequestUpdateInput.Type;
1114
1115/**
1116 * A remark already posted, rewritten by whoever wrote it. The kind travels beside the id because
1117 * an id alone does not say which endpoint addresses it: GitHub rewrites a comment on the
1118 * conversation and a comment on a line through two different mutations.
1119 *
1120 * A review's own summary is not among them. The hosts disagree about what one even is — the entry
1121 * Bitbucket shows is a vote this page renders as a remark, with no words behind it to rewrite —
1122 * and a control that worked on one host and failed on another is worse than one that is not there.
1123 */
1124export const PullRequestCommentUpdateInput = Schema.Struct({
1125 ...PullRequestRef.fields,
1126 commentId: TrimmedNonEmptyString,
1127 kind: Schema.Literals(["issue-comment", "review-comment"]),
1128 body: CommentBody,
1129});
1130export type PullRequestCommentUpdateInput = typeof PullRequestCommentUpdateInput.Type;
1131
1132/** The coordinates of one line in a pull request diff. */
1133export const PullRequestReviewPosition = Schema.Union([
1134 Schema.Struct({
1135 kind: Schema.Literal("added"),
1136 newLine: PositiveInt,
1137 }),
1138 Schema.Struct({
1139 kind: Schema.Literal("deleted"),
1140 oldLine: PositiveInt,
1141 }),
1142 Schema.Struct({
1143 kind: Schema.Literal("context"),
1144 oldLine: PositiveInt,
1145 newLine: PositiveInt,
1146 /** Which copy of an unchanged line the reviewer selected in a split diff. */
1147 side: PullRequestDiffSide,
1148 }),
1149]);
1150export type PullRequestReviewPosition = typeof PullRequestReviewPosition.Type;
1151
1152/** One remark in a review that has not been sent yet, anchored to a line of the diff. */
1153export const PullRequestReviewCommentDraft = Schema.Struct({
1154 path: TrimmedNonEmptyString,
1155 /**
1156 * What the file was called before the change, sent only when it differs. GitLab resolves a
1157 * position against both sides of the diff, so a comment on a renamed file needs both names;
1158 * the hosts that address a comment by one path ignore this.
1159 */
1160 oldPath: Schema.optional(TrimmedNonEmptyString),
1161 position: PullRequestReviewPosition,
1162 body: CommentBody,
1163});
1164export type PullRequestReviewCommentDraft = typeof PullRequestReviewCommentDraft.Type;
1165
1166/**
1167 * A whole review, sent in one go. The line comments travel with the verdict rather than being
1168 * posted as they are written, so a half-finished review is never visible to anyone else — and
1169 * so hosts with no notion of a pending review behave the same as the ones that have it.
1170 */
1171export const PullRequestSubmitReviewInput = Schema.Struct({
1172 ...PullRequestRef.fields,
1173 verdict: PullRequestReviewVerdict,
1174 /** The review's own words. May be empty, which is how an approval with no remarks is sent. */
1175 body: Schema.String.check(Schema.isMaxLength(65_536)),
1176 comments: Schema.Array(PullRequestReviewCommentDraft),
1177});
1178export type PullRequestSubmitReviewInput = typeof PullRequestSubmitReviewInput.Type;
1179
1180export const PullRequestThreadCommentsInput = Schema.Struct({
1181 ...PullRequestRef.fields,
1182 threadId: TrimmedNonEmptyString,
1183 cursor: TrimmedNonEmptyString,
1184});
1185export type PullRequestThreadCommentsInput = typeof PullRequestThreadCommentsInput.Type;
1186
1187export const PullRequestThreadCommentsResult = Schema.Struct({
1188 comments: Schema.Array(PullRequestThreadComment),
1189 nextCursor: Schema.NullOr(TrimmedNonEmptyString),
1190});
1191export type PullRequestThreadCommentsResult = typeof PullRequestThreadCommentsResult.Type;
1192
1193export const PullRequestThreadReplyInput = Schema.Struct({
1194 ...PullRequestRef.fields,
1195 threadId: TrimmedNonEmptyString,
1196 body: CommentBody,
1197});
1198export type PullRequestThreadReplyInput = typeof PullRequestThreadReplyInput.Type;
1199
1200export const PullRequestThreadResolutionInput = Schema.Struct({
1201 ...PullRequestRef.fields,
1202 threadId: TrimmedNonEmptyString,
1203 resolved: Schema.Boolean,
1204});
1205export type PullRequestThreadResolutionInput = typeof PullRequestThreadResolutionInput.Type;
1206
1207/**
1208 * Reacting and taking the reaction back are one operation with `reacted` turned around, which is
1209 * what pressing the same pill twice is.
1210 */
1211export const PullRequestReactionInput = Schema.Struct({
1212 ...PullRequestRef.fields,
1213 /**
1214 * Which remark to react to, as it arrived in the conversation. Absent reacts to the change
1215 * request itself, which is where its description's reactions live — the id a host uses for
1216 * that is its own to work out.
1217 */
1218 subjectId: Schema.optional(TrimmedNonEmptyString),
1219 content: PullRequestReactionContent,
1220 reacted: Schema.Boolean,
1221});
1222export type PullRequestReactionInput = typeof PullRequestReactionInput.Type;
1223
1224/**
1225 * Asking for a review and taking the request back are one operation with `requested` turned
1226 * around, and asking again somebody who has already reviewed is what a re-request is — so that is
1227 * the same operation once more rather than a third one.
1228 */
1229export const PullRequestReviewerRequestInput = Schema.Struct({
1230 ...PullRequestRef.fields,
1231 /**
1232 * Who, as the candidate list named them. Bounded because every host bounds it as well — GitHub
1233 * refuses past fifteen — and because these travel into a request body a page composed.
1234 */
1235 reviewers: Schema.Array(
1236 Schema.Struct({ id: TrimmedNonEmptyString, kind: PullRequestReviewerKind }),
1237 ).check(Schema.isMinLength(1), Schema.isMaxLength(25)),
1238 requested: Schema.Boolean,
1239});
1240export type PullRequestReviewerRequestInput = typeof PullRequestReviewerRequestInput.Type;
1241
1242/**
1243 * Putting a label on and taking it off are one operation with `applied` turned around, which is
1244 * what pressing the same row in the menu twice is. Named by the label's own name, which is how
1245 * GitHub addresses one.
1246 */
1247export const PullRequestLabelChangeInput = Schema.Struct({
1248 ...PullRequestRef.fields,
1249 labels: Schema.Array(TrimmedNonEmptyString).check(Schema.isMinLength(1), Schema.isMaxLength(25)),
1250 applied: Schema.Boolean,
1251});
1252export type PullRequestLabelChangeInput = typeof PullRequestLabelChangeInput.Type;
1253
1254export const PullRequestUnavailableReason = Schema.Literals([
1255 "cli-missing",
1256 "cli-unauthenticated",
1257 "provider-unsupported",
1258]);
1259export type PullRequestUnavailableReason = typeof PullRequestUnavailableReason.Type;
1260
1261/**
1262 * What each host needs before it can be read, so a failure names the fix rather than the
1263 * symptom. Bitbucket is credentials on the server rather than a signed-in CLI, which is why
1264 * these are whole sentences instead of a tool name to interpolate.
1265 */
1266const PROVIDER_REQUIREMENT: Partial<
1267 Record<SourceControlProviderKind, { readonly missing: string; readonly unauthenticated: string }>
1268> = {
1269 github: {
1270 missing:
1271 "GitHub CLI (`gh`) is required to browse change requests on this host. Install it from https://cli.github.com/ and reload.",
1272 unauthenticated: "GitHub CLI is not authenticated. Run `gh auth login` and retry.",
1273 },
1274 forgejo: {
1275 missing:
1276 "Install Forgejo CLI (`fj` 0.6 or later) from https://codeberg.org/forgejo-contrib/forgejo-cli or Gitea CLI (`tea` 0.16 or later) from https://gitea.com/gitea/tea to browse Forgejo pull requests.",
1277 unauthenticated:
1278 "Authenticate your Forgejo or Gitea server with `fj --host <server-url> auth add-token` on the T3 Code server. If fj is missing or unconfigured for that server, use `tea login add`. A configured fj account must be repaired with fj.",
1279 },
1280 gitlab: {
1281 missing:
1282 "GitLab CLI (`glab`) is required to browse change requests on this host. Install it from https://gitlab.com/gitlab-org/cli and reload.",
1283 unauthenticated: "GitLab CLI is not authenticated. Run `glab auth login` and retry.",
1284 },
1285 "azure-devops": {
1286 missing:
1287 "Azure CLI (`az`) with the Azure DevOps extension is required. Install `az`, then run `az extension add --name azure-devops`.",
1288 unauthenticated: "Azure CLI is not signed in. Run `az login` and retry.",
1289 },
1290 bitbucket: {
1291 missing:
1292 "Bitbucket needs API credentials on the server. Add them in Settings → Source Control.",
1293 unauthenticated:
1294 "Bitbucket rejected the configured credentials. Check them in Settings → Source Control.",
1295 },
1296};
1297
1298/**
1299 * The host a project's repository is addressed below. `canonicalKey` is the normalized remote,
1300 * `host/owner/repo`, so its first segment is the host; the provider kind stands in when there is
1301 * no key to read, which keeps one bucket per kind for identities recorded before it existed.
1302 *
1303 * Shared between the server and the page so both bucket a workspace the same way — the page
1304 * knows its hosts before the listing answers, and the two must agree on what they are called.
1305 */
1306export function pullRequestHostOf(
1307 identity:
1308 | {
1309 readonly canonicalKey?: string | undefined;
1310 readonly locator?: { readonly remoteUrl: string } | undefined;
1311 }
1312 | null
1313 | undefined,
1314 kind: SourceControlProviderKind,
1315): string {
1316 if (kind === "forgejo") {
1317 try {
1318 const remote = new URL(identity?.locator?.remoteUrl ?? "");
1319 if (remote.protocol === "http:" || remote.protocol === "https:")
1320 return remote.host.toLowerCase();
1321 } catch {
1322 // SSH remotes retain their canonical host; the CLI resolves their web endpoint.
1323 }
1324 }
1325 const host = identity?.canonicalKey?.split("/")[0]?.trim();
1326 return host === undefined || host.length === 0 ? kind : host.toLowerCase();
1327}
1328
1329/**
1330 * `author:me` names whoever is signed in to the host being read, GitHub's `@me` spelled either
1331 * way. A host's own search would answer it, but the page and the server both re-check an author
1332 * against the row's login, so the name is resolved before either comparison rather than being
1333 * matched as the literal login "me".
1334 *
1335 * Null viewer where the host has not said who the reader is; the filter then stands as typed,
1336 * which is the same answer a search for a user named "me" would give.
1337 *
1338 * Shared between the server and the page so both narrow by the same login.
1339 */
1340export function resolvePullRequestAuthorFilter(
1341 author: string,
1342 viewer: string | null | undefined,
1343): string {
1344 const trimmed = author.trim();
1345 if (!/^@?me$/i.test(trimmed)) return trimmed;
1346 return viewer === null || viewer === undefined || viewer.trim().length === 0 ? trimmed : viewer;
1347}
1348
1349/**
1350 * What a host needs before it can be read, as a sentence to show wherever that host is reported
1351 * as unusable — the whole page when nothing can be read, and one entry in the host switcher when
1352 * only that host cannot. Null when the reason is not about setting a host up.
1353 */
1354export function pullRequestProviderRequirement(
1355 provider: SourceControlProviderKind,
1356 reason: PullRequestUnavailableReason,
1357): string | null {
1358 const requirement = PROVIDER_REQUIREMENT[provider];
1359 if (requirement === undefined) return null;
1360 switch (reason) {
1361 case "cli-missing":
1362 return requirement.missing;
1363 case "cli-unauthenticated":
1364 return requirement.unauthenticated;
1365 case "provider-unsupported":
1366 return null;
1367 }
1368}
1369
1370/**
1371 * The feature is switched off entirely. The message is derived from `reason` and the host
1372 * rather than from whatever the CLI printed, so it stays a stable sentence the UI can show
1373 * as-is; the underlying failure travels in `cause` (absent for `provider-unsupported`, which
1374 * has none).
1375 */
1376export class PullRequestUnavailableError extends Schema.TaggedError<PullRequestUnavailableError>()(
1377 "PullRequestUnavailableError",
1378 {
1379 reason: PullRequestUnavailableReason,
1380 provider: Schema.optional(SourceControlProviderKind),
1381 cause: Schema.optional(Schema.Defect()),
1382 },
1383 { httpApiStatus: 503 },
1384) {
1385 [HttpServerRespondable.symbol]() {
1386 return HttpServerResponse.schemaJson(PullRequestUnavailableError)(this, { status: 503 });
1387 }
1388
1389 override get message(): string {
1390 const requirement =
1391 this.provider === undefined ? undefined : PROVIDER_REQUIREMENT[this.provider];
1392 switch (this.reason) {
1393 case "cli-missing":
1394 return (
1395 requirement?.missing ?? "The tool this host is read through is not installed or set up."
1396 );
1397 case "cli-unauthenticated":
1398 return requirement?.unauthenticated ?? "This host has no working credentials.";
1399 case "provider-unsupported":
1400 return "Change requests cannot be browsed for this project's host yet.";
1401 }
1402 }
1403}
1404
1405export class PullRequestOperationError extends Schema.TaggedError<PullRequestOperationError>()(
1406 "PullRequestOperationError",
1407 {
1408 operation: Schema.String,
1409 detail: TrimmedNonEmptyString,
1410 cause: Schema.optional(Schema.Defect()),
1411 },
1412 { httpApiStatus: 502 },
1413) {
1414 [HttpServerRespondable.symbol]() {
1415 return HttpServerResponse.schemaJson(PullRequestOperationError)(this, { status: 502 });
1416 }
1417
1418 override get message(): string {
1419 return `Pull request operation ${this.operation} failed: ${this.detail}`;
1420 }
1421}