-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
300 lines (277 loc) · 8.34 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
import { Command } from "@cliffy/command";
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 } from "@std/path";
async function getCurrentBranch(): Promise<string> {
const process = new Deno.Command("git", {
args: ["symbolic-ref", "--short", "HEAD"],
});
const { stdout } = await process.output();
return new TextDecoder().decode(stdout).trim();
}
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 getIssueId(): Promise<string | null> {
const branch = await getCurrentBranch();
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 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 = Deno.env.get("LINEAR_API_KEY");
if (!apiKey) {
throw new Error("LINEAR_API_KEY environment variable is not set.");
}
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 fetchIssueDetails(
issueId: string,
showSpinner = false,
): Promise<{ title: string; description: string | null; url: string }> {
const spinner = showSpinner ? new Spinner() : null;
spinner?.start();
try {
const query =
`query($id: String!) { issue(id: $id) { title, description, url } }`;
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() {
const teamId = await getTeamId();
if (!teamId) {
console.error("Could not determine team id from directory name.");
Deno.exit(1);
}
const workspace = Deno.env.get("LINEAR_WORKSPACE");
if (!workspace) {
console.error("LINEAR_WORKSPACE environment variable is not set.");
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, { app: { name: "Linear" } });
}
async function openIssuePage() {
const issueId = await getIssueId();
if (!issueId) {
console.error(
"The current branch does not contain a valid linear issue id.",
);
Deno.exit(1);
}
const workspace = Deno.env.get("LINEAR_WORKSPACE");
if (!workspace) {
console.error("LINEAR_WORKSPACE environment variable is not set.");
Deno.exit(1);
}
const url = `https://linear.app/${workspace}/issue/${issueId}`;
console.log(`Opening ${url} in Linear.app`);
await open(url, { app: { name: "Linear" } });
}
const teamCommand = new Command()
.description("Manage Linear teams")
.action(openTeamPage)
.command("open", "Open the team page in Linear.app")
.alias("o")
.action(openTeamPage)
.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 = Deno.env.get("LINEAR_WORKSPACE");
if (!workspace) {
console.error("LINEAR_WORKSPACE environment variable is not set.");
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")
.action(openIssuePage)
.command("open", "Open the issue in Linear.app")
.alias("o")
.action(openIssuePage)
.command("print", "Print the issue details")
.alias("p")
.option("--no-color", "Disable colored output")
.action(async ({ color }) => {
const issueId = await getIssueId();
if (!issueId) {
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(
issueId,
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 id in the current git branch")
.action(async () => {
const issueId = await getIssueId();
if (issueId) {
console.log(issueId);
} else {
console.error(
"The current branch does not contain a valid linear issue id.",
);
Deno.exit(1);
}
})
.command("title", "Print the issue title")
.action(async () => {
const issueId = await getIssueId();
if (!issueId) {
console.error(
"The current branch does not contain a valid linear issue id.",
);
Deno.exit(1);
}
const { title } = await fetchIssueDetails(issueId, false);
console.log(title);
})
.command("url", "Print the issue URL")
.action(async () => {
const issueId = await getIssueId();
if (!issueId) {
console.error(
"The current branch does not contain a valid linear issue id.",
);
Deno.exit(1);
}
const { url } = await fetchIssueDetails(issueId, 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",
)
.action(async ({ base, draft }) => {
const issueId = await getIssueId();
if (!issueId) {
console.error(
"The current branch does not contain a valid linear issue id.",
);
Deno.exit(1);
}
const { title, url } = await fetchIssueDetails(
issueId,
Deno.stdout.isTerminal(),
);
const process = new Deno.Command("gh", {
args: [
"pr",
"create",
"--title",
`${issueId} ${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())
.parse(Deno.args);