Skip to content

Commit 6efd13b

Browse files
author
Brett Uglow
committed
feat(rules): add support for optional scopes
E.g. `fix: allow scope to be optional` is now a valid message
1 parent a6fcad9 commit 6efd13b

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

lib/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function validateMessage(firstLine, fullMsg) {
104104
}
105105

106106
let type = match[1];
107-
let scope = match[3];
107+
let scope = match[3]; // This may be undefined, because scope is optional. e.g. 'feat: foo' is perfectly fine
108108
let subject = match[4];
109109
let allowedScopesForType = SCOPE_OVERRIDES[type] ? SCOPE_OVERRIDES[type] : [];
110110

@@ -115,7 +115,7 @@ function validateMessage(firstLine, fullMsg) {
115115
return false;
116116
}
117117

118-
if (!SCOPES.length && !allowCustomScopes && allowedScopesForType.indexOf(scope) === -1) {
118+
if (scope && !SCOPES.length && !allowCustomScopes && allowedScopesForType.indexOf(scope) === -1) {
119119
commitError(`No valid scopes defined! Check your package.json and cz-customisable rules file and define the "scopes" there (or set allowCustomScopes to true)`);
120120
return false;
121121
}
@@ -125,7 +125,7 @@ function validateMessage(firstLine, fullMsg) {
125125
return false;
126126
}
127127

128-
if (!allowCustomScopes && allowedScopesForType.indexOf(scope) === -1) {
128+
if (scope && !allowCustomScopes && allowedScopesForType.indexOf(scope) === -1) {
129129
commitError(`"${scope}" is not allowed scope for a "${type}" commit!\nValid "${type}" scopes: ${allowedScopesForType.sort().join(', ')}`);
130130
return false;
131131
}
@@ -201,16 +201,16 @@ function appendIssueToCommit(lines, issue) {
201201

202202
function processCLI(commitMsgFileName, cb) {
203203
let incorrectLogFileName = commitMsgFileName.replace('COMMIT_EDITMSG', 'logs/incorrect-commit-msgs');
204-
let callback = cb || function() {}; // Used for testing
204+
let callback = cb || (() => {}); // Used for testing
205205

206206
appendBranchNameToCommitMessage = czConfig && czConfig.appendBranchNameToCommitMessage != undefined ? czConfig.appendBranchNameToCommitMessage : appendBranchNameToCommitMessage;
207207

208-
fs.readFile(commitMsgFileName, function(err, buffer) {
208+
fs.readFile(commitMsgFileName, (err, buffer) => {
209209
let lines = getLinesFromBuffer(buffer);
210210
let msg = lines.join('\n');
211211

212212
if (!validateMessage(lines[0], msg)) {
213-
fs.appendFile(incorrectLogFileName, msg + '\n', function() {
213+
fs.appendFile(incorrectLogFileName, msg + '\n', () => {
214214
process.exit(1);
215215
callback();
216216
});
@@ -223,10 +223,10 @@ function processCLI(commitMsgFileName, cb) {
223223
callback();
224224
} else {
225225
// If valid, add the issue/branch name to the last line
226-
exec('git rev-parse --abbrev-ref HEAD', function(err, stdout/* , stderr*/) {
226+
exec('git rev-parse --abbrev-ref HEAD', (err, stdout/* , stderr*/) => {
227227
let lastline = appendIssueToCommit(lines, getIssueFromBranch(stdout));
228228

229-
fs.appendFile(commitMsgFileName, lastline, function() {
229+
fs.appendFile(commitMsgFileName, lastline, () => {
230230
process.exit(0);
231231
callback();
232232
});

test/ccg.spec.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ describe('cz-customizable-ghooks', () => {
6060
{msg: 'docs(ccc): breaking change allowed\n\nBREAKING CHANGE: blah', expectedResult: false},
6161
{msg: 'fix(ccc): breaking change allowed\n\nBREAKING CHANGE: blah', expectedResult: true},
6262
{msg: 'feat(ccc): breaking change allowed\n\nBREAKING CHANGE: blah', expectedResult: true},
63+
{msg: 'docs: type with no scope is valid', expectedResult: true},
64+
{msg: 'fix(): but blank scopes are invalid', expectedResult: false},
6365
{msg: 'docs(dddd): but this is ok', expectedResult: true},
6466
{msg: 'feat(customScope): this ok', expectedResult: true},
6567
];
@@ -119,6 +121,8 @@ describe('cz-customizable-ghooks', () => {
119121
{msg: 'feat(customScope): this not ok', expectedResult: false},
120122
{msg: 'docs(custom): docs has an override scope', expectedResult: true},
121123
{msg: 'fix(merge): and so does fix', expectedResult: true},
124+
{msg: 'fix: and so does fix with no scope', expectedResult: true},
125+
{msg: 'fix(): but blank scopes are invalid', expectedResult: false},
122126
{msg: 'docs(invalidCustom): not a valid custom scope', expectedResult: false},
123127
];
124128

@@ -152,7 +156,6 @@ describe('cz-customizable-ghooks', () => {
152156
{value: 'fix', name: 'fix: A bug fix'},
153157
{value: 'docs', name: 'docs: Documentation only changes'},
154158
],
155-
156159
scopeOverrides: {
157160
fix: baseScopes,
158161
docs: baseScopes.concat({name: 'custom'}),
@@ -169,6 +172,8 @@ describe('cz-customizable-ghooks', () => {
169172
{msg: 'docs(custom): this has an override scope', expectedResult: true},
170173
{msg: 'feat(merge): no scopes for feature', expectedResult: false},
171174
{msg: 'docs(invalidCustom): not a valid custom scope', expectedResult: false},
175+
{msg: 'docs(): but blank scopes are invalid', expectedResult: false},
176+
{msg: 'docs: the scope is in fact optional', expectedResult: true},
172177
];
173178

174179
let consoleData = '';
@@ -451,6 +456,30 @@ describe('cz-customizable-ghooks', () => {
451456
});
452457

453458

459+
it('should try to append to the incorrect-log-file when the commit message is invalid()', (done) => {
460+
createCommitMessageFile('foo');
461+
462+
let fileNameThatIsAppended;
463+
let fileDataThatIsAppended;
464+
465+
revert2 = module.__set__({
466+
'validateMessage': () => false,
467+
'fs.appendFile': (name, data, callback) => {
468+
fileNameThatIsAppended = name;
469+
fileDataThatIsAppended = data;
470+
callback();
471+
},
472+
});
473+
474+
function cb() {
475+
assert(fileNameThatIsAppended.indexOf('test/COMMIT_MSG') !== -1);
476+
assert.equal(fileDataThatIsAppended, 'foo\n');
477+
done();
478+
}
479+
module.processCLI(commitMsgFileName, cb);
480+
});
481+
482+
454483
it('should display "Commit message is valid." when the commit message is valid', (done) => {
455484
createCommitMessageFile('feat(a): something');
456485

0 commit comments

Comments
 (0)