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 IssueEventReference = Schema.Struct({
154 repository: TrimmedNonEmptyString,
155 number: PositiveInt,
156 kind: Schema.Literals(["issue", "pull-request"]),
157 url: TrimmedNonEmptyString,
158});
159export type IssueEventReference = typeof IssueEventReference.Type;
160
161export const IssueEvent = Schema.Struct({
162 id: TrimmedNonEmptyString,
163 kind: IssueEventKind,
164 actor: Schema.NullOr(IssueActor),
165 createdAt: IsoDateTime,
166 /** What the event was about, where it has a subject: a label name, a login, a new title. */
167 detail: Schema.NullOr(TrimmedNonEmptyString),
168 /** The issue or change request a `referenced` event came from, so a surface can open it. */
169 reference: Schema.optional(IssueEventReference),
170});
171export type IssueEvent = typeof IssueEvent.Type;
172
173/** A change request the tracker says closes or mentions this issue. */
174export const IssueLinkedPullRequest = Schema.Struct({
175 repository: TrimmedNonEmptyString,
176 number: PositiveInt,
177 title: TrimmedNonEmptyString,
178 url: TrimmedNonEmptyString,
179 state: PullRequestState,
180 isDraft: Schema.Boolean,
181 /** Merging it closes this issue, rather than only citing it. */
182 closesIssue: Schema.Boolean,
183});
184export type IssueLinkedPullRequest = typeof IssueLinkedPullRequest.Type;
185
186/**
187 * What a tracker can do at all, so a surface never offers an action that would fail. The
188 * service refuses a call outside these before it reaches the driver.
189 */
190export const IssueCapabilities = Schema.Struct({
191 create: Schema.Boolean,
192 /** Title and body can be rewritten after filing. */
193 edit: Schema.Boolean,
194 comment: Schema.Boolean,
195 labels: Schema.Boolean,
196 assignees: Schema.Boolean,
197 /** Reactions can be read and written on the issue and its comments. */
198 reactions: Schema.Boolean,
199 /** The scope offers starting points for a new issue. */
200 templates: Schema.Boolean,
201 /** The tracker reports linked change requests of its own accord. */
202 linkedPullRequests: Schema.Boolean,
203 /** State changes arrive as events beside the comments, not only as comments. */
204 events: Schema.Boolean,
205 /** The tracker narrows a listing by free text itself. */
206 search: Schema.Boolean,
207});
208export type IssueCapabilities = typeof IssueCapabilities.Type;
209
210/**
211 * What the signed-in account may do with this issue, which `capabilities` says nothing about.
212 * A permission the tracker reports nothing about is granted, so a refusal comes from the
213 * tracker, which says why, rather than from a missing control.
214 */
215export const IssueViewerPermissions = Schema.Struct({
216 setState: Schema.Boolean,
217 comment: Schema.Boolean,
218 edit: Schema.Boolean,
219 labels: Schema.Boolean,
220 assignees: Schema.Boolean,
221});
222export type IssueViewerPermissions = typeof IssueViewerPermissions.Type;
223
224export const IssueDetail = Schema.Struct({
225 ...IssueSummary.fields,
226 projectId: ProjectId,
227 projectTitle: TrimmedNonEmptyString,
228 /** False for an issue outside every project's scope, which is read-only through this server. */
229 writable: Schema.Boolean,
230 capabilities: IssueCapabilities,
231 viewerPermissions: IssueViewerPermissions,
232 /**
233 * Every state this issue can be in, in the tracker's workflow order, its current one included.
234 * Empty means its state cannot be changed from here.
235 */
236 states: Schema.Array(IssueState),
237 /** The signed-in account, so a comment edit is offered only on the viewer's own remarks. */
238 viewer: Schema.NullOr(TrimmedNonEmptyString),
239 body: Schema.String,
240 reactions: Schema.optional(Schema.Array(IssueReaction)),
241 comments: Schema.Array(IssueComment),
242 /** Where older comments carry on; null once the conversation is whole. */
243 commentsCursor: Schema.NullOr(TrimmedNonEmptyString),
244 events: Schema.Array(IssueEvent),
245 linkedPullRequests: Schema.Array(IssueLinkedPullRequest),
246});
247export type IssueDetail = typeof IssueDetail.Type;
248
249/** Older comments, one page past a cursor the detail or a previous page handed over. */
250export const IssueCommentsPageInput = Schema.Struct({
251 ...IssueRef.fields,
252 cursor: TrimmedNonEmptyString.check(Schema.isMaxLength(4_096)),
253});
254export type IssueCommentsPageInput = typeof IssueCommentsPageInput.Type;
255
256export const IssueCommentsPage = Schema.Struct({
257 comments: Schema.Array(IssueComment),
258 nextCursor: Schema.NullOr(TrimmedNonEmptyString),
259});
260export type IssueCommentsPage = typeof IssueCommentsPage.Type;
261
262/** Where each scope a listing already reached carries on, keyed `"<host> <scope>"`. */
263export const IssueListCursors = Schema.Record(
264 TrimmedNonEmptyString,
265 TrimmedNonEmptyString.check(Schema.isMaxLength(4_096)),
266);
267export type IssueListCursors = typeof IssueListCursors.Type;
268
269export const ISSUE_LIST_MAX_EXCLUDED_PROJECTS = 1_000;
270
271export const IssueListInput = Schema.Struct({
272 state: IssueListState,
273 involvement: Schema.optional(IssueInvolvement),
274 projectId: Schema.optional(ProjectId),
275 /** Projects left out of the listing, as the issues page hides them. */
276 excludeProjectIds: Schema.optional(
277 Schema.Array(ProjectId).check(Schema.isMaxLength(ISSUE_LIST_MAX_EXCLUDED_PROJECTS)),
278 ),
279 /** Narrows to one host, named as the host itself. Absent means every host the workspace has. */
280 host: Schema.optional(TrimmedNonEmptyString),
281 /** Rows per scope, which with a continuation is rows per slice. */
282 limit: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 200 }))),
283 /** Carry on from an earlier answer: only the scopes named here are read, one slice each. */
284 cursors: Schema.optional(IssueListCursors),
285 /** An explicit refresh of the first page may also refresh tracker metadata. */
286 refresh: Schema.optional(Schema.Boolean),
287 /** Free text the trackers themselves are asked to match. */
288 query: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(200))),
289 /** Label names an issue must carry, every one of them. */
290 labels: Schema.optional(
291 Schema.Array(TrimmedNonEmptyString.check(Schema.isMaxLength(200))).check(
292 Schema.isMaxLength(20),
293 ),
294 ),
295 /** An assignee's `login`, as an issue of that tracker names them. */
296 assignee: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(320))),
297 /** Absent means `updated`. */
298 sort: Schema.optional(IssueListSort),
299});
300export type IssueListInput = typeof IssueListInput.Type;
301
302/** One tracker account the workspace has projects on, and whether it can be read right now. */
303export const IssueTrackerSummary = Schema.Struct({
304 tracker: IssueTrackerKind,
305 host: TrimmedNonEmptyString,
306 projectCount: PositiveInt,
307 capabilities: Schema.NullOr(IssueCapabilities),
308 /** False when the tracker's tool or credentials are missing, with `detail` saying which. */
309 configured: Schema.Boolean,
310 detail: Schema.NullOr(TrimmedNonEmptyString),
311});
312export type IssueTrackerSummary = typeof IssueTrackerSummary.Type;
313
314export const IssueListProjectError = Schema.Struct({
315 projectId: ProjectId,
316 projectTitle: TrimmedNonEmptyString,
317 message: TrimmedNonEmptyString,
318});
319export type IssueListProjectError = typeof IssueListProjectError.Type;
320
321export const IssueListResult = Schema.Struct({
322 /** The signed-in account per host that involvement compared against. Empty for `all`. */
323 viewers: Schema.Record(TrimmedNonEmptyString, TrimmedNonEmptyString),
324 trackers: Schema.Array(IssueTrackerSummary),
325 /** By update, newest first, across every scope this answer covers. */
326 entries: Schema.Array(IssueListEntry),
327 errors: Schema.Array(IssueListProjectError),
328 /** Where each scope carries on, sent straight back as `cursors`. Absent once a scope is done. */
329 nextCursors: IssueListCursors,
330 /**
331 * Every scope the listing covers, matched or not, with one project that reads it. A Linear team
332 * bound in settings is named nowhere else. Absent from servers that predate it.
333 */
334 scopes: Schema.optional(Schema.Array(IssueScopeRef)),
335});
336export type IssueListResult = typeof IssueListResult.Type;
337
338const IssueTitle = TrimmedNonEmptyString.check(Schema.isMaxLength(1024));
339// Not trimmed: a body is markdown, where leading spaces open a code block. The bound keeps
340// oversized payloads off the wire; the service rejects a comment that is only whitespace.
341const IssueBody = Schema.String.check(Schema.isMaxLength(65_536));
342const CommentBody = Schema.String.check(Schema.isNonEmpty()).check(Schema.isMaxLength(65_536));
343
344export const IssueCreateInput = Schema.Struct({
345 ...IssueScopeRef.fields,
346 title: IssueTitle,
347 /** May be empty: an issue with a title and nothing else is a legitimate one. */
348 body: IssueBody,
349 labels: Schema.Array(TrimmedNonEmptyString).check(Schema.isMaxLength(50)),
350 assignees: Schema.Array(TrimmedNonEmptyString).check(Schema.isMaxLength(25)),
351});
352export type IssueCreateInput = typeof IssueCreateInput.Type;
353
354export const IssueCreateResult = Schema.Struct({
355 key: TrimmedNonEmptyString,
356 displayKey: TrimmedNonEmptyString,
357 url: TrimmedNonEmptyString,
358});
359export type IssueCreateResult = typeof IssueCreateResult.Type;
360
361export const IssueCommentInput = Schema.Struct({
362 ...IssueRef.fields,
363 body: CommentBody,
364});
365export type IssueCommentInput = typeof IssueCommentInput.Type;
366
367/**
368 * Rewrites the issue's own fields. Every field is optional and only the present ones are
369 * written; `labels` and `assignees` are the whole set, which is how every tracker writes them.
370 * The service refuses a request with nothing present.
371 */
372export const IssueUpdateInput = Schema.Struct({
373 ...IssueRef.fields,
374 title: Schema.optional(IssueTitle),
375 body: Schema.optional(IssueBody),
376 labels: Schema.optional(Schema.Array(TrimmedNonEmptyString).check(Schema.isMaxLength(50))),
377 assignees: Schema.optional(Schema.Array(TrimmedNonEmptyString).check(Schema.isMaxLength(25))),
378});
379export type IssueUpdateInput = typeof IssueUpdateInput.Type;
380
381/** Moves the issue to one of the states the tracker declared in `capabilities.states`. */
382export const IssueSetStateInput = Schema.Struct({
383 ...IssueRef.fields,
384 /** The name of one of the issue's `states`. */
385 state: TrimmedNonEmptyString,
386});
387export type IssueSetStateInput = typeof IssueSetStateInput.Type;
388
389export const IssueReactionInput = Schema.Struct({
390 ...IssueRef.fields,
391 /** Absent reacts to the issue itself; present names a comment id as the detail carried it. */
392 subjectId: Schema.optional(TrimmedNonEmptyString),
393 content: IssueReactionContent,
394 reacted: Schema.Boolean,
395});
396export type IssueReactionInput = typeof IssueReactionInput.Type;
397
398/**
399 * One starting point a scope offers for a new issue. Only the body-shaped kind travels here:
400 * GitHub's typed issue forms are flattened to a body by the driver, so a composer on any
401 * tracker is one text box with a draft in it.
402 */
403export const IssueTemplate = Schema.Struct({
404 key: TrimmedNonEmptyString,
405 name: TrimmedNonEmptyString,
406 about: Schema.String,
407 title: Schema.String,
408 body: Schema.String,
409 labels: Schema.Array(TrimmedNonEmptyString),
410 assignees: Schema.Array(TrimmedNonEmptyString),
411});
412export type IssueTemplate = typeof IssueTemplate.Type;
413
414/** A starting point only the tracker's own site can render: Linear's form and rich templates. */
415export const IssueExternalTemplate = Schema.Struct({
416 name: TrimmedNonEmptyString,
417 about: Schema.String,
418 /** Opens the exact template on the tracker, ready to fill in. */
419 url: TrimmedNonEmptyString,
420});
421export type IssueExternalTemplate = typeof IssueExternalTemplate.Type;
422
423export const IssueTemplateList = Schema.Struct({
424 templates: Schema.Array(IssueTemplate),
425 /** Absent from a tracker whose every template is body-shaped. */
426 externalTemplates: Schema.optional(Schema.Array(IssueExternalTemplate)),
427 /** False where the scope's config forbids an issue filed from nothing. */
428 blankIssuesEnabled: Schema.Boolean,
429});
430export type IssueTemplateList = typeof IssueTemplateList.Type;
431
432export const IssueLabelCandidate = Schema.Struct({
433 ...IssueLabel.fields,
434 description: Schema.NullOr(Schema.String),
435});
436export type IssueLabelCandidate = typeof IssueLabelCandidate.Type;
437
438export const IssueAssigneeCandidate = Schema.Struct({
439 ...IssueActor.fields,
440 /** How the tracker addresses this person on a write, which is not always the login. */
441 id: TrimmedNonEmptyString,
442});
443export type IssueAssigneeCandidate = typeof IssueAssigneeCandidate.Type;
444
445/** The labels and people a scope offers, read when a picker opens rather than with every issue. */
446export const IssueCandidates = Schema.Struct({
447 labels: Schema.Array(IssueLabelCandidate),
448 assignees: Schema.Array(IssueAssigneeCandidate),
449 /** The tracker has more than were read, so somebody missing is not necessarily ineligible. */
450 truncated: Schema.Boolean,
451});
452export type IssueCandidates = typeof IssueCandidates.Type;
453
454export const IssueUnavailableReason = Schema.Literals([
455 "tool-missing",
456 "unauthenticated",
457 "tracker-unsupported",
458]);
459export type IssueUnavailableReason = typeof IssueUnavailableReason.Type;
460
461/** The tracker cannot be read at all for this project, as opposed to one request failing. */
462export class IssueUnavailableError extends Schema.TaggedError<IssueUnavailableError>()(
463 "IssueUnavailableError",
464 {
465 reason: IssueUnavailableReason,
466 tracker: Schema.optional(IssueTrackerKind),
467 cause: Schema.optional(Schema.Defect()),
468 },
469 { httpApiStatus: 503 },
470) {
471 [HttpServerRespondable.symbol]() {
472 return HttpServerResponse.schemaJson(IssueUnavailableError)(this, { status: 503 });
473 }
474
475 override get message(): string {
476 switch (this.reason) {
477 case "tool-missing":
478 return "The tool this tracker is read through is not installed or set up.";
479 case "unauthenticated":
480 return "This tracker has no working credentials.";
481 case "tracker-unsupported":
482 return "Issues cannot be browsed for this project yet.";
483 }
484 }
485}
486
487export class IssueOperationError extends Schema.TaggedError<IssueOperationError>()(
488 "IssueOperationError",
489 {
490 operation: Schema.String,
491 detail: TrimmedNonEmptyString,
492 reason: Schema.optional(Schema.Literals(["not-found", "read-only", "rate-limited"])),
493 retryAt: Schema.optional(Schema.Number),
494 cause: Schema.optional(Schema.Defect()),
495 },
496 { httpApiStatus: 502 },
497) {
498 [HttpServerRespondable.symbol]() {
499 return HttpServerResponse.schemaJson(IssueOperationError)(this, { status: 502 });
500 }
501
502 override get message(): string {
503 return `Issue operation ${this.operation} failed: ${this.detail}`;
504 }
505}
506
507export type IssueError = IssueUnavailableError | IssueOperationError;
508
509/** Who created a thread ↔ issue link. */
510export const ThreadIssueLinkSource = Schema.Literals(["manual", "agent"]);
511export type ThreadIssueLinkSource = typeof ThreadIssueLinkSource.Type;
512
513/**
514 * Identity of an issue as a thread link sees it: tracker-level, so the same issue linked through
515 * two projects compares equal. `host` is always written here, unlike `IssueRef`, because a link
516 * outlives the project whose binding supplied the default.
517 */
518export const ThreadIssueKey = Schema.Struct({
519 tracker: IssueTrackerKind,
520 host: TrimmedNonEmptyString,
521 scope: TrimmedNonEmptyString,
522 key: TrimmedNonEmptyString,
523});
524export type ThreadIssueKey = typeof ThreadIssueKey.Type;
525
526/**
527 * An issue linked to a thread. `projectId` is the project the issue is read through, which the
528 * detail panel needs; `url` is what the agent or user handed over.
529 */
530export const ThreadIssueLink = Schema.Struct({
531 ...ThreadIssueKey.fields,
532 projectId: ProjectId,
533 url: TrimmedNonEmptyString,
534 source: ThreadIssueLinkSource,
535 linkedAt: IsoDateTime,
536 /** As the tracker named the issue when it was linked; opening it reads the live state. */
537 displayKey: TrimmedNonEmptyString,
538 title: TrimmedNonEmptyString,
539});
540export type ThreadIssueLink = typeof ThreadIssueLink.Type;
541
542export function threadIssueKeyOf(key: ThreadIssueKey): string {
543 return `${key.tracker}\u0000${key.host.trim().toLowerCase()}\u0000${key.scope.trim().toLowerCase()}\u0000${key.key.trim()}`;
544}
545
546export function threadIssueKeysEqual(left: ThreadIssueKey, right: ThreadIssueKey): boolean {
547 return threadIssueKeyOf(left) === threadIssueKeyOf(right);
548}
549
550/** The threads an issue is linked to, most recently active first. */
551export const IssueLinkedThreadsResult = Schema.Struct({
552 threads: Schema.Array(
553 Schema.Struct({
554 id: ThreadId,
555 projectId: ProjectId,
556 title: Schema.String,
557 archivedAt: Schema.NullOr(IsoDateTime),
558 settled: Schema.Boolean,
559 }),
560 ),
561});
562export type IssueLinkedThreadsResult = typeof IssueLinkedThreadsResult.Type;
563
564/**
565 * A tracker that is not a git host: it is reached with its own credential rather than a
566 * checkout's, and a project opts into it by binding one of the tracker's scopes. Everything a
567 * client needs to draw the connect flow for any such tracker without knowing which one it is.
568 */
569export const StandaloneIssueTrackerDescriptor = Schema.Struct({
570 tracker: IssueTrackerKind,
571 displayName: TrimmedNonEmptyString,
572 /** Where the tracker is served from; what `IssueRef.host` carries for its issues. */
573 host: TrimmedNonEmptyString,
574 /** What the tracker calls its container, in the singular: "team" on Linear, "project" on Jira. */
575 scopeNoun: TrimmedNonEmptyString,
576 credential: Schema.Struct({
577 /** What the tracker calls the credential: "Personal API key", "API token". */
578 label: TrimmedNonEmptyString,
579 /** Where to mint one, or null when the tracker has no stable page for it. */
580 helpUrl: Schema.NullOr(TrimmedNonEmptyString),
581 }),
582});
583export type StandaloneIssueTrackerDescriptor = typeof StandaloneIssueTrackerDescriptor.Type;
584
585export const MAX_ISSUE_TRACKER_ADDITIONAL_SCOPES = 250;
586
587/**
588 * Which standalone tracker scopes a project's issues come from, beside its git host's own.
589 * `scope` is where agents file new issues; `additionalScopes` are listed and written too.
590 */
591export const IssueTrackerProjectBinding = Schema.Struct({
592 tracker: IssueTrackerKind,
593 scope: TrimmedNonEmptyString,
594 additionalScopes: Schema.optionalKey(
595 Schema.Array(TrimmedNonEmptyString).check(
596 Schema.isMaxLength(MAX_ISSUE_TRACKER_ADDITIONAL_SCOPES),
597 ),
598 ),
599});
600export type IssueTrackerProjectBinding = typeof IssueTrackerProjectBinding.Type;
601
602export const issueTrackerBindingScopes = (binding: IssueTrackerProjectBinding) => [
603 binding.scope,
604 ...(binding.additionalScopes ?? []),
605];
606
607/** One container the connected account can read, offered when a project is bound. */
608export const IssueTrackerScope = Schema.Struct({
609 key: TrimmedNonEmptyString,
610 name: TrimmedNonEmptyString,
611});
612export type IssueTrackerScope = typeof IssueTrackerScope.Type;
613
614/**
615 * A standalone tracker as the settings page sees it: the descriptor, whether a credential is
616 * stored and still works, and the scopes it can reach. `scopes` is empty until connected.
617 */
618export const IssueTrackerConnection = Schema.Struct({
619 ...StandaloneIssueTrackerDescriptor.fields,
620 /** A credential is stored. It may still be rejected, which `account` being null reports. */
621 connected: Schema.Boolean,
622 /** Who the credential signs in as, or null when there is none or it was refused. */
623 account: Schema.NullOr(TrimmedNonEmptyString),
624 /** Why a stored credential did not work, when it did not. */
625 detail: Schema.NullOr(TrimmedNonEmptyString),
626 /** The workspace the credential reaches, as its issue URLs name it; links elsewhere are foreign. */
627 workspace: Schema.optional(TrimmedNonEmptyString),
628 scopes: Schema.Array(IssueTrackerScope),
629});
630export type IssueTrackerConnection = typeof IssueTrackerConnection.Type;
631
632export const IssueTrackerConnectionList = Schema.Struct({
633 trackers: Schema.Array(IssueTrackerConnection),
634 /** Which standalone tracker scopes each bound project reads; a project absent here reads none. */
635 bindings: Schema.Record(ProjectId, IssueTrackerProjectBinding),
636});
637export type IssueTrackerConnectionList = typeof IssueTrackerConnectionList.Type;
638
639/** A credential for one standalone tracker; `null` disconnects it and unbinds its projects. */
640export const IssueTrackerConnectInput = Schema.Struct({
641 tracker: IssueTrackerKind,
642 token: Schema.NullOr(TrimmedNonEmptyString.check(Schema.isMaxLength(4_096))),
643});
644export type IssueTrackerConnectInput = typeof IssueTrackerConnectInput.Type;
645
646export const IssueTrackerBindProjectInput = Schema.Struct({
647 projectId: ProjectId,
648 /** Null unbinds. */
649 binding: Schema.NullOr(IssueTrackerProjectBinding),
650});
651export type IssueTrackerBindProjectInput = typeof IssueTrackerBindProjectInput.Type;
652
653/**
654 * A standalone tracker the connect flow could not reach or that refused the credential, or a
655 * binding that could not be saved.
656 */
657export class IssueTrackerConnectionError extends Schema.TaggedError<IssueTrackerConnectionError>()(
658 "IssueTrackerConnectionError",
659 {
660 tracker: IssueTrackerKind,
661 reason: Schema.Literals(["unknown-tracker", "rejected", "failed"]),
662 detail: TrimmedNonEmptyString,
663 cause: Schema.optional(Schema.Defect()),
664 },
665 { httpApiStatus: 502 },
666) {
667 [HttpServerRespondable.symbol]() {
668 return HttpServerResponse.schemaJson(IssueTrackerConnectionError)(this, { status: 502 });
669 }
670
671 override get message(): string {
672 return `${this.tracker} connection failed: ${this.detail}`;
673 }
674}