forked from danielmeppiel/meeting-2-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-issues.ts
More file actions
149 lines (131 loc) · 5.38 KB
/
github-issues.ts
File metadata and controls
149 lines (131 loc) · 5.38 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
import { exec } from "child_process";
import { promisify } from "util";
const execAsync = promisify(exec);
const OWNER = "danielmeppiel";
const REPO = "corporate-website";
interface GapItem {
id: number;
requirement: string;
currentState: string;
gap: string;
complexity: "Low" | "Medium" | "High" | "Critical";
estimatedEffort: string;
details: string;
}
interface CreatedIssue {
id: number;
title: string;
number: number;
url: string;
error?: string;
}
interface CreateIssuesOptions {
gaps: GapItem[];
epicIssueNumber?: number;
onProgress?: (current: number, total: number, message: string) => void;
onIssueCreated?: (issue: CreatedIssue) => void;
onLog?: (message: string) => void;
}
export async function createGithubIssues(
options: CreateIssuesOptions,
): Promise<CreatedIssue[]> {
const progress = options.onProgress ?? (() => {});
const onIssueCreated = options.onIssueCreated ?? (() => {});
const log = options.onLog ?? (() => {});
const total = options.gaps.length;
progress(0, total, "Connecting to GitHub...");
log("Creating GitHub issues via gh CLI...");
console.log(`[github-issues] Creating ${total} issues via gh CLI...`);
const createdIssues: CreatedIssue[] = [];
for (let i = 0; i < options.gaps.length; i++) {
const gap = options.gaps[i]!;
const label = gap.requirement.length > 50 ? gap.requirement.substring(0, 50) + "..." : gap.requirement;
progress(i + 1, total, `Creating issue ${i + 1}/${total}: ${label}`);
log(`Creating issue ${i + 1}/${total}: ${label}`);
console.log(`[github-issues] Creating issue ${i + 1}/${total}...`);
const title = `[Contoso Redesign] ${gap.requirement}`;
const bodyParts = [
"## Description",
gap.gap,
"",
"## Current State",
gap.currentState,
"",
"## Acceptance Criteria",
"- The gap described above is fully addressed",
"",
"## Technical Details",
gap.details,
"",
"## Estimated Effort",
`${gap.estimatedEffort} | Complexity: ${gap.complexity}`,
];
if (options.epicIssueNumber && options.epicIssueNumber > 0) {
bodyParts.push("", `Part of #${options.epicIssueNumber}`);
}
const body = bodyParts.join("\n");
try {
// Use gh issue create with --json to get structured output
const { stdout, stderr } = await execAsync(
`gh issue create --title ${shellEscape(title)} --body ${shellEscape(body)} --label enhancement -R ${OWNER}/${REPO}`,
{ timeout: 30_000, env: { ...process.env, GITHUB_TOKEN: undefined, GH_PAGER: "cat" } },
);
if (stderr) console.log(`[github-issues] stderr for issue ${i + 1}:`, stderr.trim());
// gh issue create outputs a URL like: https://github.com/owner/repo/issues/123
const url = stdout.trim();
const numberMatch = url.match(/\/issues\/(\d+)/);
const issueNumber = numberMatch ? parseInt(numberMatch[1]!, 10) : 0;
if (issueNumber > 0) {
const issue: CreatedIssue = {
id: gap.id,
title,
number: issueNumber,
url,
};
createdIssues.push(issue);
onIssueCreated(issue);
log(`✔ Issue #${issueNumber} created: ${title.substring(0, 60)}`);
console.log(`[github-issues] Issue ${i + 1} created: #${issueNumber} → ${url}`);
} else {
console.error(`[github-issues] Unexpected gh output for issue ${i + 1}:`, stdout);
log(`⚠ Issue ${i + 1}: unexpected output — ${stdout.substring(0, 100)}`);
const issue: CreatedIssue = {
id: gap.id,
title,
number: 0,
url: "#",
error: `Unexpected CLI output: ${stdout.substring(0, 100)}`,
};
createdIssues.push(issue);
onIssueCreated(issue);
}
} catch (err) {
const errorMsg = err instanceof Error ? err.message : String(err);
console.error(`[github-issues] Error creating issue ${i + 1}:`, errorMsg);
log(`✘ Error creating issue ${i + 1}: ${errorMsg.substring(0, 150)}`);
const issue: CreatedIssue = {
id: gap.id,
title,
number: 0,
url: "#",
error: errorMsg.substring(0, 200),
};
createdIssues.push(issue);
onIssueCreated(issue);
}
}
const successCount = createdIssues.filter(i => i.number > 0).length;
const failCount = total - successCount;
if (failCount > 0) {
log(`⚠ Done — ${successCount}/${total} issues created, ${failCount} failed`);
} else {
log(`✔ Done — ${successCount} issues created successfully`);
}
console.log(`[github-issues] Done. ${successCount}/${total} issues created.`);
return createdIssues;
}
/** Shell-escape a string for use in a command argument */
function shellEscape(str: string): string {
// Replace single quotes, then wrap in single quotes
return "'" + str.replace(/'/g, "'\\''") + "'";
}