-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-issue-ops.ts
More file actions
268 lines (244 loc) · 10.5 KB
/
github-issue-ops.ts
File metadata and controls
268 lines (244 loc) · 10.5 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
#!/usr/bin/env bun
// ─── github-issue-ops ─────────────────────────────────────────────────────────
//
// CLI entry point. Mirrors the structure of github-code-search:
// - program.exitOverride() so errors propagate via process.exit(1)
// - Errors written to fd 2 via writeFileSync(2, ...) for clean UX
// - GITHUB_TOKEN env var read automatically
// - Help output colorized with picocolors when stdout is a TTY
import { program } from "commander";
import { writeFileSync } from "fs";
import pc from "picocolors";
import { createAction } from "./src/commands/create.ts";
import { refreshAction } from "./src/commands/refresh.ts";
import { dispatchAction } from "./src/commands/dispatch.ts";
import { checkForUpdate, performUpgrade } from "./src/upgrade.ts";
declare const BUILD_VERSION: string;
declare const BUILD_COMMIT: string;
const version =
typeof BUILD_VERSION !== "undefined"
? BUILD_VERSION
: (process.env["npm_package_version"] ?? "0.0.0-dev");
const commit = typeof BUILD_COMMIT !== "undefined" ? BUILD_COMMIT : "dev";
// ─── Help colorization ────────────────────────────────────────────────────────
// Only apply colours when stdout is connected to a real terminal.
// Pipes, CI redirects, and --no-color environments stay plain-text.
const HAS_COLOR = Boolean(process.stdout.isTTY);
/**
* Walk a multi-line description and colour:
* • "Docs: <url>" → dim label + cyan underlined URL
* • "Example: …" → dim label + italic value
* • indent lines (code-like) → dim
*/
function colorDesc(s: string): string {
if (!HAS_COLOR) return s;
return s
.split("\n")
.map((line) => {
const docsMatch = line.match(/^(\s*Docs:\s*)(https?:\/\/\S+)$/);
if (docsMatch) return pc.dim(docsMatch[1]) + pc.cyan(pc.underline(docsMatch[2]));
const exampleMatch = line.match(/^(\s*Example:\s*)(.+)$/);
if (exampleMatch) return pc.dim(exampleMatch[1]) + pc.italic(exampleMatch[2]);
if (/^\s+(e\.g\.|owner\/|acme\/|fulll\/)/.test(line)) return pc.dim(line);
return line;
})
.join("\n");
}
/** Cyan underlined hyperlink — falls back to plain when not a TTY. */
function helpLink(url: string): string {
return HAS_COLOR ? pc.cyan(pc.underline(url)) : url;
}
/** Labelled help-text footer block (bold label + link on next line). */
function helpSection(label: string, url: string): string {
const t = HAS_COLOR ? pc.bold(label) : label;
return `\n${t}\n ${helpLink(url)}`;
}
/** Commander configureHelp options shared by all commands. */
const helpFormatConfig = {
styleTitle: (s: string) => (HAS_COLOR ? pc.bold(pc.yellow(s)) : s),
styleCommandText: (s: string) => (HAS_COLOR ? pc.bold(s) : s),
styleSubcommandText: (s: string) => (HAS_COLOR ? pc.cyan(s) : s),
styleArgumentText: (s: string) => (HAS_COLOR ? pc.yellow(s) : s),
styleOptionText: (s: string) => (HAS_COLOR ? pc.green(s) : s),
styleOptionTerm: (s: string) => (HAS_COLOR ? pc.green(s) : s),
styleSubcommandTerm: (s: string) => (HAS_COLOR ? pc.cyan(s) : s),
styleArgumentTerm: (s: string) => (HAS_COLOR ? pc.yellow(s) : s),
styleOptionDescription: colorDesc,
styleSubcommandDescription: colorDesc,
styleArgumentDescription: colorDesc,
styleCommandDescription: colorDesc,
styleDescriptionText: colorDesc,
};
function getToken(): string {
const token = process.env["GITHUB_TOKEN"] ?? process.env["GH_TOKEN"];
if (!token) {
writeFileSync(
2,
(HAS_COLOR ? pc.red("Error: ") : "Error: ") +
"GITHUB_TOKEN (or GH_TOKEN) environment variable is not set.\n",
);
process.exit(1);
}
return token;
}
// ─── Program ──────────────────────────────────────────────────────────────────
program
.name("github-issue-ops")
.description("Industrialize GitHub issue campaigns from code-search results")
.version(`${version} (${commit})`)
.exitOverride()
.configureHelp(helpFormatConfig)
.addHelpText("after", helpSection("Documentation:", "https://fulll.github.io/github-issue-ops/"));
// ─── issue commands ───────────────────────────────────────────────────────────
const issue = program
.command("issue")
.description("Manage EPIC and dispatch issues")
.configureHelp(helpFormatConfig);
// issue create
issue
.command("create")
.description("Create an EPIC issue from stdin (github-code-search output or JSON)")
.configureHelp(helpFormatConfig)
.addHelpText(
"after",
helpSection("Documentation:", "https://fulll.github.io/github-issue-ops/reference/create"),
)
.requiredOption("-r, --repo <owner/repo>", "Target repository for the EPIC issue")
.option("-t, --title <title>", "Issue title")
.option("-l, --label <label>", "Label to apply (repeatable)", collect, [])
.option("-a, --assignee <login>", "Assignee login (repeatable)", collect, [])
.option("--template <name>", "Issue template filename (e.g. epic.md)")
.option(
"--team-prefix <prefix>",
[
"Team name prefix for ownership resolution (repeatable).",
"Example: team-backend,team-platform",
"Docs: https://fulll.github.io/github-issue-ops/reference/create#team-prefix",
].join("\n"),
collect,
[],
)
.option("--non-interactive", "Disable interactive prompts", false)
.option("--dry-run", "Print body without creating the issue", false)
.action(async (opts) => {
await createAction({ ...opts, token: getToken() }).catch(exitOnError);
});
// issue refresh
issue
.command("refresh")
.description("Refresh an existing EPIC issue with updated results from stdin")
.configureHelp(helpFormatConfig)
.addHelpText(
"after",
helpSection("Documentation:", "https://fulll.github.io/github-issue-ops/reference/refresh"),
)
.requiredOption(
"-i, --issue <owner/repo#number>",
"EPIC issue reference (e.g. fulll/platform#42)",
)
.option("--non-interactive", "Disable interactive prompts", false)
.option("--dry-run", "Print updated body without patching the issue", false)
.action(async (opts) => {
await refreshAction({ ...opts, token: getToken() }).catch(exitOnError);
});
// issue dispatch
issue
.command("dispatch")
.description("Create one sub-issue per repository found in an EPIC checklist")
.configureHelp(helpFormatConfig)
.addHelpText(
"after",
helpSection("Documentation:", "https://fulll.github.io/github-issue-ops/reference/dispatch"),
)
.requiredOption("-e, --epic <owner/repo#number>", "EPIC issue reference (e.g. fulll/platform#42)")
.option("-l, --label <label>", "Extra label to apply to each sub-issue (repeatable)", collect, [])
.option(
"--team-prefix <prefix>",
[
"Team name prefix for ownership resolution (repeatable).",
"Example: team-backend,team-platform",
"Docs: https://fulll.github.io/github-issue-ops/reference/dispatch#team-prefix",
].join("\n"),
collect,
[],
)
.option(
"--central-repo <owner/repo>",
[
"Repo containing .github-issue-ops/owners.json mapping.",
"Example: acme/platform",
"Docs: https://fulll.github.io/github-issue-ops/reference/dispatch#central-repo",
].join("\n"),
)
.option("-m, --mode <plan|apply>", "plan = preview what would happen; apply = execute", "plan")
.option("--non-interactive", "Disable interactive prompts", false)
.action(async (opts) => {
await dispatchAction({ ...opts, token: getToken() }).catch(exitOnError);
});
// ─── upgrade ──────────────────────────────────────────────────────────────────
program
.command("upgrade")
.description("Upgrade github-issue-ops to the latest release")
.configureHelp(helpFormatConfig)
.option("--dry-run", "Check for updates without installing", false)
.addHelpText(
"after",
"\n" +
(HAS_COLOR
? pc.dim("Requires GITHUB_TOKEN for private release assets.")
: "Requires GITHUB_TOKEN for private release assets."),
)
.action(async (opts: { dryRun: boolean }) => {
if (opts.dryRun) {
const latest = await checkForUpdate(version, process.env["GITHUB_TOKEN"]).catch(exitOnError);
if (latest) {
process.stdout.write(
(HAS_COLOR ? pc.green("Update available: ") : "Update available: ") + latest + "\n",
);
process.stdout.write(
HAS_COLOR
? pc.dim("Run without --dry-run to install.\n")
: "Run without --dry-run to install.\n",
);
} else {
process.stdout.write(
HAS_COLOR
? pc.dim(`Already up to date (${version}).\n`)
: `Already up to date (${version}).\n`,
);
}
return;
}
await performUpgrade(version, process.execPath, process.env["GITHUB_TOKEN"]).catch(exitOnError);
});
// ─── Parse ────────────────────────────────────────────────────────────────────
async function main(): Promise<void> {
try {
await program.parseAsync(process.argv);
} catch (err) {
exitOnError(err as Error);
}
}
main().catch(exitOnError);
// ─── Helpers ──────────────────────────────────────────────────────────────────
function collect(value: string, previous: string[]): string[] {
return [...previous, value];
}
function exitOnError(err: Error): never {
// Commander throws CommanderError on --help / --version — ignore those.
// Possible codes: "commander.helpDisplayed" (--help flag) or "commander.help"
// (built-in `help <cmd>` invocation). Both have exitCode 0.
const code = (err as { code?: string; exitCode?: number }).code;
const exitCode = (err as { exitCode?: number }).exitCode;
if (
code === "commander.helpDisplayed" ||
code === "commander.help" ||
code === "commander.version" ||
exitCode === 0
) {
process.exit(0);
}
const prefix = HAS_COLOR ? pc.red("Error: ") : "Error: ";
writeFileSync(2, `${prefix}${err.message}\n`);
process.exit(1);
}