@theholocron/holocron-plugin-linear 5.0.0-alpha.503 → 5.0.0-alpha.505

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.
Files changed (2) hide show
  1. package/dist/index.mjs +181 -164
  2. package/package.json +5 -5
package/dist/index.mjs CHANGED
@@ -92,6 +92,179 @@ function normalizeKey(key, teamKey) {
92
92
  const trimmed = key.trim().replace(/^#/, "");
93
93
  return /^\d+$/.test(trimmed) ? `${teamKey}-${trimmed}` : trimmed.toUpperCase();
94
94
  }
95
+ // src/capabilities/issues-update.ts
96
+ /** Linear's estimate for each effort, on the usual 1-3-5 scale. */
97
+ const ESTIMATE = {
98
+ low: 1,
99
+ medium: 3,
100
+ high: 5
101
+ };
102
+ /** The canonical label that stands for each kind where there is no Type. */
103
+ const KIND_LABELS = {
104
+ bug: "bug",
105
+ feature: "enhancement",
106
+ task: "chore"
107
+ };
108
+ /** A person can read why a call failed. */
109
+ const why = (err) => err instanceof Error ? err.message : String(err);
110
+ /** Runs one step; a failure becomes a `dropped` line, because the issue exists (or the rest of the patch is still good). */
111
+ async function attempt$1(what, dropped, step) {
112
+ try {
113
+ await step();
114
+ } catch (err) {
115
+ dropped.push(`${what} (${why(err)})`);
116
+ }
117
+ }
118
+ const sameDate = (a, b) => a !== null && a.slice(0, 10) === b;
119
+ /** Sets what Linear can hold of `fields` on `issue`, which was just made, and says what it cannot. */
120
+ async function applyNewIssueFields(scope, issue, fields) {
121
+ const dropped = [];
122
+ const { client } = scope;
123
+ if (fields.startDate) dropped.push(`startDate (Linear has no start date): ${fields.startDate}`);
124
+ if (fields.effort) await attempt$1(`effort ${fields.effort}`, dropped, async () => {
125
+ await client.issues.update(issue.id, { estimate: ESTIMATE[fields.effort] });
126
+ });
127
+ for (const key of fields.blockedBy ?? []) await attempt$1(`blocked by ${key}`, dropped, async () => {
128
+ const blocker = await client.issues.get(normalizeKey(key, scope.teamKey));
129
+ await client.issues.relate(blocker.id, issue.id, "blocks");
130
+ });
131
+ return dropped;
132
+ }
133
+ /** Changes a Linear issue to match the patch; whatever is already as asked is not written. */
134
+ async function updateLinearIssue(scope, key, patch, lookups) {
135
+ const { client } = scope;
136
+ const issue = await client.issues.get(normalizeKey(key, scope.teamKey));
137
+ const changed = [];
138
+ const dropped = [];
139
+ const clear = new Set(patch.clear ?? []);
140
+ const input = {};
141
+ if (patch.title !== void 0 && patch.title !== issue.title) {
142
+ input.title = patch.title;
143
+ changed.push("title");
144
+ }
145
+ if (patch.body !== void 0 && patch.body !== (issue.description ?? "")) {
146
+ input.description = patch.body;
147
+ changed.push("body");
148
+ }
149
+ if (patch.priority && patch.priority !== "none" && toLinearPriority(patch.priority) !== issue.priority) {
150
+ input.priority = toLinearPriority(patch.priority);
151
+ changed.push(`priority: ${patch.priority}`);
152
+ } else if (clear.has("priority") && issue.priority !== 0) {
153
+ input.priority = 0;
154
+ changed.push("priority cleared");
155
+ }
156
+ if (patch.targetDate && !sameDate(issue.dueDate, patch.targetDate)) {
157
+ input.dueDate = patch.targetDate;
158
+ changed.push(`targetDate: ${patch.targetDate}`);
159
+ } else if (clear.has("targetDate") && issue.dueDate) {
160
+ input.dueDate = null;
161
+ changed.push("targetDate cleared");
162
+ }
163
+ let estimate;
164
+ if (patch.effort && ESTIMATE[patch.effort] !== issue.estimate) estimate = {
165
+ value: ESTIMATE[patch.effort],
166
+ said: `effort: ${patch.effort}`
167
+ };
168
+ else if (clear.has("effort") && issue.estimate !== null) estimate = {
169
+ value: null,
170
+ said: "effort cleared"
171
+ };
172
+ if (patch.startDate) dropped.push(`startDate (Linear has no start date): ${patch.startDate}`);
173
+ if (clear.has("startDate")) dropped.push("startDate cleared (Linear has no start date)");
174
+ const [wanted, ...extra] = patch.assignees ?? [];
175
+ if (wanted) {
176
+ const found = await lookups.assignee(wanted);
177
+ if ("problem" in found) dropped.push(`assignee (${found.problem}): ${wanted}`);
178
+ else if (found.id !== issue.assignee?.id) {
179
+ input.assigneeId = found.id;
180
+ changed.push(`assignees: ${wanted}`);
181
+ }
182
+ if (extra.length > 0) dropped.push(`assignee (Linear holds one): ${extra.join(", ")}`);
183
+ } else if (clear.has("assignees") && issue.assignee) {
184
+ input.assigneeId = null;
185
+ changed.push("assignees cleared");
186
+ }
187
+ if (patch.milestone) {
188
+ const projectId = await lookups.project(patch.milestone);
189
+ if (projectId !== issue.project?.id) {
190
+ input.projectId = projectId;
191
+ changed.push(`milestone: ${patch.milestone}`);
192
+ }
193
+ } else if (clear.has("milestone") && issue.project) {
194
+ input.projectId = null;
195
+ changed.push("milestone cleared");
196
+ }
197
+ if (patch.parent) {
198
+ const parent = await client.issues.get(normalizeKey(patch.parent, scope.teamKey));
199
+ if (parent.id !== issue.parent?.id) {
200
+ input.parentId = parent.id;
201
+ changed.push(`parent: ${patch.parent}`);
202
+ }
203
+ } else if (clear.has("parent") && issue.parent) {
204
+ input.parentId = null;
205
+ changed.push("parent cleared");
206
+ }
207
+ const kindLabel = patch.kind ? KIND_LABELS[patch.kind] : void 0;
208
+ const have = new Map(issue.labels.nodes.map((l) => [l.name.toLowerCase(), l]));
209
+ const add = [...patch.addLabels ?? [], ...kindLabel ? [kindLabel] : []].filter((l) => !have.has(l.toLowerCase()));
210
+ const remove = [...patch.removeLabels ?? [], ...clear.has("kind") && !patch.kind ? Object.values(KIND_LABELS) : []].map((l) => have.get(l.toLowerCase())).filter((l) => l !== void 0);
211
+ if (add.length > 0 || remove.length > 0) {
212
+ const added = add.length > 0 ? await lookups.labelIds([...new Set(add)]) : [];
213
+ const removed = new Set(remove.map((l) => l.id));
214
+ input.labelIds = [.../* @__PURE__ */ new Set([...issue.labels.nodes.map((l) => l.id).filter((id) => !removed.has(id)), ...added])];
215
+ if (add.length > 0) changed.push(`labels added: ${[...new Set(add)].join(", ")}`);
216
+ for (const l of remove) changed.push(`label removed: ${l.name}`);
217
+ }
218
+ if (patch.state) {
219
+ const closed = patch.state === "closed";
220
+ const target = pickState(await client.teams.states((await scope.team()).id), { category: closed ? "done" : "open" });
221
+ if (!target) dropped.push(`state ${patch.state} (the team has no matching state)`);
222
+ else if (target.id !== issue.state.id) {
223
+ input.stateId = target.id;
224
+ changed.push(`state: ${patch.state}`);
225
+ }
226
+ }
227
+ if (Object.keys(input).length > 0) await client.issues.update(issue.id, input);
228
+ if (estimate) {
229
+ const { value, said } = estimate;
230
+ await attempt$1(said, dropped, async () => {
231
+ await client.issues.update(issue.id, { estimate: value });
232
+ changed.push(said);
233
+ });
234
+ }
235
+ const blocked = new Set(issue.inverseRelations.nodes.filter((r) => r.type === "blocks").map((r) => r.issue.identifier));
236
+ for (const blocker of patch.blockedBy ?? []) await attempt$1(`blocked by ${blocker}`, dropped, async () => {
237
+ const found = await client.issues.get(normalizeKey(blocker, scope.teamKey));
238
+ if (blocked.has(found.identifier)) return;
239
+ await client.issues.relate(found.id, issue.id, "blocks");
240
+ changed.push(`blocked by: ${blocker}`);
241
+ });
242
+ for (const blocker of patch.unblock ?? []) await attempt$1(`unblock ${blocker}`, dropped, async () => {
243
+ const identifier = normalizeKey(blocker, scope.teamKey);
244
+ const relation = issue.inverseRelations.nodes.find((r) => r.type === "blocks" && r.issue.identifier === identifier);
245
+ if (!relation) return;
246
+ await client.issues.unrelate(relation.id);
247
+ changed.push(`unblocked: ${blocker}`);
248
+ });
249
+ return {
250
+ key: issue.identifier,
251
+ changed,
252
+ ...dropped.length > 0 ? { dropped } : {}
253
+ };
254
+ }
255
+ // src/capabilities/issue-kind.ts
256
+ const LABEL_KINDS = new Map(Object.entries(KIND_LABELS).map(([kind, label]) => [label, kind]));
257
+ /** The one kind the labels name; two different kinds name none. */
258
+ function kindOfLabels(names) {
259
+ const named = new Set(names.flatMap((n) => LABEL_KINDS.get(n.toLowerCase()) ?? []));
260
+ return named.size === 1 ? [...named][0] : void 0;
261
+ }
262
+ /** The labels a record becomes: its own, plus its kind's label when they do not already name it. */
263
+ function labelsFor(record) {
264
+ if (!record.kind) return record.labels;
265
+ const label = KIND_LABELS[record.kind];
266
+ return record.labels.some((n) => n.toLowerCase() === label) ? record.labels : [...record.labels, label];
267
+ }
95
268
  // src/capabilities/issues-migration.ts
96
269
  /**
97
270
  * Export and import of Linear issues for `holocron migrate`.
@@ -168,6 +341,8 @@ async function exportIssues(scope, selector, info = {}) {
168
341
  const closed = isClosed(issue.state.type);
169
342
  const comments = await client.issues.comments(issue.id);
170
343
  const priority = fromLinearPriority(issue.priority);
344
+ const labelNames = issue.labels.nodes.map((l) => l.name);
345
+ const kind = kindOfLabels(labelNames);
171
346
  const migratedFrom = issue.attachments?.nodes.find((a) => a.title.startsWith(MIGRATED_TITLE_PREFIX))?.url;
172
347
  records.push({
173
348
  key: issue.identifier,
@@ -178,7 +353,8 @@ async function exportIssues(scope, selector, info = {}) {
178
353
  statusCategory: statusCategory(issue.state),
179
354
  closed,
180
355
  ...priority ? { priority } : {},
181
- labels: issue.labels.nodes.map((l) => l.name),
356
+ labels: labelNames,
357
+ ...kind ? { kind } : {},
182
358
  ...issue.assignee ? { assignee: {
183
359
  id: issue.assignee.id,
184
360
  displayName: issue.assignee.displayName || issue.assignee.name,
@@ -258,7 +434,7 @@ async function importIssues(scope, records, options = {}, results = []) {
258
434
  closed: record.closed
259
435
  });
260
436
  const ids = [];
261
- for (const name of record.labels) {
437
+ for (const name of labelsFor(record)) {
262
438
  let id = labelIds.get(name.toLowerCase());
263
439
  if (!id) {
264
440
  id = (await client.teams.createLabel({
@@ -391,11 +567,12 @@ async function syncIssues(scope, records, options = {}) {
391
567
  patch.stateId = wanted.id;
392
568
  changes.push("state");
393
569
  }
394
- if (sorted(record.labels) !== sorted(issue.labels.nodes.map((l) => l.name))) {
570
+ const wantedLabels = labelsFor(record);
571
+ if (sorted(wantedLabels) !== sorted(issue.labels.nodes.map((l) => l.name))) {
395
572
  changes.push("labels");
396
573
  if (!options.dryRun) {
397
574
  const ids = [];
398
- for (const name of record.labels) {
575
+ for (const name of wantedLabels) {
399
576
  let id = labelIds.get(name.toLowerCase());
400
577
  if (!id) {
401
578
  id = (await client.teams.createLabel({
@@ -437,166 +614,6 @@ async function syncIssues(scope, records, options = {}) {
437
614
  }
438
615
  return records.filter((r) => results.has(r.key)).map((r) => results.get(r.key));
439
616
  }
440
- // src/capabilities/issues-update.ts
441
- /** Linear's estimate for each effort, on the usual 1-3-5 scale. */
442
- const ESTIMATE = {
443
- low: 1,
444
- medium: 3,
445
- high: 5
446
- };
447
- /** The canonical label that stands for each kind where there is no Type. */
448
- const KIND_LABELS = {
449
- bug: "bug",
450
- feature: "enhancement",
451
- task: "chore"
452
- };
453
- /** A person can read why a call failed. */
454
- const why = (err) => err instanceof Error ? err.message : String(err);
455
- /** Runs one step; a failure becomes a `dropped` line, because the issue exists (or the rest of the patch is still good). */
456
- async function attempt$1(what, dropped, step) {
457
- try {
458
- await step();
459
- } catch (err) {
460
- dropped.push(`${what} (${why(err)})`);
461
- }
462
- }
463
- const sameDate = (a, b) => a !== null && a.slice(0, 10) === b;
464
- /** Sets what Linear can hold of `fields` on `issue`, which was just made, and says what it cannot. */
465
- async function applyNewIssueFields(scope, issue, fields) {
466
- const dropped = [];
467
- const { client } = scope;
468
- if (fields.startDate) dropped.push(`startDate (Linear has no start date): ${fields.startDate}`);
469
- if (fields.effort) await attempt$1(`effort ${fields.effort}`, dropped, async () => {
470
- await client.issues.update(issue.id, { estimate: ESTIMATE[fields.effort] });
471
- });
472
- for (const key of fields.blockedBy ?? []) await attempt$1(`blocked by ${key}`, dropped, async () => {
473
- const blocker = await client.issues.get(normalizeKey(key, scope.teamKey));
474
- await client.issues.relate(blocker.id, issue.id, "blocks");
475
- });
476
- return dropped;
477
- }
478
- /** Changes a Linear issue to match the patch; whatever is already as asked is not written. */
479
- async function updateLinearIssue(scope, key, patch, lookups) {
480
- const { client } = scope;
481
- const issue = await client.issues.get(normalizeKey(key, scope.teamKey));
482
- const changed = [];
483
- const dropped = [];
484
- const clear = new Set(patch.clear ?? []);
485
- const input = {};
486
- if (patch.title !== void 0 && patch.title !== issue.title) {
487
- input.title = patch.title;
488
- changed.push("title");
489
- }
490
- if (patch.body !== void 0 && patch.body !== (issue.description ?? "")) {
491
- input.description = patch.body;
492
- changed.push("body");
493
- }
494
- if (patch.priority && patch.priority !== "none" && toLinearPriority(patch.priority) !== issue.priority) {
495
- input.priority = toLinearPriority(patch.priority);
496
- changed.push(`priority: ${patch.priority}`);
497
- } else if (clear.has("priority") && issue.priority !== 0) {
498
- input.priority = 0;
499
- changed.push("priority cleared");
500
- }
501
- if (patch.targetDate && !sameDate(issue.dueDate, patch.targetDate)) {
502
- input.dueDate = patch.targetDate;
503
- changed.push(`targetDate: ${patch.targetDate}`);
504
- } else if (clear.has("targetDate") && issue.dueDate) {
505
- input.dueDate = null;
506
- changed.push("targetDate cleared");
507
- }
508
- let estimate;
509
- if (patch.effort && ESTIMATE[patch.effort] !== issue.estimate) estimate = {
510
- value: ESTIMATE[patch.effort],
511
- said: `effort: ${patch.effort}`
512
- };
513
- else if (clear.has("effort") && issue.estimate !== null) estimate = {
514
- value: null,
515
- said: "effort cleared"
516
- };
517
- if (patch.startDate) dropped.push(`startDate (Linear has no start date): ${patch.startDate}`);
518
- if (clear.has("startDate")) dropped.push("startDate cleared (Linear has no start date)");
519
- const [wanted, ...extra] = patch.assignees ?? [];
520
- if (wanted) {
521
- const found = await lookups.assignee(wanted);
522
- if ("problem" in found) dropped.push(`assignee (${found.problem}): ${wanted}`);
523
- else if (found.id !== issue.assignee?.id) {
524
- input.assigneeId = found.id;
525
- changed.push(`assignees: ${wanted}`);
526
- }
527
- if (extra.length > 0) dropped.push(`assignee (Linear holds one): ${extra.join(", ")}`);
528
- } else if (clear.has("assignees") && issue.assignee) {
529
- input.assigneeId = null;
530
- changed.push("assignees cleared");
531
- }
532
- if (patch.milestone) {
533
- const projectId = await lookups.project(patch.milestone);
534
- if (projectId !== issue.project?.id) {
535
- input.projectId = projectId;
536
- changed.push(`milestone: ${patch.milestone}`);
537
- }
538
- } else if (clear.has("milestone") && issue.project) {
539
- input.projectId = null;
540
- changed.push("milestone cleared");
541
- }
542
- if (patch.parent) {
543
- const parent = await client.issues.get(normalizeKey(patch.parent, scope.teamKey));
544
- if (parent.id !== issue.parent?.id) {
545
- input.parentId = parent.id;
546
- changed.push(`parent: ${patch.parent}`);
547
- }
548
- } else if (clear.has("parent") && issue.parent) {
549
- input.parentId = null;
550
- changed.push("parent cleared");
551
- }
552
- const kindLabel = patch.kind ? KIND_LABELS[patch.kind] : void 0;
553
- const have = new Map(issue.labels.nodes.map((l) => [l.name.toLowerCase(), l]));
554
- const add = [...patch.addLabels ?? [], ...kindLabel ? [kindLabel] : []].filter((l) => !have.has(l.toLowerCase()));
555
- const remove = [...patch.removeLabels ?? [], ...clear.has("kind") && !patch.kind ? Object.values(KIND_LABELS) : []].map((l) => have.get(l.toLowerCase())).filter((l) => l !== void 0);
556
- if (add.length > 0 || remove.length > 0) {
557
- const added = add.length > 0 ? await lookups.labelIds([...new Set(add)]) : [];
558
- const removed = new Set(remove.map((l) => l.id));
559
- input.labelIds = [.../* @__PURE__ */ new Set([...issue.labels.nodes.map((l) => l.id).filter((id) => !removed.has(id)), ...added])];
560
- if (add.length > 0) changed.push(`labels added: ${[...new Set(add)].join(", ")}`);
561
- for (const l of remove) changed.push(`label removed: ${l.name}`);
562
- }
563
- if (patch.state) {
564
- const closed = patch.state === "closed";
565
- const target = pickState(await client.teams.states((await scope.team()).id), { category: closed ? "done" : "open" });
566
- if (!target) dropped.push(`state ${patch.state} (the team has no matching state)`);
567
- else if (target.id !== issue.state.id) {
568
- input.stateId = target.id;
569
- changed.push(`state: ${patch.state}`);
570
- }
571
- }
572
- if (Object.keys(input).length > 0) await client.issues.update(issue.id, input);
573
- if (estimate) {
574
- const { value, said } = estimate;
575
- await attempt$1(said, dropped, async () => {
576
- await client.issues.update(issue.id, { estimate: value });
577
- changed.push(said);
578
- });
579
- }
580
- const blocked = new Set(issue.inverseRelations.nodes.filter((r) => r.type === "blocks").map((r) => r.issue.identifier));
581
- for (const blocker of patch.blockedBy ?? []) await attempt$1(`blocked by ${blocker}`, dropped, async () => {
582
- const found = await client.issues.get(normalizeKey(blocker, scope.teamKey));
583
- if (blocked.has(found.identifier)) return;
584
- await client.issues.relate(found.id, issue.id, "blocks");
585
- changed.push(`blocked by: ${blocker}`);
586
- });
587
- for (const blocker of patch.unblock ?? []) await attempt$1(`unblock ${blocker}`, dropped, async () => {
588
- const identifier = normalizeKey(blocker, scope.teamKey);
589
- const relation = issue.inverseRelations.nodes.find((r) => r.type === "blocks" && r.issue.identifier === identifier);
590
- if (!relation) return;
591
- await client.issues.unrelate(relation.id);
592
- changed.push(`unblocked: ${blocker}`);
593
- });
594
- return {
595
- key: issue.identifier,
596
- changed,
597
- ...dropped.length > 0 ? { dropped } : {}
598
- };
599
- }
600
617
  // src/capabilities/issues.ts
601
618
  function toUser(u) {
602
619
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theholocron/holocron-plugin-linear",
3
- "version": "5.0.0-alpha.503",
3
+ "version": "5.0.0-alpha.505",
4
4
  "description": "Holocron plugin for Linear. Implements the issues and projects capabilities against the Linear GraphQL API, with export and import for migrating to and from other trackers.",
5
5
  "keywords": [
6
6
  "holocron",
@@ -40,18 +40,18 @@
40
40
  "tsdown": "^0.23.7",
41
41
  "typescript": "^5.9.3",
42
42
  "vitest": "^5.1.0",
43
- "@theholocron/cli": "5.0.0-alpha.503",
44
- "@theholocron/tsdown-config": "^8.9.0-alpha.21",
45
- "@theholocron/misc-utils": "^1.17.0-alpha.5",
43
+ "@theholocron/cli": "5.0.0-alpha.505",
46
44
  "@theholocron/eslint-config": "^8.9.0-alpha.21",
45
+ "@theholocron/misc-utils": "^1.17.0-alpha.5",
47
46
  "@theholocron/tsconfig": "^8.9.0-alpha.21",
48
47
  "@theholocron/linear-client": "^1.58.0-alpha.46",
48
+ "@theholocron/tsdown-config": "^8.9.0-alpha.21",
49
49
  "@theholocron/vitest-config": "^8.9.0-alpha.21"
50
50
  },
51
51
  "peerDependencies": {
52
52
  "@theholocron/linear-client": "^1.58.0",
53
53
  "@theholocron/misc-utils": "^1.16.0",
54
- "@theholocron/cli": "5.0.0-alpha.503"
54
+ "@theholocron/cli": "5.0.0-alpha.505"
55
55
  },
56
56
  "peerDependenciesMeta": {
57
57
  "@theholocron/linear-client": {}