-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
1036 lines (957 loc) · 29.4 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { Command, EnumType } from "@cliffy/command";
import { load } from "@std/dotenv";
import { getOption } from "./config.ts";
import { prompt, Select } from "@cliffy/prompt";
// Try loading .env from current directory first, then from git root if not found
if (await Deno.stat(".env").catch(() => null)) {
await load({ export: true });
} else {
try {
const gitRoot = new TextDecoder().decode(
await new Deno.Command("git", {
args: ["rev-parse", "--show-toplevel"],
}).output().then((output) => output.stdout),
).trim();
const gitRootEnvPath = join(gitRoot, ".env");
if (await Deno.stat(gitRootEnvPath).catch(() => null)) {
await load({ envPath: gitRootEnvPath, export: true });
}
} catch {
// Silently continue if not in a git repo
}
}
const SortType = new EnumType(["manual", "priority"]);
const StateType = new EnumType([
"triage",
"backlog",
"unstarted",
"started",
"completed",
"canceled",
]);
import { Spinner } from "@std/cli/unstable-spinner";
import { open } from "@opensrc/deno-open";
import { CompletionsCommand } from "@cliffy/command/completions";
import denoConfig from "./deno.json" with { type: "json" };
import { encodeBase64 } from "@std/encoding/base64";
import { renderMarkdown } from "@littletof/charmd";
import { basename, join } from "@std/path";
import { unicodeWidth } from "@std/cli";
interface Label {
name: string;
color: string;
}
interface Issue {
id: string;
identifier: string;
title: string;
priority: number;
estimate: number | null;
labels: { nodes: Label[] };
state: {
id: string;
name: string;
color: string;
};
updatedAt: string;
}
function padDisplay(s: string, width: number): string {
const w = unicodeWidth(s);
return s + " ".repeat(Math.max(0, width - w));
}
function stripConsoleFormat(s: string): string {
return s.replace(/%c/g, "");
}
function padDisplayFormatted(s: string, width: number): string {
const plain = stripConsoleFormat(s);
const w = unicodeWidth(plain);
return s + " ".repeat(Math.max(0, width - w));
}
function getTimeAgo(date: Date): string {
const now = new Date();
const diffMs = now.getTime() - date.getTime();
const diffMins = Math.floor(diffMs / 60000);
const diffHours = Math.floor(diffMins / 60);
const diffDays = Math.floor(diffHours / 24);
if (diffMins < 1) return "just now";
if (diffMins < 60) return `${diffMins} minutes ago`;
if (diffHours < 24) {
return `about ${diffHours} hour${diffHours === 1 ? "" : "s"} ago`;
}
return `${diffDays} day${diffDays === 1 ? "" : "s"} ago`;
}
async function getCurrentBranch(): Promise<string | null> {
const process = new Deno.Command("git", {
args: ["symbolic-ref", "--short", "HEAD"],
});
const { stdout } = await process.output();
const branch = new TextDecoder().decode(stdout).trim();
return branch || null;
}
async function getRepoDir(): Promise<string> {
const process = new Deno.Command("git", {
args: ["rev-parse", "--show-toplevel"],
});
const { stdout } = await process.output();
const fullPath = new TextDecoder().decode(stdout).trim();
return basename(fullPath);
}
async function branchExists(branch: string): Promise<boolean> {
try {
const process = new Deno.Command("git", {
args: ["rev-parse", "--verify", branch],
});
const { success } = await process.output();
return success;
} catch {
return false;
}
}
async function getStartedState(
teamId: string,
): Promise<{ id: string; name: string }> {
const query = /* GraphQL */ `
query($teamId: String!) {
team(id: $teamId) {
states {
nodes {
id
name
type
position
}
}
}
}
`;
const result = await fetchGraphQL(query, { teamId });
const states = result.data.team.states.nodes;
const startedStates = states
.filter((s: { type: string }) => s.type === "started")
.sort((a: { position: number }, b: { position: number }) =>
a.position - b.position
);
if (!startedStates.length) {
throw new Error("No 'started' state found in workflow");
}
const startedState = startedStates[0];
return { id: startedState.id, name: startedState.name };
}
async function updateIssueState(
issueId: string,
stateId: string,
): Promise<void> {
const mutation = `
mutation($issueId: String!, $stateId: String!) {
issueUpdate(
id: $issueId,
input: { stateId: $stateId }
) {
success
}
}
`;
await fetchGraphQL(mutation, { issueId, stateId });
}
function isValidLinearId(id: string): boolean {
return /^[A-Za-z]{2,5}-[1-9][0-9]*$/.test(id);
}
async function getIssueId(providedId?: string): Promise<string | null> {
if (providedId) {
if (!isValidLinearId(providedId)) {
console.error(`Invalid Linear issue ID format: ${providedId}`);
console.error("Issue IDs should look like: ABC-123");
Deno.exit(1);
}
return providedId.toUpperCase();
}
const branch = await getCurrentBranch();
if (!branch) return null;
const match = branch.match(/[a-zA-Z]{2,5}-[1-9][0-9]*/i);
return match ? match[0].toUpperCase() : null;
}
async function getTeamId(): Promise<string | null> {
const teamId = getOption("team_id");
if (teamId) {
return teamId.toUpperCase();
}
const dir = await getRepoDir();
const match = dir.match(/^[a-zA-Z]{2,5}/);
return match ? match[0].toUpperCase() : null;
}
async function fetchGraphQL(query: string, variables: Record<string, unknown>) {
const apiKey = getOption("api_key");
if (!apiKey) {
throw new Error(
"api_key is not set via command line, configuration file, or environment.",
);
}
const response = await fetch("https://api.linear.app/graphql", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": apiKey,
},
body: JSON.stringify({ query, variables }),
});
const data = await response.json();
if (data.errors) {
throw new Error(JSON.stringify(data.errors, null, 2));
}
return data;
}
async function fetchIssuesForState(teamId: string, state: string) {
const sort = getOption("issue_sort") as "manual" | "priority" | undefined;
if (!sort) {
console.error(
"Sort must be provided via configuration file or LINEAR_ISSUE_SORT environment variable",
);
Deno.exit(1);
}
const query = /* GraphQL */ `
query issues($teamId: String!, $sort: [IssueSortInput!], $states: [String!]) {
issues(
filter: {
team: { key: { eq: $teamId } }
assignee: { isMe: { eq: true } }
state: { type: { in: $states } }
}
sort: $sort
) {
nodes {
id
identifier
title
priority
estimate
state {
id
name
color
}
labels {
nodes {
id
name
color
}
}
updatedAt
}
}
}
`;
const sortPayload = sort === "manual"
? [{ manual: { nulls: "last", order: "Ascending" } }]
: [{ priority: { nulls: "last", order: "Descending" } }];
return await fetchGraphQL(query, {
teamId,
sort: sortPayload,
states: [state],
});
}
function getPriorityDisplay(priority: number): string {
if (priority === 0) {
return "---";
} else if (priority === 1 || priority === 2) {
return "▄▆█";
} else if (priority === 3) {
return "▄▆ ";
} else if (priority === 4) {
return "▄ ";
}
return priority.toString();
}
async function fetchIssueDetails(
issueId: string,
showSpinner = false,
): Promise<
{ title: string; description: string | null; url: string; branchName: string }
> {
const spinner = showSpinner ? new Spinner() : null;
spinner?.start();
try {
const query =
`query($id: String!) { issue(id: $id) { title, description, url, branchName } }`;
const data = await fetchGraphQL(query, { id: issueId });
spinner?.stop();
return data.data.issue;
} catch (error) {
spinner?.stop();
console.error("✗ Failed to fetch issue details");
throw error;
}
}
async function openTeamPage(options: { app?: boolean } = {}) {
const teamId = await getTeamId();
if (!teamId) {
console.error(
"Could not determine team id from configuration or directory name.",
);
Deno.exit(1);
}
const workspace = getOption("workspace");
if (!workspace) {
console.error(
"workspace is not set via command line, configuration file, or environment.",
);
Deno.exit(1);
}
const filterObj = {
"and": [{ "assignee": { "or": [{ "isMe": { "eq": true } }] } }],
};
const filter = encodeBase64(JSON.stringify(filterObj)).replace(/=/g, "");
const url =
`https://linear.app/${workspace}/team/${teamId}/active?filter=${filter}`;
await open(url, options.app ? { app: { name: "Linear" } } : undefined);
}
async function openIssuePage(
providedId?: string,
options: { app?: boolean; web?: boolean } = {},
) {
const issueId = await getIssueId(providedId);
if (!issueId) {
console.error(
"The current branch does not contain a valid linear issue id.",
);
Deno.exit(1);
}
const workspace = getOption("workspace");
if (!workspace) {
console.error(
"workspace is not set via command line, configuration file, or environment.",
);
Deno.exit(1);
}
const url = `https://linear.app/${workspace}/issue/${issueId}`;
const destination = options.app ? "Linear.app" : "web browser";
console.log(`Opening ${url} in ${destination}`);
await open(url, options.app ? { app: { name: "Linear" } } : undefined);
}
const teamCommand = new Command()
.description(
"Manage Linear teams (deprecated: use `linear issue list --app` instead)",
)
.action(() => {
console.error(
"Warning: 'linear team' is deprecated and will be removed in a future release.",
);
console.error("Please use 'linear issue list --app' instead.");
return openTeamPage({ app: true });
})
.command("open", "Open the team page in Linear.app")
.alias("o")
.action(() => openTeamPage({ app: true }))
.command("id", "Print the team id derived from the repository name")
.action(async () => {
const teamId = await getTeamId();
if (teamId) {
console.log(teamId);
} else {
console.error("Could not determine team id from directory name.");
Deno.exit(1);
}
})
.command(
"autolinks",
"Configure GitHub repository autolinks for Linear issues",
)
.action(async () => {
const teamId = await getTeamId();
if (!teamId) {
console.error("Could not determine team id from directory name.");
Deno.exit(1);
}
const workspace = getOption("workspace");
if (!workspace) {
console.error(
"workspace is not set via command line, configuration file, or environment.",
);
Deno.exit(1);
}
const process = new Deno.Command("gh", {
args: [
"api",
"repos/{owner}/{repo}/autolinks",
"-f",
`key_prefix=${teamId}-`,
"-f",
`url_template=https://linear.app/${workspace}/issue/${teamId}-<num>`,
],
stdin: "inherit",
stdout: "inherit",
stderr: "inherit",
});
const status = await process.spawn().status;
if (!status.success) {
console.error("Failed to configure autolinks");
Deno.exit(1);
}
});
const issueCommand = new Command()
.description("Manage Linear issues")
.arguments("[issueId:string]")
.action((_, issueId) => openIssuePage(issueId))
.command(
"open",
"Open the issue in Linear.app (deprecated: use `linear issue view --app` instead)",
)
.alias("o")
.arguments("[issueId:string]")
.action((_, issueId) => {
console.error(
"Warning: 'linear issue open' is deprecated and will be removed in a future release.",
);
console.error("Please use 'linear issue view --app' instead.");
return openIssuePage(issueId, { app: true });
})
.command(
"print",
"Print the issue details (deprecated: use `linear issue view` instead)",
)
.alias("p")
.arguments("[issueId:string]")
.option("--no-color", "Disable colored output")
.action(async ({ color }, issueId) => {
console.error(
"Warning: 'linear issue print' is deprecated and will be removed in a future release.",
);
console.error("Please use 'linear issue view' instead.");
const resolvedId = await getIssueId(issueId);
if (!resolvedId) {
console.error(
"The current branch does not contain a valid linear issue id.",
);
Deno.exit(1);
}
const showSpinner = color && Deno.stdout.isTerminal();
const { title, description } = await fetchIssueDetails(
resolvedId,
showSpinner,
);
const markdown = `# ${title}${description ? "\n\n" + description : ""}`;
if (color && Deno.stdout.isTerminal()) {
console.log(renderMarkdown(markdown));
} else {
console.log(markdown);
}
})
.command("id", "Print the issue based on the current git branch")
.action(async (_) => {
const resolvedId = await getIssueId();
if (resolvedId) {
console.log(resolvedId);
} else {
console.error(
"The current branch does not contain a valid linear issue id.",
);
Deno.exit(1);
}
})
.command("list", "List your issues")
.type("sort", SortType)
.type("state", StateType)
.option(
"--sort <sort:sort>",
"Sort order (can also be set via LINEAR_ISSUE_SORT)",
{
required: false,
},
)
.option(
"-s, --state <state:state>",
"Filter by issue state",
{
default: "unstarted",
},
)
.option("-w, --web", "Open in web browser")
.option("-a, --app", "Open in Linear.app")
.action(async ({ sort: sortFlag, state, web, app }) => {
if (web || app) {
await openTeamPage({ app });
return;
}
const sort = sortFlag ||
getOption("issue_sort") as "manual" | "priority" | undefined;
if (!sort) {
console.error(
"Sort must be provided via command line flag, configuration file, or LINEAR_ISSUE_SORT environment variable",
);
Deno.exit(1);
}
if (!SortType.values().includes(sort)) {
console.error(`Sort must be one of: ${SortType.values().join(", ")}`);
Deno.exit(1);
}
const teamId = await getTeamId();
if (!teamId) {
console.error("Could not determine team id from directory name.");
Deno.exit(1);
}
try {
const result = await fetchIssuesForState(teamId, state);
const issues = result.data.issues.nodes;
if (issues.length === 0) {
console.log("No unstarted issues found.");
return;
}
// Define column widths first
const { columns } = Deno.consoleSize();
const PRIORITY_WIDTH = 4;
const ID_WIDTH = 8;
const LABEL_WIDTH = columns <= 100 ? 12 : 24; // adjust label width based on terminal size
const ESTIMATE_WIDTH = 2; // fixed width for estimate
const STATE_WIDTH = 12; // fixed width for state
const SPACE_WIDTH = 4;
const updatedHeader = "UPDATED";
const UPDATED_WIDTH = Math.max(
unicodeWidth(updatedHeader),
...issues.map((issue: Issue) =>
unicodeWidth(getTimeAgo(new Date(issue.updatedAt)))
),
);
type TableRow = {
priorityStr: string;
priorityStyles: string[];
identifier: string;
title: string;
labelsFormat: string;
labelsStyles: string[];
state: string;
stateStyles: string[];
timeAgo: string;
estimate: number | null;
};
const tableData: Array<TableRow> = issues.map((issue: Issue) => {
// First build the plain text version to measure length
const plainLabels = issue.labels.nodes.map((l: Label) => l.name).join(
", ",
);
let labelsFormat: string;
let labelsStyles: string[] = [];
if (issue.labels.nodes.length === 0) {
labelsFormat = "";
} else {
const truncatedLabels = plainLabels.length > LABEL_WIDTH
? plainLabels.slice(0, LABEL_WIDTH - 3) + "..."
: plainLabels;
// Then format the truncated version with colors
labelsFormat = truncatedLabels
.split(", ")
.map((name) => `%c${name}%c`)
.join(", ");
labelsStyles = issue.labels.nodes
.filter((_, i) => i < truncatedLabels.split(", ").length)
.flatMap((l: Label) => [`color: ${l.color}`, ""]);
}
const updatedAt = new Date(issue.updatedAt);
const timeAgo = getTimeAgo(updatedAt);
let priorityStr = "";
let priorityStyles: string[] = [];
if (issue.priority === 0) {
priorityStr = "%c---%c";
priorityStyles = ["color: silver", ""];
} else if (issue.priority === 1 || issue.priority === 2) {
// ▄▆█
priorityStr = "%c▄%c▆%c█%c";
priorityStyles = ["", "", "", ""];
} else if (issue.priority === 3) {
priorityStr = "%c▄%c▆%c█%c";
priorityStyles = ["", "", "color: silver", ""];
} else if (issue.priority === 4) {
priorityStr = "%c▄%c▆%c█%c";
priorityStyles = ["", "color: silver", "color: silver", ""];
} else {
priorityStr = issue.priority.toString();
priorityStyles = [];
}
return {
priorityStr,
priorityStyles,
identifier: issue.identifier,
title: issue.title,
labelsFormat,
labelsStyles,
state: `%c${issue.state.name}%c`,
stateStyles: [`color: ${issue.state.color}`, ""],
timeAgo,
estimate: issue.estimate,
};
});
const fixed = PRIORITY_WIDTH + ID_WIDTH + UPDATED_WIDTH + SPACE_WIDTH +
LABEL_WIDTH + ESTIMATE_WIDTH + STATE_WIDTH + SPACE_WIDTH; // sum of fixed columns including spacing for estimate
const PADDING = 1;
const maxTitleWidth = Math.max(
...tableData.map((row) => unicodeWidth(row.title)),
);
const availableWidth = Math.max(columns - PADDING - fixed, 0);
const titleWidth = Math.min(maxTitleWidth, availableWidth); // use smaller of max title width or available space
const headerCells = [
padDisplay("◌", PRIORITY_WIDTH),
padDisplay("ID", ID_WIDTH),
padDisplay("TITLE", titleWidth),
padDisplay("LABELS", LABEL_WIDTH),
padDisplay("E", ESTIMATE_WIDTH),
padDisplay("STATE", STATE_WIDTH),
padDisplay(updatedHeader, UPDATED_WIDTH),
];
let headerMsg = "";
const headerStyles: Array<string> = [];
headerCells.forEach((cell, index) => {
headerMsg += `%c${cell}`;
headerStyles.push("text-decoration: underline");
if (index < headerCells.length - 1) {
headerMsg += "%c %c"; // non-underlined space between cells
headerStyles.push("text-decoration: none");
headerStyles.push("text-decoration: underline");
}
});
console.log(headerMsg, ...headerStyles);
// Print each issue
for (const row of tableData) {
const {
priorityStr,
priorityStyles,
identifier,
title,
labelsFormat,
labelsStyles,
state,
stateStyles,
timeAgo,
} = row;
const truncTitle = title.length > titleWidth
? title.slice(0, titleWidth - 3) + "..."
: padDisplay(title, titleWidth);
console.log(
`${padDisplayFormatted(priorityStr, 4)} ${
padDisplay(identifier, 8)
} ${truncTitle} ${padDisplayFormatted(labelsFormat, LABEL_WIDTH)} ${
padDisplay(row.estimate?.toString() || "-", ESTIMATE_WIDTH)
} ${padDisplayFormatted(state, STATE_WIDTH)} %c${
padDisplay(timeAgo, UPDATED_WIDTH)
}%c`,
...priorityStyles,
...labelsStyles,
...stateStyles,
"color: gray",
"",
);
}
} catch (error) {
console.error("Failed to fetch issues:", error);
Deno.exit(1);
}
})
.command("title", "Print the issue title")
.arguments("[issueId:string]")
.action(async (_, issueId) => {
const resolvedId = await getIssueId(issueId);
if (!resolvedId) {
console.error(
"The current branch does not contain a valid linear issue id.",
);
Deno.exit(1);
}
const { title } = await fetchIssueDetails(resolvedId, false);
console.log(title);
})
.command("start", "Start working on an issue")
.arguments("[issueId:string]")
.action(async (_, issueId) => {
let resolvedId = await getIssueId(issueId);
if (!resolvedId) {
const teamId = await getTeamId();
if (!teamId) {
console.error("Could not determine team ID");
Deno.exit(1);
}
try {
const result = await fetchIssuesForState(teamId, "unstarted");
const issues = result.data.issues.nodes;
if (issues.length === 0) {
console.error("No unstarted issues found.");
Deno.exit(1);
}
const answer = await Select.prompt({
message: "Select an issue to start:",
options: issues.map((
issue: { identifier: string; title: string; priority: number },
) => ({
name: getPriorityDisplay(issue.priority) +
` ${issue.identifier}: ${issue.title}`,
value: issue.identifier,
})),
});
resolvedId = answer as string;
} catch (error) {
console.error("Failed to fetch issues:", error);
Deno.exit(1);
}
}
const teamId = await getTeamId();
if (!teamId) {
console.error("Could not determine team ID");
Deno.exit(1);
}
if (!resolvedId) {
console.error("No issue ID resolved");
Deno.exit(1);
}
const { branchName } = await fetchIssueDetails(resolvedId, true);
// Check if branch exists
if (await branchExists(branchName)) {
const answer = await Select.prompt({
message:
`Branch ${branchName} already exists. What would you like to do?`,
options: [
{ name: "Switch to existing branch", value: "switch" },
{ name: "Create new branch with suffix", value: "create" },
],
});
if (answer === "switch") {
const process = new Deno.Command("git", {
args: ["checkout", branchName],
});
await process.output();
console.log(`✓ Switched to '${branchName}'`);
} else {
// Find next available suffix
let suffix = 1;
let newBranch = `${branchName}-${suffix}`;
while (await branchExists(newBranch)) {
suffix++;
newBranch = `${branchName}-${suffix}`;
}
const process = new Deno.Command("git", {
args: ["checkout", "-b", newBranch],
});
await process.output();
console.log(`✓ Created and switched to branch '${newBranch}'`);
}
} else {
// Create and checkout the branch
const process = new Deno.Command("git", {
args: ["checkout", "-b", branchName],
});
await process.output();
console.log(`✓ Created and switched to branch '${branchName}'`);
}
// Update issue state
try {
const state = await getStartedState(teamId);
if (!resolvedId) {
console.error("No issue ID resolved");
Deno.exit(1);
}
await updateIssueState(resolvedId, state.id);
console.log(`✓ Issue state updated to '${state.name}'`);
} catch (error) {
console.error("Failed to update issue state:", error);
}
})
.command("view", "View issue details (default) or open in browser/app")
.alias("v")
.arguments("[issueId:string]")
.option("-w, --web", "Open in web browser")
.option("-a, --app", "Open in Linear.app")
.action(async ({ web, app }, issueId) => {
if (web || app) {
await openIssuePage(issueId, { app, web: !app });
return;
}
const resolvedId = await getIssueId(issueId);
if (!resolvedId) {
console.error(
"The current branch does not contain a valid linear issue id.",
);
Deno.exit(1);
}
const { title, description } = await fetchIssueDetails(
resolvedId,
Deno.stdout.isTerminal(),
);
const markdown = `# ${title}${description ? "\n\n" + description : ""}`;
if (Deno.stdout.isTerminal()) {
console.log(renderMarkdown(markdown));
} else {
console.log(markdown);
}
})
.command("url", "Print the issue URL")
.arguments("[issueId:string]")
.action(async (_, issueId) => {
const resolvedId = await getIssueId(issueId);
if (!resolvedId) {
console.error(
"The current branch does not contain a valid linear issue id.",
);
Deno.exit(1);
}
const { url } = await fetchIssueDetails(resolvedId, false);
console.log(url);
})
.command("pull-request", "Create a GitHub pull request with issue details")
.alias("pr")
.option(
"--base <branch:string>",
"The branch into which you want your code merged",
)
.option(
"--draft",
"Create the pull request as a draft",
)
.option(
"-t, --title <title:string>",
"Optional title for the pull request (Linear issue ID will be prefixed)",
)
.arguments("[issueId:string]")
.action(async ({ base, draft, title: customTitle }, issueId) => {
const resolvedId = await getIssueId(issueId);
if (!resolvedId) {
console.error(
"The current branch does not contain a valid linear issue id.",
);
Deno.exit(1);
}
const { title, url } = await fetchIssueDetails(
resolvedId,
Deno.stdout.isTerminal(),
);
const process = new Deno.Command("gh", {
args: [
"pr",
"create",
"--title",
`${resolvedId} ${customTitle ?? title}`,
"--body",
url,
...(base ? ["--base", base] : []),
...(draft ? ["--draft"] : ["--web"]),
],
stdin: "inherit",
stdout: "inherit",
stderr: "inherit",
});
const status = await process.spawn().status;
if (!status.success) {
console.error("Failed to create pull request");
Deno.exit(1);
}
});
await new Command()
.name("linear")
.version(denoConfig.version)
.description("Handy linear commands from the command line")
.action(() => {
console.log("Use --help to see available commands");
})
.command("issue", issueCommand)
.alias("i")
.command("team", teamCommand)
.alias("t")
.command("completions", new CompletionsCommand())
.command("config", "Interactively generate .linear.toml configuration")
.action(async () => {
const apiKey = Deno.env.get("LINEAR_API_KEY");
if (!apiKey) {
console.error("The LINEAR_API_KEY environment variable is required.");
console.error(
"Create an API key at https://linear.app/settings/account/security",
);
console.error("For bash/zsh, run: export LINEAR_API_KEY=your_key");
console.error("For fish, run: set -gx LINEAR_API_KEY your_key");
Deno.exit(1);
}
const query = `
query {
viewer {
organization {
urlKey
}
}
teams {
nodes {
id
key
name
}
}
}
`;
const response = await fetch("https://api.linear.app/graphql", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": apiKey,
},
body: JSON.stringify({ query }),
});
const result = await response.json();
if (result.errors) {
console.error(
"Error fetching data from Linear GraphQL API:",
result.errors,
);
Deno.exit(1);
}
const workspace = result.data.viewer.organization.urlKey;
const teams = result.data.teams.nodes;
interface Team {
id: string;
key: string;
name: string;
}
teams.sort((a: Team, b: Team) => a.name.localeCompare(b.name));
const teamChoices = teams.map((team: Team) => ({
name: `${team.name} (${team.key})`,
value: team.key,
}));
const responses = await prompt([
{
name: "team_id",
message: "Select a team:",
type: Select,
options: teamChoices,
},
{
name: "sort",
message: "Select sort order:",
type: Select,
options: [
{ name: "manual", value: "manual" },
{ name: "priority", value: "priority" },
],