mirror of
https://github.com/apricote/releaser-pleaser.git
synced 2026-02-07 10:17:02 +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
|
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(
|
parserMachine := parser.NewMachine(
|
||||||
parser.WithBestEffort(),
|
parser.WithBestEffort(),
|
||||||
parser.WithTypes(conventionalcommits.TypesConventional),
|
parser.WithTypes(conventionalcommits.TypesConventional),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
return &ConventionalCommitsParser{
|
||||||
|
machine: parserMachine,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ConventionalCommitsParser) AnalyzeCommits(commits []Commit) ([]AnalyzedCommit, error) {
|
||||||
analyzedCommits := make([]AnalyzedCommit, 0, len(commits))
|
analyzedCommits := make([]AnalyzedCommit, 0, len(commits))
|
||||||
|
|
||||||
highestVersionBump := conventionalcommits.UnknownVersion
|
|
||||||
|
|
||||||
for _, commit := range commits {
|
for _, commit := range commits {
|
||||||
msg, err := parserMachine.Parse([]byte(commit.Message))
|
msg, err := c.machine.Parse([]byte(commit.Message))
|
||||||
if err != nil {
|
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)
|
conventionalCommit, ok := msg.(*conventionalcommits.ConventionalCommit)
|
||||||
if !ok {
|
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)
|
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 (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/leodido/go-conventionalcommits"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -12,14 +11,12 @@ func TestAnalyzeCommits(t *testing.T) {
|
||||||
name string
|
name string
|
||||||
commits []Commit
|
commits []Commit
|
||||||
expectedCommits []AnalyzedCommit
|
expectedCommits []AnalyzedCommit
|
||||||
expectedBump conventionalcommits.VersionBump
|
|
||||||
wantErr assert.ErrorAssertionFunc
|
wantErr assert.ErrorAssertionFunc
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "empty commits",
|
name: "empty commits",
|
||||||
commits: []Commit{},
|
commits: []Commit{},
|
||||||
expectedCommits: []AnalyzedCommit{},
|
expectedCommits: []AnalyzedCommit{},
|
||||||
expectedBump: conventionalcommits.UnknownVersion,
|
|
||||||
wantErr: assert.NoError,
|
wantErr: assert.NoError,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
@ -30,7 +27,6 @@ func TestAnalyzeCommits(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expectedCommits: nil,
|
expectedCommits: nil,
|
||||||
expectedBump: conventionalcommits.UnknownVersion,
|
|
||||||
wantErr: assert.Error,
|
wantErr: assert.Error,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
@ -41,7 +37,6 @@ func TestAnalyzeCommits(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expectedCommits: []AnalyzedCommit{},
|
expectedCommits: []AnalyzedCommit{},
|
||||||
expectedBump: conventionalcommits.UnknownVersion,
|
|
||||||
wantErr: assert.NoError,
|
wantErr: assert.NoError,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
@ -61,8 +56,7 @@ func TestAnalyzeCommits(t *testing.T) {
|
||||||
Description: "blabla",
|
Description: "blabla",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expectedBump: conventionalcommits.PatchVersion,
|
wantErr: assert.NoError,
|
||||||
wantErr: assert.NoError,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "highest bump (minor)",
|
name: "highest bump (minor)",
|
||||||
|
|
@ -86,8 +80,7 @@ func TestAnalyzeCommits(t *testing.T) {
|
||||||
Description: "foobar",
|
Description: "foobar",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expectedBump: conventionalcommits.MinorVersion,
|
wantErr: assert.NoError,
|
||||||
wantErr: assert.NoError,
|
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
@ -113,19 +106,17 @@ func TestAnalyzeCommits(t *testing.T) {
|
||||||
BreakingChange: true,
|
BreakingChange: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expectedBump: conventionalcommits.MajorVersion,
|
wantErr: assert.NoError,
|
||||||
wantErr: assert.NoError,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
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) {
|
if !tt.wantErr(t, err) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Equal(t, tt.expectedCommits, analyzedCommits)
|
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())
|
log = log.With("pullrequest.id", pullrequest.GetID())
|
||||||
|
|
||||||
// TODO: Parse PR description for overrides
|
// TODO: Parse PR description for overrides
|
||||||
changelogEntries, _, err := AnalyzeCommits([]Commit{commit})
|
changelogEntries, err := NewConventionalCommitsParser().AnalyzeCommits([]Commit{commit})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("unable to parse changelog entries", "error", err)
|
log.Warn("unable to parse changelog entries", "error", err)
|
||||||
continue
|
continue
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue