diff --git a/bin/cmd.js b/bin/cmd.js index bb0d394..841cb90 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -17,6 +17,7 @@ const knownOpts = { help: Boolean, version: Boolean, 'validate-metadata': Boolean, + 'validate-no-metadata': Boolean, tap: Boolean, out: path, list: Boolean, diff --git a/lib/rules/pr-url.js b/lib/rules/pr-url.js index e09af3d..f4e1706 100644 --- a/lib/rules/pr-url.js +++ b/lib/rules/pr-url.js @@ -10,6 +10,17 @@ export default { defaults: {}, options: {}, validate: (context, rule) => { + if (rule.options.expectsMissing) { + context.report({ + id, + message: 'Commit must not have a PR-URL.', + string: context.prUrl, + line: 0, + column: 0, + level: context.prUrl ? 'fail' : 'pass' + }) + return + } if (!context.prUrl) { context.report({ id, diff --git a/lib/rules/reviewers.js b/lib/rules/reviewers.js index 091a702..4c936fa 100644 --- a/lib/rules/reviewers.js +++ b/lib/rules/reviewers.js @@ -21,6 +21,16 @@ export default { return } + if (rule.options.expectsMissing) { + return context.report({ + id, + message: 'Commit must not have any reviewer.', + string: null, + line: 0, + column: 0, + level: parsed.reviewers.length ? 'fail' : 'pass' + }) + } if (!Array.isArray(parsed.reviewers) || !parsed.reviewers.length) { // See nodejs/node#5aac4c42da104c30d8f701f1042d61c2f06b7e6c // for an example diff --git a/lib/validator.js b/lib/validator.js index bbd26bf..464fe9d 100644 --- a/lib/validator.js +++ b/lib/validator.js @@ -47,7 +47,10 @@ export default class ValidateCommit extends EE { for (const key of keys) { this.rules.set(key, new BaseRule(RULES[key])) } - if (!this.opts['validate-metadata']) { + if (this.opts['validate-no-metadata']) { + this.rules.get('pr-url').options.expectsMissing = true + this.rules.get('reviewers').options.expectsMissing = true + } else if (!this.opts['validate-metadata']) { this.disableRule('pr-url') this.disableRule('reviewers') this.disableRule('metadata-end') diff --git a/test/rules/pr-url.js b/test/rules/pr-url.js index 20bddcd..2a36c19 100644 --- a/test/rules/pr-url.js +++ b/test/rules/pr-url.js @@ -21,7 +21,7 @@ test('rule: pr-url', (t) => { } } - Rule.validate(context) + Rule.validate(context, { options: {} }) }) t.test('invalid numeric', (tt) => { @@ -43,7 +43,7 @@ test('rule: pr-url', (t) => { } } - Rule.validate(context) + Rule.validate(context, { options: {} }) }) t.test('invalid', (tt) => { @@ -66,7 +66,7 @@ test('rule: pr-url', (t) => { } } - Rule.validate(context) + Rule.validate(context, { options: {} }) }) t.test('valid', (tt) => { @@ -89,7 +89,7 @@ test('rule: pr-url', (t) => { } } - Rule.validate(context) + Rule.validate(context, { options: {} }) }) t.test('valid URL containing hyphen', (tt) => { @@ -112,7 +112,7 @@ test('rule: pr-url', (t) => { } } - Rule.validate(context) + Rule.validate(context, { options: {} }) }) t.test('valid URL with trailing slash', (tt) => { @@ -135,7 +135,7 @@ test('rule: pr-url', (t) => { } } - Rule.validate(context) + Rule.validate(context, { options: {} }) }) t.end() diff --git a/test/rules/reviewers.js b/test/rules/reviewers.js index d76865a..1fb3ac8 100644 --- a/test/rules/reviewers.js +++ b/test/rules/reviewers.js @@ -30,7 +30,7 @@ This is a test` tt.equal(opts.level, 'fail', 'level') } - Rule.validate(context) + Rule.validate(context, { options: {} }) }) t.test('skip for release commit', (tt) => { @@ -58,7 +58,7 @@ This is a test` }) } - Rule.validate(context) + Rule.validate(context, { options: {} }) }) t.end() diff --git a/test/validator.js b/test/validator.js index d1c16d2..2a7f7d1 100644 --- a/test/validator.js +++ b/test/validator.js @@ -453,5 +453,35 @@ test('Validator - real commits', (t) => { }) }) + t.test('validate no metadata should report commits for having metadata', (tt) => { + const v = new Validator({ + 'validate-no-metadata': true + }) + v.lint(str) + v.on('commit', (data) => { + const msgs = data.messages + const filtered = msgs.filter((item) => { + return item.level === 'fail' + }) + tt.equal(filtered.length, 2, 'messages.length') + tt.end() + }) + }) + + t.test('validate no metadata should not report commits without metadata', (tt) => { + const v = new Validator({ + 'validate-no-metadata': true + }) + v.lint(str5) + v.on('commit', (data) => { + const msgs = data.messages + const filtered = msgs.filter((item) => { + return item.level === 'fail' + }) + tt.equal(filtered.length, 0, 'messages.length') + tt.end() + }) + }) + t.end() })