Skip to content

Commit 2434d71

Browse files
SimeonCmarionebl
authored andcommitted
feat: config based is-ignored overrides (#595)
* feat: config based is-ignored overrides * style: apply formatting * refactor: simplify configurable is-ignored
1 parent f7e4e68 commit 2434d71

File tree

6 files changed

+121
-8
lines changed

6 files changed

+121
-8
lines changed

@commitlint/is-ignored/src/index.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import semver from 'semver';
22

3-
const WILDCARDS = [
3+
export const WILDCARDS = [
44
c =>
55
c.match(
66
/^((Merge pull request)|(Merge (.*?) into (.*?)|(Merge branch (.*?)))(?:\r?\n)*$)/m
@@ -21,6 +21,25 @@ const WILDCARDS = [
2121
c => c.match(/^Auto-merged (.*?) into (.*)/)
2222
];
2323

24-
export default function isIgnored(commit = '') {
25-
return WILDCARDS.some(w => w(commit));
24+
export default function isIgnored(commit = '', opts = {}) {
25+
const ignores = typeof opts.ignores === 'undefined' ? [] : opts.ignores;
26+
27+
if (!Array.isArray(ignores)) {
28+
throw new Error(
29+
`ignores must be of type array, received ${ignores} of type ${typeof ignores}`
30+
);
31+
}
32+
33+
const invalids = ignores.filter(c => typeof c !== 'function');
34+
35+
if (invalids.length > 0) {
36+
throw new Error(
37+
`ignores must be array of type function, received items of type: ${invalids
38+
.map(i => typeof i)
39+
.join(', ')}`
40+
);
41+
}
42+
43+
const base = opts.defaults === false ? [] : WILDCARDS;
44+
return [...base, ...ignores].some(w => w(commit));
2645
}

@commitlint/is-ignored/src/index.test.js

+34
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,37 @@ test('should return true for automatic merge commits', t => {
117117
test('should return false for commits containing, but not starting, with merge branch', t => {
118118
t.false(isIgnored('foo bar Merge branch xxx'));
119119
});
120+
121+
test('should return false for ignored message if defaults is false', t => {
122+
t.false(
123+
isIgnored('Auto-merged develop into master', {
124+
defaults: false
125+
})
126+
);
127+
});
128+
129+
test('should return false for ignored message if custom ignores and defaults is false', t => {
130+
t.false(
131+
isIgnored('Auto-merged develop into master', {
132+
defaults: false
133+
})
134+
);
135+
});
136+
137+
test('should throw error if ignores is not an array', t => {
138+
const ignoredString = 'this should be ignored';
139+
t.throws(() => {
140+
isIgnored(ignoredString, {
141+
ignores: 'throws error'
142+
});
143+
});
144+
});
145+
146+
test('should return true for custom ignores as function', t => {
147+
const ignoredString = 'this should be ignored';
148+
t.true(
149+
isIgnored(ignoredString, {
150+
ignores: [c => c === ignoredString]
151+
})
152+
);
153+
});

@commitlint/lint/src/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ const buildCommitMesage = ({header, body, footer}) => {
1515

1616
export default async (message, rules = {}, opts = {}) => {
1717
// Found a wildcard match, skip
18-
if (isIgnored(message)) {
18+
if (
19+
isIgnored(message, {defaults: opts.defaultIgnores, ignores: opts.ignores})
20+
) {
1921
return {
2022
valid: true,
2123
errors: [],

@commitlint/lint/src/index.test.js

+28
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,34 @@ test('positive on ignored message and broken rule', async t => {
3838
t.is(actual.input, 'Revert "some bogus commit"');
3939
});
4040

41+
test('negative on ignored message, disabled ignored messages and broken rule', async t => {
42+
const actual = await lint(
43+
'Revert "some bogus commit"',
44+
{
45+
'type-empty': [2, 'never']
46+
},
47+
{
48+
defaultIgnores: false
49+
}
50+
);
51+
t.false(actual.valid);
52+
});
53+
54+
test('positive on custom ignored message and broken rule', async t => {
55+
const ignoredMessage = 'some ignored custom message';
56+
const actual = await lint(
57+
ignoredMessage,
58+
{
59+
'type-empty': [2, 'never']
60+
},
61+
{
62+
ignores: [c => c === ignoredMessage]
63+
}
64+
);
65+
t.true(actual.valid);
66+
t.is(actual.input, ignoredMessage);
67+
});
68+
4169
test('positive on stub message and opts', async t => {
4270
const actual = await lint(
4371
'foo-bar',

@commitlint/load/src/index.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,16 @@ import loadPlugin from './utils/loadPlugin';
88

99
const w = (a, b) => (Array.isArray(b) ? b : undefined);
1010
const valid = input =>
11-
pick(input, 'extends', 'plugins', 'rules', 'parserPreset', 'formatter');
11+
pick(
12+
input,
13+
'extends',
14+
'rules',
15+
'plugins',
16+
'parserPreset',
17+
'formatter',
18+
'ignores',
19+
'defaultIgnores'
20+
);
1221

1322
export default async (seed = {}, options = {cwd: process.cwd()}) => {
1423
const loaded = await loadConfig(options.cwd, options.file);
@@ -17,8 +26,8 @@ export default async (seed = {}, options = {cwd: process.cwd()}) => {
1726
// Merge passed config with file based options
1827
const config = valid(merge(loaded.config, seed));
1928
const opts = merge(
20-
{extends: [], plugins: [], rules: {}, formatter: '@commitlint/format'},
21-
pick(config, 'extends', 'plugins')
29+
{extends: [], rules: {}, formatter: '@commitlint/format'},
30+
pick(config, 'extends', 'plugins', 'ignores', 'defaultIgnores')
2231
);
2332

2433
// Resolve parserPreset key

docs/reference-configuration.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ type Config = {
2626
* Rules to check against
2727
*/
2828
rules?: {[name: string]: Rule};
29+
/*
30+
* Custom list of Messages to Ignore, string values will be compiled as RegExp
31+
*/
32+
ignoredMessages?: Array<string | RegExp | string => boolean>;
33+
/*
34+
* If this is true we will not use any of the default is-ignored rules
35+
*/
36+
disableDefaultIgnoredMessages?: boolean;
2937
}
3038

3139
const Configuration: Config = {
@@ -49,7 +57,20 @@ const Configuration: Config = {
4957
*/
5058
rules: {
5159
'type-enum': [2, 'always', ['foo']]
52-
}
60+
},
61+
/*
62+
* These RegExp and functions are used to ignore messages that shouldn't be linted
63+
*/
64+
ignoredMessages: [
65+
'^Entire Message to Ignore$',
66+
/^(ci|github):/,
67+
(commit) => commit === ''
68+
],
69+
/*
70+
* If this is true then the default ignores like `Merge commit` are not ignored
71+
* and will cause commitlint to fail
72+
*/
73+
disableDefaultIgnoredMessages: true
5374
};
5475

5576
module.exports = Configuration;

0 commit comments

Comments
 (0)