-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanalyze-labels.js
More file actions
226 lines (193 loc) · 7.01 KB
/
analyze-labels.js
File metadata and controls
226 lines (193 loc) · 7.01 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
#!/usr/bin/env node
/**
* Apply better labels to issues based on content analysis
* Usage: node analyze-labels.js <owner/repo> [--owner OWNER] [--repo REPO]
*/
const BASE = 'https://api.github.com';
const token = process.env.GITHUB_TOKEN;
if (!token) {
console.error('❌ Error: GITHUB_TOKEN environment variable not set');
process.exit(1);
}
// Parse command-line arguments
let owner, repo;
const args = process.argv.slice(2);
if (args.length === 0) {
console.error('❌ Error: owner/repo required');
console.error('Usage: node analyze-labels.js owner/repo');
console.error(' or: node analyze-labels.js --owner OWNER --repo REPO');
process.exit(1);
}
// Check for --owner --repo flags
let i = 0;
while (i < args.length) {
if (args[i] === '--owner' && i + 1 < args.length) {
owner = args[i + 1];
i += 2;
} else if (args[i] === '--repo' && i + 1 < args.length) {
repo = args[i + 1];
i += 2;
} else if (!args[i].startsWith('--')) {
// Parse owner/repo format
const [o, r] = args[i].split('/');
if (!owner) owner = o;
if (!repo) repo = r;
i++;
} else {
i++;
}
}
if (!owner || !repo) {
console.error('❌ Error: invalid owner/repo format');
console.error('Usage: node analyze-labels.js owner/repo');
console.error(' or: node analyze-labels.js --owner OWNER --repo REPO');
process.exit(1);
}
async function ghFetch(path, options = {}) {
const res = await fetch(`${BASE}${path}`, {
...options,
headers: {
Authorization: `Bearer ${token}`,
Accept: 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
'Content-Type': 'application/json',
...(options.headers || {}),
},
});
if (!res.ok) {
let message = res.statusText;
try {
const body = await res.json();
message = body.message || message;
} catch { /* ignore */ }
throw new Error(`GitHub ${res.status}: ${message}`);
}
if (res.status === 204) return null;
return res.json();
}
async function getIssues() {
const owner_enc = encodeURIComponent(owner);
const repo_enc = encodeURIComponent(repo);
const issues = [];
let page = 1;
while (true) {
const batch = await ghFetch(
`/repos/${owner_enc}/${repo_enc}/issues?state=all&per_page=100&page=${page}&sort=created&direction=desc`
);
const onlyIssues = batch.filter((i) => !i.pull_request);
issues.push(...onlyIssues);
if (batch.length < 100) break;
page++;
}
return issues.slice(0, 21).reverse(); // Get the last 21 (newly imported)
}
function analyzeIssue(issue) {
const title = issue.title.toLowerCase();
const body = issue.body.toLowerCase();
const content = title + ' ' + body;
const labels = new Set();
// Detect type
if (content.includes('修复') || content.includes('fix')) {
labels.add('bug');
} else if (content.includes('重构') || content.includes('refactor')) {
labels.add('refactor');
} else if (content.includes('添加') || content.includes('support') || content.includes('enhance')) {
labels.add('feature');
} else if (content.includes('更新')) {
labels.add('enhancement');
}
// Platform detection
if (content.includes('windows')) {
labels.add('windows');
}
if (content.includes('linux')) {
labels.add('linux');
}
if (content.includes('macos') || content.includes('ios') || content.includes('ipad')) {
labels.add('macos');
}
if (content.includes('android') || content.includes('andriod')) {
labels.add('android');
}
// Component detection
if (content.includes('qt')) {
labels.add('qt');
}
if (content.includes('ffmpeg')) {
labels.add('ffmpeg');
}
if (content.includes('usb')) {
labels.add('usb');
}
if (content.includes('gpu') || content.includes('decoder') || content.includes('硬件加速')) {
labels.add('hardware-acceleration');
}
if (content.includes('jenkins') || content.includes('build')) {
labels.add('ci/cd');
}
if (content.includes('kvm')) {
labels.add('kvm');
}
if (content.includes('翻译') || content.includes('translation')) {
labels.add('i18n');
}
if (content.includes('installer') || content.includes('flatpak') || content.includes('package')) {
labels.add('distribution');
}
// Priority (keep from original)
const priorityMatch = issue.body.match(/Priority:\s*(\w+)/);
const existingLabels = issue.labels.map(l => l.name);
if (existingLabels.includes('High')) labels.add('High');
if (existingLabels.includes('Medium')) labels.add('Medium');
if (existingLabels.includes('Low')) labels.add('Low');
return Array.from(labels).filter(l => l); // Remove High/Medium/Low if not found
}
async function updateIssueLables(issueNumber, labels) {
const owner_enc = encodeURIComponent(owner);
const repo_enc = encodeURIComponent(repo);
const body = JSON.stringify({ labels });
return ghFetch(
`/repos/${owner_enc}/${repo_enc}/issues/${issueNumber}`,
{ method: 'PATCH', body }
);
}
async function main() {
console.log('🔍 Analyzing issues and suggesting labels...\n');
const issues = await getIssues();
const suggestions = [];
for (const issue of issues) {
const suggestedLabels = analyzeIssue(issue);
const currentLabels = issue.labels.map(l => l.name);
// Keep Medium/High/Low/Priority labels
const priorityLabels = currentLabels.filter(l => ['High', 'Medium', 'Low'].includes(l));
const allLabels = [...new Set([...suggestedLabels, ...priorityLabels])];
suggestions.push({
number: issue.number,
title: issue.title,
current: currentLabels,
suggested: allLabels,
changed: JSON.stringify(currentLabels.sort()) !== JSON.stringify(allLabels.sort())
});
}
// Display summary
console.log('📊 Label suggestions:\n');
for (const s of suggestions) {
if (s.changed) {
console.log(`✏️ Issue #${s.number}: ${s.title.substring(0, 50)}`);
console.log(` Current: ${s.current.join(', ') || '(none)'}`);
console.log(` Suggested: ${s.suggested.join(', ')}`);
console.log('');
}
}
const changedCount = suggestions.filter(s => s.changed).length;
console.log(`\n📈 ${changedCount}/${suggestions.length} issues need label updates`);
// Ask for confirmation
if (changedCount > 0) {
console.log('\n⚠️ To apply these labels, run: node apply-labels.js');
}
// Export for apply script
import('fs').then(({ writeFileSync }) => {
writeFileSync('label-suggestions.json', JSON.stringify(suggestions, null, 2));
});
}
main().catch(console.error);