@cvr/linear 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  import { Context, Effect, Layer } from "effect";
2
2
  import { LinearClient } from "@linear/sdk";
3
- import type { Issue, User, Team } from "@linear/sdk";
3
+ import type { Issue, Project, Team, User } from "@linear/sdk";
4
4
  import { LinearApiError, InvalidTokenError, TokenNotFoundError } from "../lib/errors.js";
5
5
  import { ConfigService } from "./Config.js";
6
6
 
@@ -11,7 +11,65 @@ export interface IssueFilter {
11
11
  readonly limit?: number;
12
12
  }
13
13
 
14
- export class LinearService extends Context.Tag("@linear/LinearService")<
14
+ export interface IssueDetailsOptions {
15
+ readonly comments: boolean;
16
+ readonly children: boolean;
17
+ readonly relations: boolean;
18
+ }
19
+
20
+ export interface IssueReference {
21
+ readonly id: string;
22
+ readonly identifier: string;
23
+ readonly title: string;
24
+ readonly url: string;
25
+ }
26
+
27
+ export interface IssueDetails extends IssueReference {
28
+ readonly description?: string;
29
+ readonly branchName: string;
30
+ readonly priority: {
31
+ readonly value: number;
32
+ readonly label: string;
33
+ };
34
+ readonly state?: {
35
+ readonly id: string;
36
+ readonly name: string;
37
+ readonly type: string;
38
+ };
39
+ readonly team?: {
40
+ readonly id: string;
41
+ readonly key: string;
42
+ readonly name: string;
43
+ };
44
+ readonly assignee?: {
45
+ readonly id: string;
46
+ readonly name: string;
47
+ readonly email: string;
48
+ };
49
+ readonly project?: {
50
+ readonly id: string;
51
+ readonly name: string;
52
+ readonly url: string;
53
+ };
54
+ readonly parent?: IssueReference;
55
+ readonly labels: ReadonlyArray<{ readonly id: string; readonly name: string }>;
56
+ readonly children?: ReadonlyArray<IssueReference>;
57
+ readonly comments?: ReadonlyArray<{
58
+ readonly id: string;
59
+ readonly body: string;
60
+ readonly createdAt: string;
61
+ readonly url: string;
62
+ readonly author?: { readonly id: string; readonly name: string };
63
+ }>;
64
+ readonly relations?: ReadonlyArray<{
65
+ readonly id: string;
66
+ readonly type: string;
67
+ readonly direction: "outbound" | "inbound";
68
+ readonly issue: IssueReference;
69
+ }>;
70
+ }
71
+
72
+ export class LinearService extends Context.Service<
15
73
  LinearService,
16
74
  {
17
75
  readonly getViewer: Effect.Effect<
@@ -19,10 +77,15 @@ export class LinearService extends Context.Tag("@linear/LinearService")<
19
77
  LinearApiError | InvalidTokenError | TokenNotFoundError
20
78
  >;
21
79
  readonly getTeams: Effect.Effect<readonly Team[], LinearApiError | TokenNotFoundError>;
80
+ readonly getProjects: Effect.Effect<readonly Project[], LinearApiError | TokenNotFoundError>;
22
81
  readonly getIssues: (
23
82
  filter: IssueFilter,
24
83
  ) => Effect.Effect<readonly Issue[], LinearApiError | TokenNotFoundError>;
25
84
  readonly getIssue: (id: string) => Effect.Effect<Issue, LinearApiError | TokenNotFoundError>;
85
+ readonly getIssueDetails: (
86
+ id: string,
87
+ options: IssueDetailsOptions,
88
+ ) => Effect.Effect<IssueDetails, LinearApiError | TokenNotFoundError>;
26
89
  readonly getMyIssues: (filter?: {
27
90
  state?: string;
28
91
  }) => Effect.Effect<readonly Issue[], LinearApiError | TokenNotFoundError>;
@@ -34,9 +97,23 @@ export class LinearService extends Context.Tag("@linear/LinearService")<
34
97
  title: string;
35
98
  teamId: string;
36
99
  description?: string;
100
+ parentId?: string;
101
+ projectId?: string;
102
+ priority?: number;
37
103
  }) => Effect.Effect<Issue, LinearApiError | TokenNotFoundError>;
104
+ readonly createComment: (
105
+ issueId: string,
106
+ body: string,
107
+ ) => Effect.Effect<
108
+ { readonly id: string; readonly url: string },
109
+ LinearApiError | TokenNotFoundError
110
+ >;
111
+ readonly rawQuery: (
112
+ query: string,
113
+ variables: Readonly<Record<string, unknown>>,
114
+ ) => Effect.Effect<unknown, LinearApiError | TokenNotFoundError>;
38
115
  }
39
- >() {
116
+ >()("@cvr/linear/services/Linear/LinearService") {
40
117
  static readonly layer = Layer.effect(
41
118
  LinearService,
42
119
  Effect.gen(function* () {
@@ -73,6 +150,15 @@ export class LinearService extends Context.Tag("@linear/LinearService")<
73
150
  return connection.nodes;
74
151
  }).pipe(Effect.withSpan("LinearService.getTeams"));
75
152
 
153
+ const getProjects = Effect.gen(function* () {
154
+ const client = yield* getClient;
155
+ const connection = yield* Effect.tryPromise({
156
+ try: () => client.projects(),
157
+ catch: (error) => LinearApiError.make({ message: String(error) }),
158
+ });
159
+ return connection.nodes;
160
+ }).pipe(Effect.withSpan("LinearService.getProjects"));
161
+
76
162
  const getIssues = Effect.fn("LinearService.getIssues")((filter: IssueFilter) =>
77
163
  Effect.gen(function* () {
78
164
  const client = yield* getClient;
@@ -81,9 +167,10 @@ export class LinearService extends Context.Tag("@linear/LinearService")<
81
167
  client.issues({
82
168
  first: filter.limit ?? 50,
83
169
  filter: {
84
- team: filter.teamId ? { id: { eq: filter.teamId } } : undefined,
85
- state: filter.state ? { type: { eq: filter.state } } : undefined,
86
- assignee: filter.assigneeId ? { id: { eq: filter.assigneeId } } : undefined,
170
+ team: filter.teamId !== undefined ? { id: { eq: filter.teamId } } : undefined,
171
+ state: filter.state !== undefined ? { type: { eq: filter.state } } : undefined,
172
+ assignee:
173
+ filter.assigneeId !== undefined ? { id: { eq: filter.assigneeId } } : undefined,
87
174
  },
88
175
  }),
89
176
  catch: (e) => LinearApiError.make({ message: String(e) }),
@@ -103,7 +190,10 @@ export class LinearService extends Context.Tag("@linear/LinearService")<
103
190
  try: () =>
104
191
  viewer.assignedIssues({
105
192
  first: 50,
106
- filter: filter?.state ? { state: { type: { eq: filter.state } } } : undefined,
193
+ filter:
194
+ filter?.state !== undefined
195
+ ? { state: { type: { eq: filter.state } } }
196
+ : undefined,
107
197
  }),
108
198
  catch: (e) => LinearApiError.make({ message: String(e) }),
109
199
  });
@@ -121,6 +211,123 @@ export class LinearService extends Context.Tag("@linear/LinearService")<
121
211
  }),
122
212
  );
123
213
 
214
+ const getIssueDetails = Effect.fn("LinearService.getIssueDetails")(
215
+ (id: string, options: IssueDetailsOptions) =>
216
+ Effect.gen(function* () {
217
+ const issue = yield* getIssue(normalizeIssueId(id));
218
+ const [state, team, assignee, project, parent, labels] = yield* Effect.all([
219
+ resolveLinearFetch(issue.state),
220
+ resolveLinearFetch(issue.team),
221
+ resolveLinearFetch(issue.assignee),
222
+ resolveLinearFetch(issue.project),
223
+ resolveLinearFetch(issue.parent),
224
+ loadConnection(() => issue.labels()),
225
+ ]);
226
+
227
+ const children = options.children
228
+ ? yield* loadConnection(() => issue.children())
229
+ : undefined;
230
+ const comments = options.comments
231
+ ? yield* loadConnection(() => issue.comments())
232
+ : undefined;
233
+ const outboundRelations = options.relations
234
+ ? yield* loadConnection(() => issue.relations())
235
+ : undefined;
236
+ const inboundRelations = options.relations
237
+ ? yield* loadConnection(() => issue.inverseRelations())
238
+ : undefined;
239
+
240
+ const detailedComments =
241
+ comments === undefined
242
+ ? undefined
243
+ : yield* Effect.forEach(
244
+ comments,
245
+ (comment) =>
246
+ resolveLinearFetch(comment.user).pipe(
247
+ Effect.map((author) => ({
248
+ id: comment.id,
249
+ body: comment.body,
250
+ createdAt: comment.createdAt.toISOString(),
251
+ url: comment.url,
252
+ author:
253
+ author === undefined ? undefined : { id: author.id, name: author.name },
254
+ })),
255
+ ),
256
+ { concurrency: 4 },
257
+ );
258
+
259
+ const relations =
260
+ outboundRelations === undefined || inboundRelations === undefined
261
+ ? undefined
262
+ : yield* Effect.forEach(
263
+ outboundRelations
264
+ .map(
265
+ (
266
+ relation,
267
+ ): {
268
+ readonly relation: typeof relation;
269
+ readonly direction: "outbound" | "inbound";
270
+ } => ({ relation, direction: "outbound" }),
271
+ )
272
+ .concat(
273
+ inboundRelations.map((relation) => ({
274
+ relation,
275
+ direction: "inbound" as const,
276
+ })),
277
+ ),
278
+ ({ direction, relation }) =>
279
+ resolveLinearFetch(
280
+ direction === "outbound" ? relation.relatedIssue : relation.issue,
281
+ ).pipe(
282
+ Effect.flatMap((relatedIssue) =>
283
+ relatedIssue === undefined
284
+ ? Effect.succeed(undefined)
285
+ : Effect.succeed({
286
+ id: relation.id,
287
+ type: relation.type,
288
+ direction,
289
+ issue: toIssueReference(relatedIssue),
290
+ }),
291
+ ),
292
+ ),
293
+ { concurrency: 4 },
294
+ ).pipe(
295
+ Effect.map((items) =>
296
+ items.filter((item): item is NonNullable<typeof item> => item !== undefined),
297
+ ),
298
+ );
299
+
300
+ return {
301
+ id: issue.id,
302
+ identifier: issue.identifier,
303
+ title: issue.title,
304
+ url: issue.url,
305
+ description: issue.description ?? undefined,
306
+ branchName: issue.branchName,
307
+ priority: { value: issue.priority, label: issue.priorityLabel },
308
+ state:
309
+ state === undefined
310
+ ? undefined
311
+ : { id: state.id, name: state.name, type: state.type },
312
+ team:
313
+ team === undefined ? undefined : { id: team.id, key: team.key, name: team.name },
314
+ assignee:
315
+ assignee === undefined
316
+ ? undefined
317
+ : { id: assignee.id, name: assignee.name, email: assignee.email },
318
+ project:
319
+ project === undefined
320
+ ? undefined
321
+ : { id: project.id, name: project.name, url: project.url },
322
+ parent: parent === undefined ? undefined : toIssueReference(parent),
323
+ labels: labels.map((label) => ({ id: label.id, name: label.name })),
324
+ children: children?.map(toIssueReference),
325
+ comments: detailedComments,
326
+ relations,
327
+ } satisfies IssueDetails;
328
+ }),
329
+ );
330
+
124
331
  const updateIssueState = Effect.fn("LinearService.updateIssueState")(
125
332
  (issueId: string, stateId: string) =>
126
333
  Effect.gen(function* () {
@@ -133,7 +340,14 @@ export class LinearService extends Context.Tag("@linear/LinearService")<
133
340
  );
134
341
 
135
342
  const createIssue = Effect.fn("LinearService.createIssue")(
136
- (input: { title: string; teamId: string; description?: string }) =>
343
+ (input: {
344
+ title: string;
345
+ teamId: string;
346
+ description?: string;
347
+ parentId?: string;
348
+ projectId?: string;
349
+ priority?: number;
350
+ }) =>
137
351
  Effect.gen(function* () {
138
352
  const client = yield* getClient;
139
353
  const payload = yield* Effect.tryPromise({
@@ -142,11 +356,14 @@ export class LinearService extends Context.Tag("@linear/LinearService")<
142
356
  title: input.title,
143
357
  teamId: input.teamId,
144
358
  description: input.description,
359
+ parentId: input.parentId,
360
+ projectId: input.projectId,
361
+ priority: input.priority,
145
362
  }),
146
363
  catch: (e) => LinearApiError.make({ message: String(e) }),
147
364
  });
148
365
  const issueFetch = payload.issue;
149
- if (!issueFetch) {
366
+ if (issueFetch === undefined) {
150
367
  return yield* LinearApiError.make({ message: "Failed to create issue" });
151
368
  }
152
369
  return yield* Effect.tryPromise({
@@ -156,38 +373,121 @@ export class LinearService extends Context.Tag("@linear/LinearService")<
156
373
  }),
157
374
  );
158
375
 
376
+ const createComment = Effect.fn("LinearService.createComment")(
377
+ (issueId: string, body: string) =>
378
+ Effect.gen(function* () {
379
+ const client = yield* getClient;
380
+ const payload = yield* Effect.tryPromise({
381
+ try: () => client.createComment({ issueId: normalizeIssueId(issueId), body }),
382
+ catch: (error) => LinearApiError.make({ message: String(error) }),
383
+ });
384
+ const comment = yield* resolveLinearFetch(payload.comment);
385
+ return comment === undefined
386
+ ? yield* LinearApiError.make({ message: "Linear did not return the created comment" })
387
+ : { id: comment.id, url: comment.url };
388
+ }),
389
+ );
390
+
391
+ const rawQuery = Effect.fn("LinearService.rawQuery")(
392
+ (query: string, variables: Readonly<Record<string, unknown>>) =>
393
+ Effect.gen(function* () {
394
+ const client = yield* getClient;
395
+ const response = yield* Effect.tryPromise({
396
+ try: () =>
397
+ client.client.rawRequest<unknown, Record<string, unknown>>(query, variables),
398
+ catch: (error) => LinearApiError.make({ message: String(error) }),
399
+ });
400
+ return response.data;
401
+ }),
402
+ );
403
+
159
404
  return LinearService.of({
160
405
  getViewer,
161
406
  getTeams,
407
+ getProjects,
162
408
  getIssues,
163
409
  getMyIssues,
164
410
  getIssue,
411
+ getIssueDetails,
165
412
  updateIssueState,
166
413
  createIssue,
414
+ createComment,
415
+ rawQuery,
167
416
  });
168
417
  }),
169
418
  );
170
419
 
171
- static readonly testLayer = (options?: {
420
+ static readonly layerTest = (options?: {
172
421
  viewer?: User;
173
422
  teams?: readonly Team[];
174
423
  issues?: readonly Issue[];
175
424
  }) =>
176
425
  Layer.succeed(LinearService, {
177
- getViewer: options?.viewer
178
- ? Effect.succeed(options.viewer)
179
- : Effect.fail(LinearApiError.make({ message: "No viewer in test" })),
426
+ getViewer:
427
+ options?.viewer !== undefined
428
+ ? Effect.succeed(options.viewer)
429
+ : Effect.fail(LinearApiError.make({ message: "No viewer in test" })),
180
430
  getTeams: Effect.succeed(options?.teams ?? []),
431
+ getProjects: Effect.succeed([]),
181
432
  getIssues: (_filter) => Effect.succeed(options?.issues ?? []),
182
433
  getMyIssues: (_filter) => Effect.succeed(options?.issues ?? []),
183
434
  getIssue: (id) => {
184
435
  const issue = options?.issues?.find((i) => i.id === id || i.identifier === id);
185
- return issue
436
+ return issue !== undefined
186
437
  ? Effect.succeed(issue)
187
438
  : Effect.fail(LinearApiError.make({ message: `Issue ${id} not found` }));
188
439
  },
440
+ getIssueDetails: (id, _options) => {
441
+ const issue = options?.issues?.find(
442
+ (candidate) => candidate.id === id || candidate.identifier === id,
443
+ );
444
+ return issue === undefined
445
+ ? Effect.fail(LinearApiError.make({ message: `Issue ${id} not found` }))
446
+ : Effect.succeed({
447
+ id: issue.id,
448
+ identifier: issue.identifier,
449
+ title: issue.title,
450
+ url: issue.url,
451
+ description: issue.description ?? undefined,
452
+ branchName: issue.branchName,
453
+ priority: { value: issue.priority, label: issue.priorityLabel },
454
+ labels: [],
455
+ });
456
+ },
189
457
  updateIssueState: (_issueId, _stateId) => Effect.void,
190
458
  createIssue: (_input) =>
191
459
  Effect.fail(LinearApiError.make({ message: "Not implemented in test" })),
460
+ createComment: (_issueId, _body) =>
461
+ Effect.fail(LinearApiError.make({ message: "Not implemented in test" })),
462
+ rawQuery: (_query, _variables) =>
463
+ Effect.fail(LinearApiError.make({ message: "Not implemented in test" })),
192
464
  });
193
465
  }
466
+
467
+ const normalizeIssueId = (input: string): string => {
468
+ const match = input.match(/\/issue\/([A-Za-z]+-\d+)(?:\/|$)/);
469
+ return match?.[1] ?? input;
470
+ };
471
+
472
+ const toIssueReference = (issue: Issue): IssueReference => ({
473
+ id: issue.id,
474
+ identifier: issue.identifier,
475
+ title: issue.title,
476
+ url: issue.url,
477
+ });
478
+
479
+ const resolveLinearFetch = <T>(
480
+ fetch: PromiseLike<T> | undefined,
481
+ ): Effect.Effect<T | undefined, LinearApiError> =>
482
+ fetch === undefined
483
+ ? Effect.succeed<T | undefined>(undefined)
484
+ : Effect.tryPromise({
485
+ try: () => Promise.resolve(fetch),
486
+ catch: (error) => LinearApiError.make({ message: String(error) }),
487
+ });
488
+
489
+ const loadConnection = <T>(load: () => PromiseLike<{ readonly nodes: ReadonlyArray<T> }>) =>
490
+ Effect.tryPromise({
491
+ try: () => Promise.resolve(load()),
492
+ catch: (error) => LinearApiError.make({ message: String(error) }),
493
+ }).pipe(Effect.map((connection) => connection.nodes));
@@ -1,33 +1,26 @@
1
- import { Context, Effect, Layer } from "effect";
1
+ import { Console, Context, Effect, Layer, Terminal } from "effect";
2
2
 
3
3
  /**
4
4
  * Simple stdin reading service that works with Bun compiled binaries.
5
5
  * Uses Node's readline API which Bun supports.
6
6
  */
7
- export class StdinService extends Context.Tag("StdinService")<
7
+ export class StdinService extends Context.Service<
8
8
  StdinService,
9
9
  {
10
- readonly readLine: (prompt: string) => Effect.Effect<string>;
10
+ readonly readLine: (prompt: string) => Effect.Effect<string, Terminal.QuitError>;
11
11
  }
12
- >() {
13
- static readonly layer = Layer.succeed(StdinService, {
14
- readLine: (prompt) =>
15
- Effect.async<string>((resume) => {
16
- // Use Node's readline API (Bun compatible)
17
- const readline = require("node:readline");
18
- const rl = readline.createInterface({
19
- input: process.stdin,
20
- output: process.stdout,
21
- });
12
+ >()("@cvr/linear/services/Stdin/StdinService") {
13
+ static readonly layer = Layer.effect(
14
+ StdinService,
15
+ Effect.gen(function* () {
16
+ const terminal = yield* Terminal.Terminal;
17
+ return StdinService.of({
18
+ readLine: (prompt) => Console.log(prompt).pipe(Effect.andThen(terminal.readLine)),
19
+ });
20
+ }),
21
+ );
22
22
 
23
- rl.question(prompt, (answer: string) => {
24
- rl.close();
25
- resume(Effect.succeed(answer));
26
- });
27
- }),
28
- });
29
-
30
- static readonly testLayer = (input: string) =>
23
+ static readonly layerTest = (input: string) =>
31
24
  Layer.succeed(StdinService, {
32
25
  readLine: (_prompt) => Effect.succeed(input),
33
26
  });
package/tsconfig.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "target": "ES2022",
4
- "module": "NodeNext",
5
- "moduleResolution": "NodeNext",
3
+ "target": "ESNext",
4
+ "module": "ESNext",
5
+ "moduleResolution": "bundler",
6
6
  "moduleDetection": "force",
7
7
  "verbatimModuleSyntax": true,
8
8
  "esModuleInterop": true,
9
+ "resolveJsonModule": true,
9
10
  "strict": true,
11
+ "noUncheckedIndexedAccess": true,
12
+ "noFallthroughCasesInSwitch": true,
13
+ "noPropertyAccessFromIndexSignature": true,
10
14
  "noUnusedLocals": true,
11
15
  "noImplicitOverride": true,
12
16
  "skipLibCheck": true,
@@ -14,14 +18,70 @@
14
18
  "declarationMap": true,
15
19
  "sourceMap": true,
16
20
  "outDir": "dist",
17
- "rootDir": "src",
18
- "types": ["bun-types"],
21
+ "rootDir": ".",
22
+ "types": ["bun"],
19
23
  "plugins": [
20
24
  {
21
- "name": "@effect/language-service"
25
+ "name": "@effect/language-service",
26
+ "diagnostics": true,
27
+ "diagnosticsName": true,
28
+ "includeSuggestionsInTsc": true,
29
+ "ignoreEffectSuggestionsInTscExitCode": false,
30
+ "ignoreEffectWarningsInTscExitCode": false,
31
+ "ignoreEffectErrorsInTscExitCode": false,
32
+ "diagnosticSeverity": {
33
+ "anyUnknownInErrorContext": "error",
34
+ "deterministicKeys": "error",
35
+ "importFromBarrel": "warning",
36
+ "instanceOfSchema": "warning",
37
+ "missedPipeableOpportunity": "suggestion",
38
+ "missingEffectServiceDependency": "warning",
39
+ "schemaUnionOfLiterals": "warning",
40
+ "strictBooleanExpressions": "warning",
41
+ "strictEffectProvide": "warning",
42
+ "catchAllToMapError": "warning",
43
+ "catchUnfailableEffect": "warning",
44
+ "effectFnOpportunity": "warning",
45
+ "effectMapVoid": "warning",
46
+ "effectSucceedWithVoid": "warning",
47
+ "leakingRequirements": "warning",
48
+ "preferSchemaOverJson": "warning",
49
+ "redundantSchemaTagIdentifier": "warning",
50
+ "returnEffectInGen": "warning",
51
+ "runEffectInsideEffect": "error",
52
+ "schemaStructWithTag": "warning",
53
+ "schemaSyncInEffect": "warning",
54
+ "tryCatchInEffectGen": "warning",
55
+ "unnecessaryEffectGen": "warning",
56
+ "unnecessaryFailYieldableError": "warning",
57
+ "unnecessaryPipe": "warning",
58
+ "unnecessaryPipeChain": "warning"
59
+ },
60
+ "keyPatterns": [
61
+ {
62
+ "target": "service",
63
+ "pattern": "default",
64
+ "skipLeadingPath": ["src/"]
65
+ },
66
+ {
67
+ "target": "error",
68
+ "pattern": "default",
69
+ "skipLeadingPath": ["src/"]
70
+ }
71
+ ],
72
+ "overrides": [
73
+ {
74
+ "include": ["**/tests/**/*.ts", "**/*.test.ts"],
75
+ "options": {
76
+ "diagnosticSeverity": {
77
+ "strictEffectProvide": "off"
78
+ }
79
+ }
80
+ }
81
+ ]
22
82
  }
23
83
  ]
24
84
  },
25
- "include": ["src/**/*"],
85
+ "include": ["src/**/*", "tests/**/*", "vitest.config.ts"],
26
86
  "exclude": ["node_modules", "dist", "bin"]
27
87
  }