From 2f7c3abadcee1e47dc653fb3b60459b4e37ed4de Mon Sep 17 00:00:00 2001 From: Pete Cook Date: Sun, 23 May 2021 18:04:23 +0100 Subject: [PATCH] Add --hide-empty-releases Fixes https://github.com/cookpete/auto-changelog/issues/189 --- README.md | 1 + src/releases.js | 10 +++++++++- src/run.js | 1 + test/releases.js | 11 +++++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 180e2ef1..799b411c 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ Options: --sort-commits [property] # sort commits by property [relevance, date, date-desc, subject, subject-desc], default: relevance --release-summary # display tagged commit message body as release summary --unreleased-only # only output unreleased changes + --hide-empty-releases # hide empty releases --hide-credit # hide auto-changelog credit --handlebars-setup [file] # handlebars setup file --append-git-log [string] # string to append to git log command diff --git a/src/releases.js b/src/releases.js index e8c81898..2904fc2b 100644 --- a/src/releases.js +++ b/src/releases.js @@ -5,7 +5,7 @@ const MERGE_COMMIT_PATTERN = /^Merge (remote-tracking )?branch '.+'/ const COMMIT_MESSAGE_PATTERN = /\n+([\S\s]+)/ const parseReleases = async (tags, options, onParsed) => { - return Promise.all(tags.map(async tag => { + const releases = await Promise.all(tags.map(async tag => { const commits = await fetchCommits(tag.diff, options) const merges = commits.filter(commit => commit.merge).map(commit => commit.merge) const fixes = commits.filter(commit => commit.fixes).map(commit => ({ fixes: commit.fixes, commit })) @@ -27,6 +27,7 @@ const parseReleases = async (tags, options, onParsed) => { fixes } })) + return releases.filter(filterReleases(options)) } const filterCommits = merges => commit => { @@ -80,6 +81,13 @@ const getSummary = (message, { releaseSummary }) => { return null } +const filterReleases = options => ({ merges, fixes, commits }) => { + if (options.hideEmptyReleases && (merges.length + fixes.length + commits.length) === 0) { + return false + } + return true +} + module.exports = { parseReleases } diff --git a/src/run.js b/src/run.js index 29e1f718..9406d2a3 100644 --- a/src/run.js +++ b/src/run.js @@ -48,6 +48,7 @@ const getOptions = async argv => { .option('--sort-commits ', `sort commits by property [relevance, date, date-desc], default: ${DEFAULT_OPTIONS.sortCommits}`) .option('--release-summary', 'use tagged commit message body as release summary') .option('--unreleased-only', 'only output unreleased changes') + .option('--hide-empty-releases', 'hide empty releases') .option('--hide-credit', 'hide auto-changelog credit') .option('--handlebars-setup ', 'handlebars setup file') .option('--append-git-log ', 'string to append to git log command') diff --git a/test/releases.js b/test/releases.js index e8009503..f86ed99e 100644 --- a/test/releases.js +++ b/test/releases.js @@ -117,4 +117,15 @@ describe('parseReleases', () => { expect(releases[0].commits).to.have.lengthOf(1) expect(releases[0].commits[0]).to.include({ subject: 'First commit' }) }) + + it('hides empty releases', async () => { + const map = { + 'v1.0.0': [] + } + mock('fetchCommits', diff => Promise.resolve(map[diff])) + const options = { hideEmptyReleases: true } + const tags = [{ tag: 'v1.0.0', date: '2000-01-01', diff: 'v1.0.0' }] + const releases = await parseReleases(tags, options) + expect(releases).to.have.lengthOf(0) + }) })