-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcommit-stream.js
121 lines (107 loc) · 3.49 KB
/
commit-stream.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
import through2 from 'through2'
import stripAnsi from 'strip-ansi'
export default function commitStream (ghUser, ghProject) {
let commit
return through2.obj(onLine, onEnd)
function addLine (line) {
line = stripAnsi(line)
if (line.length === 0) return
let old, m
if (/^commit \w+$/.test(line)) {
old = commit
commit = { sha: line.split(' ')[1] }
} else if ((m = line.match(/^CommitDate:\s+(.*)$/)) !== null) {
if (commit === undefined || commit === null) {
throw new Error('Something unexpected occurred')
}
commit.commitDate = m[1].trim()
} else if (
(m = line.match(
/^\s*(?:Author|Co[- ]?authored[- ]?by):?\s*([^<]+) <([^>]+)>\s*$/i
)) !== null
) {
if (commit === undefined || commit === null) {
throw new Error('Something unexpected occurred')
}
if (commit.authors === undefined || commit.authors === null) {
commit.authors = []
}
commit.authors.push({ name: m[1], email: m[2] })
} else if ((m = line.match(/^(?:AuthorDate|Date):\s+(.*)$/)) !== null) {
if (commit === undefined || commit === null) {
throw new Error('Something unexpected occurred')
}
commit.authorDate = m[1].trim()
} else if (
(m = line.match(/^\s+Reviewed[- ]?By:?\s*([^<]+) <([^>]+)>\s*$/i)) !==
null
) {
if (commit.reviewers === undefined || commit.reviewers === null) {
commit.reviewers = []
}
commit.reviewers.push({ name: m[1], email: m[2] })
} else if ((m = line.match(/\bCVE-ID:\s+(CVE-\d{4}-\d{5})\b/)) !== null) {
commit.cveId = m[1];
} else if ((m = line.match(/^\s+PR(?:[- ]?URL)?:?\s*(.+)\s*$/) || line.match(/\(#(\d+)\)$/)) !== null) {
commit.prUrl = m[1]
if (
typeof ghUser === 'string' &&
ghUser.length !== 0 &&
typeof ghProject === 'string' &&
ghProject.length !== 0 &&
(m = commit.prUrl.match(/^\s*#?(\d+)\s*$/)) !== null
) {
commit.prUrl = `https://github.com/${ghUser}/${ghProject}/pull/${m[1]}`
}
if (
(m = commit.prUrl.match(
/^(https?:\/\/.+\/([^/]+)\/([^/]+))\/\w+\/(\d+)(\/)?$/i
)) !== null
) {
commit.ghIssue = parseInt(m[4])
commit.ghUser = m[2]
commit.ghProject = m[3]
}
if ((m = line.match(/^ {4}(.*)\s\(#\d+\)$/)) && !commit.summary) {
commit.summary = m[1]
}
} else if (/^ {4}/.test(line) && (line = line.trim()).length !== 0) {
if (commit.summary === undefined || commit.summary === null) {
commit.summary = line
} else {
if (commit.description === undefined || commit.description === null) {
commit.description = []
}
commit.description.push(line)
}
} else if (
/^ [^ ]/.test(line) &&
(line = line.trim()).length !== 0 &&
(m = line.match(
/^(\d+) files? changed(?:, (\d+) insertions?\(\+\))?(?:, (\d+) deletions?\(-\))?/
)) !== null
) {
commit.changes = {
files: parseInt(m[1]),
insertions: parseInt(m[2] ?? 0),
deletions: parseInt(m[3] ?? 0)
}
}
return old
}
function onLine (line, _, callback) {
const commit = addLine(line)
if (commit?.authors?.length > 0) {
commit.author = commit.authors[0]
this.push(commit)
}
callback()
}
function onEnd (callback) {
if (commit?.authors?.length > 0) {
commit.author = commit.authors[0]
this.push(commit)
}
callback()
}
}