Repository navigation
Expand file tree
/
Copy pathcheck-duplicates.js
More file actions
221 lines (176 loc) · 6.59 KB
/
Copy pathcheck-duplicates.js
File metadata and controls
221 lines (176 loc) · 6.59 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
const fs = require('fs');
const path = require('path');
const github = require('@actions/github');
const { GoogleGenerativeAI } = require('@google/generative-ai');
function cosineSimilarity(vecA, vecB) {
let dotProduct = 0.0;
let normA = 0.0;
let normB = 0.0;
for (let i = 0; i < vecA.length; i++) {
dotProduct += vecA[i] * vecB[i];
normA += vecA[i] * vecA[i];
normB += vecB[i] * vecB[i];
}
if (normA === 0 || normB === 0) {
return 0;
}
return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
}
async function run() {
const geminiApiKey = process.env.GEMINI_API_KEY;
const githubToken = process.env.GITHUB_PAT || process.env.GITHUB_TOKEN;
if (!geminiApiKey) {
throw new Error('❌ Missing GEMINI_API_KEY environment variable.');
}
if (!githubToken) {
throw new Error('❌ Missing GITHUB_PAT or GITHUB_TOKEN environment variable.');
}
const octokit = github.getOctokit(githubToken);
const { owner, repo } = github.context.repo;
console.log(`🤖 Starting semantic scan for duplicates in ${owner}/${repo}...`);
const currentIssueNumber = github.context.payload.issue?.number;
// Prevent failures when executed from pull_request,
// workflow_dispatch, schedule, etc.
if (!currentIssueNumber) {
console.log('ℹ️ No issue payload detected. Skipping duplicate scan.');
return;
}
console.log('Fetching all open issues...');
const allIssues = await octokit.paginate(octokit.rest.issues.listForRepo, {
owner,
repo,
state: 'open',
per_page: 100,
});
const openIssues = allIssues.filter((issue) => !issue.pull_request);
console.log(`Found ${openIssues.length} open issues (excluding Pull Requests).`);
if (openIssues.length < 2) {
console.log('ℹ️ Not enough issues to compare. Ending scan.');
return;
}
const currentIssue = openIssues.find((issue) => issue.number === currentIssueNumber);
if (!currentIssue) {
console.log(`ℹ️ Triggering issue #${currentIssueNumber} is not open. Skipping scan.`);
return;
}
const genAI = new GoogleGenerativeAI(geminiApiKey);
const model = genAI.getGenerativeModel({
model: 'gemini-embedding-001',
});
const cacheDir = path.join(__dirname, '.cache');
const cacheFile = path.join(cacheDir, 'embeddings-cache.json');
let cache = {};
if (fs.existsSync(cacheFile)) {
try {
cache = JSON.parse(fs.readFileSync(cacheFile, 'utf8'));
console.log(`Loaded ${Object.keys(cache).length} cached embeddings.`);
} catch (error) {
console.warn('⚠️ Failed to parse embeddings cache, starting fresh:', error.message);
}
}
const currentText = `Title: ${currentIssue.title}\nBody: ${currentIssue.body || ''}`.slice(
0,
3000
);
let currentEmbedding;
const currentCacheKey = String(currentIssue.number);
if (cache[currentCacheKey] && cache[currentCacheKey].updated_at === currentIssue.updated_at) {
console.log(`Using cached embedding for current Issue #${currentIssue.number}`);
currentEmbedding = cache[currentCacheKey].embedding;
} else {
console.log(`Generating embedding for current Issue #${currentIssue.number}...`);
const currentResult = await model.embedContent(currentText);
currentEmbedding = currentResult.embedding.values;
cache[currentCacheKey] = {
updated_at: currentIssue.updated_at,
embedding: currentEmbedding,
};
}
const { data: existingComments } = await octokit.rest.issues.listComments({
owner,
repo,
issue_number: currentIssue.number,
});
const candidateIssues = openIssues.filter((issue) => issue.number !== currentIssue.number);
console.log(`Comparing against ${candidateIssues.length} existing issues...`);
let duplicatesCount = 0;
for (const candidateIssue of candidateIssues) {
const candKey = String(candidateIssue.number);
const candidateText =
`Title: ${candidateIssue.title}\nBody: ${candidateIssue.body || ''}`.slice(0, 3000);
let candidateEmbedding;
if (cache[candKey] && cache[candKey].updated_at === candidateIssue.updated_at) {
console.log(`Using cached embedding for Issue #${candidateIssue.number}`);
candidateEmbedding = cache[candKey].embedding;
} else {
console.log(`Generating embedding for Issue #${candidateIssue.number}...`);
try {
const candidateResult = await model.embedContent(candidateText);
candidateEmbedding = candidateResult.embedding.values;
cache[candKey] = {
updated_at: candidateIssue.updated_at,
embedding: candidateEmbedding,
};
} catch (error) {
console.warn(
`⚠️ Failed to generate embedding for Issue #${candidateIssue.number}:`,
error.message
);
continue;
}
}
const similarity = cosineSimilarity(currentEmbedding, candidateEmbedding);
if (similarity < 0.85) {
continue;
}
const similarityPercent = (similarity * 100).toFixed(1);
console.log(
`⚠️ Possible duplicate: #${currentIssue.number} ↔ #${candidateIssue.number} (${similarityPercent}%)`
);
const duplicateMessage = `My semantic scan detected that this issue might be a duplicate of #${candidateIssue.number}`;
const alreadyFlagged = existingComments.some(
(comment) => comment.body && comment.body.includes(duplicateMessage)
);
if (alreadyFlagged) {
continue;
}
const author = currentIssue.user.login;
await octokit.rest.issues.createComment({
owner,
repo,
issue_number: currentIssue.number,
body:
`Hey @${author}! 🤖\n\n` +
`${duplicateMessage} (Similarity: **${similarityPercent}%**).\n\n` +
'Please review both issues and close this one if it is a duplicate.',
});
try {
await octokit.rest.issues.addLabels({
owner,
repo,
issue_number: currentIssue.number,
labels: ['possible-duplicate'],
});
} catch (error) {
console.warn(`⚠️ Failed to add label to Issue #${currentIssue.number}:`, error.message);
}
duplicatesCount++;
}
console.log(
`🎉 Semantic duplicate scan complete! Flagged ${duplicatesCount} potential duplicates.`
);
// Save cache back to file
try {
if (!fs.existsSync(cacheDir)) {
fs.mkdirSync(cacheDir, { recursive: true });
}
fs.writeFileSync(cacheFile, JSON.stringify(cache, null, 2), 'utf8');
console.log(`Saved ${Object.keys(cache).length} embeddings to cache.`);
} catch (error) {
console.warn('⚠️ Failed to save embeddings cache:', error.message);
}
}
run().catch((error) => {
console.error('❌ Execution failed:', error.message);
process.exit(1);
});