Open History to see the last commit
1import * as Schema from "effect/Schema";
2import * as HttpServerRespondable from "effect/http/HttpServerRespondable";
3import * as HttpServerResponse from "effect/http/HttpServerResponse";
4
5import {
6 IsoDateTime,
7 NonNegativeInt,
8 PositiveInt,
9 ProjectId,
10 ThreadId,
11 TrimmedNonEmptyString,
12} from "./baseSchemas.ts";
13import {
14 PullRequestActor,
15 PullRequestLabel,
16 PullRequestReaction,
17 PullRequestReactionContent,
18 PullRequestState,
19} from "./pullRequest.ts";
20
21/**
22 * Which tracker an issue lives in. Git hosts reuse their `SourceControlProviderKind` spelling
23 * (`github`, `gitlab`); standalone trackers get their own (`linear`, `jira`). Branded apart from
24 * the source control kind because the two sets are not the same: Bitbucket is a git host with no
25 * tracker, and Linear is a tracker with no git host.
26 */
27export const IssueTrackerKind = TrimmedNonEmptyString.check(
28 Schema.isMaxLength(64),
29 Schema.isPattern(/^[a-z][a-z0-9-]*$/),
30).pipe(Schema.brand("IssueTrackerKind"));
31export type IssueTrackerKind = typeof IssueTrackerKind.Type;
32
33export const IssueActor = PullRequestActor;
34export type IssueActor = typeof IssueActor.Type;
35
36export const IssueLabel = PullRequestLabel;
37export type IssueLabel = typeof IssueLabel.Type;
38
39/**
40 * Where an issue stands: the tracker's own name for the state ("In Review", "Closed as not
41 * planned"), which is what people read and what a move is asked for by, and the category every
42 * tracker has, which only filters and colours key off. Nothing moves an issue to "open" or
43 * "closed": it moves to one of the states its `IssueDetail.states` lists.
44 */
45export const IssueStateCategory = Schema.Literals(["open", "closed"]);
46export type IssueStateCategory = typeof IssueStateCategory.Type;
47
48export const IssueState = Schema.Struct({
49 category: IssueStateCategory,
50 name: TrimmedNonEmptyString,
51 /** How a closed state ended, where the tracker says: `completed`, or its word for not done. */
52 closeReason: Schema.optional(TrimmedNonEmptyString),
53});
54export type IssueState = typeof IssueState.Type;
55
56/** What a listing asks for: either category, or both. */
57export const IssueListState = Schema.Literals(["all", "open", "closed"]);
58export type IssueListState = typeof IssueListState.Type;
59
60export const IssueInvolvement = Schema.Literals(["all", "assigned", "authored", "mentioned"]);
61export type IssueInvolvement = typeof IssueInvolvement.Type;
62
63/** Newest first by the named date. */
64export const IssueListSort = Schema.Literals(["updated", "created"]);
65export type IssueListSort = typeof IssueListSort.Type;
66
67export const IssueReactionContent = PullRequestReactionContent;
68export type IssueReactionContent = typeof IssueReactionContent.Type;
69
70export const IssueReaction = PullRequestReaction;
71export type IssueReaction = typeof IssueReaction.Type;
72
73/**
74 * Addresses one issue: `scope` is the tracker's own container (`owner/repo`, a team key) and
75 * `key` the issue's native id in it. `host` is absent for the kind's public host. `projectId`
76 * names the project whose credentials serve the read, which need not own the issue.
77 */
78export const IssueRef = Schema.Struct({
79 projectId: ProjectId,
80 tracker: IssueTrackerKind,
81 host: Schema.optional(TrimmedNonEmptyString),
82 scope: TrimmedNonEmptyString,
83 key: TrimmedNonEmptyString,
84});
85export type IssueRef = typeof IssueRef.Type;
86
87/** A scope with no issue in it, for reads about the container itself: templates, candidates. */
88export const IssueScopeRef = Schema.Struct({
89 projectId: ProjectId,
90 tracker: IssueTrackerKind,
91 host: Schema.optional(TrimmedNonEmptyString),
92 scope: TrimmedNonEmptyString,
93});
94export type IssueScopeRef = typeof IssueScopeRef.Type;
95
96/** One row of a listing. Carries everything a list needs and nothing a detail needs. */
97export const IssueSummary = Schema.Struct({
98 tracker: IssueTrackerKind,
99 host: TrimmedNonEmptyString,
100 scope: TrimmedNonEmptyString,
101 key: TrimmedNonEmptyString,
102 /** How the tracker writes the key for people: `#123` on a git host, `ENG-123` on Linear. */
103 displayKey: TrimmedNonEmptyString,
104 title: TrimmedNonEmptyString,
105 url: TrimmedNonEmptyString,
106 author: Schema.NullOr(IssueActor),
107 state: IssueState,
108 createdAt: IsoDateTime,
109 updatedAt: IsoDateTime,
110 closedAt: Schema.NullOr(IsoDateTime),
111 assignees: Schema.Array(IssueActor),
112 labels: Schema.Array(IssueLabel),
113 commentCount: NonNegativeInt,
114});
115export type IssueSummary = typeof IssueSummary.Type;
116
117export const IssueListEntry = Schema.Struct({
118 ...IssueSummary.fields,
119 projectId: ProjectId,
120 projectTitle: TrimmedNonEmptyString,
121});
122export type IssueListEntry = typeof IssueListEntry.Type;
123
124export const IssueComment = Schema.Struct({
125 id: TrimmedNonEmptyString,
126 author: Schema.NullOr(IssueActor),
127 body: Schema.String,
128 createdAt: IsoDateTime,
129 url: Schema.NullOr(Schema.String),
130 /** Absent from a tracker with no reactions, which differs from none on this comment. */
131 reactions: Schema.optional(Schema.Array(IssueReaction)),
132});
133export type IssueComment = typeof IssueComment.Type;
134
135/**
136 * Something that happened to an issue that nobody wrote words for: it was closed, labelled,
137 * assigned, renamed. Kinds are the common vocabulary; a tracker drops what it cannot map rather
138 * than guessing.
139 */
140export const IssueEventKind = Schema.Literals([
141 "closed",
142 "reopened",
143 "labeled",
144 "unlabeled",
145 "assigned",
146 "unassigned",
147 "renamed",
148 "referenced",
149 "state-changed",
150]);
151export type IssueEventKind = typeof IssueEventKind.Type;
152
153export const IssueEvent = Schema.Struct({
154 id: TrimmedNonEmptyString,
155 kind: IssueEventKind,
156 actor: Schema.NullOr(IssueActor),
157 createdAt: IsoDateTime,
158 /** What the event was about, where it has a subject: a label name, a login, a new title. */
159 detail: Schema.NullOr(TrimmedNonEmptyString),
160});
161export type IssueEvent = typeof IssueEvent.Type;
162
163/** A change request the tracker says closes or mentions this issue. */
164export const IssueLinkedPullRequest = Schema.Struct({
165 repository: TrimmedNonEmptyString,
166 number: PositiveInt,
167 title: TrimmedNonEmptyString,
168 url: TrimmedNonEmptyString,
169 state: PullRequestState,
170 isDraft: Schema.Boolean,
171 /** Merging it closes this issue, rather than only citing it. */
172 closesIssue: Schema.Boolean,
173});
174export type IssueLinkedPullRequest = typeof IssueLinkedPullRequest.Type;
175
176/**
177 * What a tracker can do at all, so a surface never offers an action that would fail. The
178 * service refuses a call outside these before it reaches the driver.
179 */
180export const IssueCapabilities = Schema.Struct({
181 create: Schema.Boolean,
182 /** Title and body can be rewritten after filing. */
183 edit: Schema.Boolean,
184 comment: Schema.Boolean,
185 labels: Schema.Boolean,
186 assignees: Schema.Boolean,
187 /** Reactions can be read and written on the issue and its comments. */
188 reactions: Schema.Boolean,
189 /** The scope offers starting points for a new issue. */
190 templates: Schema.Boolean,
191 /** The tracker reports linked change requests of its own accord. */
192 linkedPullRequests: Schema.Boolean,
193 /** State changes arrive as events beside the comments, not only as comments. */
194 events: Schema.Boolean,
195 /** The tracker narrows a listing by free text itself. */
196 search: Schema.Boolean,
197});
198export type IssueCapabilities = typeof IssueCapabilities.Type;
199
200/**
201 * What the signed-in account may do with this issue, which `capabilities` says nothing about.
202 * A permission the tracker reports nothing about is granted, so a refusal comes from the
203 * tracker, which says why, rather than from a missing control.
204 */
205export const IssueViewerPermissions = Schema.Struct({
206 setState: Schema.Boolean,
207 comment: Schema.Boolean,
208 edit: Schema.Boolean,
209 labels: Schema.Boolean,
210 assignees: Schema.Boolean,
211});
212export type IssueViewerPermissions = typeof IssueViewerPermissions.Type;
213
214export const IssueDetail = Schema.Struct({
215 ...IssueSummary.fields,
216 projectId: ProjectId,
217 projectTitle: TrimmedNonEmptyString,
218 /** False for an issue outside every project's scope, which is read-only through this server. */
219 writable: Schema.Boolean,
220 capabilities: IssueCapabilities,
221 viewerPermissions: IssueViewerPermissions,
222 /**
223 * Every state this issue can be in, in the tracker's workflow order, its current one included.
224 * Empty means its state cannot be changed from here.
225 */
226 states: Schema.Array(IssueState),
227 /** The signed-in account, so a comment edit is offered only on the viewer's own remarks. */
228 viewer: Schema.NullOr(TrimmedNonEmptyString),
229 body: Schema.String,
230 reactions: Schema.optional(Schema.Array(IssueReaction)),
231 comments: Schema.Array(IssueComment),
232 /** Where older comments carry on; null once the conversation is whole. */
233 commentsCursor: Schema.NullOr(TrimmedNonEmptyString),
234 events: Schema.Array(IssueEvent),
235 linkedPullRequests: Schema.Array(IssueLinkedPullRequest),
236});
237export type IssueDetail = typeof IssueDetail.Type;
238
239/** Older comments, one page past a cursor the detail or a previous page handed over. */
240export const IssueCommentsPageInput = Schema.Struct({
241 ...IssueRef.fields,
242 cursor: TrimmedNonEmptyString.check(Schema.isMaxLength(4_096)),
243});
244export type IssueCommentsPageInput = typeof IssueCommentsPageInput.Type;
245
246export const IssueCommentsPage = Schema.Struct({
247 comments: Schema.Array(IssueComment),
248 nextCursor: Schema.NullOr(TrimmedNonEmptyString),
249});
250export type IssueCommentsPage = typeof IssueCommentsPage.Type;
251
252/** Where each scope a listing already reached carries on, keyed `"<host> <scope>"`. */
253export const IssueListCursors = Schema.Record(
254 TrimmedNonEmptyString,
255 TrimmedNonEmptyString.check(Schema.isMaxLength(4_096)),
256);
257export type IssueListCursors = typeof IssueListCursors.Type;
258
259export const IssueListInput = Schema.Struct({
260 state: IssueListState,
261 involvement: Schema.optional(IssueInvolvement),
262 projectId: Schema.optional(ProjectId),
263 /** Narrows to one host, named as the host itself. Absent means every host the workspace has. */
264 host: Schema.optional(TrimmedNonEmptyString),
265 /** Rows per scope, which with a continuation is rows per slice. */
266 limit: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 200 }))),
267 /** Carry on from an earlier answer: only the scopes named here are read, one slice each. */
268 cursors: Schema.optional(IssueListCursors),
269 /** An explicit refresh of the first page may also refresh tracker metadata. */
270 refresh: Schema.optional(Schema.Boolean),
271 /** Free text the trackers themselves are asked to match. */
272 query: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(200))),
273 /** Label names an issue must carry, every one of them. */
274 labels: Schema.optional(
275 Schema.Array(TrimmedNonEmptyString.check(Schema.isMaxLength(200))).check(
276 Schema.isMaxLength(20),
277 ),
278 ),
279 /** An assignee's `login`, as an issue of that tracker names them. */
280 assignee: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(320))),
281 /** Absent means `updated`. */
282 sort: Schema.optional(IssueListSort),
283});
284export type IssueListInput = typeof IssueListInput.Type;
285
286/** One tracker account the workspace has projects on, and whether it can be read right now. */
287export const IssueTrackerSummary = Schema.Struct({
288 tracker: IssueTrackerKind,
289 host: TrimmedNonEmptyString,
290 projectCount: PositiveInt,
291 capabilities: Schema.NullOr(IssueCapabilities),
292 /** False when the tracker's tool or credentials are missing, with `detail` saying which. */
293 configured: Schema.Boolean,
294 detail: Schema.NullOr(TrimmedNonEmptyString),
295});
296export type IssueTrackerSummary = typeof IssueTrackerSummary.Type;
297
298export const IssueListProjectError = Schema.Struct({
299 projectId: ProjectId,
300 projectTitle: TrimmedNonEmptyString,
301 message: TrimmedNonEmptyString,
302});
303export type IssueListProjectError = typeof IssueListProjectError.Type;
304
305export const IssueListResult = Schema.Struct({
306 /** The signed-in account per host that involvement compared against. Empty for `all`. */
307 viewers: Schema.Record(TrimmedNonEmptyString, TrimmedNonEmptyString),
308 trackers: Schema.Array(IssueTrackerSummary),
309 /** By update, newest first, across every scope this answer covers. */
310 entries: Schema.Array(IssueListEntry),
311 errors: Schema.Array(IssueListProjectError),
312 /** Where each scope carries on, sent straight back as `cursors`. Absent once a scope is done. */
313 nextCursors: IssueListCursors,
314 /**
315 * Every scope the listing covers, matched or not, with one project that reads it. A Linear team
316 * bound in settings is named nowhere else. Absent from servers that predate it.
317 */
318 scopes: Schema.optional(Schema.Array(IssueScopeRef)),
319});
320export type IssueListResult = typeof IssueListResult.Type;
321
322const IssueTitle = TrimmedNonEmptyString.check(Schema.isMaxLength(1024));
323// Not trimmed: a body is markdown, where leading spaces open a code block. The bound keeps
324// oversized payloads off the wire; the service rejects a comment that is only whitespace.
325const IssueBody = Schema.String.check(Schema.isMaxLength(65_536));
326const CommentBody = Schema.String.check(Schema.isNonEmpty()).check(Schema.isMaxLength(65_536));
327
328export const IssueCreateInput = Schema.Struct({
329 ...IssueScopeRef.fields,
330 title: IssueTitle,
331 /** May be empty: an issue with a title and nothing else is a legitimate one. */
332 body: IssueBody,
333 labels: Schema.Array(TrimmedNonEmptyString).check(Schema.isMaxLength(50)),
334 assignees: Schema.Array(TrimmedNonEmptyString).check(Schema.isMaxLength(25)),
335});
336export type IssueCreateInput = typeof IssueCreateInput.Type;
337
338export const IssueCreateResult = Schema.Struct({
339 key: TrimmedNonEmptyString,
340 displayKey: TrimmedNonEmptyString,
341 url: TrimmedNonEmptyString,
342});
343export type IssueCreateResult = typeof IssueCreateResult.Type;
344
345export const IssueCommentInput = Schema.Struct({
346 ...IssueRef.fields,
347 body: CommentBody,
348});
349export type IssueCommentInput = typeof IssueCommentInput.Type;
350
351/**
352 * Rewrites the issue's own fields. Every field is optional and only the present ones are
353 * written; `labels` and `assignees` are the whole set, which is how every tracker writes them.
354 * The service refuses a request with nothing present.
355 */
356export const IssueUpdateInput = Schema.Struct({
357 ...IssueRef.fields,
358 title: Schema.optional(IssueTitle),
359 body: Schema.optional(IssueBody),
360 labels: Schema.optional(Schema.Array(TrimmedNonEmptyString).check(Schema.isMaxLength(50))),
361 assignees: Schema.optional(Schema.Array(TrimmedNonEmptyString).check(Schema.isMaxLength(25))),
362});
363export type IssueUpdateInput = typeof IssueUpdateInput.Type;
364
365/** Moves the issue to one of the states the tracker declared in `capabilities.states`. */
366export const IssueSetStateInput = Schema.Struct({
367 ...IssueRef.fields,
368 /** The name of one of the issue's `states`. */
369 state: TrimmedNonEmptyString,
370});
371export type IssueSetStateInput = typeof IssueSetStateInput.Type;
372
373export const IssueReactionInput = Schema.Struct({
374 ...IssueRef.fields,
375 /** Absent reacts to the issue itself; present names a comment id as the detail carried it. */
376 subjectId: Schema.optional(TrimmedNonEmptyString),
377 content: IssueReactionContent,
378 reacted: Schema.Boolean,
379});
380export type IssueReactionInput = typeof IssueReactionInput.Type;
381
382/**
383 * One starting point a scope offers for a new issue. Only the body-shaped kind travels here:
384 * GitHub's typed issue forms are flattened to a body by the driver, so a composer on any
385 * tracker is one text box with a draft in it.
386 */
387export const IssueTemplate = Schema.Struct({
388 key: TrimmedNonEmptyString,
389 name: TrimmedNonEmptyString,
390 about: Schema.String,
391 title: Schema.String,
392 body: Schema.String,
393 labels: Schema.Array(TrimmedNonEmptyString),
394 assignees: Schema.Array(TrimmedNonEmptyString),
395});
396export type IssueTemplate = typeof IssueTemplate.Type;
397
398/** A starting point only the tracker's own site can render: Linear's form and rich templates. */
399export const IssueExternalTemplate = Schema.Struct({
400 name: TrimmedNonEmptyString,
401 about: Schema.String,
402 /** Opens the exact template on the tracker, ready to fill in. */
403 url: TrimmedNonEmptyString,
404});
405export type IssueExternalTemplate = typeof IssueExternalTemplate.Type;
406
407export const IssueTemplateList = Schema.Struct({
408 templates: Schema.Array(IssueTemplate),
409 /** Absent from a tracker whose every template is body-shaped. */
410 externalTemplates: Schema.optional(Schema.Array(IssueExternalTemplate)),
411 /** False where the scope's config forbids an issue filed from nothing. */
412 blankIssuesEnabled: Schema.Boolean,
413});
414export type IssueTemplateList = typeof IssueTemplateList.Type;
415
416export const IssueLabelCandidate = Schema.Struct({
417 ...IssueLabel.fields,
418 description: Schema.NullOr(Schema.String),
419});
420export type IssueLabelCandidate = typeof IssueLabelCandidate.Type;
421
422export const IssueAssigneeCandidate = Schema.Struct({
423 ...IssueActor.fields,
424 /** How the tracker addresses this person on a write, which is not always the login. */
425 id: TrimmedNonEmptyString,
426});
427export type IssueAssigneeCandidate = typeof IssueAssigneeCandidate.Type;
428
429/** The labels and people a scope offers, read when a picker opens rather than with every issue. */
430export const IssueCandidates = Schema.Struct({
431 labels: Schema.Array(IssueLabelCandidate),
432 assignees: Schema.Array(IssueAssigneeCandidate),
433 /** The tracker has more than were read, so somebody missing is not necessarily ineligible. */
434 truncated: Schema.Boolean,
435});
436export type IssueCandidates = typeof IssueCandidates.Type;
437
438export const IssueUnavailableReason = Schema.Literals([
439 "tool-missing",
440 "unauthenticated",
441 "tracker-unsupported",
442]);
443export type IssueUnavailableReason = typeof IssueUnavailableReason.Type;
444
445/** The tracker cannot be read at all for this project, as opposed to one request failing. */
446export class IssueUnavailableError extends Schema.TaggedError<IssueUnavailableError>()(
447 "IssueUnavailableError",
448 {
449 reason: IssueUnavailableReason,
450 tracker: Schema.optional(IssueTrackerKind),
451 cause: Schema.optional(Schema.Defect()),
452 },
453 { httpApiStatus: 503 },
454) {
455 [HttpServerRespondable.symbol]() {
456 return HttpServerResponse.schemaJson(IssueUnavailableError)(this, { status: 503 });
457 }
458
459 override get message(): string {
460 switch (this.reason) {
461 case "tool-missing":
462 return "The tool this tracker is read through is not installed or set up.";
463 case "unauthenticated":
464 return "This tracker has no working credentials.";
465 case "tracker-unsupported":
466 return "Issues cannot be browsed for this project yet.";
467 }
468 }
469}
470
471export class IssueOperationError extends Schema.TaggedError<IssueOperationError>()(
472 "IssueOperationError",
473 {
474 operation: Schema.String,
475 detail: TrimmedNonEmptyString,
476 reason: Schema.optional(Schema.Literals(["not-found", "read-only", "rate-limited"])),
477 retryAt: Schema.optional(Schema.Number),
478 cause: Schema.optional(Schema.Defect()),
479 },
480 { httpApiStatus: 502 },
481) {
482 [HttpServerRespondable.symbol]() {
483 return HttpServerResponse.schemaJson(IssueOperationError)(this, { status: 502 });
484 }
485
486 override get message(): string {
487 return `Issue operation ${this.operation} failed: ${this.detail}`;
488 }
489}
490
491export type IssueError = IssueUnavailableError | IssueOperationError;
492
493/** Who created a thread ↔ issue link. */
494export const ThreadIssueLinkSource = Schema.Literals(["manual", "agent"]);
495export type ThreadIssueLinkSource = typeof ThreadIssueLinkSource.Type;
496
497/**
498 * Identity of an issue as a thread link sees it: tracker-level, so the same issue linked through
499 * two projects compares equal. `host` is always written here, unlike `IssueRef`, because a link
500 * outlives the project whose binding supplied the default.
501 */
502export const ThreadIssueKey = Schema.Struct({
503 tracker: IssueTrackerKind,
504 host: TrimmedNonEmptyString,
505 scope: TrimmedNonEmptyString,
506 key: TrimmedNonEmptyString,
507});
508export type ThreadIssueKey = typeof ThreadIssueKey.Type;
509
510/**
511 * An issue linked to a thread. `projectId` is the project the issue is read through, which the
512 * detail panel needs; `url` is what the agent or user handed over.
513 */
514export const ThreadIssueLink = Schema.Struct({
515 ...ThreadIssueKey.fields,
516 projectId: ProjectId,
517 url: TrimmedNonEmptyString,
518 source: ThreadIssueLinkSource,
519 linkedAt: IsoDateTime,
520 /** As the tracker named the issue when it was linked; opening it reads the live state. */
521 displayKey: TrimmedNonEmptyString,
522 title: TrimmedNonEmptyString,
523});
524export type ThreadIssueLink = typeof ThreadIssueLink.Type;
525
526export function threadIssueKeyOf(key: ThreadIssueKey): string {
527 return `${key.tracker}\u0000${key.host.trim().toLowerCase()}\u0000${key.scope.trim().toLowerCase()}\u0000${key.key.trim()}`;
528}
529
530export function threadIssueKeysEqual(left: ThreadIssueKey, right: ThreadIssueKey): boolean {
531 return threadIssueKeyOf(left) === threadIssueKeyOf(right);
532}
533
534/** The threads an issue is linked to, most recently active first. */
535export const IssueLinkedThreadsResult = Schema.Struct({
536 threads: Schema.Array(
537 Schema.Struct({
538 id: ThreadId,
539 projectId: ProjectId,
540 title: Schema.String,
541 archivedAt: Schema.NullOr(IsoDateTime),
542 settled: Schema.Boolean,
543 }),
544 ),
545});
546export type IssueLinkedThreadsResult = typeof IssueLinkedThreadsResult.Type;
547
548/**
549 * A tracker that is not a git host: it is reached with its own credential rather than a
550 * checkout's, and a project opts into it by binding one of the tracker's scopes. Everything a
551 * client needs to draw the connect flow for any such tracker without knowing which one it is.
552 */
553export const StandaloneIssueTrackerDescriptor = Schema.Struct({
554 tracker: IssueTrackerKind,
555 displayName: TrimmedNonEmptyString,
556 /** Where the tracker is served from; what `IssueRef.host` carries for its issues. */
557 host: TrimmedNonEmptyString,
558 /** What the tracker calls its container, in the singular: "team" on Linear, "project" on Jira. */
559 scopeNoun: TrimmedNonEmptyString,
560 credential: Schema.Struct({
561 /** What the tracker calls the credential: "Personal API key", "API token". */
562 label: TrimmedNonEmptyString,
563 /** Where to mint one, or null when the tracker has no stable page for it. */
564 helpUrl: Schema.NullOr(TrimmedNonEmptyString),
565 }),
566});
567export type StandaloneIssueTrackerDescriptor = typeof StandaloneIssueTrackerDescriptor.Type;
568
569/** Which standalone tracker scope a project's issues come from, beside its git host's own. */
570export const IssueTrackerProjectBinding = Schema.Struct({
571 tracker: IssueTrackerKind,
572 scope: TrimmedNonEmptyString,
573});
574export type IssueTrackerProjectBinding = typeof IssueTrackerProjectBinding.Type;
575
576/** One container the connected account can read, offered when a project is bound. */
577export const IssueTrackerScope = Schema.Struct({
578 key: TrimmedNonEmptyString,
579 name: TrimmedNonEmptyString,
580});
581export type IssueTrackerScope = typeof IssueTrackerScope.Type;
582
583/**
584 * A standalone tracker as the settings page sees it: the descriptor, whether a credential is
585 * stored and still works, and the scopes it can reach. `scopes` is empty until connected.
586 */
587export const IssueTrackerConnection = Schema.Struct({
588 ...StandaloneIssueTrackerDescriptor.fields,
589 /** A credential is stored. It may still be rejected, which `account` being null reports. */
590 connected: Schema.Boolean,
591 /** Who the credential signs in as, or null when there is none or it was refused. */
592 account: Schema.NullOr(TrimmedNonEmptyString),
593 /** Why a stored credential did not work, when it did not. */
594 detail: Schema.NullOr(TrimmedNonEmptyString),
595 /** The workspace the credential reaches, as its issue URLs name it; links elsewhere are foreign. */
596 workspace: Schema.optional(TrimmedNonEmptyString),
597 scopes: Schema.Array(IssueTrackerScope),
598});
599export type IssueTrackerConnection = typeof IssueTrackerConnection.Type;
600
601export const IssueTrackerConnectionList = Schema.Struct({
602 trackers: Schema.Array(IssueTrackerConnection),
603 /** Which standalone tracker scope each bound project reads; a project absent here reads none. */
604 bindings: Schema.Record(ProjectId, IssueTrackerProjectBinding),
605});
606export type IssueTrackerConnectionList = typeof IssueTrackerConnectionList.Type;
607
608/** A credential for one standalone tracker; `null` disconnects it and unbinds its projects. */
609export const IssueTrackerConnectInput = Schema.Struct({
610 tracker: IssueTrackerKind,
611 token: Schema.NullOr(TrimmedNonEmptyString.check(Schema.isMaxLength(4_096))),
612});
613export type IssueTrackerConnectInput = typeof IssueTrackerConnectInput.Type;
614
615export const IssueTrackerBindProjectInput = Schema.Struct({
616 projectId: ProjectId,
617 /** Null unbinds. */
618 binding: Schema.NullOr(IssueTrackerProjectBinding),
619});
620export type IssueTrackerBindProjectInput = typeof IssueTrackerBindProjectInput.Type;
621
622/**
623 * A standalone tracker the connect flow could not reach or that refused the credential, or a
624 * binding that could not be saved.
625 */
626export class IssueTrackerConnectionError extends Schema.TaggedError<IssueTrackerConnectionError>()(
627 "IssueTrackerConnectionError",
628 {
629 tracker: IssueTrackerKind,
630 reason: Schema.Literals(["unknown-tracker", "rejected", "failed"]),
631 detail: TrimmedNonEmptyString,
632 cause: Schema.optional(Schema.Defect()),
633 },
634 { httpApiStatus: 502 },
635) {
636 [HttpServerRespondable.symbol]() {
637 return HttpServerResponse.schemaJson(IssueTrackerConnectionError)(this, { status: 502 });
638 }
639
640 override get message(): string {
641 return `${this.tracker} connection failed: ${this.detail}`;
642 }
643}