Skip to content

Commit

Permalink
fix(commit): the bang (!) indicates a breaking change
Browse files Browse the repository at this point in the history
A commit is considered a breaking (major) change when a bang  char is
encountered prior to the colon + subject and no prior commit notes are
found.

Ref: gh-5
  • Loading branch information
esatterwhite committed Feb 11, 2021
1 parent 32e43d8 commit 034b30d
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 4 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const templates = require('./lib/templates/index.js')
const commit = require('./lib/commit.js')
const constants = require('./lib/constants.js')
const now = new Date()
const year = now.getFullYear()
const day = String(now.getDate()).padStart(2, '0')
Expand All @@ -13,6 +14,7 @@ module.exports = {
, parserOpts: {
noteKeywords: ['BREAKING CHANGES', 'BREAKING CHANGE', 'BREAKING']
, headerPattern: /^(\w*)(?:\((.*)\))?!?: (.*)$/
, breakingHeaderPattern: constants.BREAKING_HEADER_REGEX
, headerCorrespondence: ['type', 'scope', 'subject']
, issuePrefixes: ['#', 'gh-']
, referenceActions: [
Expand Down
1 change: 1 addition & 0 deletions lib/commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function typeOf(type) {
function transform(commit) {
commit.type = typeOf(commit.type)
commit.shortHash = commit.hash.substring(0, 7)

for (const note of commit.notes) {
note.title = '**BREAKING CHANGES**'
}
Expand Down
5 changes: 5 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict'

module.exports = {
BREAKING_HEADER_REGEX: /^(\w*)(?:\((.*)\))?!: (.*)$/
}
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@
"extends": "logdna",
"parserOptions": {
"ecmaVersion": 2019
},
"rules": {
"no-unused-vars": [2, {
"vars": "all",
"args": "none",
"varsIgnorePattern": "^_$"
}]
}
},
"tap": {
Expand Down
9 changes: 5 additions & 4 deletions test/unit/commit/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ test('commit#transform', async (t) => {
'long': 'c644dd5aa62fa579bef2d793b6bc9d711f308a9b'
, 'short': 'c644dd5'
}
, notes: [{
title: '**BREAKING CHANGES**'
, text: 'This will break things'
}]
, hash: 'c644dd5aa62fa579bef2d793b6bc9d711f308a9b'
, type: 'feat'
, scope: 'core'
, notes: [{
title: 'Does not matter'
, text: 'BREAKING CHANGE: This will break things'
}]
, header: 'feat(core): this is a simple commit'
, references: [{
issue: '1000'
, raw: 'Ref: gh-1000'
Expand Down
2 changes: 2 additions & 0 deletions test/unit/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {test, threw} = require('tap')
const config = require('../../index.js')
const constants = require('../../lib/constants.js')

function sortByType(a, b) {
return a.type < b.type ? -1 : 1
Expand All @@ -16,6 +17,7 @@ test('semantic-release-config-logdna', async (t) => {
t.match(config.parserOpts, {
noteKeywords: ['BREAKING CHANGES', 'BREAKING CHANGE', 'BREAKING']
, headerPattern: /^(\w*)(?:\((.*)\))?!?: (.*)$/
, breakingHeaderPattern: constants.BREAKING_HEADER_REGEX
, headerCorrespondence: ['type', 'scope', 'subject']
, issuePrefixes: ['#', 'gh-']
, referenceActions: [
Expand Down
34 changes: 34 additions & 0 deletions test/unit/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict'

const {test, threw} = require('tap')
const constants = require('../../lib/constants.js')

test('constants', async (t) => {
t.strictEqual(Object.keys(constants).length, 1, 'expected number of constants')

t.match(constants, {
BREAKING_HEADER_REGEX: RegExp
})

t.test('BREAKING_HEADER_REGEX', async (t) => {
const {BREAKING_HEADER_REGEX} = constants
t.test('breaking change w/o scope', async (t) => {
const match = BREAKING_HEADER_REGEX.exec('test!: this is breaking')
const [_, type, scope, subject] = match
t.strictEqual(type, 'test', 'commit type')
t.strictEqual(scope, undefined, 'commit scope')
t.strictEqual(subject, 'this is breaking', 'commit subject')
})
t.test('breaking change w/ scope', async (t) => {
const match = BREAKING_HEADER_REGEX.exec('doc(readme)!: this is breaking')
const [_, type, scope, subject] = match
t.strictEqual(type, 'doc', 'commit type')
t.strictEqual(scope, 'readme', 'commit scope')
t.strictEqual(subject, 'this is breaking', 'commit subject')
})
t.test('non-breaking change', async (t) => {
const match = BREAKING_HEADER_REGEX.exec('doc(readme): this is breaking')
t.notOk(match, 'no match found')
})
})
}).catch(threw)

0 comments on commit 034b30d

Please sign in to comment.