Skip to content

Commit fbeda20

Browse files
committed
feat: add --validate-no-metadata flag
Useful to ensure the commit message doesn't include metadata that should be added by automation (PR-URL and reviewers).
1 parent 4556b7c commit fbeda20

File tree

7 files changed

+64
-9
lines changed

7 files changed

+64
-9
lines changed

bin/cmd.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const knownOpts = {
1717
help: Boolean,
1818
version: Boolean,
1919
'validate-metadata': Boolean,
20+
'validate-no-metadata': Boolean,
2021
tap: Boolean,
2122
out: path,
2223
list: Boolean,

lib/rules/pr-url.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ export default {
1010
defaults: {},
1111
options: {},
1212
validate: (context, rule) => {
13+
if (rule.options.expectsMissing) {
14+
context.report({
15+
id,
16+
message: 'Commit must not have a PR-URL.',
17+
string: context.prUrl,
18+
line: 0,
19+
column: 0,
20+
level: context.prUrl ? 'fail' : 'pass'
21+
})
22+
return
23+
}
1324
if (!context.prUrl) {
1425
context.report({
1526
id,

lib/rules/reviewers.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ export default {
2121
return
2222
}
2323

24+
if (rule.options.expectsMissing) {
25+
return context.report({
26+
id,
27+
message: 'Commit must not have any reviewer.',
28+
string: null,
29+
line: 0,
30+
column: 0,
31+
level: parsed.reviewers.length ? 'fail' : 'pass'
32+
})
33+
}
2434
if (!Array.isArray(parsed.reviewers) || !parsed.reviewers.length) {
2535
// See nodejs/node#5aac4c42da104c30d8f701f1042d61c2f06b7e6c
2636
// for an example

lib/validator.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ export default class ValidateCommit extends EE {
4747
for (const key of keys) {
4848
this.rules.set(key, new BaseRule(RULES[key]))
4949
}
50-
if (!this.opts['validate-metadata']) {
50+
if (this.opts['validate-no-metadata']) {
51+
this.rules.get('pr-url').options.expectsMissing = true
52+
this.rules.get('reviewers').options.expectsMissing = true
53+
} else if (!this.opts['validate-metadata']) {
5154
this.disableRule('pr-url')
5255
this.disableRule('reviewers')
5356
this.disableRule('metadata-end')

test/rules/pr-url.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ test('rule: pr-url', (t) => {
2121
}
2222
}
2323

24-
Rule.validate(context)
24+
Rule.validate(context, { options: {} })
2525
})
2626

2727
t.test('invalid numeric', (tt) => {
@@ -43,7 +43,7 @@ test('rule: pr-url', (t) => {
4343
}
4444
}
4545

46-
Rule.validate(context)
46+
Rule.validate(context, { options: {} })
4747
})
4848

4949
t.test('invalid', (tt) => {
@@ -66,7 +66,7 @@ test('rule: pr-url', (t) => {
6666
}
6767
}
6868

69-
Rule.validate(context)
69+
Rule.validate(context, { options: {} })
7070
})
7171

7272
t.test('valid', (tt) => {
@@ -89,7 +89,7 @@ test('rule: pr-url', (t) => {
8989
}
9090
}
9191

92-
Rule.validate(context)
92+
Rule.validate(context, { options: {} })
9393
})
9494

9595
t.test('valid URL containing hyphen', (tt) => {
@@ -112,7 +112,7 @@ test('rule: pr-url', (t) => {
112112
}
113113
}
114114

115-
Rule.validate(context)
115+
Rule.validate(context, { options: {} })
116116
})
117117

118118
t.test('valid URL with trailing slash', (tt) => {
@@ -135,7 +135,7 @@ test('rule: pr-url', (t) => {
135135
}
136136
}
137137

138-
Rule.validate(context)
138+
Rule.validate(context, { options: {} })
139139
})
140140

141141
t.end()

test/rules/reviewers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ This is a test`
3030
tt.equal(opts.level, 'fail', 'level')
3131
}
3232

33-
Rule.validate(context)
33+
Rule.validate(context, { options: {} })
3434
})
3535

3636
t.test('skip for release commit', (tt) => {
@@ -58,7 +58,7 @@ This is a test`
5858
})
5959
}
6060

61-
Rule.validate(context)
61+
Rule.validate(context, { options: {} })
6262
})
6363

6464
t.end()

test/validator.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,5 +453,35 @@ test('Validator - real commits', (t) => {
453453
})
454454
})
455455

456+
t.test('validate no metadata should report commits for having metadata', (tt) => {
457+
const v = new Validator({
458+
'validate-no-metadata': true
459+
})
460+
v.lint(str)
461+
v.on('commit', (data) => {
462+
const msgs = data.messages
463+
const filtered = msgs.filter((item) => {
464+
return item.level === 'fail'
465+
})
466+
tt.equal(filtered.length, 2, 'messages.length')
467+
tt.end()
468+
})
469+
})
470+
471+
t.test('validate no metadata should not report commits without metadata', (tt) => {
472+
const v = new Validator({
473+
'validate-no-metadata': true
474+
})
475+
v.lint(str5)
476+
v.on('commit', (data) => {
477+
const msgs = data.messages
478+
const filtered = msgs.filter((item) => {
479+
return item.level === 'fail'
480+
})
481+
tt.equal(filtered.length, 0, 'messages.length')
482+
tt.end()
483+
})
484+
})
485+
456486
t.end()
457487
})

0 commit comments

Comments
 (0)