@cvr/linear 0.2.0 → 0.4.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.
- package/README.md +43 -10
- package/bin/linear +0 -0
- package/package.json +25 -21
- package/skills/linear/SKILL.md +146 -0
- package/src/cli.ts +14 -0
- package/src/commands/api.ts +148 -0
- package/src/commands/auth.ts +46 -34
- package/src/commands/issue.ts +475 -201
- package/src/commands/project.ts +51 -0
- package/src/commands/team.ts +28 -5
- package/src/domain/Input.ts +55 -0
- package/src/domain/Linear.ts +134 -0
- package/src/lib/errors.ts +11 -18
- package/src/lib/flags.ts +5 -0
- package/src/lib/json.ts +5 -0
- package/src/main.ts +18 -30
- package/src/services/Config.ts +118 -129
- package/src/services/Linear.ts +421 -159
- package/tsconfig.json +67 -7
- package/src/services/Browser.ts +0 -23
- package/src/services/Stdin.ts +0 -34
package/src/services/Linear.ts
CHANGED
|
@@ -1,193 +1,455 @@
|
|
|
1
|
-
import { Context, Effect, Layer } from "effect";
|
|
2
1
|
import { LinearClient } from "@linear/sdk";
|
|
3
|
-
import type { Issue,
|
|
4
|
-
import {
|
|
2
|
+
import type { Issue, IssueRelation as SdkIssueRelation, User } from "@linear/sdk";
|
|
3
|
+
import { Context, Effect, Layer, Redacted } from "effect";
|
|
4
|
+
import type {
|
|
5
|
+
CreateIssueInput,
|
|
6
|
+
CreatedIssue,
|
|
7
|
+
IssueDetails,
|
|
8
|
+
IssueDetailsOptions,
|
|
9
|
+
IssueReference,
|
|
10
|
+
IssueSelector,
|
|
11
|
+
IssueSummary,
|
|
12
|
+
Project,
|
|
13
|
+
StartedIssue,
|
|
14
|
+
Team,
|
|
15
|
+
Viewer,
|
|
16
|
+
} from "../domain/Linear.js";
|
|
17
|
+
import type { ConfigError, TokenNotFoundError } from "../lib/errors.js";
|
|
18
|
+
import { InvalidInputError, InvalidTokenError, LinearApiError } from "../lib/errors.js";
|
|
5
19
|
import { ConfigService } from "./Config.js";
|
|
6
20
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
readonly
|
|
11
|
-
readonly
|
|
21
|
+
type LinearError = ConfigError | InvalidTokenError | LinearApiError | TokenNotFoundError;
|
|
22
|
+
|
|
23
|
+
interface LinearOperations {
|
|
24
|
+
readonly authenticate: (token: Redacted.Redacted<string>) => Effect.Effect<Viewer, LinearError>;
|
|
25
|
+
readonly getViewer: Effect.Effect<Viewer, LinearError>;
|
|
26
|
+
readonly getTeams: Effect.Effect<readonly Team[], LinearError>;
|
|
27
|
+
readonly getProjects: Effect.Effect<readonly Project[], LinearError>;
|
|
28
|
+
readonly getMyIssues: (filter?: {
|
|
29
|
+
readonly state?: string;
|
|
30
|
+
readonly limit?: number;
|
|
31
|
+
}) => Effect.Effect<readonly IssueSummary[], LinearError>;
|
|
32
|
+
readonly getIssueDetails: (
|
|
33
|
+
id: IssueSelector,
|
|
34
|
+
options: IssueDetailsOptions,
|
|
35
|
+
) => Effect.Effect<IssueDetails, LinearError>;
|
|
36
|
+
readonly startIssue: (
|
|
37
|
+
id: IssueSelector,
|
|
38
|
+
options: { readonly dryRun: boolean },
|
|
39
|
+
) => Effect.Effect<StartedIssue, LinearError | InvalidInputError>;
|
|
40
|
+
readonly createIssue: (input: CreateIssueInput) => Effect.Effect<CreatedIssue, LinearError>;
|
|
41
|
+
readonly createComment: (
|
|
42
|
+
issueId: IssueSelector,
|
|
43
|
+
body: string,
|
|
44
|
+
) => Effect.Effect<{ readonly id: string; readonly url: string }, LinearError>;
|
|
45
|
+
readonly rawQuery: (
|
|
46
|
+
query: string,
|
|
47
|
+
variables: Readonly<Record<string, unknown>>,
|
|
48
|
+
) => Effect.Effect<unknown, LinearError>;
|
|
12
49
|
}
|
|
13
50
|
|
|
14
|
-
export class LinearService extends Context.
|
|
15
|
-
LinearService,
|
|
16
|
-
|
|
17
|
-
readonly getViewer: Effect.Effect<
|
|
18
|
-
User,
|
|
19
|
-
LinearApiError | InvalidTokenError | TokenNotFoundError
|
|
20
|
-
>;
|
|
21
|
-
readonly getTeams: Effect.Effect<readonly Team[], LinearApiError | TokenNotFoundError>;
|
|
22
|
-
readonly getIssues: (
|
|
23
|
-
filter: IssueFilter,
|
|
24
|
-
) => Effect.Effect<readonly Issue[], LinearApiError | TokenNotFoundError>;
|
|
25
|
-
readonly getIssue: (id: string) => Effect.Effect<Issue, LinearApiError | TokenNotFoundError>;
|
|
26
|
-
readonly getMyIssues: (filter?: {
|
|
27
|
-
state?: string;
|
|
28
|
-
}) => Effect.Effect<readonly Issue[], LinearApiError | TokenNotFoundError>;
|
|
29
|
-
readonly updateIssueState: (
|
|
30
|
-
issueId: string,
|
|
31
|
-
stateId: string,
|
|
32
|
-
) => Effect.Effect<void, LinearApiError | TokenNotFoundError>;
|
|
33
|
-
readonly createIssue: (input: {
|
|
34
|
-
title: string;
|
|
35
|
-
teamId: string;
|
|
36
|
-
description?: string;
|
|
37
|
-
}) => Effect.Effect<Issue, LinearApiError | TokenNotFoundError>;
|
|
38
|
-
}
|
|
39
|
-
>() {
|
|
51
|
+
export class LinearService extends Context.Service<LinearService, LinearOperations>()(
|
|
52
|
+
"@cvr/linear/services/Linear/LinearService",
|
|
53
|
+
) {
|
|
40
54
|
static readonly layer = Layer.effect(
|
|
41
55
|
LinearService,
|
|
42
56
|
Effect.gen(function* () {
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
Effect.map((token) => new LinearClient({ apiKey: token })),
|
|
49
|
-
Effect.cached,
|
|
50
|
-
Effect.runSync,
|
|
57
|
+
const config = yield* ConfigService;
|
|
58
|
+
const getClient = yield* Effect.cached(
|
|
59
|
+
config.getToken.pipe(
|
|
60
|
+
Effect.map((token) => new LinearClient({ apiKey: Redacted.value(token) })),
|
|
61
|
+
),
|
|
51
62
|
);
|
|
52
63
|
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
64
|
+
const withClient = <A>(
|
|
65
|
+
operation: (client: LinearClient) => PromiseLike<A>,
|
|
66
|
+
): Effect.Effect<A, LinearError> =>
|
|
67
|
+
getClient.pipe(
|
|
68
|
+
Effect.flatMap((client) =>
|
|
69
|
+
Effect.tryPromise({
|
|
70
|
+
try: () => Promise.resolve(operation(client)),
|
|
71
|
+
catch: toLinearError,
|
|
72
|
+
}),
|
|
73
|
+
),
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
const authenticate = Effect.fn("LinearService.authenticate")(function* (
|
|
77
|
+
token: Redacted.Redacted<string>,
|
|
78
|
+
) {
|
|
79
|
+
const client = new LinearClient({ apiKey: Redacted.value(token) });
|
|
80
|
+
const viewer = yield* Effect.tryPromise({
|
|
56
81
|
try: () => client.viewer,
|
|
57
|
-
catch:
|
|
58
|
-
const msg = String(e);
|
|
59
|
-
if (msg.includes("Authentication") || msg.includes("401")) {
|
|
60
|
-
return InvalidTokenError.make({ message: "Invalid API token" });
|
|
61
|
-
}
|
|
62
|
-
return LinearApiError.make({ message: msg });
|
|
63
|
-
},
|
|
82
|
+
catch: toLinearError,
|
|
64
83
|
});
|
|
65
|
-
|
|
84
|
+
yield* config.saveToken(token);
|
|
85
|
+
return toViewer(viewer);
|
|
86
|
+
});
|
|
66
87
|
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
try: () => client.teams(),
|
|
71
|
-
catch: (e) => LinearApiError.make({ message: String(e) }),
|
|
72
|
-
});
|
|
73
|
-
return connection.nodes;
|
|
74
|
-
}).pipe(Effect.withSpan("LinearService.getTeams"));
|
|
75
|
-
|
|
76
|
-
const getIssues = Effect.fn("LinearService.getIssues")((filter: IssueFilter) =>
|
|
77
|
-
Effect.gen(function* () {
|
|
78
|
-
const client = yield* getClient;
|
|
79
|
-
const connection = yield* Effect.tryPromise({
|
|
80
|
-
try: () =>
|
|
81
|
-
client.issues({
|
|
82
|
-
first: filter.limit ?? 50,
|
|
83
|
-
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,
|
|
87
|
-
},
|
|
88
|
-
}),
|
|
89
|
-
catch: (e) => LinearApiError.make({ message: String(e) }),
|
|
90
|
-
});
|
|
91
|
-
return connection.nodes;
|
|
92
|
-
}),
|
|
88
|
+
const getViewer = withClient((client) => client.viewer).pipe(
|
|
89
|
+
Effect.map(toViewer),
|
|
90
|
+
Effect.withSpan("LinearService.getViewer"),
|
|
93
91
|
);
|
|
94
92
|
|
|
95
|
-
const
|
|
96
|
-
Effect.
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
try: () => client.viewer,
|
|
100
|
-
catch: (e) => LinearApiError.make({ message: String(e) }),
|
|
101
|
-
});
|
|
102
|
-
const connection = yield* Effect.tryPromise({
|
|
103
|
-
try: () =>
|
|
104
|
-
viewer.assignedIssues({
|
|
105
|
-
first: 50,
|
|
106
|
-
filter: filter?.state ? { state: { type: { eq: filter.state } } } : undefined,
|
|
107
|
-
}),
|
|
108
|
-
catch: (e) => LinearApiError.make({ message: String(e) }),
|
|
109
|
-
});
|
|
110
|
-
return connection.nodes;
|
|
111
|
-
}),
|
|
93
|
+
const getTeams = withClient((client) => client.teams()).pipe(
|
|
94
|
+
Effect.flatMap(loadAllPages),
|
|
95
|
+
Effect.map((teams) => teams.map(toTeam)),
|
|
96
|
+
Effect.withSpan("LinearService.getTeams"),
|
|
112
97
|
);
|
|
113
98
|
|
|
114
|
-
const
|
|
115
|
-
Effect.
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
try: () => client.issue(id),
|
|
119
|
-
catch: (e) => LinearApiError.make({ message: String(e) }),
|
|
120
|
-
});
|
|
121
|
-
}),
|
|
99
|
+
const getProjects = withClient((client) => client.projects()).pipe(
|
|
100
|
+
Effect.flatMap(loadAllPages),
|
|
101
|
+
Effect.map((projects) => projects.map(toProject)),
|
|
102
|
+
Effect.withSpan("LinearService.getProjects"),
|
|
122
103
|
);
|
|
123
104
|
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
105
|
+
const getMyIssues = Effect.fn("LinearService.getMyIssues")(function* (filter?: {
|
|
106
|
+
readonly state?: string;
|
|
107
|
+
readonly limit?: number;
|
|
108
|
+
}) {
|
|
109
|
+
const viewer = yield* withClient((client) => client.viewer);
|
|
110
|
+
const connection = yield* resolveLinearFetch(
|
|
111
|
+
viewer.assignedIssues({
|
|
112
|
+
first: filter?.limit ?? 50,
|
|
113
|
+
filter:
|
|
114
|
+
filter?.state === undefined ? undefined : { state: { type: { eq: filter.state } } },
|
|
132
115
|
}),
|
|
133
|
-
|
|
116
|
+
);
|
|
117
|
+
return yield* Effect.forEach(connection.nodes, toIssueSummary, { concurrency: 4 });
|
|
118
|
+
});
|
|
134
119
|
|
|
135
|
-
const
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
120
|
+
const getIssue = (id: IssueSelector) => withClient((client) => client.issue(id));
|
|
121
|
+
|
|
122
|
+
const getIssueDetails = Effect.fn("LinearService.getIssueDetails")(function* (
|
|
123
|
+
id: IssueSelector,
|
|
124
|
+
options: IssueDetailsOptions,
|
|
125
|
+
) {
|
|
126
|
+
const issue = yield* getIssue(id);
|
|
127
|
+
const [summary, team, assignee, project, parent, labels] = yield* Effect.all([
|
|
128
|
+
toIssueSummary(issue),
|
|
129
|
+
resolveOptionalFetch(issue.team),
|
|
130
|
+
resolveOptionalFetch(issue.assignee),
|
|
131
|
+
resolveOptionalFetch(issue.project),
|
|
132
|
+
resolveOptionalFetch(issue.parent),
|
|
133
|
+
loadConnection(() => issue.labels()),
|
|
134
|
+
]);
|
|
135
|
+
|
|
136
|
+
const children = options.children
|
|
137
|
+
? yield* loadConnection(() => issue.children())
|
|
138
|
+
: undefined;
|
|
139
|
+
const comments = options.comments
|
|
140
|
+
? yield* loadConnection(() => issue.comments())
|
|
141
|
+
: undefined;
|
|
142
|
+
const outbound = options.relations
|
|
143
|
+
? yield* loadConnection(() => issue.relations())
|
|
144
|
+
: undefined;
|
|
145
|
+
const inbound = options.relations
|
|
146
|
+
? yield* loadConnection(() => issue.inverseRelations())
|
|
147
|
+
: undefined;
|
|
148
|
+
|
|
149
|
+
const detailedComments =
|
|
150
|
+
comments === undefined
|
|
151
|
+
? undefined
|
|
152
|
+
: yield* Effect.forEach(
|
|
153
|
+
comments,
|
|
154
|
+
(comment) =>
|
|
155
|
+
resolveOptionalFetch(comment.user).pipe(
|
|
156
|
+
Effect.map((author) => ({
|
|
157
|
+
id: comment.id,
|
|
158
|
+
body: comment.body,
|
|
159
|
+
createdAt: comment.createdAt.toISOString(),
|
|
160
|
+
url: comment.url,
|
|
161
|
+
author:
|
|
162
|
+
author === undefined ? undefined : { id: author.id, name: author.name },
|
|
163
|
+
})),
|
|
164
|
+
),
|
|
165
|
+
{ concurrency: 4 },
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
const relations =
|
|
169
|
+
outbound === undefined || inbound === undefined
|
|
170
|
+
? undefined
|
|
171
|
+
: yield* Effect.forEach(
|
|
172
|
+
outbound
|
|
173
|
+
.map<{
|
|
174
|
+
readonly relation: SdkIssueRelation;
|
|
175
|
+
readonly direction: "outbound" | "inbound";
|
|
176
|
+
}>((relation) => ({ relation, direction: "outbound" }))
|
|
177
|
+
.concat(inbound.map((relation) => ({ relation, direction: "inbound" }))),
|
|
178
|
+
Effect.fn("LinearService.resolveRelation")(function* ({ direction, relation }) {
|
|
179
|
+
const related = yield* resolveOptionalFetch(
|
|
180
|
+
direction === "outbound" ? relation.relatedIssue : relation.issue,
|
|
181
|
+
);
|
|
182
|
+
return related === undefined
|
|
183
|
+
? undefined
|
|
184
|
+
: {
|
|
185
|
+
id: relation.id,
|
|
186
|
+
type: relation.type,
|
|
187
|
+
direction,
|
|
188
|
+
issue: toIssueReference(related),
|
|
189
|
+
};
|
|
145
190
|
}),
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
191
|
+
{ concurrency: 4 },
|
|
192
|
+
).pipe(
|
|
193
|
+
Effect.map((items) =>
|
|
194
|
+
items.filter((item): item is NonNullable<typeof item> => item !== undefined),
|
|
195
|
+
),
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
return {
|
|
199
|
+
id: summary.id,
|
|
200
|
+
identifier: summary.identifier,
|
|
201
|
+
title: summary.title,
|
|
202
|
+
url: summary.url,
|
|
203
|
+
branchName: summary.branchName,
|
|
204
|
+
priority: summary.priority,
|
|
205
|
+
state: summary.state,
|
|
206
|
+
description: issue.description ?? undefined,
|
|
207
|
+
team: team === undefined ? undefined : { id: team.id, key: team.key, name: team.name },
|
|
208
|
+
assignee:
|
|
209
|
+
assignee === undefined
|
|
210
|
+
? undefined
|
|
211
|
+
: { id: assignee.id, name: assignee.name, email: assignee.email },
|
|
212
|
+
project:
|
|
213
|
+
project === undefined
|
|
214
|
+
? undefined
|
|
215
|
+
: { id: project.id, name: project.name, url: project.url },
|
|
216
|
+
parent: parent === undefined ? undefined : toIssueReference(parent),
|
|
217
|
+
labels: labels.map((label) => ({ id: label.id, name: label.name })),
|
|
218
|
+
children: children?.map(toIssueReference),
|
|
219
|
+
comments: detailedComments,
|
|
220
|
+
relations,
|
|
221
|
+
} satisfies IssueDetails;
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
const startIssue = Effect.fn("LinearService.startIssue")(function* (
|
|
225
|
+
id: IssueSelector,
|
|
226
|
+
options: { readonly dryRun: boolean },
|
|
227
|
+
) {
|
|
228
|
+
const issue = yield* getIssue(id);
|
|
229
|
+
const team = yield* resolveOptionalFetch(issue.team);
|
|
230
|
+
if (team === undefined) {
|
|
231
|
+
return yield* InvalidInputError.make({
|
|
232
|
+
message: `Issue ${issue.identifier} is not associated with a team`,
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
const states = yield* loadConnection(() => team.states());
|
|
236
|
+
const started = states.find(
|
|
237
|
+
(state) => state.type === "started" || state.name.toLowerCase() === "in progress",
|
|
238
|
+
);
|
|
239
|
+
if (started === undefined) {
|
|
240
|
+
return yield* InvalidInputError.make({
|
|
241
|
+
message: `No started state exists for team ${team.key}`,
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
if (!options.dryRun) {
|
|
245
|
+
yield* withClient((client) => client.updateIssue(issue.id, { stateId: started.id }));
|
|
246
|
+
}
|
|
247
|
+
return {
|
|
248
|
+
issue: toIssueReference(issue),
|
|
249
|
+
state: { id: started.id, name: started.name, type: started.type },
|
|
250
|
+
branchName: issue.branchName,
|
|
251
|
+
};
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
const createIssue = Effect.fn("LinearService.createIssue")(function* (
|
|
255
|
+
input: CreateIssueInput,
|
|
256
|
+
) {
|
|
257
|
+
const parentId =
|
|
258
|
+
input.parent === undefined ? undefined : (yield* getIssue(input.parent)).id;
|
|
259
|
+
const payload = yield* withClient((client) =>
|
|
260
|
+
client.createIssue({
|
|
261
|
+
title: input.title,
|
|
262
|
+
teamId: input.teamId,
|
|
263
|
+
description: input.description,
|
|
264
|
+
parentId,
|
|
265
|
+
projectId: input.projectId,
|
|
266
|
+
priority: input.priority,
|
|
156
267
|
}),
|
|
157
|
-
|
|
268
|
+
);
|
|
269
|
+
if (payload.issue === undefined) {
|
|
270
|
+
return yield* LinearApiError.make({ message: "Linear did not return the created issue" });
|
|
271
|
+
}
|
|
272
|
+
return toIssueReference(yield* resolveLinearFetch(payload.issue));
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
const createComment = Effect.fn("LinearService.createComment")(function* (
|
|
276
|
+
issueId: IssueSelector,
|
|
277
|
+
body: string,
|
|
278
|
+
) {
|
|
279
|
+
const payload = yield* withClient((client) => client.createComment({ issueId, body }));
|
|
280
|
+
const comment = yield* resolveOptionalFetch(payload.comment);
|
|
281
|
+
return comment === undefined
|
|
282
|
+
? yield* LinearApiError.make({ message: "Linear did not return the created comment" })
|
|
283
|
+
: { id: comment.id, url: comment.url };
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
const rawQuery = Effect.fn("LinearService.rawQuery")(function* (
|
|
287
|
+
query: string,
|
|
288
|
+
variables: Readonly<Record<string, unknown>>,
|
|
289
|
+
) {
|
|
290
|
+
const response = yield* withClient((client) =>
|
|
291
|
+
client.client.rawRequest<unknown, Record<string, unknown>>(query, variables),
|
|
292
|
+
);
|
|
293
|
+
return response.data;
|
|
294
|
+
});
|
|
158
295
|
|
|
159
296
|
return LinearService.of({
|
|
297
|
+
authenticate,
|
|
160
298
|
getViewer,
|
|
161
299
|
getTeams,
|
|
162
|
-
|
|
300
|
+
getProjects,
|
|
163
301
|
getMyIssues,
|
|
164
|
-
|
|
165
|
-
|
|
302
|
+
getIssueDetails,
|
|
303
|
+
startIssue,
|
|
166
304
|
createIssue,
|
|
305
|
+
createComment,
|
|
306
|
+
rawQuery,
|
|
167
307
|
});
|
|
168
308
|
}),
|
|
169
309
|
);
|
|
170
310
|
|
|
171
|
-
static readonly
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
311
|
+
static readonly layerTest = (overrides: Partial<LinearOperations> = {}) =>
|
|
312
|
+
Layer.succeed(
|
|
313
|
+
LinearService,
|
|
314
|
+
Object.assign(
|
|
315
|
+
{
|
|
316
|
+
authenticate: (token) =>
|
|
317
|
+
Effect.succeed({
|
|
318
|
+
id: "viewer",
|
|
319
|
+
name: "Test User",
|
|
320
|
+
email: `${Redacted.value(token)}@example.com`,
|
|
321
|
+
admin: false,
|
|
322
|
+
}),
|
|
323
|
+
getViewer: Effect.succeed({
|
|
324
|
+
id: "viewer",
|
|
325
|
+
name: "Test User",
|
|
326
|
+
email: "test@example.com",
|
|
327
|
+
admin: false,
|
|
328
|
+
}),
|
|
329
|
+
getTeams: Effect.succeed([]),
|
|
330
|
+
getProjects: Effect.succeed([]),
|
|
331
|
+
getMyIssues: () => Effect.succeed([]),
|
|
332
|
+
getIssueDetails: (id) =>
|
|
333
|
+
Effect.succeed({
|
|
334
|
+
id,
|
|
335
|
+
identifier: id,
|
|
336
|
+
title: "Test issue",
|
|
337
|
+
url: `https://linear.app/issue/${id}`,
|
|
338
|
+
branchName: id.toLowerCase(),
|
|
339
|
+
priority: { value: 0, label: "No priority" },
|
|
340
|
+
labels: [],
|
|
341
|
+
}),
|
|
342
|
+
startIssue: (id) =>
|
|
343
|
+
Effect.succeed({
|
|
344
|
+
issue: {
|
|
345
|
+
id,
|
|
346
|
+
identifier: id,
|
|
347
|
+
title: "Test issue",
|
|
348
|
+
url: `https://linear.app/issue/${id}`,
|
|
349
|
+
},
|
|
350
|
+
state: { id: "started", name: "In Progress", type: "started" },
|
|
351
|
+
branchName: id.toLowerCase(),
|
|
352
|
+
}),
|
|
353
|
+
createIssue: (input) =>
|
|
354
|
+
Effect.succeed({
|
|
355
|
+
id: "created",
|
|
356
|
+
identifier: "TEST-1",
|
|
357
|
+
title: input.title,
|
|
358
|
+
url: "https://linear.app/issue/TEST-1",
|
|
359
|
+
}),
|
|
360
|
+
createComment: () =>
|
|
361
|
+
Effect.succeed({ id: "comment", url: "https://linear.app/comment/comment" }),
|
|
362
|
+
rawQuery: () => Effect.succeed({}),
|
|
363
|
+
} satisfies LinearOperations,
|
|
364
|
+
overrides,
|
|
365
|
+
),
|
|
366
|
+
);
|
|
193
367
|
}
|
|
368
|
+
|
|
369
|
+
const toLinearError = (error: unknown): InvalidTokenError | LinearApiError => {
|
|
370
|
+
const message = String(error);
|
|
371
|
+
return message.includes("Authentication") || message.includes("401")
|
|
372
|
+
? InvalidTokenError.make({ message: "Invalid API token" })
|
|
373
|
+
: LinearApiError.make({ message });
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
const toViewer = (viewer: User): Viewer => ({
|
|
377
|
+
id: viewer.id,
|
|
378
|
+
name: viewer.name,
|
|
379
|
+
email: viewer.email,
|
|
380
|
+
admin: viewer.admin,
|
|
381
|
+
status:
|
|
382
|
+
viewer.statusEmoji === undefined && viewer.statusLabel === undefined
|
|
383
|
+
? undefined
|
|
384
|
+
: {
|
|
385
|
+
emoji: viewer.statusEmoji ?? undefined,
|
|
386
|
+
label: viewer.statusLabel ?? undefined,
|
|
387
|
+
},
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
const toTeam = (team: Awaited<ReturnType<LinearClient["team"]>>): Team => ({
|
|
391
|
+
id: team.id,
|
|
392
|
+
key: team.key,
|
|
393
|
+
name: team.name,
|
|
394
|
+
description: team.description ?? undefined,
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
const toProject = (project: Awaited<ReturnType<LinearClient["project"]>>): Project => ({
|
|
398
|
+
id: project.id,
|
|
399
|
+
name: project.name,
|
|
400
|
+
slug: project.slugId,
|
|
401
|
+
state: project.state,
|
|
402
|
+
url: project.url,
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
const toIssueReference = (issue: Issue): IssueReference => ({
|
|
406
|
+
id: issue.id,
|
|
407
|
+
identifier: issue.identifier,
|
|
408
|
+
title: issue.title,
|
|
409
|
+
url: issue.url,
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
const toIssueSummary = Effect.fn("LinearService.toIssueSummary")(function* (
|
|
413
|
+
issue: Issue,
|
|
414
|
+
): Effect.fn.Return<IssueSummary, LinearApiError> {
|
|
415
|
+
const state = yield* resolveOptionalFetch(issue.state);
|
|
416
|
+
return {
|
|
417
|
+
id: issue.id,
|
|
418
|
+
identifier: issue.identifier,
|
|
419
|
+
title: issue.title,
|
|
420
|
+
url: issue.url,
|
|
421
|
+
branchName: issue.branchName,
|
|
422
|
+
priority: { value: issue.priority, label: issue.priorityLabel },
|
|
423
|
+
state: state === undefined ? undefined : { id: state.id, name: state.name, type: state.type },
|
|
424
|
+
};
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
const resolveLinearFetch = <T>(fetch: PromiseLike<T>): Effect.Effect<T, LinearApiError> =>
|
|
428
|
+
Effect.tryPromise({
|
|
429
|
+
try: () => Promise.resolve(fetch),
|
|
430
|
+
catch: (error) => LinearApiError.make({ message: String(error) }),
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
const resolveOptionalFetch = <T>(
|
|
434
|
+
fetch: PromiseLike<T> | undefined,
|
|
435
|
+
): Effect.Effect<T | undefined, LinearApiError> =>
|
|
436
|
+
fetch === undefined ? Effect.succeed(undefined) : resolveLinearFetch(fetch);
|
|
437
|
+
|
|
438
|
+
interface PaginatedConnection<T> {
|
|
439
|
+
readonly nodes: ReadonlyArray<T>;
|
|
440
|
+
readonly pageInfo: { readonly hasNextPage: boolean };
|
|
441
|
+
readonly fetchNext: () => PromiseLike<PaginatedConnection<T>>;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
const loadAllPages = Effect.fn("LinearService.loadAllPages")(function* <T>(
|
|
445
|
+
initial: PaginatedConnection<T>,
|
|
446
|
+
) {
|
|
447
|
+
let connection = initial;
|
|
448
|
+
while (connection.pageInfo.hasNextPage) {
|
|
449
|
+
connection = yield* resolveLinearFetch(connection.fetchNext());
|
|
450
|
+
}
|
|
451
|
+
return connection.nodes;
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
const loadConnection = <T>(load: () => PromiseLike<PaginatedConnection<T>>) =>
|
|
455
|
+
resolveLinearFetch(load()).pipe(Effect.flatMap(loadAllPages));
|