Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(branchNameStrict)!: remove forward slashes from non-prefix part of branchName #32278

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/usage/configuration-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- prettier-ignore -->
!!! 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

Expand Down
16 changes: 16 additions & 0 deletions lib/workers/repository/updates/branch-name.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 11 additions & 2 deletions lib/workers/repository/updates/branch-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -125,6 +133,7 @@ export function generateBranchName(update: RenovateConfig): void {
}
update.branchName = cleanBranchName(
update.branchName,
update.branchPrefix!,
update.branchNameStrict,
);
}
Loading