forked from nodejs/core-validate-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitlint-parser.js
More file actions
171 lines (152 loc) · 4.34 KB
/
gitlint-parser.js
File metadata and controls
171 lines (152 loc) · 4.34 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { spawnSync } from 'node:child_process'
import Base from 'gitlint-parser-base'
const revertRE = /Revert "(.*)"$/
const workingRE = /Working on v([\d]+)\.([\d]+).([\d]+)$/
const releaseRE = /([\d]{4})-([\d]{2})-([\d]{2}),? Version/
const reviewedByRE = /^Reviewed-By: (.*)$/
const fixesRE = /^Fixes: (.*)$/
const prUrlRE = /^PR-URL: (.*)$/
const refsRE = /^Refs?: (.*)$/
export default class Parser extends Base {
constructor (str, validator) {
super(str, validator)
this.subsystems = []
this.fixes = []
this.prUrl = null
this.refs = []
this.reviewers = []
this._metaStart = 0
this._metaEnd = 0
this._parse()
}
_setMetaStart (n) {
if (this._metaStart) return
this._metaStart = n
}
_setMetaEnd (n) {
if (n < this._metaEnd) return
this._metaEnd = n
}
_parseTrailers (body) {
const interpretTrailers = commitMessage => spawnSync('git', [
'interpret-trailers', '--only-trailers', '--only-input', '--no-divider'
], {
encoding: 'utf-8',
input: `'dummy subject\n\n${commitMessage.join('\n')}\n`
}).stdout
let originalTrailers
try {
originalTrailers = interpretTrailers(body).trim()
} catch (err) {
console.warn('git is not available, trailers detection might be a bit ' +
'off which is acceptable in most cases', err)
return body
}
const trailerFreeBody = body.slice(1) // clone, and remove the first empty line
const stillInTrailers = () => {
const result = interpretTrailers(trailerFreeBody)
return result.length && originalTrailers.startsWith(result.trim())
}
for (let i = trailerFreeBody.length - 1; stillInTrailers(); i--) {
// Remove last line until git no longer detects any trailers
trailerFreeBody.pop()
}
this._metaStart = trailerFreeBody.length + 1 // the subject line needs to be counted
for (let i = trailerFreeBody.length - 1; trailerFreeBody[i] === ''; i--) {
// Remove additional empty line(s)
trailerFreeBody.pop()
}
this._metaEnd = body.length - 1
this.trailerFreeBody = trailerFreeBody
return (this.trailers = originalTrailers.split('\n'))
}
_parse () {
const revert = this.isRevert()
if (!revert) {
this.subsystems = getSubsystems(this.title || '')
} else {
const matches = this.title.match(revertRE)
if (matches) {
const title = matches[1]
this.subsystems = getSubsystems(title)
}
}
const trailers = this._parseTrailers(this.body)
for (let i = 0; i < trailers.length; i++) {
const line = trailers[i]
const reviewedBy = reviewedByRE.exec(line)
if (reviewedBy) {
this._setMetaStart(i)
this._setMetaEnd(i)
this.reviewers.push(reviewedBy[1])
continue
}
const fixes = fixesRE.exec(line)
if (fixes) {
this._setMetaStart(i)
this._setMetaEnd(i)
this.fixes.push(fixes[1])
continue
}
const prUrl = prUrlRE.exec(line)
if (prUrl) {
this._setMetaStart(i)
this._setMetaEnd(i)
this.prUrl = prUrl[1]
continue
}
const refs = refsRE.exec(line)
if (refs) {
this._setMetaStart(i)
this._setMetaEnd(i)
this.refs.push(refs[1])
continue
}
if (this._metaStart && !this._metaEnd) { this._setMetaEnd(i) }
}
}
isRevert () {
return revertRE.test(this.title)
}
isWorkingCommit () {
return workingRE.test(this.title)
}
isReleaseCommit () {
return releaseRE.test(this.title)
}
toJSON () {
return {
sha: this.sha,
title: this.title,
subsystems: this.subsystems,
author: this.author,
date: this.date,
fixes: this.fixes,
refs: this.refs,
prUrl: this.prUrl,
reviewers: this.reviewers,
body: this.body,
trailers: this.trailers,
trailerFreeBody: this.trailerFreeBody,
revert: this.isRevert(),
release: this.isReleaseCommit(),
working: this.isWorkingCommit(),
metadata: {
start: this._metaStart,
end: this._metaEnd
}
}
}
}
function getSubsystems (str) {
str = str || ''
const colon = str.indexOf(':')
if (colon === -1) {
return []
}
const subStr = str.slice(0, colon)
const subs = subStr.split(',')
return subs.map((item) => {
return item.trim()
})
}