From c41e345e499e282322cc0ff3d54a1d776a23851d Mon Sep 17 00:00:00 2001 From: Brad Adams <hi@breadadams.com> Date: Fri, 6 Dec 2024 10:38:56 +0100 Subject: [PATCH] feat: calculate `semanticCommitType` priority (#32069) Co-authored-by: Rhys Arkins <rhys@arkins.net> --- .../repository/updates/generate.spec.ts | 48 +++++++++++++++++++ lib/workers/repository/updates/generate.ts | 27 +++++++++++ 2 files changed, 75 insertions(+) diff --git a/lib/workers/repository/updates/generate.spec.ts b/lib/workers/repository/updates/generate.spec.ts index 3332212e32..6769da86bb 100644 --- a/lib/workers/repository/updates/generate.spec.ts +++ b/lib/workers/repository/updates/generate.spec.ts @@ -703,6 +703,54 @@ describe('workers/repository/updates/generate', () => { ); }); + it('calculates the highest priority semanticCommitType', () => { + const branch = [ + { + ...requiredDefaultOptions, + manager: 'some-manager', + depName: 'some-dep', + semanticCommits: 'enabled', + semanticCommitType: 'chore', + semanticCommitScope: 'package', + newValue: '1.2.0', + isSingleVersion: true, + newVersion: '1.2.0', + branchName: 'some-branch', + }, + { + ...requiredDefaultOptions, + manager: 'some-manager', + depName: 'some-dep', + semanticCommits: 'enabled', + semanticCommitType: 'feat', + semanticCommitScope: 'package', + newValue: '1.2.0', + isSingleVersion: true, + newVersion: '1.2.0', + branchName: 'some-branch', + }, + { + ...requiredDefaultOptions, + manager: 'some-manager', + depName: 'some-dep', + semanticCommits: 'enabled', + semanticCommitType: 'fix', + semanticCommitScope: 'package', + newValue: '1.2.0', + isSingleVersion: true, + newVersion: '1.2.0', + branchName: 'some-branch', + }, + ] satisfies BranchUpgradeConfig[]; + const res = generateBranchConfig(branch); + expect(res.prTitle).toBe( + 'feat(package): update dependency some-dep to v1.2.0', + ); + expect(res.commitMessage).toBe( + 'feat(package): update dependency some-dep to v1.2.0', + ); + }); + it('scopes monorepo commits', () => { const branch = [ { diff --git a/lib/workers/repository/updates/generate.ts b/lib/workers/repository/updates/generate.ts index 234e20e40a..f2637f2fb9 100644 --- a/lib/workers/repository/updates/generate.ts +++ b/lib/workers/repository/updates/generate.ts @@ -159,6 +159,9 @@ function compilePrTitle( logger.trace(`prTitle: ` + JSON.stringify(upgrade.prTitle)); } +// Sorted by priority, from low to high +const semanticCommitTypeByPriority = ['chore', 'ci', 'build', 'fix', 'feat']; + export function generateBranchConfig( upgrades: BranchUpgradeConfig[], ): BranchConfig { @@ -355,6 +358,30 @@ export function generateBranchConfig( releaseTimestamp: releaseTimestamp!, }; // TODO: fixme (#9666) + // Enable `semanticCommits` if one of the branches has it enabled + if ( + config.upgrades.some((upgrade) => upgrade.semanticCommits === 'enabled') + ) { + config.semanticCommits = 'enabled'; + // Calculate the highest priority `semanticCommitType` + let highestIndex = -1; + for (const upgrade of config.upgrades) { + if (upgrade.semanticCommits === 'enabled' && upgrade.semanticCommitType) { + const priorityIndex = semanticCommitTypeByPriority.indexOf( + upgrade.semanticCommitType, + ); + + if (priorityIndex > highestIndex) { + highestIndex = priorityIndex; + } + } + } + + if (highestIndex > -1) { + config.semanticCommitType = semanticCommitTypeByPriority[highestIndex]; + } + } + // Use templates to generate strings const commitMessage = compileCommitMessage(config); compilePrTitle(config, commitMessage); -- GitLab