-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
203 lines (155 loc) · 3.62 KB
/
parser.go
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package conventionalcommitparser
// Conventional Commits specification
// https://www.conventionalcommits.org/en/v1.0.0/
import (
"regexp"
"strings"
)
type Message struct {
Header string
Body string
Footer []string
}
var (
emptyLinePattern = regexp.MustCompile(`^\s*$`)
revertBodyPattern = regexp.MustCompile(`(?i)This\sreverts\scommit\s(\w+)\.?`)
)
func splitToLines(text string) []string {
return strings.Split(strings.ReplaceAll(text, "\r\n", "\n"), "\n")
}
func (m *Message) ParseHeader() Header {
return parseHeader(m.Header)
}
func (m *Message) ParseFooter() []Footer {
footers := make([]Footer, 0)
for _, m := range m.Footer {
footers = append(footers, parseFooter(m))
}
header := m.ParseHeader()
if header.Type == "revert" {
matcher := revertBodyPattern.FindStringSubmatch(m.Body)
content := ""
if len(matcher) > 0 {
content = matcher[1]
}
footer := Footer{
Tag: "revert",
Title: header.Subject,
Content: content,
}
footers = append(footers, footer)
}
return footers
}
func (m *Message) GetCloses() []string {
footer := m.GetFooterByField("Close", "close", "Closes", "closes", "Fix", "fix", "Fixes", "fixes")
closes := make([]string, 0)
if footer == nil {
return nil
}
// #1, #2, #3,#4, #110,#123
arr := strings.Split(footer.Title, ",")
for _, r := range arr {
hash := strings.TrimSpace(r)
if hash != "" {
closes = append(closes, strings.TrimSpace(r))
}
}
return closes
}
func (m *Message) GetFooterByField(tags ...string) *Footer {
footers := m.ParseFooter()
for _, tag := range tags {
for _, f := range footers {
if strings.EqualFold(f.Tag, tag) {
return &f
}
}
}
return nil
}
/**
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]
fix: prevent racing of requests
Introduce a request id and a reference to latest request. Dismiss
incoming responses other than from latest request.
Remove timeouts which were used to mitigate the racing issue but are
obsolete now.
BREAKING CHANGE: use `.use()` instea of `.load()`
before:
```javascript
app.load({})
```
after:
```javascript
app.use({})
```
Reviewed-by: Z
Refs: #123
*/
func Parse(message string) *Message {
var (
msg Message
header string = ""
body []string = make([]string, 0)
footer []string = make([]string, 0)
)
lines := splitToLines(message)
index := 0
for {
// last break
if index >= len(lines) {
break
}
line := lines[index]
// The first line should be header
if index == 0 {
header = line
index++
continue
}
// The second line should be blank
if index == 1 {
body = append(body, line)
index++
continue
}
previousLine := lines[index-1]
// if is a footer start
if isFooterParagraph(line) && (emptyLinePattern.MatchString(previousLine) || isFooterParagraph(previousLine)) {
footerContent := []string{line}
index++
// if this line is last line
if index >= len(lines) {
footer = append(footer, strings.TrimSpace(strings.Join(footerContent, "\n")))
continue
}
innerLoop:
for {
if index >= len(lines) {
footer = append(footer, strings.TrimSpace(strings.Join(footerContent, "\n")))
break innerLoop
}
line := lines[index]
// if match the next footer tag
if isFooterParagraph(line) {
footer = append(footer, strings.TrimSpace(strings.Join(footerContent, "\n")))
break innerLoop
} else {
footerContent = append(footerContent, line)
index++
}
}
} else {
body = append(body, line)
index++
continue
}
}
msg.Header = header
msg.Body = strings.TrimSpace(strings.Join(body, "\n"))
msg.Footer = footer
return &msg
}