Skip to content
Snippets Groups Projects
Commit 236195ce authored by Christoph Witzko's avatar Christoph Witzko
Browse files

feat: use release rules logic

parent 3843c1ab
No related branches found
No related tags found
No related merge requests found
...@@ -13,7 +13,7 @@ jobs: ...@@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- uses: actions/setup-go@v4 - uses: actions/setup-go@v4
with: with:
go-version: '1.21' go-version: '1.22'
- uses: golangci/golangci-lint-action@v3 - uses: golangci/golangci-lint-action@v3
build: build:
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
...@@ -26,7 +26,7 @@ jobs: ...@@ -26,7 +26,7 @@ jobs:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- uses: actions/setup-go@v4 - uses: actions/setup-go@v4
with: with:
go-version: '1.21' go-version: '1.22'
- run: go build ./cmd/commit-analyzer-cz/ - run: go build ./cmd/commit-analyzer-cz/
- run: go test -v ./... - run: go test -v ./...
release: release:
...@@ -36,7 +36,7 @@ jobs: ...@@ -36,7 +36,7 @@ jobs:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- uses: actions/setup-go@v4 - uses: actions/setup-go@v4
with: with:
go-version: '1.21' go-version: '1.22'
- uses: go-semantic-release/action@v1 - uses: go-semantic-release/action@v1
with: with:
hooks: goreleaser,plugin-registry-update hooks: goreleaser,plugin-registry-update
......
package analyzer package analyzer
import ( import (
"cmp"
"fmt"
"strings" "strings"
"github.com/go-semantic-release/semantic-release/v2/pkg/semrel" "github.com/go-semantic-release/semantic-release/v2/pkg/semrel"
...@@ -8,10 +10,26 @@ import ( ...@@ -8,10 +10,26 @@ import (
var CAVERSION = "dev" var CAVERSION = "dev"
type DefaultCommitAnalyzer struct{} type DefaultCommitAnalyzer struct {
majorReleaseRules releaseRules
minorReleaseRules releaseRules
patchReleaseRules releaseRules
}
func (da *DefaultCommitAnalyzer) Init(m map[string]string) error { 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 return nil
} }
...@@ -24,21 +42,20 @@ func (da *DefaultCommitAnalyzer) Version() string { ...@@ -24,21 +42,20 @@ func (da *DefaultCommitAnalyzer) Version() string {
} }
func (da *DefaultCommitAnalyzer) setTypeAndChange(c *semrel.Commit) { func (da *DefaultCommitAnalyzer) setTypeAndChange(c *semrel.Commit) {
found := commitPattern.FindAllStringSubmatch(c.Raw[0], -1) pc := parseCommit(c.Raw[0])
if len(found) < 1 { if pc == nil {
// commit message does not match pattern
return return
} }
c.Type = strings.ToLower(found[0][1]) c.Type = pc.Type
c.Scope = found[0][2] c.Scope = pc.Scope
c.Message = found[0][4] c.Message = pc.Message
c.Change = &semrel.Change{ c.Change = &semrel.Change{
// either uses the `!` convention or has a breaking change section // either uses the `!` convention or has a breaking change section
Major: found[0][3] == "!" || matchesBreakingPattern(c), Major: da.majorReleaseRules.Matches(pc) || matchesBreakingPattern(c),
Minor: c.Type == "feat", Minor: da.minorReleaseRules.Matches(pc),
Patch: c.Type == "fix", Patch: da.patchReleaseRules.Matches(pc),
} }
} }
......
...@@ -20,6 +20,7 @@ func createRawCommit(sha, message string) *semrel.RawCommit { ...@@ -20,6 +20,7 @@ func createRawCommit(sha, message string) *semrel.RawCommit {
func TestAnnotations(t *testing.T) { func TestAnnotations(t *testing.T) {
defaultAnalyzer := &DefaultCommitAnalyzer{} 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") rawCommit := createRawCommit("a", "fix: bug #123 and #243\nthanks @Test-user for providing this fix\n\nCloses #22")
commit := defaultAnalyzer.analyzeSingleCommit(rawCommit) commit := defaultAnalyzer.analyzeSingleCommit(rawCommit)
require.Equal(t, rawCommit.SHA, commit.SHA) require.Equal(t, rawCommit.SHA, commit.SHA)
...@@ -64,13 +65,13 @@ func TestDefaultAnalyzer(t *testing.T) { ...@@ -64,13 +65,13 @@ func TestDefaultAnalyzer(t *testing.T) {
createRawCommit("e", "feat!: modified login endpoint"), createRawCommit("e", "feat!: modified login endpoint"),
"feat", "feat",
"", "",
&semrel.Change{Major: true, Minor: true, Patch: false}, &semrel.Change{Major: true, Minor: false, Patch: false},
}, },
{ {
createRawCommit("f", "fix!: fixed a typo"), createRawCommit("f", "fix!: fixed a typo"),
"fix", "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."), 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) { ...@@ -99,6 +100,7 @@ func TestDefaultAnalyzer(t *testing.T) {
} }
defaultAnalyzer := &DefaultCommitAnalyzer{} defaultAnalyzer := &DefaultCommitAnalyzer{}
require.NoError(t, defaultAnalyzer.Init(map[string]string{}))
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.RawCommit.RawMessage, func(t *testing.T) { t.Run(tc.RawCommit.RawMessage, func(t *testing.T) {
analyzedCommit := defaultAnalyzer.analyzeSingleCommit(tc.RawCommit) analyzedCommit := defaultAnalyzer.analyzeSingleCommit(tc.RawCommit)
......
...@@ -7,7 +7,7 @@ import ( ...@@ -7,7 +7,7 @@ import (
) )
var ( var (
defaultMajorReleaseRules = "*(*)!" defaultMajorReleaseRules = "*!"
defaultMinorReleaseRules = "feat" defaultMinorReleaseRules = "feat"
defaultPatchReleaseRules = "fix" defaultPatchReleaseRules = "fix"
) )
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment