-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove-self-comments.ts
More file actions
95 lines (77 loc) · 3.24 KB
/
remove-self-comments.ts
File metadata and controls
95 lines (77 loc) · 3.24 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
import axios from 'axios';
import fs from 'fs';
const GITHUB_TOKEN = "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"
if (!GITHUB_TOKEN) {
throw new Error('GITHUB_TOKEN environment variable is not set');
}
interface Bug {
repo: string;
prNumber: number;
commentAuthor: string;
prAuthor?: string;
commentLink: string;
commentBody: string;
[key: string]: any;
}
async function getPRAuthor(repo: string, prNumber: number): Promise<string> {
try {
const response = await axios.get(`https://api.github.com/repos/${repo}/pulls/${prNumber}`, {
headers: {
'Authorization': `Bearer ${GITHUB_TOKEN}`,
'Accept': 'application/vnd.github+json'
}
});
return response.data.user.login;
} catch (error) {
console.error(`Error fetching PR author for ${repo}#${prNumber}:`, error);
return '';
}
}
async function main() {
console.log('Starting to process bugs.json...');
// Read bugs.json
const bugsData = JSON.parse(fs.readFileSync('bugs.json', 'utf8'));
const bugs: Bug[] = Array.isArray(bugsData) ? bugsData : Object.values(bugsData);
console.log(`Total bugs before filtering: ${bugs.length}`);
// Process bugs in batches to avoid rate limiting
const batchSize = 10;
const filteredBugs: Bug[] = [];
let selfCommentCount = 0;
for (let i = 0; i < bugs.length; i += batchSize) {
const batch = bugs.slice(i, i + batchSize);
// Process each bug in the batch
await Promise.all(batch.map(async (bug: Bug) => {
// Get PR author if not already present
if (!bug.prAuthor) {
bug.prAuthor = await getPRAuthor(bug.repo, bug.prNumber);
}
// Only include if not a self-comment
if (bug.commentAuthor !== bug.prAuthor) {
filteredBugs.push(bug);
} else {
selfCommentCount++;
console.log(`Found self-comment in ${bug.repo}#${bug.prNumber}:`);
console.log(` PR Author: ${bug.prAuthor}`);
console.log(` Comment Author: ${bug.commentAuthor}`);
console.log(` Comment: ${bug.commentBody.slice(0, 100)}...`);
console.log(` Link: ${bug.commentLink}\n`);
}
}));
console.log(`Processed ${Math.min(i + batchSize, bugs.length)}/${bugs.length} bugs...`);
}
console.log('\nSummary:');
console.log(`Total bugs: ${bugs.length}`);
console.log(`Self-comments found: ${selfCommentCount}`);
console.log(`Remaining bugs: ${filteredBugs.length}`);
// Backup original file
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
fs.copyFileSync('bugs.json', `bugs.backup.${timestamp}.json`);
console.log(`\nCreated backup at bugs.backup.${timestamp}.json`);
// Write filtered data back to bugs.json
const outputData = Array.isArray(bugsData)
? filteredBugs
: Object.fromEntries(filteredBugs.map((bug, index) => [index.toString(), bug]));
fs.writeFileSync('bugs.json', JSON.stringify(outputData, null, 2));
console.log('Updated bugs.json with self-comments removed');
}
main().catch(console.error);