-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: split-up logic in multiple files
- Loading branch information
1 parent
2967329
commit 3843c1a
Showing
8 changed files
with
340 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package analyzer | ||
|
||
import "strings" | ||
|
||
type parsedCommit struct { | ||
Type string | ||
Scope string | ||
Modifier string | ||
Message string | ||
} | ||
|
||
func parseCommit(msg string) *parsedCommit { | ||
found := commitPattern.FindAllStringSubmatch(msg, -1) | ||
if len(found) < 1 { | ||
// commit message does not match pattern | ||
return nil | ||
} | ||
|
||
return &parsedCommit{ | ||
Type: strings.ToLower(found[0][1]), | ||
Scope: found[0][2], | ||
Modifier: found[0][3], | ||
Message: found[0][4], | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package analyzer | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestParseCommit(t *testing.T) { | ||
testCases := []struct { | ||
message string | ||
wanted *parsedCommit | ||
}{ | ||
{ | ||
message: "feat: new feature", | ||
wanted: &parsedCommit{"feat", "", "", "new feature"}, | ||
}, | ||
{ | ||
message: "feat!: new feature", | ||
wanted: &parsedCommit{"feat", "", "!", "new feature"}, | ||
}, | ||
{ | ||
message: "feat(api): new feature", | ||
wanted: &parsedCommit{"feat", "api", "", "new feature"}, | ||
}, | ||
{ | ||
message: "feat(api): a(b): c:", | ||
wanted: &parsedCommit{"feat", "api", "", "a(b): c:"}, | ||
}, | ||
{ | ||
message: "feat(new cool-api): feature", | ||
wanted: &parsedCommit{"feat", "new cool-api", "", "feature"}, | ||
}, | ||
{ | ||
message: "feat(😅): cool", | ||
wanted: &parsedCommit{"feat", "😅", "", "cool"}, | ||
}, | ||
{ | ||
message: "this-is-also(valid): cool", | ||
wanted: &parsedCommit{"this-is-also", "valid", "", "cool"}, | ||
}, | ||
{ | ||
message: "feat((x)): test", | ||
wanted: &parsedCommit{"feat", "(x", ")", "test"}, | ||
}, | ||
{ | ||
message: "feat(x)?!: test", | ||
wanted: &parsedCommit{"feat", "x", "?!", "test"}, | ||
}, | ||
{ | ||
message: "feat(x): test", | ||
wanted: &parsedCommit{"feat", "x", "", "test"}, | ||
}, | ||
{ | ||
message: "feat(x): : test", | ||
wanted: &parsedCommit{"feat", "x", "", ": test"}, | ||
}, | ||
{ | ||
message: "feat!: test", | ||
wanted: &parsedCommit{"feat", "", "!", "test"}, | ||
}, | ||
// invalid messages | ||
{ | ||
message: "feat (new api): feature", | ||
wanted: nil, | ||
}, | ||
{ | ||
message: "feat:test", | ||
wanted: nil, | ||
}, | ||
{ | ||
message: "🚀(🦄): emojis!", | ||
wanted: nil, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.message, func(t *testing.T) { | ||
c := parseCommit(tc.message) | ||
require.Equal(t, tc.wanted, c) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package analyzer | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/go-semantic-release/semantic-release/v2/pkg/semrel" | ||
) | ||
|
||
var ( | ||
releaseRulePattern = regexp.MustCompile(`^([\w-\*]+)(?:\(([^\)]*)\))?(\S*)$`) | ||
commitPattern = regexp.MustCompile(`^([\w-]+)(?:\(([^\)]*)\))?(\S*)\: (.*)$`) | ||
breakingPattern = regexp.MustCompile("BREAKING CHANGES?") | ||
mentionedIssuesPattern = regexp.MustCompile(`#(\d+)`) | ||
mentionedUsersPattern = regexp.MustCompile(`(?i)@([a-z\d]([a-z\d]|-[a-z\d])+)`) | ||
) | ||
|
||
func extractMentions(re *regexp.Regexp, s string) string { | ||
ret := make([]string, 0) | ||
for _, m := range re.FindAllStringSubmatch(s, -1) { | ||
ret = append(ret, m[1]) | ||
} | ||
return strings.Join(ret, ",") | ||
} | ||
|
||
func matchesBreakingPattern(c *semrel.Commit) bool { | ||
return breakingPattern.MatchString(strings.Join(c.Raw, "\n")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package analyzer | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestExtractIssues(t *testing.T) { | ||
testCases := []struct { | ||
message string | ||
wanted string | ||
}{ | ||
{ | ||
message: "feat: new feature #123", | ||
wanted: "123", | ||
}, | ||
{ | ||
message: "feat!: new feature closes #123 and #456", | ||
wanted: "123,456", | ||
}, | ||
} | ||
for _, testCase := range testCases { | ||
t.Run(testCase.message, func(t *testing.T) { | ||
issues := extractMentions(mentionedIssuesPattern, testCase.message) | ||
require.Equal(t, testCase.wanted, issues) | ||
}) | ||
} | ||
} | ||
|
||
func TestExtractMentions(t *testing.T) { | ||
testCases := []struct { | ||
message string | ||
wanted string | ||
}{ | ||
{ | ||
message: "feat: new feature by @user", | ||
wanted: "user", | ||
}, | ||
{ | ||
message: "feat!: new feature by @user and @user-2", | ||
wanted: "user,user-2", | ||
}, | ||
} | ||
for _, testCase := range testCases { | ||
t.Run(testCase.message, func(t *testing.T) { | ||
issues := extractMentions(mentionedUsersPattern, testCase.message) | ||
require.Equal(t, testCase.wanted, issues) | ||
}) | ||
} | ||
} |
Oops, something went wrong.