From b178d5d8ef8a8c769b082a2ec59f6809bebd46f0 Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Fri, 1 Nov 2024 14:49:26 +0100 Subject: [PATCH 1/2] don't apply strict checks to branchPrefix --- docs/usage/configuration-options.md | 7 ++++++- lib/workers/repository/updates/branch-name.ts | 11 ++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/docs/usage/configuration-options.md b/docs/usage/configuration-options.md index fa203a61a394a1..4c890092e8ddb1 100644 --- a/docs/usage/configuration-options.md +++ b/docs/usage/configuration-options.md @@ -394,7 +394,12 @@ If `true`, Renovate removes special characters when slugifying the branch name: - only alphabetic characters are allowed - hyphens `-` are used to separate sections -The default `false` behavior will mean that special characters like `.` may end up in the branch name. +The default `false` behavior will mean that special characters like `.` and `/` may end up in the branch name. + + +!!! note + Renovate will not apply any search/replace to the `branchPrefix` part of the branch name. + If you don't want any `/` in your branch name then you will also need to change `branchPrefix` from the default `renovate/` value to something like `renovate-`. ## branchPrefix diff --git a/lib/workers/repository/updates/branch-name.ts b/lib/workers/repository/updates/branch-name.ts index bc42966fd0ded7..dc43d5fc5bd42c 100644 --- a/lib/workers/repository/updates/branch-name.ts +++ b/lib/workers/repository/updates/branch-name.ts @@ -26,12 +26,20 @@ const RE_SPECIAL_CHARS_STRICT = regEx(/[`~!@#$%^&*()_=+[\]\\|{};':",.<>?]/g); */ function cleanBranchName( branchName: string, + branchPrefix: string, branchNameStrict?: boolean, ): string { let cleanedBranchName = branchName; + let existingBranchPrefix = ''; if (branchNameStrict) { - cleanedBranchName = cleanedBranchName.replace(RE_SPECIAL_CHARS_STRICT, '-'); // massage out all special characters that slip through slugify + if (cleanedBranchName.startsWith(branchPrefix)) { + existingBranchPrefix = branchPrefix; + cleanedBranchName = cleanedBranchName.slice(branchPrefix.length); + } + cleanedBranchName = + existingBranchPrefix + + cleanedBranchName.replace(RE_SPECIAL_CHARS_STRICT, '-'); // massage out all special characters that slip through slugify } return cleanGitRef @@ -125,6 +133,7 @@ export function generateBranchName(update: RenovateConfig): void { } update.branchName = cleanBranchName( update.branchName, + update.branchPrefix!, update.branchNameStrict, ); } From 653347461998e52aac76d4ad8e955bcba666083b Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Fri, 1 Nov 2024 14:54:59 +0100 Subject: [PATCH 2/2] sanitize forward slash in non-prefix part --- .../repository/updates/branch-name.spec.ts | 16 ++++++++++++++++ lib/workers/repository/updates/branch-name.ts | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/workers/repository/updates/branch-name.spec.ts b/lib/workers/repository/updates/branch-name.spec.ts index 297d7012bcd4af..a4b4dd5993954e 100644 --- a/lib/workers/repository/updates/branch-name.spec.ts +++ b/lib/workers/repository/updates/branch-name.spec.ts @@ -213,6 +213,22 @@ describe('workers/repository/updates/branch-name', () => { expect(upgrade.branchName).toBe('renovate/jest-42-x'); }); + it('removes slashes from the non-suffix part', () => { + const upgrade: RenovateConfig = { + branchNameStrict: true, + branchName: + '{{{branchPrefix}}}{{{additionalBranchPrefix}}}{{{branchTopic}}}', + branchTopic: + '{{{depNameSanitized}}}-{{{newMajor}}}{{#if isPatch}}.{{{newMinor}}}{{/if}}.x{{#if isLockfileUpdate}}-lockfile{{/if}}', + branchPrefix: 'renovate/', + depNameSanitized: '@foo/jest', + newMajor: '42', + group: {}, + }; + generateBranchName(upgrade); + expect(upgrade.branchName).toBe('renovate/foo-jest-42-x'); + }); + it('hashedBranchLength hashing', () => { const upgrade: RenovateConfig = { branchName: diff --git a/lib/workers/repository/updates/branch-name.ts b/lib/workers/repository/updates/branch-name.ts index dc43d5fc5bd42c..d151c0772b8c24 100644 --- a/lib/workers/repository/updates/branch-name.ts +++ b/lib/workers/repository/updates/branch-name.ts @@ -11,7 +11,7 @@ const MIN_HASH_LENGTH = 6; const RE_MULTIPLE_DASH = regEx(/--+/g); -const RE_SPECIAL_CHARS_STRICT = regEx(/[`~!@#$%^&*()_=+[\]\\|{};':",.<>?]/g); +const RE_SPECIAL_CHARS_STRICT = regEx(/[`~!@#$%^&*()_=+[\]\\|{};':",.<>?/]/g); /** * Clean git branch name