From 5240b1e5298a2a0bef405100ab03636b491306c0 Mon Sep 17 00:00:00 2001 From: Pete Cook Date: Thu, 8 Nov 2018 16:02:51 +0000 Subject: [PATCH] Add --backfill-limit option Previously, releases without any merges or fixes were listed in the changelog as an empty release This will backfill empty releases with a list of commits up to a certain limit (3 by default) --- README.md | 1 + src/releases.js | 16 +++++++++++----- src/run.js | 2 ++ test/releases.js | 14 ++++++++++++++ 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3c704b6f..c6814ff5 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ Options: -v, --latest-version [version] # use specified version as latest release -u, --unreleased # include section for unreleased changes -l, --commit-limit [count] # number of commits to display per release, default: 3 + -b, --backfill-limit [count] # number of commits to backfill empty releases with, default: 3 -i, --issue-url [url] # override url for issues, use {id} for issue id --issue-pattern [regex] # override regex pattern for issues in commit messages --breaking-pattern [regex] # regex pattern for breaking change commits diff --git a/src/releases.js b/src/releases.js index dccd5986..d139fa22 100644 --- a/src/releases.js +++ b/src/releases.js @@ -17,7 +17,7 @@ export function parseReleases (commits, remote, latestVersion, options) { release.tag ? `${options.tagPrefix}${release.tag}` : 'HEAD', remote ), - commits: release.commits.sort(sortCommits), + commits: sliceCommits(release.commits.sort(sortCommits), options, release), major: !options.tagPattern && commit.tag && release.tag && semver.diff(commit.tag, release.tag) === 'major' }) } @@ -76,10 +76,7 @@ function filterCommit (commit, release, limit) { // Filter out commits with the same message as an existing merge return false } - if (limit === false) { - return true - } - return release.commits.length < limit + return true } function getCompareLink (from, to, remote) { @@ -110,3 +107,12 @@ function sortCommits (a, b) { if (a.breaking && !b.breaking) return 1 return (b.insertions + b.deletions) - (a.insertions + a.deletions) } + +function sliceCommits (commits, options, release) { + if (options.commitLimit === false) { + return commits + } + const emptyRelease = release.fixes.length === 0 && release.merges.length === 0 + const limit = emptyRelease ? options.backfillLimit : options.commitLimit + return commits.slice(0, limit) +} diff --git a/src/run.js b/src/run.js index fc7b1a1d..4e1c3fc5 100644 --- a/src/run.js +++ b/src/run.js @@ -13,6 +13,7 @@ const DEFAULT_OPTIONS = { template: 'compact', remote: 'origin', commitLimit: 3, + backfillLimit: 3, tagPrefix: '' } @@ -29,6 +30,7 @@ function getOptions (argv, pkg, dotOptions) { .option('-v, --latest-version [version]', 'use specified version as latest release') .option('-u, --unreleased', 'include section for unreleased changes') .option('-l, --commit-limit [count]', `number of commits to display per release, default: ${DEFAULT_OPTIONS.commitLimit}`, parseLimit) + .option('-b, --backfill-limit [count]', `number of commits to backfill empty releases with, default: ${DEFAULT_OPTIONS.backfillLimit}`, parseLimit) .option('-i, --issue-url [url]', 'override url for issues, use {id} for issue id') .option('--issue-pattern [regex]', 'override regex pattern for issues in commit messages') .option('--breaking-pattern [regex]', 'regex pattern for breaking change commits') diff --git a/test/releases.js b/test/releases.js index 84af0fc2..caf2b1d0 100644 --- a/test/releases.js +++ b/test/releases.js @@ -8,6 +8,7 @@ import { parseReleases, sortReleases } from '../src/releases' const options = { unreleased: false, commitLimit: 3, + backfillLimit: 3, tagPrefix: '' } @@ -20,6 +21,19 @@ describe('parseReleases', () => { expect(parseReleases(commits, remotes.github, null, { ...options, commitLimit: false })).to.deep.equal(releases) }) + it('parses releases with backfill limit', () => { + const releases = parseReleases(commits, remotes.github, null, { + ...options, + commitLimit: 0, + backfillLimit: 1 + }) + for (const release of releases) { + if (release.fixes.length === 0 && release.merges.length === 0) { + expect(release.commits.length).to.equal(1) + } + } + }) + it('parses releases with summary', () => { const releases = parseReleases(commits, remotes.bitbucket, null, { ...options, releaseSummary: true }) expect(releases[0].summary).to.equal('This is my major release description.\n\n- And a bullet point')