feat: calculate semanticCommitType priority (#32069)

Co-authored-by: Rhys Arkins <rhys@arkins.net>
This commit is contained in:
Brad Adams 2024-12-06 10:38:56 +01:00 committed by GitHub
parent 6690a6ec34
commit c41e345e49
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 75 additions and 0 deletions

View file

@ -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 = [
{

View file

@ -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);