Skip to content

Commit

Permalink
feat: use release rules logic
Browse files Browse the repository at this point in the history
  • Loading branch information
christophwitzko committed Feb 17, 2024
1 parent 3843c1a commit 236195c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 18 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: '1.21'
go-version: '1.22'
- uses: golangci/golangci-lint-action@v3
build:
runs-on: ${{ matrix.os }}
Expand All @@ -26,7 +26,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: '1.21'
go-version: '1.22'
- run: go build ./cmd/commit-analyzer-cz/
- run: go test -v ./...
release:
Expand All @@ -36,7 +36,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: '1.21'
go-version: '1.22'
- uses: go-semantic-release/action@v1
with:
hooks: goreleaser,plugin-registry-update
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-semantic-release/commit-analyzer-cz

go 1.21
go 1.22

require (
github.com/go-semantic-release/semantic-release/v2 v2.28.0
Expand Down
39 changes: 28 additions & 11 deletions pkg/analyzer/commit_analyzer.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
package analyzer

import (
"cmp"
"fmt"
"strings"

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

var CAVERSION = "dev"

type DefaultCommitAnalyzer struct{}
type DefaultCommitAnalyzer struct {
majorReleaseRules releaseRules
minorReleaseRules releaseRules
patchReleaseRules releaseRules
}

func (da *DefaultCommitAnalyzer) Init(m map[string]string) error {
// TODO: implement config parsing
var err error
da.majorReleaseRules, err = parseRules(cmp.Or(m["major_release_rules"], defaultMajorReleaseRules))
if err != nil {
return fmt.Errorf("failed to parse major release rules: %w", err)
}
da.minorReleaseRules, err = parseRules(cmp.Or(m["minor_release_rules"], defaultMinorReleaseRules))
if err != nil {
return fmt.Errorf("failed to parse minor release rules: %w", err)
}
da.patchReleaseRules, err = parseRules(cmp.Or(m["patch_release_rules"], defaultPatchReleaseRules))
if err != nil {
return fmt.Errorf("failed to parse patch release rules: %w", err)
}
return nil
}

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

func (da *DefaultCommitAnalyzer) setTypeAndChange(c *semrel.Commit) {
found := commitPattern.FindAllStringSubmatch(c.Raw[0], -1)
if len(found) < 1 {
// commit message does not match pattern
pc := parseCommit(c.Raw[0])
if pc == nil {
return
}

c.Type = strings.ToLower(found[0][1])
c.Scope = found[0][2]
c.Message = found[0][4]
c.Type = pc.Type
c.Scope = pc.Scope
c.Message = pc.Message

c.Change = &semrel.Change{
// either uses the `!` convention or has a breaking change section
Major: found[0][3] == "!" || matchesBreakingPattern(c),
Minor: c.Type == "feat",
Patch: c.Type == "fix",
Major: da.majorReleaseRules.Matches(pc) || matchesBreakingPattern(c),
Minor: da.minorReleaseRules.Matches(pc),
Patch: da.patchReleaseRules.Matches(pc),
}
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/analyzer/commit_analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func createRawCommit(sha, message string) *semrel.RawCommit {

func TestAnnotations(t *testing.T) {
defaultAnalyzer := &DefaultCommitAnalyzer{}
require.NoError(t, defaultAnalyzer.Init(map[string]string{}))
rawCommit := createRawCommit("a", "fix: bug #123 and #243\nthanks @Test-user for providing this fix\n\nCloses #22")
commit := defaultAnalyzer.analyzeSingleCommit(rawCommit)
require.Equal(t, rawCommit.SHA, commit.SHA)
Expand Down Expand Up @@ -64,13 +65,13 @@ func TestDefaultAnalyzer(t *testing.T) {
createRawCommit("e", "feat!: modified login endpoint"),
"feat",
"",
&semrel.Change{Major: true, Minor: true, Patch: false},
&semrel.Change{Major: true, Minor: false, Patch: false},
},
{
createRawCommit("f", "fix!: fixed a typo"),
"fix",
"",
&semrel.Change{Major: true, Minor: false, Patch: true},
&semrel.Change{Major: true, Minor: false, Patch: false},
},
{
createRawCommit("g", "refactor(parser)!: drop support for Node 6\n\nBREAKING CHANGE: refactor to use JavaScript features not available in Node 6."),
Expand Down Expand Up @@ -99,6 +100,7 @@ func TestDefaultAnalyzer(t *testing.T) {
}

defaultAnalyzer := &DefaultCommitAnalyzer{}
require.NoError(t, defaultAnalyzer.Init(map[string]string{}))
for _, tc := range testCases {
t.Run(tc.RawCommit.RawMessage, func(t *testing.T) {
analyzedCommit := defaultAnalyzer.analyzeSingleCommit(tc.RawCommit)
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

var (
defaultMajorReleaseRules = "*(*)!"
defaultMajorReleaseRules = "*!"
defaultMinorReleaseRules = "feat"
defaultPatchReleaseRules = "fix"
)
Expand Down

0 comments on commit 236195c

Please sign in to comment.