forked from nodejs/core-validate-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (68 loc) · 1.69 KB
/
index.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
'use strict'
const EE = require('events')
const Parser = require('gitlint-parser-node')
const BaseRule = require('./rule')
const RULES = require('./rules')
module.exports = 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: 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)
})
}
}