-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathissues.js
More file actions
383 lines (304 loc) · 9.65 KB
/
issues.js
File metadata and controls
383 lines (304 loc) · 9.65 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
#!/usr/bin/env node
/**
* GitHub Issues 下载脚本 (Node.js版本)
* 从指定仓库下载所有issues并保存为markdown文件
*/
const fs = require("fs").promises;
const path = require("path");
class GitHubIssuesDownloader {
constructor(repoOwner, repoName, token = null) {
this.repoOwner = repoOwner;
this.repoName = repoName;
this.token = token;
this.baseUrl = "https://api.github.com";
this.headers = {
Accept: "application/vnd.github.v3+json",
"User-Agent": "GitHub-Issues-Downloader",
};
if (token) {
this.headers["Authorization"] = `token ${token}`;
}
}
async delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async fetchWithRetry(url, options = {}, retries = 3) {
for (let i = 0; i < retries; i++) {
try {
const response = await fetch(url, {
...options,
headers: this.headers,
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
return await response.json();
} catch (error) {
console.log(`尝试 ${i + 1}/${retries} 失败: ${error.message}`);
if (i === retries - 1) throw error;
await this.delay(1000 * (i + 1)); // 递增延迟
}
}
}
async getAllIssues(state = "all", perPage = 100) {
const issues = [];
let page = 1;
while (true) {
const url = `${this.baseUrl}/repos/${this.repoOwner}/${this.repoName}/issues`;
const params = new URLSearchParams({
state,
per_page: perPage,
page: page,
sort: "created",
direction: "desc",
});
console.log(`获取第 ${page} 页issues...`);
try {
const pageIssues = await this.fetchWithRetry(`${url}?${params}`);
if (!pageIssues || pageIssues.length === 0) {
break;
}
issues.push(...pageIssues);
page++;
// 避免触发API限制
await this.delay(500);
} catch (error) {
console.error(`获取issues失败: ${error.message}`);
break;
}
}
return issues;
}
async getIssueComments(issueNumber) {
try {
const url = `${this.baseUrl}/repos/${this.repoOwner}/${this.repoName}/issues/${issueNumber}/comments`;
return await this.fetchWithRetry(url);
} catch (error) {
console.error(`获取issue #${issueNumber} 评论失败: ${error.message}`);
return [];
}
}
formatIssueToMarkdown(issue, comments = []) {
const labels = issue.labels.map((label) => label.name).join(", ");
let mdContent = `# ${issue.title}
**Issue #**: ${issue.number}
**状态**: ${issue.state}
**作者**: ${issue.user.login}
**创建时间**: ${issue.created_at}
**更新时间**: ${issue.updated_at}
**标签**: ${labels}
**URL**: ${issue.html_url}
## 描述
${issue.body || "无描述"}
`;
if (comments && comments.length > 0) {
mdContent += "## 评论\n\n";
comments.forEach((comment, index) => {
const createdAt = new Date(comment.created_at).toLocaleString("zh-CN");
mdContent += `### 评论 ${index + 1} - ${
comment.user.login
} (${createdAt})
${comment.body}
---
`;
});
}
return mdContent;
}
sanitizeFilename(filename) {
// 移除或替换不安全的文件名字符
return filename
.replace(/[<>:"/\\|?*]/g, "_")
.replace(/\s+/g, "_")
.substring(0, 50)
.trim();
}
async ensureDirectory(dirPath) {
try {
await fs.access(dirPath);
} catch {
await fs.mkdir(dirPath, { recursive: true });
}
}
async saveIssuesToFiles(outputDir = "issues") {
const basePath = path.resolve(outputDir);
const openDir = path.join(basePath, "open");
const closedDir = path.join(basePath, "closed");
// 创建目录
await this.ensureDirectory(openDir);
await this.ensureDirectory(closedDir);
console.log("开始下载issues...");
const issues = await this.getAllIssues();
console.log(`共找到 ${issues.length} 个issues`);
let openCount = 0;
let closedCount = 0;
for (const issue of issues) {
// 跳过Pull Requests
if (issue.pull_request) {
continue;
}
console.log(`处理 Issue #${issue.number}: ${issue.title}`);
// 获取评论
const comments = await this.getIssueComments(issue.number);
// 转换为markdown
const mdContent = this.formatIssueToMarkdown(issue, comments);
// 确定保存目录
const targetDir = issue.state === "open" ? openDir : closedDir;
// 生成安全的文件名
const safeTitle = this.sanitizeFilename(issue.title);
const filename = `${issue.number
.toString()
.padStart(3, "0")}_${safeTitle}.md`;
const filePath = path.join(targetDir, filename);
// 保存文件
try {
await fs.writeFile(filePath, mdContent, "utf-8");
if (issue.state === "open") {
openCount++;
} else {
closedCount++;
}
} catch (error) {
console.error(`保存文件失败 ${filename}: ${error.message}`);
}
// 避免过于频繁的请求
await this.delay(300);
}
console.log(`\n下载完成!`);
console.log(`未解决的issues: ${openCount} 个`);
console.log(`已解决的issues: ${closedCount} 个`);
console.log(`文件保存在: ${basePath}`);
// 创建README
const readmeContent = `# ${this.repoOwner}/${this.repoName} Issues
下载时间: ${new Date().toLocaleString("zh-CN")}
## 统计
- 未解决的issues: ${openCount} 个 (保存在 \`open/\` 文件夹)
- 已解决的issues: ${closedCount} 个 (保存在 \`closed/\` 文件夹)
- 总计: ${openCount + closedCount} 个
## 使用方法
这些markdown文件可以直接在Cursor中打开,利用其语义化搜索功能来查找相关问题。
例如搜索关键词:
- "chrome.debugger"
- "private mode"
- "password autofill"
- "attachment problem"
## 文件命名规则
文件名格式: \`编号_标题.md\`
- 编号用3位数字补齐 (如: 001, 023, 156)
- 标题去除特殊字符,最多50个字符
`;
await fs.writeFile(
path.join(basePath, "README.md"),
readmeContent,
"utf-8"
);
}
}
// GitHub URL 解析函数
function parseGitHubUrl(url) {
try {
// 支持多种 GitHub URL 格式
const patterns = [
/https?:\/\/github\.com\/([^\/]+)\/([^\/]+?)(?:\.git)?(?:\/.*)?$/,
/git@github\.com:([^\/]+)\/([^\/]+?)(?:\.git)?$/,
];
for (const pattern of patterns) {
const match = url.match(pattern);
if (match) {
return {
owner: match[1],
repo: match[2],
};
}
}
throw new Error("无法解析的 GitHub URL 格式");
} catch (error) {
throw new Error(`URL解析失败: ${error.message}`);
}
}
// 显示使用帮助
function showHelp() {
console.log(`
GitHub Issues 下载器
使用方法:
node issues.js [GitHub仓库URL]
参数:
GitHub仓库URL - GitHub仓库的URL,支持HTTPS和SSH格式
示例:
node issues.js https://github.com/microsoft/playwright
node issues.js https://github.com/microsoft/playwright.git
node issues.js git@github.com:microsoft/playwright.git
环境变量:
GITHUB_TOKEN - GitHub访问令牌(可选,但强烈推荐)
不提供URL参数时,将使用默认配置的仓库。
`);
}
// 主函数
async function main() {
// 检查命令行参数
const args = process.argv.slice(2);
// 如果请求帮助
if (args.includes("--help") || args.includes("-h")) {
showHelp();
return;
}
let REPO_OWNER, REPO_NAME, OUTPUT_DIR;
// 如果提供了 GitHub URL 参数
if (args.length > 0) {
try {
const parsed = parseGitHubUrl(args[0]);
REPO_OWNER = parsed.owner;
REPO_NAME = parsed.repo;
OUTPUT_DIR = `${REPO_OWNER}-${REPO_NAME}-issues`;
console.log(`📂 从URL解析到仓库: ${REPO_OWNER}/${REPO_NAME}`);
} catch (error) {
console.error(`❌ ${error.message}`);
console.log(`\n请使用正确的GitHub URL格式,例如:`);
console.log(` https://github.com/owner/repo`);
console.log(` https://github.com/owner/repo.git`);
console.log(` git@github.com:owner/repo.git\n`);
showHelp();
process.exit(1);
}
} else {
// 使用默认配置
REPO_OWNER = "ruifigueira";
REPO_NAME = "playwright-crx";
OUTPUT_DIR = "playwright-crx-issues";
console.log(`📂 使用默认仓库: ${REPO_OWNER}/${REPO_NAME}`);
console.log(`💡 提示: 你也可以传入GitHub URL参数来下载其他仓库的issues`);
console.log(
` 例如: node issues.js https://github.com/microsoft/playwright\n`
);
}
// GitHub Token (可选,但强烈建议设置)
const GITHUB_TOKEN = process.env.GITHUB_TOKEN || null;
if (!GITHUB_TOKEN) {
console.log("⚠️ 建议设置 GITHUB_TOKEN 环境变量以提高API限制");
console.log(" export GITHUB_TOKEN=your_token");
console.log(" 然后重新运行脚本\n");
}
console.log(`开始下载 ${REPO_OWNER}/${REPO_NAME} 的issues...`);
try {
const downloader = new GitHubIssuesDownloader(
REPO_OWNER,
REPO_NAME,
GITHUB_TOKEN
);
await downloader.saveIssuesToFiles(OUTPUT_DIR);
console.log("\n🎉 下载完成!现在可以在Cursor中打开文件夹进行语义化搜索了");
} catch (error) {
console.error("❌ 下载失败:", error.message);
process.exit(1);
}
}
// 检查Node.js版本
if (process.version < "v18.0.0") {
console.error("❌ 需要 Node.js 18+ 版本(支持原生fetch)");
process.exit(1);
}
// 运行主函数
if (require.main === module) {
main().catch(console.error);
}
module.exports = GitHubIssuesDownloader;