Skip to content

Commit

Permalink
feat: add --validate-no-metadata flag
Browse files Browse the repository at this point in the history
Useful to ensure the commit message doesn't include metadata that should
be added by automation (PR-URL and reviewers).
  • Loading branch information
aduh95 committed Sep 2, 2024
1 parent 4556b7c commit fbeda20
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 9 deletions.
1 change: 1 addition & 0 deletions bin/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const knownOpts = {
help: Boolean,
version: Boolean,
'validate-metadata': Boolean,
'validate-no-metadata': Boolean,
tap: Boolean,
out: path,
list: Boolean,
Expand Down
11 changes: 11 additions & 0 deletions lib/rules/pr-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions lib/rules/reviewers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
12 changes: 6 additions & 6 deletions test/rules/pr-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test('rule: pr-url', (t) => {
}
}

Rule.validate(context)
Rule.validate(context, { options: {} })
})

t.test('invalid numeric', (tt) => {
Expand All @@ -43,7 +43,7 @@ test('rule: pr-url', (t) => {
}
}

Rule.validate(context)
Rule.validate(context, { options: {} })
})

t.test('invalid', (tt) => {
Expand All @@ -66,7 +66,7 @@ test('rule: pr-url', (t) => {
}
}

Rule.validate(context)
Rule.validate(context, { options: {} })
})

t.test('valid', (tt) => {
Expand All @@ -89,7 +89,7 @@ test('rule: pr-url', (t) => {
}
}

Rule.validate(context)
Rule.validate(context, { options: {} })
})

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

Rule.validate(context)
Rule.validate(context, { options: {} })
})

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

Rule.validate(context)
Rule.validate(context, { options: {} })
})

t.end()
Expand Down
4 changes: 2 additions & 2 deletions test/rules/reviewers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -58,7 +58,7 @@ This is a test`
})
}

Rule.validate(context)
Rule.validate(context, { options: {} })
})

t.end()
Expand Down
30 changes: 30 additions & 0 deletions test/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

0 comments on commit fbeda20

Please sign in to comment.