Skip to content

Commit 2967329

Browse files
feat: introduce setTypeAndChange helper
1 parent 6fdc3bb commit 2967329

File tree

2 files changed

+64
-66
lines changed

2 files changed

+64
-66
lines changed

β€Žpkg/analyzer/commit_analyzer.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,29 @@ func extractMentions(re *regexp.Regexp, s string) string {
2323
return strings.Join(ret, ",")
2424
}
2525

26+
func matchesBreakingPattern(c *semrel.Commit) bool {
27+
return breakingPattern.MatchString(strings.Join(c.Raw, "\n"))
28+
}
29+
30+
func setTypeAndChange(c *semrel.Commit) {
31+
found := commitPattern.FindAllStringSubmatch(c.Raw[0], -1)
32+
if len(found) < 1 {
33+
// commit message does not match pattern
34+
return
35+
}
36+
37+
c.Type = strings.ToLower(found[0][1])
38+
c.Scope = found[0][2]
39+
c.Message = found[0][4]
40+
41+
c.Change = &semrel.Change{
42+
// either uses the `!` convention or has a breaking change section
43+
Major: found[0][3] == "!" || matchesBreakingPattern(c),
44+
Minor: c.Type == "feat",
45+
Patch: c.Type == "fix",
46+
}
47+
}
48+
2649
type DefaultCommitAnalyzer struct{}
2750

2851
func (da *DefaultCommitAnalyzer) Init(_ map[string]string) error {
@@ -47,30 +70,7 @@ func (da *DefaultCommitAnalyzer) analyzeSingleCommit(rawCommit *semrel.RawCommit
4770
c.Annotations["mentioned_issues"] = extractMentions(mentionedIssuesPattern, rawCommit.RawMessage)
4871
c.Annotations["mentioned_users"] = extractMentions(mentionedUsersPattern, rawCommit.RawMessage)
4972

50-
found := commitPattern.FindAllStringSubmatch(c.Raw[0], -1)
51-
if len(found) < 1 {
52-
return c
53-
}
54-
c.Type = strings.ToLower(found[0][1])
55-
c.Scope = found[0][2]
56-
breakingChange := found[0][3]
57-
c.Message = found[0][4]
58-
59-
isMajorChange := breakingPattern.MatchString(rawCommit.RawMessage)
60-
isMinorChange := c.Type == "feat"
61-
isPatchChange := c.Type == "fix"
62-
63-
if len(breakingChange) > 0 {
64-
isMajorChange = true
65-
isMinorChange = false
66-
isPatchChange = false
67-
}
68-
69-
c.Change = &semrel.Change{
70-
Major: isMajorChange,
71-
Minor: isMinorChange,
72-
Patch: isPatchChange,
73-
}
73+
setTypeAndChange(c)
7474
return c
7575
}
7676

β€Žpkg/analyzer/commit_analyzer_test.go

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,6 @@ import (
99
"github.com/stretchr/testify/require"
1010
)
1111

12-
func compareCommit(c *semrel.Commit, t, s string, change *semrel.Change) bool {
13-
if c.Type != t || c.Scope != s {
14-
return false
15-
}
16-
if c.Change.Major != change.Major ||
17-
c.Change.Minor != change.Minor ||
18-
c.Change.Patch != change.Patch {
19-
return false
20-
}
21-
return true
22-
}
23-
2412
func createRawCommit(sha, message string) *semrel.RawCommit {
2513
return &semrel.RawCommit{
2614
SHA: sha,
@@ -77,13 +65,13 @@ func TestDefaultAnalyzer(t *testing.T) {
7765
createRawCommit("e", "feat!: modified login endpoint"),
7866
"feat",
7967
"",
80-
&semrel.Change{Major: true, Minor: false, Patch: false},
68+
&semrel.Change{Major: true, Minor: true, Patch: false},
8169
},
8270
{
8371
createRawCommit("f", "fix!: fixed a typo"),
8472
"fix",
8573
"",
86-
&semrel.Change{Major: true, Minor: false, Patch: false},
74+
&semrel.Change{Major: true, Minor: false, Patch: true},
8775
},
8876
{
8977
createRawCommit("g", "refactor(parser)!: drop support for Node 6\n\nBREAKING CHANGE: refactor to use JavaScript features not available in Node 6."),
@@ -103,71 +91,81 @@ func TestDefaultAnalyzer(t *testing.T) {
10391
"",
10492
&semrel.Change{Major: false, Minor: false, Patch: false},
10593
},
94+
{
95+
createRawCommit("i", "feat(deps): update deps\n\nBREAKING CHANGE: update to new version of dep"),
96+
"feat",
97+
"deps",
98+
&semrel.Change{Major: true, Minor: true, Patch: false},
99+
},
106100
}
107101

108102
defaultAnalyzer := &DefaultCommitAnalyzer{}
109103
for _, tc := range testCases {
110104
t.Run(fmt.Sprintf("AnalyzeCommitMessage: %s", tc.RawCommit.RawMessage), func(t *testing.T) {
111-
require.True(t, compareCommit(defaultAnalyzer.analyzeSingleCommit(tc.RawCommit), tc.Type, tc.Scope, tc.Change))
105+
analyzedCommit := defaultAnalyzer.analyzeSingleCommit(tc.RawCommit)
106+
require.Equal(t, tc.Type, analyzedCommit.Type, "Type")
107+
require.Equal(t, tc.Scope, analyzedCommit.Scope, "Scope")
108+
require.Equal(t, tc.Change.Major, analyzedCommit.Change.Major, "Major")
109+
require.Equal(t, tc.Change.Minor, analyzedCommit.Change.Minor, "Minor")
110+
require.Equal(t, tc.Change.Patch, analyzedCommit.Change.Patch, "Patch")
112111
})
113112
}
114113
}
115114

116115
func TestCommitPattern(t *testing.T) {
117116
testCases := []struct {
118-
rawMessage string
119-
wanted []string
117+
message string
118+
wanted []string
120119
}{
121120
{
122-
rawMessage: "feat: new feature",
123-
wanted: []string{"feat", "", "", "new feature"},
121+
message: "feat: new feature",
122+
wanted: []string{"feat", "", "", "new feature"},
124123
},
125124
{
126-
rawMessage: "feat!: new feature",
127-
wanted: []string{"feat", "", "!", "new feature"},
125+
message: "feat!: new feature",
126+
wanted: []string{"feat", "", "!", "new feature"},
128127
},
129128
{
130-
rawMessage: "feat(api): new feature",
131-
wanted: []string{"feat", "api", "", "new feature"},
129+
message: "feat(api): new feature",
130+
wanted: []string{"feat", "api", "", "new feature"},
132131
},
133132
{
134-
rawMessage: "feat(api): a(b): c:",
135-
wanted: []string{"feat", "api", "", "a(b): c:"},
133+
message: "feat(api): a(b): c:",
134+
wanted: []string{"feat", "api", "", "a(b): c:"},
136135
},
137136
{
138-
rawMessage: "feat(new cool-api): feature",
139-
wanted: []string{"feat", "new cool-api", "", "feature"},
137+
message: "feat(new cool-api): feature",
138+
wanted: []string{"feat", "new cool-api", "", "feature"},
140139
},
141140
{
142-
rawMessage: "feat(πŸ˜…): cool",
143-
wanted: []string{"feat", "πŸ˜…", "", "cool"},
141+
message: "feat(πŸ˜…): cool",
142+
wanted: []string{"feat", "πŸ˜…", "", "cool"},
144143
},
145144
{
146-
rawMessage: "this-is-also(valid): cool",
147-
wanted: []string{"this-is-also", "valid", "", "cool"},
145+
message: "this-is-also(valid): cool",
146+
wanted: []string{"this-is-also", "valid", "", "cool"},
148147
},
149148
{
150-
rawMessage: "πŸš€(πŸ¦„): emojis!",
151-
wanted: []string{"πŸš€", "πŸ¦„", "", "emojis!"},
149+
message: "πŸš€(πŸ¦„): emojis!",
150+
wanted: []string{"πŸš€", "πŸ¦„", "", "emojis!"},
152151
},
153152
// invalid messages
154153
{
155-
rawMessage: "feat (new api): feature",
156-
wanted: nil,
154+
message: "feat (new api): feature",
155+
wanted: nil,
157156
},
158157
{
159-
rawMessage: "feat((x)): test",
160-
wanted: nil,
158+
message: "feat((x)): test",
159+
wanted: nil,
161160
},
162161
{
163-
rawMessage: "feat:test",
164-
wanted: nil,
162+
message: "feat:test",
163+
wanted: nil,
165164
},
166165
}
167166
for _, tc := range testCases {
168-
t.Run(fmt.Sprintf("CommitPattern: %s", tc.rawMessage), func(t *testing.T) {
169-
rawMessageLines := strings.Split(tc.rawMessage, "\n")
170-
found := commitPattern.FindAllStringSubmatch(rawMessageLines[0], -1)
167+
t.Run(fmt.Sprintf("CommitPattern: %s", tc.message), func(t *testing.T) {
168+
found := commitPattern.FindAllStringSubmatch(tc.message, -1)
171169
if len(tc.wanted) == 0 {
172170
require.Len(t, found, 0)
173171
return

0 commit comments

Comments
Β (0)