Skip to content

Commit

Permalink
feat: improve commit pattern regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
christophwitzko committed Feb 14, 2024
1 parent d4eb733 commit 5259606
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/analyzer/commit_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

var (
CAVERSION = "dev"
commitPattern = regexp.MustCompile(`^(\w*)(?:\((.*)\))?(\!)?\: (.*)$`)
commitPattern = regexp.MustCompile(`^([^\s\(\!]+)(?:\(([^\)]*)\))?(\!)?\: (.*)$`)
breakingPattern = regexp.MustCompile("BREAKING CHANGES?")
mentionedIssuesPattern = regexp.MustCompile(`#(\d+)`)
mentionedUsersPattern = regexp.MustCompile(`(?i)@([a-z\d]([a-z\d]|-[a-z\d])+)`)
Expand Down
66 changes: 66 additions & 0 deletions pkg/analyzer/commit_analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,69 @@ func TestDefaultAnalyzer(t *testing.T) {
})
}
}

func TestCommitPattern(t *testing.T) {
testCases := []struct {
rawMessage string
wanted []string
}{
{
rawMessage: "feat: new feature",
wanted: []string{"feat", "", "", "new feature"},
},
{
rawMessage: "feat!: new feature",
wanted: []string{"feat", "", "!", "new feature"},
},
{
rawMessage: "feat(api): new feature",
wanted: []string{"feat", "api", "", "new feature"},
},
{
rawMessage: "feat(api): a(b): c:",
wanted: []string{"feat", "api", "", "a(b): c:"},
},
{
rawMessage: "feat(new cool-api): feature",
wanted: []string{"feat", "new cool-api", "", "feature"},
},
{
rawMessage: "feat(πŸ˜…): cool",
wanted: []string{"feat", "πŸ˜…", "", "cool"},
},
{
rawMessage: "this-is-also(valid): cool",
wanted: []string{"this-is-also", "valid", "", "cool"},
},
{
rawMessage: "πŸš€(πŸ¦„): emojis!",
wanted: []string{"πŸš€", "πŸ¦„", "", "emojis!"},
},
// invalid messages
{
rawMessage: "feat (new api): feature",
wanted: nil,
},
{
rawMessage: "feat((x)): test",
wanted: nil,
},
{
rawMessage: "feat:test",
wanted: nil,
},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("CommitPattern: %s", tc.rawMessage), func(t *testing.T) {
rawMessageLines := strings.Split(tc.rawMessage, "\n")
found := commitPattern.FindAllStringSubmatch(rawMessageLines[0], -1)
if len(tc.wanted) == 0 {
require.Len(t, found, 0)
return
}
require.Len(t, found, 1)
require.Len(t, found[0], 5)
require.Equal(t, tc.wanted, found[0][1:])
})
}
}

0 comments on commit 5259606

Please sign in to comment.