forked from nodejs/core-validate-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.js
101 lines (88 loc) · 2.43 KB
/
validator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import EE from 'node:events'
import Parser from 'gitlint-parser-node'
import BaseRule from './rule.js'
// Rules
import coAuthoredByIsTrailer from './rules/co-authored-by-is-trailer.js'
import fixesUrl from './rules/fixes-url.js'
import lineAfterTitle from './rules/line-after-title.js'
import lineLength from './rules/line-length.js'
import metadataEnd from './rules/metadata-end.js'
import prUrl from './rules/pr-url.js'
import reviewers from './rules/reviewers.js'
import subsystem from './rules/subsystem.js'
import titleFormat from './rules/title-format.js'
import titleLength from './rules/title-length.js'
const RULES = {
'co-authored-by-is-trailer': coAuthoredByIsTrailer,
'fixes-url': fixesUrl,
'line-after-title': lineAfterTitle,
'line-length': lineLength,
'metadata-end': metadataEnd,
'pr-url': prUrl,
reviewers,
subsystem,
'title-format': titleFormat,
'title-length': titleLength
}
export default class ValidateCommit extends EE {
constructor (options) {
super()
this.opts = Object.assign({
'validate-metadata': true
}, options)
this.messages = new Map()
this.errors = 0
this.rules = new Map()
this.loadBaseRules()
}
loadBaseRules () {
const keys = Object.keys(RULES)
for (const key of keys) {
this.rules.set(key, new BaseRule(RULES[key]))
}
if (!this.opts['validate-metadata']) {
this.disableRule('pr-url')
this.disableRule('reviewers')
this.disableRule('metadata-end')
}
}
disableRule (id) {
if (!this.rules.has(id)) {
throw new TypeError(`Invalid rule: "${id}"`)
}
this.rules.get(id).disabled = true
}
lint (str) {
if (Array.isArray(str)) {
for (const item of str) {
this.lint(item)
}
} else {
const commit = new Parser(str, this)
for (const rule of this.rules.values()) {
if (rule.disabled) continue
rule.validate(commit)
}
setImmediate(() => {
this.emit('commit', {
commit,
messages: this.messages.get(commit.sha) || []
})
})
}
}
report (opts) {
const commit = opts.commit
const sha = commit.sha
if (!sha) {
throw new Error('Invalid report. Missing commit sha')
}
if (opts.data.level === 'fail') { this.errors++ }
const ar = this.messages.get(sha) || []
ar.push(opts.data)
this.messages.set(sha, ar)
setImmediate(() => {
this.emit('message', opts)
})
}
}