-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a8fe4ea
commit 3b0c098
Showing
9 changed files
with
123 additions
and
70 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
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,44 @@ | ||
package commit | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/go-semantic-release/semantic-release/pkg/semrel" | ||
) | ||
|
||
type Analyzer interface { | ||
Analyze([]*semrel.RawCommit) []*semrel.Commit | ||
} | ||
|
||
var commitPattern = regexp.MustCompile(`^(\w*)(?:\((.*)\))?\: (.*)$`) | ||
var breakingPattern = regexp.MustCompile("BREAKING CHANGES?") | ||
|
||
type DefaultAnalyzer struct{} | ||
|
||
func (da *DefaultAnalyzer) analyzeSingleCommit(rawCommit *semrel.RawCommit) *semrel.Commit { | ||
c := new(semrel.Commit) | ||
c.SHA = rawCommit.SHA | ||
c.Raw = strings.Split(rawCommit.RawMessage, "\n") | ||
found := commitPattern.FindAllStringSubmatch(c.Raw[0], -1) | ||
if len(found) < 1 { | ||
return c | ||
} | ||
c.Type = strings.ToLower(found[0][1]) | ||
c.Scope = found[0][2] | ||
c.Message = found[0][3] | ||
c.Change = semrel.Change{ | ||
Major: breakingPattern.MatchString(rawCommit.RawMessage), | ||
Minor: c.Type == "feat", | ||
Patch: c.Type == "fix", | ||
} | ||
return c | ||
} | ||
|
||
func (da *DefaultAnalyzer) Analyze(rawCommits []*semrel.RawCommit) []*semrel.Commit { | ||
ret := make([]*semrel.Commit, len(rawCommits)) | ||
for i, c := range rawCommits { | ||
ret[i] = da.analyzeSingleCommit(c) | ||
} | ||
return ret | ||
} |
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 commit | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/go-semantic-release/semantic-release/pkg/semrel" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func compareCommit(c *semrel.Commit, t, s string, change semrel.Change) bool { | ||
if c.Type != t || c.Scope != s { | ||
return false | ||
} | ||
if c.Change.Major != change.Major || | ||
c.Change.Minor != change.Minor || | ||
c.Change.Patch != change.Patch { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func createRawCommit(sha, message string) *semrel.RawCommit { | ||
return &semrel.RawCommit{ | ||
SHA: sha, | ||
RawMessage: message, | ||
} | ||
} | ||
|
||
func TestDefaultAnalyzer(t *testing.T) { | ||
testCases := []struct { | ||
RawCommit *semrel.RawCommit | ||
Type string | ||
Scope string | ||
Change semrel.Change | ||
}{ | ||
{ | ||
createRawCommit("a", "feat: new feature"), | ||
"feat", | ||
"", | ||
semrel.Change{Major: false, Minor: true, Patch: false}, | ||
}, | ||
} | ||
|
||
defaultAnalyzer := &DefaultAnalyzer{} | ||
for _, tc := range testCases { | ||
t.Run(fmt.Sprintf("AnalyzeCommitMessage: %s", tc.RawCommit.RawMessage), func(t *testing.T) { | ||
require.True(t, compareCommit(defaultAnalyzer.analyzeSingleCommit(tc.RawCommit), tc.Type, tc.Scope, tc.Change)) | ||
}) | ||
} | ||
} |
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
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