Skip to content

Commit

Permalink
feat(conventionalcommits): allow matching scope (#669)
Browse files Browse the repository at this point in the history
Allow matching scope when generating changelog. For instance `chore(deps)` could be included in changelog under its own section while other `chore` is hidden.

Co-authored-by: Benjamin E. Coe <bencoe@google.com>
  • Loading branch information
ext and bcoe committed Nov 4, 2020
1 parent d15b90e commit e01e027
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
25 changes: 25 additions & 0 deletions packages/conventional-changelog-conventionalcommits/test/test.js
Expand Up @@ -33,6 +33,8 @@ betterThanBefore.setups([
gitDummyCommit('fix(*): oops')
gitDummyCommit(['fix(changelog): proper issue links', ' see GH-1'])
gitDummyCommit(['feat(awesome): adress EXAMPLE-1'])
gitDummyCommit(['chore(deps): upgrade example from 1 to 2'])
gitDummyCommit(['chore(release): release 0.0.0'])
},
function () {
gitDummyCommit(['feat(awesome): addresses the issue brought up in #133'])
Expand Down Expand Up @@ -175,6 +177,29 @@ describe('conventionalcommits.org preset', function () {
}))
})

it('should allow matching "scope" to configuration', function (done) {
preparing(1)
conventionalChangelogCore({
config: require('../')({
types: [
{ type: 'chore', scope: 'deps', section: 'Dependencies' }
]
})
})
.on('error', function (err) {
done(err)
})
.pipe(through(function (chunk) {
chunk = chunk.toString()

expect(chunk).to.include('### Dependencies')
expect(chunk).to.include('**deps:** upgrade example from 1 to 2')

expect(chunk).to.not.include('release 0.0.0')
done()
}))
})

it('should properly format external repository issues', function (done) {
preparing(1)
conventionalChangelogCore({
Expand Down
25 changes: 17 additions & 8 deletions packages/conventional-changelog-conventionalcommits/writer-opts.js
Expand Up @@ -54,18 +54,27 @@ module.exports = function (config) {
})
}

function findTypeEntry (types, commit) {
const typeKey = (commit.revert ? 'revert' : (commit.type || '')).toLowerCase()
return types.find((entry) => {
if (entry.type !== typeKey) {
return false
}
if (entry.scope && entry.scope !== commit.scope) {
return false
}
return true
})
}

function getWriterOpts (config) {
config = defaultConfig(config)
const typesLookup = {}
config.types.forEach(type => {
typesLookup[type.type] = type
})

return {
transform: (commit, context) => {
let discard = true
const issues = []
const typeKey = (commit.revert ? 'revert' : (commit.type || '')).toLowerCase()
const entry = findTypeEntry(config.types, commit)

// adds additional breaking change notes
// for the special case, test(system)!: hello world, where there is
Expand All @@ -78,10 +87,10 @@ function getWriterOpts (config) {
})

// breaking changes attached to any type are still displayed.
if (discard && (typesLookup[typeKey] === undefined ||
typesLookup[typeKey].hidden)) return
if (discard && (entry === undefined ||
entry.hidden)) return

if (typesLookup[typeKey]) commit.type = typesLookup[typeKey].section
if (entry) commit.type = entry.section

if (commit.scope === '*') {
commit.scope = ''
Expand Down

0 comments on commit e01e027

Please sign in to comment.