-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
140 lines (122 loc) · 3.03 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
'use strict'
const headingRE = /([^\:]+):([\s]+)(.*)/
module.exports = class Parser {
constructor(str, validator) {
if (typeof str !== 'string' && typeof str !== 'object') {
throw new TypeError('str must be a string or an object')
}
if (!validator || typeof validator !== 'object') {
throw new TypeError('validator is required and must be an object')
}
if (!validator.report) {
throw new Error('validator.report() is required')
}
if (typeof str === 'string') {
this._type = 'cli'
this._rawstr = str
this._raw = str
} else {
this._type = 'github-api'
if (!str || !str.sha) {
throw new Error('Invalid api format')
}
this._raw = transformAPIJson(str)
this._rawstr = this._raw.message
}
this.sha = null
this.title = null
this.author = null
this.date = null
this.body = null
this.validator = validator
this.options = validator.config
this.parse()
}
toJSON() {
return {
sha: this.sha
, title: this.title
, author: this.author
, date: this.date
, body: this.body
}
}
inspect(depth, opts) {
if (opts && opts.showHidden) {
return Object.assign(this.toJSON(), {
_raw: this._raw
})
}
return this.toJSON()
}
parse() {
switch (this._type) {
case 'cli':
this._parseCLI()
break
case 'github-api':
this._parseAPI()
break
}
}
_parseAPI() {
this.sha = this._raw.sha
this.date = this._raw.date
this.author = this._raw.author
const splits = this._rawstr.split('\n')
this.title = splits.shift()
this.body = splits
}
_parseCLI() {
const splits = this._raw.split('\n')
const commitLine = splits.shift()
this.sha = commitLine.replace('commit ', '')
var line
while (line = splits.shift()) {
line = line.trim()
if (!line) break // stop on the first empty line
const matches = line.match(headingRE)
if (matches) {
const key = matches[1].toLowerCase()
const val = matches[3]
if (key === 'date' || key === 'authordate') {
this.date = val
} else if (key === 'author') {
this.author = val
}
}
}
const body = splits.map((item) => {
// TODO(evanlucas) maybe support commit messages that are not
// indented by 4 spaces
if (item.length) return item.slice(4, item.length)
return ''
})
this.title = body.shift()
this.body = body
}
report(data) {
this.validator.report({
commit: this
, data: data
})
}
}
function transformAPIJson(obj) {
const commit = obj.commit && typeof obj.commit === 'object'
&& (obj.commit.author || obj.commit.committer)
? obj.commit
: obj
const out = {
sha: obj.sha
, author: null
, date: null
, message: commit.message
}
const author = commit.author || commit.committer
if (author) {
out.author = `${author.name} <${author.email}>`
out.date = author.date
}
return out
}