-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(semver): add conventional commit template func (#5)
- Loading branch information
1 parent
8d674fd
commit 522786d
Showing
11 changed files
with
170 additions
and
5 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
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
repos: | ||
- repo: https://github.com/FalcoSuessgott/mdtmpl | ||
rev: {{ exec "git describe --tags --abbrev=0" | truncate }} | ||
rev: {{ conventionalCommitBump }} | ||
hooks: | ||
- id: mdtmpl | ||
args: [-t=README.md.tmpl, -f, -o=README.md] |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
repos: | ||
- repo: https://github.com/FalcoSuessgott/mdtmpl | ||
rev: {{ exec "git describe --tags --abbrev=0" | truncate }} | ||
rev: {{ conventionalCommitBump }} | ||
hooks: | ||
- id: mdtmpl | ||
args: [-t=README.md.tmpl, -f, -o=README.md] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package commit | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/Masterminds/semver/v3" | ||
"github.com/leodido/go-conventionalcommits" | ||
"github.com/leodido/go-conventionalcommits/parser" | ||
) | ||
|
||
type SemVerFunc func(*semver.Version) string | ||
|
||
var ( | ||
IncMajor SemVerFunc = func(v *semver.Version) string { return v.IncMajor().String() } | ||
IncMinor SemVerFunc = func(v *semver.Version) string { return v.IncMinor().String() } | ||
IncPatch SemVerFunc = func(v *semver.Version) string { return v.IncPatch().String() } | ||
) | ||
|
||
func ParseConventionalCommit(commit []byte) (SemVerFunc, error) { | ||
cc, err := parser.NewMachine( | ||
conventionalcommits.WithTypes(conventionalcommits.TypesConventional), | ||
conventionalcommits.WithBestEffort()).Parse(commit) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if cc.IsBreakingChange() { | ||
return IncMajor, nil | ||
} | ||
|
||
if cc.IsFeat() { | ||
return IncMinor, nil | ||
} | ||
|
||
if cc.IsFix() { | ||
return IncPatch, nil | ||
} | ||
|
||
return nil, errors.New("commit is not a conventional commit") | ||
} |
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,61 @@ | ||
package commit | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/Masterminds/semver/v3" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestParseConventionalCommit(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
commit string | ||
v string | ||
expV string | ||
err bool | ||
}{ | ||
{ | ||
name: "minor", | ||
commit: "feat: add new feature", | ||
v: "1.2.3", | ||
expV: "1.3.0", | ||
}, | ||
{ | ||
name: "patch", | ||
commit: "fix: add new feature", | ||
v: "1.2.3", | ||
expV: "1.2.4", | ||
}, | ||
{ | ||
name: "breaking", | ||
commit: "chore!: add new feature", | ||
v: "1.2.3", | ||
expV: "2.0.0", | ||
}, | ||
// { | ||
// name: "breaking", | ||
// commit: "chore!: add new feature", | ||
// v: "v1.2.3", | ||
// expV: "v2.0.0", | ||
// }, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
res, err := ParseConventionalCommit([]byte(tc.commit)) | ||
|
||
if tc.err { | ||
require.Error(t, err, tc.name) | ||
|
||
return | ||
} | ||
|
||
require.NoError(t, err, tc.name) | ||
|
||
v, err := semver.NewVersion(tc.v) | ||
require.NoError(t, err, tc.name) | ||
require.Equal(t, tc.expV, res(v), tc.name) | ||
}) | ||
} | ||
} |
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