diff --git a/src/action.ts b/src/action.ts index 16e381d0..dbc0ee01 100644 --- a/src/action.ts +++ b/src/action.ts @@ -110,11 +110,9 @@ export default async function main() { commits = await getCommits(previousTag.commit.sha, GITHUB_SHA); - const releaseRules = - mappedReleaseRules && - mappedReleaseRules.map( - (rule) => objectWithoutKeys(rule, ['section']) as ReleaseRule - ); + const releaseRules = mappedReleaseRules?.map( + (rule) => objectWithoutKeys(rule, ['section']) as ReleaseRule + ); let bump = await analyzeCommits( { releaseRules }, { commits, logger: { log: console.info.bind(console) } } diff --git a/src/ts.ts b/src/ts.ts index d094a22a..a3034bfa 100644 --- a/src/ts.ts +++ b/src/ts.ts @@ -30,5 +30,5 @@ export type ChangelogRule = { * Section in changelog to group commits by type. * Eg: 'Bug Fix', 'Features' etc. */ - section: string; + section?: string; }; diff --git a/tests/utils.test.ts b/tests/utils.test.ts index bfec2859..73f5cbf2 100644 --- a/tests/utils.test.ts +++ b/tests/utils.test.ts @@ -2,6 +2,7 @@ import * as utils from '../src/utils'; import { getValidTags } from '../src/utils'; import * as core from '@actions/core'; import * as github from '../src/github'; +import { defaultChangelogRules } from '../src/defaults'; jest.spyOn(core, 'debug').mockImplementation(() => {}); jest.spyOn(core, 'warning').mockImplementation(() => {}); @@ -140,7 +141,8 @@ describe('utils', () => { /* * Given */ - const customReleasesString = 'james:preminor,bond:premajor'; + const customReleasesString = + 'james:preminor,bond:premajor,007:major:Breaking Changes,feat:minor'; /* * When @@ -153,6 +155,12 @@ describe('utils', () => { expect(mappedReleases).toEqual([ { type: 'james', release: 'preminor' }, { type: 'bond', release: 'premajor' }, + { type: '007', release: 'major', section: 'Breaking Changes' }, + { + type: 'feat', + release: 'minor', + section: defaultChangelogRules['feat'].section, + }, ]); }); @@ -173,4 +181,73 @@ describe('utils', () => { expect(mappedReleases).toEqual([{ type: 'bond', release: 'premajor' }]); }); }); + + describe('method: mergeWithDefaultChangelogRules', () => { + it('combines non-existing type rules with default rules', () => { + /** + * Given + */ + const newRule = { + type: 'james', + release: 'major', + section: '007 Changes', + }; + + /** + * When + */ + const result = utils.mergeWithDefaultChangelogRules([newRule]); + + /** + * Then + */ + expect(result).toEqual([ + ...Object.values(defaultChangelogRules), + newRule, + ]); + }); + + it('overwrites existing default type rules with provided rules', () => { + /** + * Given + */ + const newRule = { + type: 'feat', + release: 'minor', + section: '007 Changes', + }; + + /** + * When + */ + const result = utils.mergeWithDefaultChangelogRules([newRule]); + const overWrittenRule = result.find((rule) => rule.type === 'feat'); + + /** + * Then + */ + expect(overWrittenRule?.section).toBe(newRule.section); + }); + + it('returns only the rules having changelog section', () => { + /** + * Given + */ + const mappedReleaseRules = [ + { type: 'james', release: 'major', section: '007 Changes' }, + { type: 'bond', release: 'minor', section: undefined }, + ]; + + /** + * When + */ + const result = utils.mergeWithDefaultChangelogRules(mappedReleaseRules); + + /** + * Then + */ + expect(result).toContainEqual(mappedReleaseRules[0]); + expect(result).not.toContainEqual(mappedReleaseRules[1]); + }); + }); });