From f730ff5394d5213dcd64fdadf9abca6bef240163 Mon Sep 17 00:00:00 2001 From: MaronHatoum <98313426+MaronHatoum@users.noreply.github.com> Date: Mon, 4 Apr 2022 18:49:19 +0300 Subject: [PATCH] fix(platform/pr-body): refactor smartTruncate function (#14915) Co-authored-by: Rhys Arkins <rhys@arkins.net> --- .../utils/__snapshots__/pr-body.spec.ts.snap | 4 +- lib/modules/platform/utils/pr-body.ts | 38 +++++++++++-------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/lib/modules/platform/utils/__snapshots__/pr-body.spec.ts.snap b/lib/modules/platform/utils/__snapshots__/pr-body.spec.ts.snap index ebd9cd6f7b..e55cc69ffb 100644 --- a/lib/modules/platform/utils/__snapshots__/pr-body.spec.ts.snap +++ b/lib/modules/platform/utils/__snapshots__/pr-body.spec.ts.snap @@ -33,9 +33,7 @@ exports[`modules/platform/utils/pr-body .smartTruncate truncates to 1000 1`] = ` ### [\`v19.47.0\`](https://togithub.com/renovatebot/renovate/releases/19.47.0) -##### Features - -- **docker:** AWS ECR authentication support ([#​4497](htt +##### Feat </details> diff --git a/lib/modules/platform/utils/pr-body.ts b/lib/modules/platform/utils/pr-body.ts index 371452e4bd..c3647402ae 100644 --- a/lib/modules/platform/utils/pr-body.ts +++ b/lib/modules/platform/utils/pr-body.ts @@ -1,23 +1,31 @@ -const re = new RegExp(`### Release Notes.*### Configuration`, 'ms'); +const re = new RegExp( + `(?<preNotes>.*### Release Notes)(?<releaseNotes>.*)### Configuration(?<postNotes>.*)`, + 's' +); export function smartTruncate(input: string, len: number): string { if (input.length < len) { return input; } - const releaseNotesMatch = re.exec(input); - if (releaseNotesMatch) { - const divider = `\n\n</details>\n\n---\n\n### Configuration`; - const [releaseNotes] = releaseNotesMatch; - const nonReleaseNotesLength = - input.length - releaseNotes.length - divider.length; - const availableLength = len - nonReleaseNotesLength; - if (availableLength <= 0) { - return input.substring(0, len); - } - return input.replace( - releaseNotes, - releaseNotes.slice(0, availableLength) + divider + + const reMatch = re.exec(input); + if (!reMatch) { + return input.substring(0, len); + } + + const divider = `\n\n</details>\n\n---\n\n### Configuration`; + const preNotes = reMatch.groups?.preNotes ?? ''; + const releaseNotes = reMatch.groups?.releaseNotes ?? ''; + const postNotes = reMatch.groups?.postNotes ?? ''; + + const availableLength = + len - (preNotes.length + postNotes.length + divider.length); + + if (availableLength <= 0) { + return input.substring(0, len); + } else { + return ( + preNotes + releaseNotes.slice(0, availableLength) + divider + postNotes ); } - return input.substring(0, len); } -- GitLab