Skip to content

feat: config based is-ignored overrides #595

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions @commitlint/is-ignored/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import semver from 'semver';

const WILDCARDS = [
export const WILDCARDS = [
c =>
c.match(
/^((Merge pull request)|(Merge (.*?) into (.*?)|(Merge branch (.*?)))(?:\r?\n)*$)/m
Expand All @@ -21,6 +21,32 @@ const WILDCARDS = [
c => c.match(/^Auto-merged (.*?) into (.*)/)
];

export default function isIgnored(commit = '') {
return WILDCARDS.some(w => w(commit));
export default function isIgnored(commit = '', opts = {}) {
let wildcards = [];
if (opts.ignoredMessages) {
if (opts.disableDefaultIgnoredMessages) {
wildcards = wildcards.concat(WILDCARDS);
}
if (!Array.isArray(opts.ignoredMessages)) {
throw new Error('ignoredMessages must be an Array');
}
opts.ignoredMessages.forEach(ignoreConfig => {
if (typeof ignoreConfig === 'function') {
wildcards.push(ignoreConfig);
} else if (ignoreConfig instanceof RegExp) {
wildcards.push(c => c.match(ignoreConfig));
} else if (typeof ignoreConfig === 'string') {
wildcards.push(c => c.match(new RegExp(ignoreConfig)));
} else {
throw new Error(
'ignoredMessage element must be a function, string or RegExp'
);
}
});
} else if (opts.disableDefaultIgnoredMessages) {
return false;
} else {
wildcards = WILDCARDS;
}
return wildcards.some(w => w(commit));
}
59 changes: 59 additions & 0 deletions @commitlint/is-ignored/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,62 @@ test('should return true for automatic merge commits', t => {
test('should return false for commits containing, but not starting, with merge branch', t => {
t.false(isIgnored('foo bar Merge branch xxx'));
});

test('should return false for ignored message if disableDefaultIgnoredMessages is true', t => {
t.false(
isIgnored('Auto-merged develop into master', {
disableDefaultIgnoredMessages: true
})
);
});

test('should return false for ignored message if custom ignoredMessages and disableDefaultIgnoredMessages is true', t => {
t.false(
isIgnored('Auto-merged develop into master', {
disableDefaultIgnoredMessages: true,
ignoredMessages: []
})
);
});

test('should throw error if ignoredMessages is not an array', t => {
const ignoredString = 'this should be ignored';
t.throws(
isIgnored(ignoredString, {
ignoredMessages: 'throws error'
})
);
});

test('should return true for custom ignoredMessages as function', t => {
const ignoredString = 'this should be ignored';
t.true(
isIgnored(ignoredString, {
ignoredMessages: [c => c === ignoredString]
})
);
});

test('should return true for custom ignoredMessages as RegExp', t => {
t.true(
isIgnored('Should Ignore', {
ignoredMessages: [/^should ignore$/i]
})
);
});

test('should return true for custom ignoredMessages as string', t => {
t.true(
isIgnored('Should Ignore', {
ignoredMessages: ['[sS]hould [iI]gnore']
})
);
});

test('should throw error if ignoredMessage value is not an RegExp, string or function', t => {
t.throws(
isIgnored('some commit message', {
ignoredMessages: [true]
})
);
});
2 changes: 1 addition & 1 deletion @commitlint/lint/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const buildCommitMesage = ({header, body, footer}) => {

export default async (message, rules = {}, opts = {}) => {
// Found a wildcard match, skip
if (isIgnored(message)) {
if (isIgnored(message, opts)) {
return {
valid: true,
errors: [],
Expand Down
28 changes: 28 additions & 0 deletions @commitlint/lint/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,34 @@ test('positive on ignored message and broken rule', async t => {
t.is(actual.input, 'Revert "some bogus commit"');
});

test('negative on ignored message, disabled ignored messages and broken rule', async t => {
const actual = await lint(
'Revert "some bogus commit"',
{
'type-empty': [2, 'never']
},
{
disableDefaultIgnoredMessages: true
}
);
t.false(actual.valid);
});

test('positive on custom ignored message and broken rule', async t => {
const ignoredMessage = 'some ignored custom message';
const actual = await lint(
ignoredMessage,
{
'type-empty': [2, 'never']
},
{
ignoredMessages: [c => c === ignoredMessage]
}
);
t.true(actual.valid);
t.is(actual.input, ignoredMessage);
});

test('positive on stub message and opts', async t => {
const actual = await lint(
'foo-bar',
Expand Down
12 changes: 10 additions & 2 deletions @commitlint/load/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ import resolveFrom from 'resolve-from';

const w = (a, b) => (Array.isArray(b) ? b : undefined);
const valid = input =>
pick(input, 'extends', 'rules', 'parserPreset', 'formatter');
pick(
input,
'extends',
'rules',
'parserPreset',
'formatter',
'ignoredMessages',
'disableDefaultIgnoredMessages'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like how long this name is, but I couldn't think of something more appropriate.

);

export default async (seed = {}, options = {cwd: process.cwd()}) => {
const loaded = await loadConfig(options.cwd, options.file);
Expand All @@ -17,7 +25,7 @@ export default async (seed = {}, options = {cwd: process.cwd()}) => {
const config = valid(merge(loaded.config, seed));
const opts = merge(
{extends: [], rules: {}, formatter: '@commitlint/format'},
pick(config, 'extends')
pick(config, 'extends', 'ignoredMessages', 'disableDefaultIgnoredMessages')
);

// Resolve parserPreset key
Expand Down
23 changes: 22 additions & 1 deletion docs/reference-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ type Config = {
* Rules to check against
*/
rules?: {[name: string]: Rule};
/*
* Custom list of Messages to Ignore, string values will be compiled as RegExp
*/
ignoredMessages?: Array<string | RegExp | string => boolean>;
/*
* If this is true we will not use any of the default is-ignored rules
*/
disableDefaultIgnoredMessages?: boolean;
}

const Configuration: Config = {
Expand All @@ -49,7 +57,20 @@ const Configuration: Config = {
*/
rules: {
'type-enum': [2, 'always', ['foo']]
}
},
/*
* These RegExp and functions are used to ignore messages that shouldn't be linted
*/
ignoredMessages: [
'^Entire Message to Ignore$',
/^(ci|github):/,
(commit) => commit === ''
],
/*
* If this is true then the default ignores like `Merge commit` are not ignored
* and will cause commitlint to fail
*/
disableDefaultIgnoredMessages: true
};

module.exports = Configuration;
Expand Down