mirror of
https://github.com/apricote/releaser-pleaser.git
synced 2026-01-13 13:21:00 +00:00
refactor: interface for commit message analyzer
This commit is contained in:
parent
8f106e4028
commit
7797a86a48
3 changed files with 24 additions and 25 deletions
30
commits.go
30
commits.go
|
|
@ -15,24 +15,36 @@ type AnalyzedCommit struct {
|
|||
BreakingChange bool
|
||||
}
|
||||
|
||||
func AnalyzeCommits(commits []Commit) ([]AnalyzedCommit, conventionalcommits.VersionBump, error) {
|
||||
type CommitParser interface {
|
||||
Analyze(commits []Commit) ([]AnalyzedCommit, error)
|
||||
}
|
||||
|
||||
type ConventionalCommitsParser struct {
|
||||
machine conventionalcommits.Machine
|
||||
}
|
||||
|
||||
func NewConventionalCommitsParser() *ConventionalCommitsParser {
|
||||
parserMachine := parser.NewMachine(
|
||||
parser.WithBestEffort(),
|
||||
parser.WithTypes(conventionalcommits.TypesConventional),
|
||||
)
|
||||
|
||||
return &ConventionalCommitsParser{
|
||||
machine: parserMachine,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *ConventionalCommitsParser) AnalyzeCommits(commits []Commit) ([]AnalyzedCommit, error) {
|
||||
analyzedCommits := make([]AnalyzedCommit, 0, len(commits))
|
||||
|
||||
highestVersionBump := conventionalcommits.UnknownVersion
|
||||
|
||||
for _, commit := range commits {
|
||||
msg, err := parserMachine.Parse([]byte(commit.Message))
|
||||
msg, err := c.machine.Parse([]byte(commit.Message))
|
||||
if err != nil {
|
||||
return nil, conventionalcommits.UnknownVersion, fmt.Errorf("failed to parse message of commit %q: %w", commit.Hash, err)
|
||||
return nil, fmt.Errorf("failed to parse message of commit %q: %w", commit.Hash, err)
|
||||
}
|
||||
conventionalCommit, ok := msg.(*conventionalcommits.ConventionalCommit)
|
||||
if !ok {
|
||||
return nil, conventionalcommits.UnknownVersion, fmt.Errorf("unable to get ConventionalCommit from parser result: %T", msg)
|
||||
return nil, fmt.Errorf("unable to get ConventionalCommit from parser result: %T", msg)
|
||||
}
|
||||
|
||||
commitVersionBump := conventionalCommit.VersionBump(conventionalcommits.DefaultStrategy)
|
||||
|
|
@ -47,11 +59,7 @@ func AnalyzeCommits(commits []Commit) ([]AnalyzedCommit, conventionalcommits.Ver
|
|||
})
|
||||
}
|
||||
|
||||
if commitVersionBump > highestVersionBump {
|
||||
// Get max version bump from all releasable commits
|
||||
highestVersionBump = commitVersionBump
|
||||
}
|
||||
}
|
||||
|
||||
return analyzedCommits, highestVersionBump, nil
|
||||
return analyzedCommits, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ package rp
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/leodido/go-conventionalcommits"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
|
@ -12,14 +11,12 @@ func TestAnalyzeCommits(t *testing.T) {
|
|||
name string
|
||||
commits []Commit
|
||||
expectedCommits []AnalyzedCommit
|
||||
expectedBump conventionalcommits.VersionBump
|
||||
wantErr assert.ErrorAssertionFunc
|
||||
}{
|
||||
{
|
||||
name: "empty commits",
|
||||
commits: []Commit{},
|
||||
expectedCommits: []AnalyzedCommit{},
|
||||
expectedBump: conventionalcommits.UnknownVersion,
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
{
|
||||
|
|
@ -30,7 +27,6 @@ func TestAnalyzeCommits(t *testing.T) {
|
|||
},
|
||||
},
|
||||
expectedCommits: nil,
|
||||
expectedBump: conventionalcommits.UnknownVersion,
|
||||
wantErr: assert.Error,
|
||||
},
|
||||
{
|
||||
|
|
@ -41,7 +37,6 @@ func TestAnalyzeCommits(t *testing.T) {
|
|||
},
|
||||
},
|
||||
expectedCommits: []AnalyzedCommit{},
|
||||
expectedBump: conventionalcommits.UnknownVersion,
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
{
|
||||
|
|
@ -61,8 +56,7 @@ func TestAnalyzeCommits(t *testing.T) {
|
|||
Description: "blabla",
|
||||
},
|
||||
},
|
||||
expectedBump: conventionalcommits.PatchVersion,
|
||||
wantErr: assert.NoError,
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
{
|
||||
name: "highest bump (minor)",
|
||||
|
|
@ -86,8 +80,7 @@ func TestAnalyzeCommits(t *testing.T) {
|
|||
Description: "foobar",
|
||||
},
|
||||
},
|
||||
expectedBump: conventionalcommits.MinorVersion,
|
||||
wantErr: assert.NoError,
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
|
||||
{
|
||||
|
|
@ -113,19 +106,17 @@ func TestAnalyzeCommits(t *testing.T) {
|
|||
BreakingChange: true,
|
||||
},
|
||||
},
|
||||
expectedBump: conventionalcommits.MajorVersion,
|
||||
wantErr: assert.NoError,
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
analyzedCommits, versionBump, err := AnalyzeCommits(tt.commits)
|
||||
analyzedCommits, err := NewConventionalCommitsParser().AnalyzeCommits(tt.commits)
|
||||
if !tt.wantErr(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
assert.Equal(t, tt.expectedCommits, analyzedCommits)
|
||||
assert.Equal(t, tt.expectedBump, versionBump)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
2
forge.go
2
forge.go
|
|
@ -308,7 +308,7 @@ func (g *GitHub) Changesets(ctx context.Context, commits []Commit) ([]Changeset,
|
|||
log = log.With("pullrequest.id", pullrequest.GetID())
|
||||
|
||||
// TODO: Parse PR description for overrides
|
||||
changelogEntries, _, err := AnalyzeCommits([]Commit{commit})
|
||||
changelogEntries, err := NewConventionalCommitsParser().AnalyzeCommits([]Commit{commit})
|
||||
if err != nil {
|
||||
log.Warn("unable to parse changelog entries", "error", err)
|
||||
continue
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue