Skip to content

Commit 91720df

Browse files
fix extractPrLinks function to pass unit tests
1 parent 93a67af commit 91720df

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

src/parse-changelog.ts

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -216,32 +216,55 @@ function extractPrLinks(changeEntry: string): {
216216
} {
217217
const lines = changeEntry.split('\n');
218218
const prNumbers: string[] = [];
219-
const cleanedLines: string[] = [];
220219

221-
for (const line of lines) {
222-
let cleanedLine = line;
220+
// Only process the first line for PR link extraction
221+
let firstLine = lines[0];
223222

224-
// Match and extract all long Markdown PR links: ([#123](...))
225-
const longMatches = [...cleanedLine.matchAll(/\[#(\d+)\]\([^)]+\)/gu)];
223+
// Example of long match group: " ([#123](...), [#456](...))"
224+
const longGroupMatchPattern = /\s+\(\s*(\[#\d+\]\([^)]+\)\s*,?\s*)+\)/gu;
225+
226+
// Example of long match: "[#123](...)"
227+
const longMatchPattern = /\[#(\d+)\]\([^)]+\)/gu;
228+
229+
// Match and extract all long PR links like ([#123](...)) separated by commas
230+
const longGroupMatches = [...firstLine.matchAll(longGroupMatchPattern)];
231+
for (const longGroupMatch of longGroupMatches) {
232+
const group = longGroupMatch[0];
233+
const longMatches = [...group.matchAll(longMatchPattern)];
226234
for (const match of longMatches) {
227235
prNumbers.push(match[1]);
228236
}
229-
cleanedLine = cleanedLine.replace(/[ ]?\(\[#\d+\]\([^)]+\)\)/gu, '');
237+
}
238+
239+
// Remove valid long PR links (grouped in parentheses, possibly comma-separated)
240+
firstLine = firstLine.replace(longGroupMatchPattern, '');
230241

231-
// Match and extract all short PR links: (#123)
232-
const shortMatches = [...cleanedLine.matchAll(/\(#(\d+)\)/gu)];
242+
// Example of short match group: " (#123), (#123)"
243+
const shortGroupMatchPattern = /\s*\(#\d+\)\s*,?\s*/gu;
244+
245+
// Example of short match: "(#123)"
246+
const shortMatchPattern = /\(#(\d+)\)/gu;
247+
248+
// Match and extract all short PR links like (#123)
249+
const shortGroupMatches = [...firstLine.matchAll(shortGroupMatchPattern)];
250+
for (const shortGroupMatch of shortGroupMatches) {
251+
const group = shortGroupMatch[0];
252+
const shortMatches = [...group.matchAll(shortMatchPattern)];
233253
for (const match of shortMatches) {
234254
prNumbers.push(match[1]);
235255
}
236-
cleanedLine = cleanedLine.replace(/[ ]?\(#\d+\)/gu, '');
237-
238-
cleanedLines.push(cleanedLine.trimEnd());
239256
}
240257

241-
const uniquePrNumbers = [...new Set(prNumbers)];
258+
// Remove valid short PR links
259+
firstLine = firstLine.replace(shortGroupMatchPattern, '');
260+
261+
// Prepare the cleaned description
262+
const cleanedLines = [firstLine.trim(), ...lines.slice(1)];
263+
const cleanedDescription = cleanedLines.join('\n');
242264

265+
// Return unique PR numbers and the cleaned description
243266
return {
244-
description: cleanedLines.join('\n').trim(),
245-
prNumbers: uniquePrNumbers,
267+
description: cleanedDescription.trim(),
268+
prNumbers: [...new Set(prNumbers)],
246269
};
247270
}

0 commit comments

Comments
 (0)