Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(options): add mergeCommitFilter option to cli (rebased) #913

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/conventional-changelog-cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ const cli = meow(`
If 0, the whole changelog will be regenerated and the outfile will be overwritten
Default: 1

-m, --merge-commit-filter Configure how to handle merge commits. Must be one of the following:
exclude: Merge commits will be ignored.
include: Merge commits will be included.
only-merges: Only merge commits will be processed.
Default: exclude

--skip-unstable If given, unstable tags will be skipped, e.g., x.x.x-alpha.1, x.x.x-rc.2

-u, --output-unreleased Output unreleased changelog
Expand Down Expand Up @@ -85,6 +91,10 @@ const cli = meow(`
'skip-unstable': {
type: 'boolean'
},
'merge-commit-filter': {
alias: 'm',
type: 'string'
},
'output-unreleased': {
alias: 'u',
type: 'boolean'
Expand Down Expand Up @@ -120,6 +130,7 @@ let sameFile = flags.sameFile
const append = flags.append
const releaseCount = flags.releaseCount
const skipUnstable = flags.skipUnstable
const mergeCommitFilter = flags.mergeCommitFilter || 'exclude'

if (infile && infile === outfile) {
sameFile = true
Expand All @@ -140,6 +151,7 @@ let options = _.omitBy({
append: append,
releaseCount: releaseCount,
skipUnstable: skipUnstable,
mergeCommitFilter: mergeCommitFilter,
outputUnreleased: flags.outputUnreleased,
lernaPackage: flags.lernaPackage,
tagPrefix: flags.tagPrefix
Expand Down Expand Up @@ -172,6 +184,15 @@ try {
}

const gitRawCommitsOpts = _.merge({}, config.gitRawCommitsOpts || {})

if (options.mergeCommitFilter === 'include') {
gitRawCommitsOpts.merges = null
} else if (options.mergeCommitFilter === 'only-merges') {
gitRawCommitsOpts.merges = true
} else { // default to 'exclude'
gitRawCommitsOpts.merges = false
}

if (flags.commitPath) gitRawCommitsOpts.path = flags.commitPath

const changelogStream = conventionalChangelog(options, templateContext, gitRawCommitsOpts, config.parserOpts, config.writerOpts)
Expand Down
25 changes: 24 additions & 1 deletion packages/standard-changelog/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ const cli = meow(`
-k, --pkg A filepath of where your package.json is located
-a, --append Should the generated block be appended
-r, --release-count How many releases to be generated from the latest
-m, --merge-commit-filter Configure how to handle merge commits. Must be one of the following:
exclude: Merge commits will be ignored.
include: Merge commits will be included.
only-merges: Only merge commits will be processed.
Default: exclude
-v, --verbose Verbose output
-c, --context A filepath of a json that is used to define template variables
-l, --lerna-package Generate a changelog for a specific lerna package (:[email protected])
Expand Down Expand Up @@ -64,6 +69,10 @@ const cli = meow(`
alias: 'r',
type: 'number'
},
'merge-commit-filter': {
alias: 'm',
type: 'string'
},
verbose: {
alias: 'v',
type: 'boolean'
Expand All @@ -89,6 +98,7 @@ const sameFile = flags.sameFile
const outfile = sameFile ? (flags.outfile || infile) : flags.outfile
const append = flags.append
const releaseCount = flags.firstRelease ? 0 : flags.releaseCount
const mergeCommitFilter = flags.mergeCommitFilter || 'exclude'

const options = _.omitBy({
preset: flags.preset,
Expand All @@ -97,6 +107,7 @@ const options = _.omitBy({
},
append: append,
releaseCount: releaseCount,
mergeCommitFilter: mergeCommitFilter,
lernaPackage: flags.lernaPackage
}, _.isUndefined)

Expand All @@ -123,7 +134,19 @@ try {
outputError(err)
}

const changelogStream = standardChangelog(options, templateContext, flags.commitPath ? { path: flags.commitPath } : {})
const gitRawCommitsOpts = {}

if (options.mergeCommitFilter === 'include') {
gitRawCommitsOpts.merges = null
} else if (options.mergeCommitFilter === 'only-merges') {
gitRawCommitsOpts.merges = true
} else { // default to 'exclude'
gitRawCommitsOpts.merges = false
}

if (flags.commitPath) gitRawCommitsOpts.path = flags.commitPath

const changelogStream = standardChangelog(options, templateContext, gitRawCommitsOpts)
.on('error', function (err) {
outputError(err)
})
Expand Down
78 changes: 78 additions & 0 deletions packages/standard-changelog/test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,84 @@ describe('standard-changelog cli', function () {
})
})

describe('generates changelog respecting --merge-commit-filter', function () {
function doGitFlow () {
shell.rm('-rf', 'CHANGELOG.md')
shell.exec('git tag -a v0.0.17 -m "old release"')
shell.exec('git checkout -b "feature/some-feature"')
shell.exec('> test.txt && git add test.txt')
shell.exec('git commit -m "feat: Test commit"')
shell.exec('git checkout master')
shell.exec('git merge feature/some-feature --no-ff')
shell.exec('git tag v0.0.18 -m "other old release"')
}

function undoGitFlow () {
shell.exec('git tag -d v0.0.17')
shell.exec('git tag -d v0.0.18')
shell.exec('git branch -d feature/some-feature')
shell.exec('git rm test.txt')
shell.exec('git checkout master')
}

it('include', function (done) {
doGitFlow()

const cp = spawn(process.execPath, [cliPath, '-m', 'include', '--first-release'], {
stdio: [process.stdin, null, null]
})

cp.on('close', function (code) {
expect(code).to.equal(0)
const modified = readFileSync('CHANGELOG.md', 'utf8')
expect(modified).to.include('First commit')
expect(modified).to.include('[0.0.17]')
expect(modified).to.include('Test commit')
expect(modified).to.include('[0.0.18]')
undoGitFlow()
done()
})
})

it('only-merges', function (done) {
doGitFlow()

const cp = spawn(process.execPath, [cliPath, '-m', 'only-merges', '--first-release'], {
stdio: [process.stdin, null, null]
})

cp.on('close', function (code) {
expect(code).to.equal(0)
const modified = readFileSync('CHANGELOG.md', 'utf8')
expect(modified).to.not.include('First commit')
expect(modified).to.not.include('[0.0.17]')
expect(modified).to.include('Test commit')
expect(modified).to.include('[0.0.18]')
undoGitFlow()
done()
})
})

it('exclude', function (done) {
doGitFlow()

const cp = spawn(process.execPath, [cliPath, '-m', 'exclude', '--first-release'], {
stdio: [process.stdin, null, null]
})

cp.on('close', function (code) {
expect(code).to.equal(0)
const modified = readFileSync('CHANGELOG.md', 'utf8')
expect(modified).to.include('First commit')
expect(modified).to.include('[0.0.17]')
expect(modified).to.include('Test commit') // will still be added under the current date but not tag
expect(modified).to.not.include('[0.0.18]')
undoGitFlow()
done()
})
})
})

it('outputs an error if context file is not found', function (done) {
let output = ''

Expand Down