-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.ts
More file actions
560 lines (509 loc) · 15.7 KB
/
mod.ts
File metadata and controls
560 lines (509 loc) · 15.7 KB
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
import { loadSync } from "@std/dotenv";
import { colorize } from "@polyseam/emoji-ansi-colorizer";
import { Node, Project, SyntaxKind, type VariableDeclaration } from "ts-morph";
import { Codepath } from "@polyseam/codepath";
export type Printer = (error: SherrorError, codepath?: Codepath) => void;
export interface SherrorError {
error_code: number;
app_message: string;
post_title: string;
post_body: string;
_discussion_link?: string;
}
export interface SherrorConfig {
category_name: string;
errors: SherrorError[];
printer?: (error: SherrorError, codepath?: string) => void;
}
interface Discussion {
id: string;
title: string;
body: string;
url?: string;
}
interface DiscussionResponse {
repository: {
discussion: Discussion | null;
} | null;
}
interface CreateDiscussionResponse {
createDiscussion: {
discussion: {
url: string;
};
};
}
export class SherrorClient {
private ghToken: string;
private sherrorConfig: SherrorConfig;
private project: Project;
/**
* Creates a SherrorClient for the given
*/
constructor(config: SherrorConfig) {
loadSync({
export: true,
});
this.ghToken = Deno.env.get("GITHUB_TOKEN") as string;
if (!this.ghToken) {
throw new Error(
"'GITHUB_TOKEN' must be set to configure GitHub Discussions",
);
}
if (!config.category_name || !config.errors) {
throw new Error("Invalid config: missing required fields");
}
this.sherrorConfig = config;
this.project = new Project();
}
private prettyJSON(obj: unknown) {
return JSON.stringify(obj, null, 2);
}
/**
* Synchronize local config errors with GitHub Discussions:
* - create discussions for new errors
* - update title/body for existing ones
*/
private async requestRepositoryInfo(
owner: string,
repo: string,
_category: string,
): Promise<{
repository: {
id: string;
discussionCategories: {
nodes: Array<{
id: string;
name: string;
}>;
};
} | null;
}> {
interface RepositoryInfoResponse {
repository: {
id: string;
discussionCategories: {
nodes: Array<{
id: string;
name: string;
}>;
};
} | null;
}
const response = await this.requestGraphQL<RepositoryInfoResponse>(
`
query RepoInfo($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
id
discussionCategories(first: 10) {
nodes {
id
name
}
}
}
}
`,
{ owner, repo },
);
return response;
}
async writebackConfig(): Promise<void> {
const cfg = this.sherrorConfig;
if (!cfg || !cfg.category_name || !cfg.errors) {
throw new Error(
"Invalid sherror config: missing category_name or errors",
);
}
// ts-morph
const sourceFile = this.project.addSourceFileAtPath("sherror.config.ts");
const exports = sourceFile.getExportedDeclarations();
let configDecls = exports.get("default");
if (!configDecls?.length) {
console.debug("No 'export default' found");
configDecls = exports.get("config");
}
if (!configDecls?.length) {
throw new Error("No 'export config' found");
}
const configDecl = configDecls[0] as VariableDeclaration;
const initializer = configDecl.getInitializerOrThrow();
// Handle both direct ObjectLiteral and satisfies Expression
let configInit;
if (initializer.getKind() === SyntaxKind.SatisfiesExpression) {
const satisfiesExpr = initializer.asKindOrThrow(
SyntaxKind.SatisfiesExpression,
);
configInit = satisfiesExpr.getExpression();
} else {
configInit = initializer;
}
if (!Node.isObjectLiteralExpression(configInit)) {
throw new Error(
`Expected config to be an object literal or satisfy an object type.`,
);
}
const errorsProp = configInit.getProperty("errors");
if (!errorsProp || !Node.isPropertyAssignment(errorsProp)) {
throw new Error(`Expected config to have a property "errors".`);
}
const arrayLiteral = errorsProp.getInitializerIfKindOrThrow(
SyntaxKind.ArrayLiteralExpression,
);
// Clear existing elements
arrayLiteral.getElements().forEach((element, index) => {
const error = cfg.errors[index];
if (error) {
const objText = `{
error_code: ${error.error_code},
app_message: ${this.prettyJSON(error.app_message)},
post_title: ${this.prettyJSON(error.post_title)},
post_body: ${this.prettyJSON(error.post_body)}${
error._discussion_link
? `,
_discussion_link: ${this.prettyJSON(error._discussion_link)}`
: ""
}
}`;
element.replaceWithText(objText);
}
});
// If there are more new errors than existing ones, add them
if (cfg.errors.length > arrayLiteral.getElements().length) {
const newElements = cfg.errors.slice(arrayLiteral.getElements().length)
.map((error) =>
`{
error_code: ${error.error_code},
app_message: ${this.prettyJSON(error.app_message)},
post_title: ${this.prettyJSON(error.post_title)},
post_body: ${this.prettyJSON(error.post_body)}${
error._discussion_link
? `,
_discussion_link: ${this.prettyJSON(error._discussion_link)}`
: ""
}
}`
);
// Add each new element one by one
for (const element of newElements) {
arrayLiteral.addElement(element);
}
}
await this.project.save();
}
async sync(): Promise<void> {
const cfg = this.sherrorConfig;
if (!cfg || !cfg.category_name || !cfg.errors) {
throw new Error(
"Invalid sherror config: missing category_name or errors",
);
}
// determine GitHub repo context from git remote
const remote = await this.getGitRemoteUrl();
const { owner, repo } = this.parseGitRemote(remote);
// fetch repository and discussion category ids
const { repository } = await this.requestRepositoryInfo(
owner,
repo,
cfg.category_name,
);
if (!repository) {
throw new Error(`Repository ${owner}/${repo} not found`);
}
const repoId = repository.id;
// Find or create the category
const category = repository.discussionCategories.nodes.find(
(cat) => cat.name === cfg.category_name,
);
let categoryId: string;
if (!category) {
// GitHub Discussions categories can't be created programmatically via their API
// The repository owner needs to create the category manually in the repository settings
// We'll provide helpful instructions instead of failing
const availableCategories = repository.discussionCategories.nodes.map(
(c) => `- ${c.name}`,
).join("\n");
console.log(`
Error: Discussion category "${cfg.category_name}" not found in ${owner}/${repo}.
Available categories:
${availableCategories}
To fix this:
1. Go to your repository settings on GitHub
2. Click on "Options" in the left sidebar
3. Scroll down to the "Features" section
4. Make sure "Discussions" is enabled
5. Click on "Set up discussions" if not already set up
6. Create a new category named "${cfg.category_name}" in the discussions settings
7. Run this command again
`);
throw new Error(
`Discussion category "${cfg.category_name}" not found and cannot be created programmatically`,
);
} else {
categoryId = category.id;
console.log(`Using existing category: ${category.name} (${categoryId})`);
}
let updated = false;
for (let index = 0; index < cfg.errors.length; index++) {
const err = cfg.errors[index];
// ensure required fields
if (
typeof err.post_title !== "string" || typeof err.post_body !== "string"
) {
throw new Error(
`Invalid error entry missing post_title or post_body: ${
this.prettyJSON(err)
}`,
);
}
if (err._discussion_link) {
// update existing discussion if changed
const num = this.parseDiscussionNumber(err._discussion_link);
const q = await this.requestGraphQL<DiscussionResponse>(
`
query GetDiscussion($owner: String!, $repo: String!, $number: Int!) {
repository(owner: $owner, name: $repo) {
discussion(number: $number) {
id
title
body
}
}
}
`,
{ owner, repo, number: num },
);
const disc = q.repository?.discussion;
if (!disc) {
throw new Error(`Discussion #${num} not found`);
}
if (disc.title !== err.post_title || disc.body !== err.post_body) {
await this.requestGraphQL<
{ updateDiscussion: { discussion: { url: string } } }
>(
`
mutation UpdateDiscussion($id: ID!, $title: String!, $body: String!) {
updateDiscussion(input: { discussionId: $id, title: $title, body: $body }) {
discussion { url }
}
}
`,
{ id: disc.id, title: err.post_title, body: err.post_body },
);
}
} else {
// create new discussion
const cr = await this.requestGraphQL<CreateDiscussionResponse>(
`
mutation CreateDiscussion($repoId: ID!, $catId: ID!, $title: String!, $body: String!) {
createDiscussion(input: { repositoryId: $repoId, categoryId: $catId, title: $title, body: $body }) {
discussion { url }
}
}
`,
{
repoId,
catId: categoryId,
title: err.post_title,
body: err.post_body,
},
);
const discussionUrl = cr.createDiscussion.discussion.url as string;
this.sherrorConfig.errors[index]._discussion_link = discussionUrl;
updated = true;
}
}
if (updated) {
// Update the config with the new discussion link(s)
await this.writebackConfig();
}
}
/**
* Get the current config, including any generated discussion links
*/
getConfig(): SherrorConfig {
return { ...this.sherrorConfig };
}
private colorize(str: string): string {
return colorize(str);
}
/**
* Get the Sherror instance for the given error code.
*/
get(
code: number,
): SherrorError & {
Codepath: typeof Codepath;
print: (codepath?: Codepath) => void;
exit: () => void;
} {
const err = this.sherrorConfig.errors.find((e) => e.error_code === code);
if (!err) throw new Error(`Error code ${code} not found in config`);
const app_message = this.colorize(err.app_message);
return {
...err,
app_message,
Codepath,
print: (codepath?: Codepath) => {
const e: SherrorError = { ...err, app_message };
this.sherrorConfig?.printer?.(e, codepath?.toString());
},
exit: () => Deno.exit(code),
};
}
private async requestGraphQL<T = unknown>(
query: string,
variables: Record<string, unknown>,
): Promise<T> {
const res = await fetch("https://api.github.com/graphql", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `bearer ${this.ghToken}`,
},
body: this.prettyJSON({ query, variables }),
});
if (!res.ok) {
const text = await res.text();
throw new Error(`GitHub GraphQL error ${res.status}: ${text}`);
}
const data = await res.json();
if (data.errors) {
throw new Error(`GraphQL errors: ${this.prettyJSON(data.errors)}`);
}
return data.data;
}
private async getGitRemoteUrl(): Promise<string> {
const cmd = new Deno.Command("git", {
args: ["config", "--get", "remote.origin.url"],
stdout: "piped",
});
const { stdout } = await cmd.output();
return new TextDecoder().decode(stdout).trim();
}
private parseGitRemote(remote: string): { owner: string; repo: string } {
// git@github.com:owner/repo.git or https://github.com/owner/repo.git
let path = "";
if (remote.startsWith("git@")) {
const parts = remote.split(":");
path = parts[1];
} else if (/^https?:\/\//.test(remote)) {
const url = new URL(remote);
path = url.pathname.slice(1);
} else {
throw new Error(`Unrecognized git remote URL: ${remote}`);
}
path = path.replace(/\.git$/, "");
const [owner, repo] = path.split("/");
if (!owner || !repo) throw new Error(`Invalid git remote path: ${path}`);
return { owner, repo };
}
private parseDiscussionNumber(link: string): number {
try {
const url = new URL(link);
const parts = url.pathname.split("/");
const num = parts.pop() || "";
return parseInt(num, 10);
} catch {
throw new Error(`Invalid discussion link: ${link}`);
}
}
/**
* Clear all discussions in the configured category.
* @returns Promise that resolves when all discussions are deleted
*/
async clear(): Promise<void> {
const cfg = this.sherrorConfig;
if (!cfg?.category_name) {
throw new Error("Invalid config: missing category_name");
}
// Get repository info
const remote = await this.getGitRemoteUrl();
const { owner, repo } = this.parseGitRemote(remote);
const { repository } = await this.requestRepositoryInfo(
owner,
repo,
cfg.category_name,
);
if (!repository) {
throw new Error(`Repository ${owner}/${repo} not found`);
}
// Find the category
const category = repository.discussionCategories.nodes.find(
(cat) => cat.name === cfg.category_name,
);
if (!category) {
console.log(`No discussions found in category "${cfg.category_name}"`);
return;
}
// Get all discussions in the category
const response = await this.requestGraphQL<{
repository: {
discussions: {
nodes: Array<{ id: string; number: number; title: string }>;
};
};
}>(
`
query GetDiscussions($owner: String!, $repo: String!, $categoryId: ID!) {
repository(owner: $owner, name: $repo) {
discussions(first: 100, categoryId: $categoryId) {
nodes {
id
number
title
}
}
}
}
`,
{ owner, repo, categoryId: category.id },
);
const discussions = response.repository.discussions.nodes;
if (discussions.length === 0) {
console.log(`No discussions found in category "${cfg.category_name}"`);
return;
}
console.log(`Found ${discussions.length} discussions to delete...`);
// Delete each discussion
for (const discussion of discussions) {
console.log(
`Deleting discussion #${discussion.number}: ${discussion.title}`,
);
try {
await this.requestGraphQL(
`
mutation DeleteDiscussion($id: ID!) {
deleteDiscussion(input: { id: $id }) {
clientMutationId
}
}
`,
{ id: discussion.id },
);
} catch (error) {
console.error(
`Failed to delete discussion #${discussion.number}:`,
error,
);
throw error;
}
}
console.log(
`Successfully deleted ${discussions.length} discussions from category "${cfg.category_name}"`,
);
// Clear the discussion links from the config
if (this.sherrorConfig.errors) {
let updated = false;
this.sherrorConfig.errors.forEach((error) => {
if (error._discussion_link) {
delete error._discussion_link;
updated = true;
}
});
if (updated) {
await this.writebackConfig();
}
}
}
}