forked from linwu-hi/coding-time
-
Notifications
You must be signed in to change notification settings - Fork 0
/
issueToDirname.js
87 lines (73 loc) · 2.41 KB
/
issueToDirname.js
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
const axios = require('axios');
const fs = require('fs');
async function getGitHubIssues(owner, repo, labels, token) {
const baseUrl = `https://api.github.com/repos/${owner}/${repo}/issues`;
const headers = token ? { Authorization: `token ${token}` } : {};
const params = { state: 'all', per_page: 100 };
const issuesByLabel = {};
let nextPage = true;
while (nextPage) {
try {
const response = await axios.get(baseUrl, { headers, params });
const data = response.data;
if (!data.length) break;
data.forEach((issue) => {
if (!issue.pull_request) {
issue.labels.forEach((label) => {
if (labels.includes(label.name)) {
if (!issuesByLabel[label.name]) {
issuesByLabel[label.name] = [];
}
issuesByLabel[label.name].push(issue);
}
});
}
});
if (response.headers.link) {
const links = response.headers.link.split(', ');
nextPage = links.some((link) => link.endsWith('rel="next"'));
if (nextPage) {
const nextPageNum = parseInt(links[links.length - 1].match(/&page=(\d+)/)[1], 10);
params.page = nextPageNum;
}
} else {
nextPage = false;
}
} catch (error) {
throw new Error(`Failed to fetch issues. Error: ${error.message}`);
}
}
return issuesByLabel;
}
// Output to Markdown file
function writeIssuesToMarkdown(issues, outputPath) {
let content = '';
Object.entries(issues).forEach(([label, issuesList]) => {
content += `## ${label}\n\n`;
issuesList.forEach((issue) => {
content += `- [${issue.title}](${issue.html_url})\n`;
});
content += '\n';
});
fs.writeFile(outputPath, content, (err) => {
if (err) {
console.error('Error writing the file:', err);
} else {
console.log('Markdown file generated successfully!');
}
});
}
// 使用示例
const owner = 'linwu-hi';
const repo = 'code-interview';
const labels = ['JavaScript', 'TypeScript','vue','vue3','react','HTTP','webpack','nodejs','linux'];
const token = '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';
const outputPath = 'output.md';
(async () => {
try {
const issues = await getGitHubIssues(owner, repo, labels, token);
writeIssuesToMarkdown(issues, outputPath);
} catch (error) {
console.error(error.message);
}
})();