-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathpr-body-cleaner.ts
More file actions
496 lines (402 loc) · 15.6 KB
/
Copy pathpr-body-cleaner.ts
File metadata and controls
496 lines (402 loc) · 15.6 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/**
* PR Body Cleaner
*
* Intelligently removes unnecessary content from PR bodies while preserving
* important technical details, code blocks, and CURSOR_SUMMARY blocks.
*/
/**
* Cleans a PR body by removing unnecessary content while preserving valuable information
* @param body The raw PR body text
* @returns Cleaned PR body with unnecessary content removed
*/
export function cleanPRBody(body: string): string {
if (!body || body.trim().length === 0) {
return body
}
let cleaned = body
// Step 1: Extract CURSOR_SUMMARY content and remove all HTML comment markers
cleaned = removeHTMLCommentsExceptCursorSummary(cleaned)
// Step 1.5: Remove Cursor Bugbot footer notes (may appear outside CURSOR_SUMMARY blocks)
cleaned = removeCursorBugbotFooters(cleaned)
// Step 2: Remove image/video markdown
cleaned = removeImageVideoMarkdown(cleaned)
// Step 3: Clean tables (remove if only images/videos)
cleaned = cleanTables(cleaned)
// Step 4: Clean external links (keep text, remove long URLs)
cleaned = cleanExternalLinks(cleaned)
// Step 5: Remove empty sections
cleaned = removeEmptySections(cleaned)
// Step 6: Remove minimal value sections
cleaned = removeMinimalValueSections(cleaned)
// Step 7: Remove redundant sections (screen captures, testing)
cleaned = removeRedundantSections(cleaned)
// Step 8: Remove redundant headers
cleaned = removeRedundantHeaders(cleaned)
// Step 9: Aggressive whitespace normalization (max 1 blank line)
cleaned = normalizeWhitespace(cleaned)
return cleaned
}
/**
* Extract CURSOR_SUMMARY content and remove all HTML comment markers
*/
function removeHTMLCommentsExceptCursorSummary(text: string): string {
// Extract CURSOR_SUMMARY content (without the comment markers)
const cursorSummaryPlaceholder = '___CURSOR_SUMMARY_PLACEHOLDER___'
const cursorSummaryRegex = /<!--\s*CURSOR_SUMMARY\s*-->([\s\S]*?)<!--\s*\/CURSOR_SUMMARY\s*-->/gi
const summaries: string[] = []
let matchIndex = 0
// Extract just the content between the markers
let textWithProtection = text.replace(cursorSummaryRegex, (match, content) => {
// Clean the content before storing: remove Cursor Bugbot footer notes
let cleanedContent = content.trim()
// Remove Cursor Bugbot footer notes (metadata lines)
cleanedContent = cleanedContent.replace(
/>\s*\[!NOTE\]\s*\n\s*>\s*<sup>\[Cursor Bugbot\].*?Configure \[here\].*?<\/sup>/gi,
'',
)
cleanedContent = cleanedContent.replace(
/>\s*<sup>Written by \[Cursor Bugbot\].*?Configure \[here\].*?<\/sup>/gi,
'',
)
cleanedContent = cleanedContent.replace(
/>\s*<sup>\[Cursor Bugbot\].*?is generating a summary.*?Configure \[here\].*?<\/sup>/gi,
'',
)
// Clean up any leftover "> " prefixes from blockquotes
cleanedContent = cleanedContent.replace(/^>\s*/gm, '')
summaries.push(cleanedContent.trim()) // Store cleaned content
return `${cursorSummaryPlaceholder}${matchIndex++}`
})
// Remove all other HTML comments
let previousTextWithProtection: string
do {
previousTextWithProtection = textWithProtection
textWithProtection = textWithProtection.replace(/<!--[\s\S]*?-->/g, '')
} while (textWithProtection !== previousTextWithProtection)
// Restore CURSOR_SUMMARY content (without comment markers and footers)
summaries.forEach((summary, index) => {
textWithProtection = textWithProtection.replace(`${cursorSummaryPlaceholder}${index}`, summary)
})
return textWithProtection
}
/**
* Remove Cursor Bugbot footer notes (metadata lines)
*/
function removeCursorBugbotFooters(text: string): string {
let cleaned = text
// Remove standalone footer notes (outside CURSOR_SUMMARY blocks)
// Pattern 1: > [!NOTE]\n> <sup>[Cursor Bugbot]...Configure [here]...</sup>
cleaned = cleaned.replace(
/>\s*\[!NOTE\]\s*\n\s*>\s*<sup>\[Cursor Bugbot\][\s\S]*?Configure \[here\][\s\S]*?<\/sup>\s*/gi,
'',
)
// Pattern 2: > <sup>Written by [Cursor Bugbot]...Configure [here]...</sup>
cleaned = cleaned.replace(/>\s*<sup>Written by \[Cursor Bugbot\][\s\S]*?Configure \[here\][\s\S]*?<\/sup>\s*/gi, '')
// Pattern 3: > <sup>[Cursor Bugbot]...is generating a summary...Configure [here]...</sup>
cleaned = cleaned.replace(
/>\s*<sup>\[Cursor Bugbot\][\s\S]*?is generating a summary[\s\S]*?Configure \[here\][\s\S]*?<\/sup>\s*/gi,
'',
)
// Pattern 4: Standalone note blocks with Cursor Bugbot (any variant)
cleaned = cleaned.replace(/>\s*\[!NOTE\]\s*\n\s*>\s*<sup>\[Cursor Bugbot\][\s\S]*?<\/sup>\s*/gi, '')
// Pattern 5: Any blockquote line containing Cursor Bugbot footer
cleaned = cleaned.replace(/>\s*<sup>\[Cursor Bugbot\][\s\S]*?<\/sup>\s*/gi, '')
return cleaned
}
/**
* Remove image and video markdown links
*/
function removeImageVideoMarkdown(text: string): string {
// Remove image markdown:  or 
let cleaned = text.replace(/!\[([^\]]*)\]\([^)]+\)/g, '')
// Remove video markdown patterns like [Screen Recording ...](url)
cleaned = cleaned.replace(/\[Screen Recording[^\]]*\]\([^)]+\)/gi, '')
cleaned = cleaned.replace(/\[Screen Recording[^\]]*\]\([^)]+\)/gi, '')
// Remove <img> tags
cleaned = cleaned.replace(/<img[^>]*>/gi, '')
// Remove video links that look like markdown
cleaned = cleaned.replace(/\[.*\.mov.*\]\([^)]+\)/gi, '')
cleaned = cleaned.replace(/\[.*\.mp4.*\]\([^)]+\)/gi, '')
return cleaned
}
/**
* Remove tables that only contain images/videos
*/
function cleanTables(text: string): string {
// Match table blocks - split into parts to avoid unsafe regex
const lines = text.split('\n')
const result: string[] = []
let inTable = false
let tableLines: string[] = []
for (let i = 0; i < lines.length; i++) {
const line = lines[i]
if (!line) {
result.push('')
continue
}
const isTableRow = line.trim().startsWith('|') && line.trim().endsWith('|')
if (isTableRow) {
if (!inTable) {
inTable = true
tableLines = []
}
tableLines.push(line)
} else {
if (inTable) {
// Process accumulated table
const tableMatch = tableLines.join('\n')
if (shouldRemoveTable(tableMatch)) {
// Skip this table
} else {
result.push(...tableLines)
}
inTable = false
tableLines = []
}
result.push(line)
}
}
// Handle table at end of text
if (inTable && tableLines.length > 0) {
const tableMatch = tableLines.join('\n')
if (!shouldRemoveTable(tableMatch)) {
result.push(...tableLines)
}
}
return result.join('\n')
}
function shouldRemoveTable(tableMatch: string): boolean {
// Remove empty tables (only separators like | --- | --- |)
const cleanedForEmpty = tableMatch.replace(/[\s|:-]/g, '')
if (cleanedForEmpty.length === 0) {
return true
}
// Check if table only contains image/video links or empty cells
const imageVideoPattern = /!\[|\]\([^)]+\)|Screen Recording|\.mov|\.mp4|\.png|\.jpg|\.jpeg|\.gif|\.webp/gi
const hasOnlyImagesOrVideos = imageVideoPattern.test(tableMatch)
const textPattern = /[a-zA-Z]{3,}/
const cleanedTable = tableMatch.replace(/!\[|\]\([^)]+\)|Screen Recording/gi, '')
const hasTextContent = textPattern.test(cleanedTable)
// If table only has images/videos and no substantial text, remove it
return hasOnlyImagesOrVideos && !hasTextContent
}
/**
* Clean external links - keep text but remove long URLs
*/
function cleanExternalLinks(text: string): string {
// Match markdown links: [text](url)
const linkRegex = /\[([^\]]+)\]\(([^)]+)\)/g
return text.replace(linkRegex, (match) => {
const linkMatch = match.match(/\[([^\]]+)\]\(([^)]+)\)/)
if (!linkMatch || !linkMatch[1] || !linkMatch[2]) {
return match
}
const linkText = linkMatch[1]
const url = linkMatch[2]
// Keep GitHub links (they're short and useful)
if (url.startsWith('https://github.com/') || url.startsWith('http://github.com/')) {
return match
}
// Keep relative links
if (url.startsWith('/') || url.startsWith('./') || url.startsWith('../')) {
return match
}
// Keep short URLs (< 50 chars)
if (url.length <= 50) {
return match
}
// For long URLs, keep only the text
return linkText
})
}
/**
* Remove empty sections (headers with no content or only whitespace)
*/
function removeEmptySections(text: string): string {
// Match sections: ## Header followed by content until next ## or end
const sectionRegex = /(##+\s+[^\n]+)\n([\s\S]*?)(?=\n##+\s+|$)/g
let cleaned = text
cleaned = cleaned.replace(sectionRegex, (match) => {
const sectionMatch = match.match(/(##+\s+[^\n]+)\n([\s\S]*?)(?=\n##+\s+|$)/)
if (!sectionMatch) {
return match
}
const content = sectionMatch[2] || ''
// Check if content is empty or only whitespace
const trimmedContent = content.trim()
if (trimmedContent.length === 0) {
return '' // Remove empty section
}
return match // Keep section with content
})
return cleaned
}
/**
* Remove minimal value sections (very short descriptions, redundant changes lists)
*/
function removeMinimalValueSections(text: string): string {
let cleaned = text
// Remove "## Description" sections that are very short and redundant
const descriptionRegex = /##\s+Description\s*\n([\s\S]*?)(?=\n## |$)/gi
cleaned = cleaned.replace(descriptionRegex, (match) => {
const sectionMatch = match.match(/##\s+Description\s*\n([\s\S]*?)(?=\n## |$)/i)
if (!sectionMatch) {
return match
}
const content = sectionMatch[1] || ''
const trimmedContent = content.trim()
// Remove if very short (< 50 chars) and doesn't add value
if (trimmedContent.length < 50) {
return ''
}
return match
})
// Remove "## Changes" sections that are just bullet lists without detail
const changesRegex = /##\s+Changes\s*\n([\s\S]*?)(?=\n## |$)/gi
cleaned = cleaned.replace(changesRegex, (match) => {
const sectionMatch = match.match(/##\s+Changes\s*\n([\s\S]*?)(?=\n## |$)/i)
if (!sectionMatch) {
return match
}
const content = sectionMatch[1] || ''
const trimmedContent = content.trim()
// Check if it's just a list of very short bullets
const lines = trimmedContent.split('\n').filter((line) => line.trim().length > 0)
const allShortBullets = lines.every((line) => {
const trimmed = line.trim()
return trimmed.startsWith('-') && trimmed.length < 80
})
// Remove if all bullets are very short (likely redundant with CURSOR_SUMMARY)
if (allShortBullets && lines.length < 5) {
return ''
}
return match
})
// Remove "## Implementation Details" that are redundant with CURSOR_SUMMARY
const implDetailsRegex = /##\s+Implementation Details\s*\n([\s\S]*?)(?=\n## |$)/gi
cleaned = cleaned.replace(implDetailsRegex, (match) => {
const sectionMatch = match.match(/##\s+Implementation Details\s*\n([\s\S]*?)(?=\n## |$)/i)
if (!sectionMatch) {
return match
}
const content = sectionMatch[1] || ''
const trimmedContent = content.trim()
// Remove if very short (< 100 chars) - likely redundant
if (trimmedContent.length < 100) {
return ''
}
return match
})
return cleaned
}
/**
* Remove redundant headers (headers with no meaningful content after them)
*/
function removeRedundantHeaders(text: string): string {
let cleaned = text
// Match sections and check if header should be removed
const sectionRegex = /(##+\s+[^\n]+)\n([\s\S]*?)(?=\n##+\s+|$)/g
cleaned = cleaned.replace(sectionRegex, (match) => {
const sectionMatch = match.match(/(##+\s+[^\n]+)\n([\s\S]*?)(?=\n##+\s+|$)/)
if (!sectionMatch) {
return match
}
const header = sectionMatch[1] || ''
const content = sectionMatch[2] || ''
const trimmedContent = content.trim()
// If header is followed immediately by CURSOR_SUMMARY-like content and nothing else
// Check if content starts with --- (CURSOR_SUMMARY marker pattern)
if (trimmedContent.startsWith('---') && trimmedContent.length < 200) {
// This might be redundant, but let's be conservative and keep it
return match
}
// Remove headers that are redundant (e.g., "## Description" when content is minimal)
const headerLower = header.toLowerCase()
if (headerLower.includes('description') && trimmedContent.length < 30) {
// Keep the content, remove the header
return trimmedContent
}
return match
})
return cleaned
}
/**
* Remove redundant sections
*/
function removeRedundantSections(text: string): string {
let cleaned = text
// Remove "Screen Captures" or "Screenshots" sections that only contain images
// Match: ## Screen Captures / ## Screenshots followed by content ending before next ##
const screenshotSectionRegex = /##\s+(Screen Captures|Screenshots|Screenshots?)\s*\n([\s\S]*?)(?=\n## |$)/gi
cleaned = cleaned.replace(screenshotSectionRegex, (match) => {
const sectionMatch = match.match(/##\s+(Screen Captures|Screenshots|Screenshots?)\s*\n([\s\S]*?)(?=\n## |$)/i)
if (!sectionMatch) {
return match
}
const content = sectionMatch[2] || ''
// If content only has images/videos/tables with images, remove it
const mediaPattern = /!\[|\]\([^)]+\)|Screen Recording|\.mov|\.mp4|<img|^\|/m
const hasOnlyMedia = mediaPattern.test(content)
const textPattern = /[a-zA-Z]{10,}/
const cleanedContent = content.replace(/!\[|\]\([^)]+\)|Screen Recording|\.mov|\.mp4|<img|^\|/gm, '')
const hasTextContent = textPattern.test(cleanedContent)
if (hasOnlyMedia && !hasTextContent) {
return ''
}
return match
})
// Remove "Testing" or "How Has This Been Tested?" sections aggressively
const testingSectionRegex = /##\s+(Testing|How Has This Been Tested\?)\s*\n([\s\S]*?)(?=\n## |$)/gi
cleaned = cleaned.replace(testingSectionRegex, (match) => {
const sectionMatch = match.match(/##\s+(Testing|How Has This Been Tested\?)\s*\n([\s\S]*?)(?=\n## |$)/i)
if (!sectionMatch) {
return match
}
const content = sectionMatch[2] || ''
const trimmedContent = content.trim()
// Remove if empty
if (trimmedContent.length === 0) {
return ''
}
// Remove if only checkboxes with no descriptions
const checkboxOnly = /^[\s-]*\[[ xX]\]/m.test(trimmedContent) && trimmedContent.length < 100
if (checkboxOnly) {
return ''
}
// Remove if content is just minimal phrases like "locally", "manually", "on simulator"
const minimalPhrases =
/^(locally|manually|on simulator|tested locally|tested manually|local|ios simulator|android sim)[\s.]*$/i
if (minimalPhrases.test(trimmedContent)) {
return ''
}
// Remove if content is very short (< 50 chars) and doesn't contain meaningful text
const meaningfulTextPattern = /[a-zA-Z]{10,}/
if (trimmedContent.length < 50 && !meaningfulTextPattern.test(trimmedContent)) {
return ''
}
// Keep if it has meaningful content (> 100 chars of actual text)
const textOnly = trimmedContent.replace(/[\s\-[\]xX]/g, '')
if (textOnly.length < 100) {
return ''
}
return match
})
return cleaned
}
/**
* Normalize whitespace - aggressive cleanup
*/
function normalizeWhitespace(text: string): string {
// Collapse multiple blank lines to max 1 consecutive blank line
let cleaned = text.replace(/\n{3,}/g, '\n\n')
// Trim trailing whitespace from lines
cleaned = cleaned
.split('\n')
.map((line) => line.trimEnd())
.join('\n')
// Remove leading/trailing blank lines
cleaned = cleaned.replace(/^\n+|\n+$/g, '')
// Remove blank lines immediately after headers
cleaned = cleaned.replace(/(##+\s+[^\n]+)\n\n+/g, '$1\n')
return cleaned
}