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