Skip to content

Commit 236195c

Browse files
feat: use release rules logic
1 parent 3843c1a commit 236195c

File tree

5 files changed

+37
-18
lines changed

5 files changed

+37
-18
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- uses: actions/checkout@v4
1414
- uses: actions/setup-go@v4
1515
with:
16-
go-version: '1.21'
16+
go-version: '1.22'
1717
- uses: golangci/golangci-lint-action@v3
1818
build:
1919
runs-on: ${{ matrix.os }}
@@ -26,7 +26,7 @@ jobs:
2626
- uses: actions/checkout@v4
2727
- uses: actions/setup-go@v4
2828
with:
29-
go-version: '1.21'
29+
go-version: '1.22'
3030
- run: go build ./cmd/commit-analyzer-cz/
3131
- run: go test -v ./...
3232
release:
@@ -36,7 +36,7 @@ jobs:
3636
- uses: actions/checkout@v4
3737
- uses: actions/setup-go@v4
3838
with:
39-
go-version: '1.21'
39+
go-version: '1.22'
4040
- uses: go-semantic-release/action@v1
4141
with:
4242
hooks: goreleaser,plugin-registry-update

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/go-semantic-release/commit-analyzer-cz
22

3-
go 1.21
3+
go 1.22
44

55
require (
66
github.com/go-semantic-release/semantic-release/v2 v2.28.0

pkg/analyzer/commit_analyzer.go

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
11
package analyzer
22

33
import (
4+
"cmp"
5+
"fmt"
46
"strings"
57

68
"github.com/go-semantic-release/semantic-release/v2/pkg/semrel"
79
)
810

911
var CAVERSION = "dev"
1012

11-
type DefaultCommitAnalyzer struct{}
13+
type DefaultCommitAnalyzer struct {
14+
majorReleaseRules releaseRules
15+
minorReleaseRules releaseRules
16+
patchReleaseRules releaseRules
17+
}
1218

1319
func (da *DefaultCommitAnalyzer) Init(m map[string]string) error {
14-
// TODO: implement config parsing
20+
var err error
21+
da.majorReleaseRules, err = parseRules(cmp.Or(m["major_release_rules"], defaultMajorReleaseRules))
22+
if err != nil {
23+
return fmt.Errorf("failed to parse major release rules: %w", err)
24+
}
25+
da.minorReleaseRules, err = parseRules(cmp.Or(m["minor_release_rules"], defaultMinorReleaseRules))
26+
if err != nil {
27+
return fmt.Errorf("failed to parse minor release rules: %w", err)
28+
}
29+
da.patchReleaseRules, err = parseRules(cmp.Or(m["patch_release_rules"], defaultPatchReleaseRules))
30+
if err != nil {
31+
return fmt.Errorf("failed to parse patch release rules: %w", err)
32+
}
1533
return nil
1634
}
1735

@@ -24,21 +42,20 @@ func (da *DefaultCommitAnalyzer) Version() string {
2442
}
2543

2644
func (da *DefaultCommitAnalyzer) setTypeAndChange(c *semrel.Commit) {
27-
found := commitPattern.FindAllStringSubmatch(c.Raw[0], -1)
28-
if len(found) < 1 {
29-
// commit message does not match pattern
45+
pc := parseCommit(c.Raw[0])
46+
if pc == nil {
3047
return
3148
}
3249

33-
c.Type = strings.ToLower(found[0][1])
34-
c.Scope = found[0][2]
35-
c.Message = found[0][4]
50+
c.Type = pc.Type
51+
c.Scope = pc.Scope
52+
c.Message = pc.Message
3653

3754
c.Change = &semrel.Change{
3855
// either uses the `!` convention or has a breaking change section
39-
Major: found[0][3] == "!" || matchesBreakingPattern(c),
40-
Minor: c.Type == "feat",
41-
Patch: c.Type == "fix",
56+
Major: da.majorReleaseRules.Matches(pc) || matchesBreakingPattern(c),
57+
Minor: da.minorReleaseRules.Matches(pc),
58+
Patch: da.patchReleaseRules.Matches(pc),
4259
}
4360
}
4461

pkg/analyzer/commit_analyzer_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func createRawCommit(sha, message string) *semrel.RawCommit {
2020

2121
func TestAnnotations(t *testing.T) {
2222
defaultAnalyzer := &DefaultCommitAnalyzer{}
23+
require.NoError(t, defaultAnalyzer.Init(map[string]string{}))
2324
rawCommit := createRawCommit("a", "fix: bug #123 and #243\nthanks @Test-user for providing this fix\n\nCloses #22")
2425
commit := defaultAnalyzer.analyzeSingleCommit(rawCommit)
2526
require.Equal(t, rawCommit.SHA, commit.SHA)
@@ -64,13 +65,13 @@ func TestDefaultAnalyzer(t *testing.T) {
6465
createRawCommit("e", "feat!: modified login endpoint"),
6566
"feat",
6667
"",
67-
&semrel.Change{Major: true, Minor: true, Patch: false},
68+
&semrel.Change{Major: true, Minor: false, Patch: false},
6869
},
6970
{
7071
createRawCommit("f", "fix!: fixed a typo"),
7172
"fix",
7273
"",
73-
&semrel.Change{Major: true, Minor: false, Patch: true},
74+
&semrel.Change{Major: true, Minor: false, Patch: false},
7475
},
7576
{
7677
createRawCommit("g", "refactor(parser)!: drop support for Node 6\n\nBREAKING CHANGE: refactor to use JavaScript features not available in Node 6."),
@@ -99,6 +100,7 @@ func TestDefaultAnalyzer(t *testing.T) {
99100
}
100101

101102
defaultAnalyzer := &DefaultCommitAnalyzer{}
103+
require.NoError(t, defaultAnalyzer.Init(map[string]string{}))
102104
for _, tc := range testCases {
103105
t.Run(tc.RawCommit.RawMessage, func(t *testing.T) {
104106
analyzedCommit := defaultAnalyzer.analyzeSingleCommit(tc.RawCommit)

pkg/analyzer/rules.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
var (
10-
defaultMajorReleaseRules = "*(*)!"
10+
defaultMajorReleaseRules = "*!"
1111
defaultMinorReleaseRules = "feat"
1212
defaultPatchReleaseRules = "fix"
1313
)

0 commit comments

Comments
 (0)