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