Skip to content

Commit 6d3b8d0

Browse files
committed
fix: parse for breaking change
1 parent 96d842d commit 6d3b8d0

2 files changed

Lines changed: 112 additions & 49 deletions

File tree

parser.go

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type Footer struct {
3030
var (
3131
EMPTY_LINE_PATTERN = regexp.MustCompile(`^\s*$`)
3232
HEADER_PATTERN = regexp.MustCompile(`^(?:fixup!\s*)?(\w*)(\(([\w\$\.\*/-]*)\))?(!?):\s(.*)$`)
33-
FOOTER_PATTERN = regexp.MustCompile(`^([\w\s\-]+):\s(.*)$`)
33+
FOOTER_PATTERN = regexp.MustCompile(`^([\w\s\-]+):(.*)$`)
3434
REVERT_HEADER_PATTERN = regexp.MustCompile(`^(?i)revert\s(.*)$`)
3535
REVERT_BODY_PATTERN = regexp.MustCompile(`(?i)This\sreverts\scommit\s(\w+)\.?`)
3636
)
@@ -82,8 +82,8 @@ func (m Message) GetFooter() []Footer {
8282
footer.Tag = ""
8383
footer.Title = line
8484
} else {
85-
footer.Tag = matcher[1]
86-
footer.Title = matcher[2]
85+
footer.Tag = strings.TrimSpace(matcher[1])
86+
footer.Title = strings.TrimSpace(matcher[2])
8787
}
8888
continue lineLoop
8989
} else {
@@ -197,47 +197,40 @@ func Parse(message string) Message {
197197

198198
previousLine := lines[index-1]
199199

200-
// parse body
201-
if !FOOTER_PATTERN.MatchString(line) {
202-
body = append(body, line)
200+
// if is a footer start
201+
if FOOTER_PATTERN.MatchString(line) && (EMPTY_LINE_PATTERN.MatchString(previousLine) || FOOTER_PATTERN.MatchString(previousLine)) {
202+
footerContent := []string{line}
203+
203204
index++
204-
continue
205-
} else {
206-
// if previous line is blank. then should be body block end
207-
// or previous line is a footer
208-
if EMPTY_LINE_PATTERN.MatchString(previousLine) || FOOTER_PATTERN.MatchString(previousLine) {
209-
footerContent := []string{line}
210205

211-
index++
206+
// if this line is last line
207+
if index >= len(lines) {
208+
footer = append(footer, strings.TrimSpace(strings.Join(footerContent, "\n")))
209+
continue
210+
}
212211

213-
// if this line is last line
212+
innerLoop:
213+
for {
214214
if index >= len(lines) {
215215
footer = append(footer, strings.TrimSpace(strings.Join(footerContent, "\n")))
216-
continue
216+
break innerLoop
217217
}
218218

219-
innerLoop:
220-
for {
221-
if index >= len(lines) {
222-
break innerLoop
223-
}
224-
225-
line := lines[index]
226-
227-
if !FOOTER_PATTERN.MatchString(line) {
228-
footerContent = append(footerContent, line)
229-
index++
230-
continue innerLoop
231-
} else {
232-
footer = append(footer, strings.TrimSpace(strings.Join(footerContent, "\n")))
233-
break innerLoop
234-
}
219+
line := lines[index]
220+
221+
if !FOOTER_PATTERN.MatchString(line) {
222+
footerContent = append(footerContent, line)
223+
index++
224+
continue innerLoop
225+
} else {
226+
footer = append(footer, strings.TrimSpace(strings.Join(footerContent, "\n")))
227+
break innerLoop
235228
}
236-
} else {
237-
body = append(body, line)
238-
index++
239-
continue
240229
}
230+
} else {
231+
body = append(body, line)
232+
index++
233+
continue
241234
}
242235
}
243236

parser_test.go

Lines changed: 84 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,72 @@ defined as:
125125
},
126126
footer: []Footer{},
127127
},
128+
{
129+
name: "common breaking change",
130+
args: args{
131+
message: `feat(BREAKING): remove hashURL function in template render
132+
133+
BREAKING CHANGE:
134+
135+
before
136+
137+
'''bash
138+
{{ hashURL .Hash}}
139+
{{ hashURL .RevertCommitHash }}
140+
'''
141+
142+
after
143+
144+
'''bash
145+
{{ .HashURL }}
146+
{{ .RevertCommitHashURL }}
147+
'''`,
148+
},
149+
want: Message{
150+
Header: "feat(BREAKING): remove hashURL function in template render",
151+
Body: "",
152+
Footer: []string{`BREAKING CHANGE:
153+
154+
before
155+
156+
'''bash
157+
{{ hashURL .Hash}}
158+
{{ hashURL .RevertCommitHash }}
159+
'''
160+
161+
after
162+
163+
'''bash
164+
{{ .HashURL }}
165+
{{ .RevertCommitHashURL }}
166+
'''`},
167+
},
168+
header: Header{
169+
Type: "feat",
170+
Scope: "BREAKING",
171+
Subject: "remove hashURL function in template render",
172+
Important: false,
173+
},
174+
footer: []Footer{
175+
{
176+
Tag: "BREAKING CHANGE",
177+
Title: "",
178+
Content: `before
179+
180+
'''bash
181+
{{ hashURL .Hash}}
182+
{{ hashURL .RevertCommitHash }}
183+
'''
184+
185+
after
186+
187+
'''bash
188+
{{ .HashURL }}
189+
{{ .RevertCommitHashURL }}
190+
'''`,
191+
},
192+
},
193+
},
128194
{
129195
name: "Commit message with description and breaking change footer",
130196
args: args{
@@ -283,14 +349,12 @@ incoming responses other than from latest request.
283349
Remove timeouts which were used to mitigate the racing issue but are
284350
obsolete now.`,
285351
Footer: []string{
286-
`BREAKING CHANGE: use '.use()' instea of '.load()'
287-
288-
before:
352+
`BREAKING CHANGE: use '.use()' instea of '.load()'`,
353+
`before:
289354
'''javascript
290355
app.load({})
291-
'''
292-
293-
after:
356+
'''`,
357+
`after:
294358
'''javascript
295359
app.use({})
296360
'''`,
@@ -306,15 +370,21 @@ app.use({})
306370
},
307371
footer: []Footer{
308372
{
309-
Tag: "BREAKING CHANGE",
310-
Title: "use '.use()' instea of '.load()'",
311-
Content: `before:
312-
'''javascript
373+
Tag: "BREAKING CHANGE",
374+
Title: "use '.use()' instea of '.load()'",
375+
Content: "",
376+
},
377+
{
378+
Tag: "before",
379+
Title: "",
380+
Content: `'''javascript
313381
app.load({})
314-
'''
315-
316-
after:
317-
'''javascript
382+
'''`,
383+
},
384+
{
385+
Tag: "after",
386+
Title: "",
387+
Content: `'''javascript
318388
app.use({})
319389
'''`,
320390
},

0 commit comments

Comments
 (0)