mirror of
https://github.com/apricote/releaser-pleaser.git
synced 2026-01-13 13:21:00 +00:00
refactor: move things to packages (#39)
This commit is contained in:
parent
44184a77f9
commit
a0a064d387
32 changed files with 923 additions and 892 deletions
17
internal/commitparser/commitparser.go
Normal file
17
internal/commitparser/commitparser.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package commitparser
|
||||
|
||||
import (
|
||||
"github.com/apricote/releaser-pleaser/internal/git"
|
||||
)
|
||||
|
||||
type CommitParser interface {
|
||||
Analyze(commits []git.Commit) ([]AnalyzedCommit, error)
|
||||
}
|
||||
|
||||
type AnalyzedCommit struct {
|
||||
git.Commit
|
||||
Type string
|
||||
Description string
|
||||
Scope *string
|
||||
BreakingChange bool
|
||||
}
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
package conventionalcommits
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/leodido/go-conventionalcommits"
|
||||
"github.com/leodido/go-conventionalcommits/parser"
|
||||
|
||||
"github.com/apricote/releaser-pleaser/internal/commitparser"
|
||||
"github.com/apricote/releaser-pleaser/internal/git"
|
||||
)
|
||||
|
||||
type Parser struct {
|
||||
machine conventionalcommits.Machine
|
||||
}
|
||||
|
||||
func NewParser() *Parser {
|
||||
parserMachine := parser.NewMachine(
|
||||
parser.WithBestEffort(),
|
||||
parser.WithTypes(conventionalcommits.TypesConventional),
|
||||
)
|
||||
|
||||
return &Parser{
|
||||
machine: parserMachine,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Parser) Analyze(commits []git.Commit) ([]commitparser.AnalyzedCommit, error) {
|
||||
analyzedCommits := make([]commitparser.AnalyzedCommit, 0, len(commits))
|
||||
|
||||
for _, commit := range commits {
|
||||
msg, err := c.machine.Parse([]byte(commit.Message))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse message of commit %q: %w", commit.Hash, err)
|
||||
}
|
||||
conventionalCommit, ok := msg.(*conventionalcommits.ConventionalCommit)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unable to get ConventionalCommit from parser result: %T", msg)
|
||||
}
|
||||
|
||||
commitVersionBump := conventionalCommit.VersionBump(conventionalcommits.DefaultStrategy)
|
||||
if commitVersionBump > conventionalcommits.UnknownVersion {
|
||||
// We only care about releasable commits
|
||||
analyzedCommits = append(analyzedCommits, commitparser.AnalyzedCommit{
|
||||
Commit: commit,
|
||||
Type: conventionalCommit.Type,
|
||||
Description: conventionalCommit.Description,
|
||||
Scope: conventionalCommit.Scope,
|
||||
BreakingChange: conventionalCommit.IsBreakingChange(),
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return analyzedCommits, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,125 @@
|
|||
package conventionalcommits
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/apricote/releaser-pleaser/internal/commitparser"
|
||||
"github.com/apricote/releaser-pleaser/internal/git"
|
||||
)
|
||||
|
||||
func TestAnalyzeCommits(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
commits []git.Commit
|
||||
expectedCommits []commitparser.AnalyzedCommit
|
||||
wantErr assert.ErrorAssertionFunc
|
||||
}{
|
||||
{
|
||||
name: "empty commits",
|
||||
commits: []git.Commit{},
|
||||
expectedCommits: []commitparser.AnalyzedCommit{},
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
{
|
||||
name: "malformed commit message",
|
||||
commits: []git.Commit{
|
||||
{
|
||||
Message: "aksdjaklsdjka",
|
||||
},
|
||||
},
|
||||
expectedCommits: nil,
|
||||
wantErr: assert.Error,
|
||||
},
|
||||
{
|
||||
name: "drops unreleasable",
|
||||
commits: []git.Commit{
|
||||
{
|
||||
Message: "chore: foobar",
|
||||
},
|
||||
},
|
||||
expectedCommits: []commitparser.AnalyzedCommit{},
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
{
|
||||
name: "highest bump (patch)",
|
||||
commits: []git.Commit{
|
||||
{
|
||||
Message: "chore: foobar",
|
||||
},
|
||||
{
|
||||
Message: "fix: blabla",
|
||||
},
|
||||
},
|
||||
expectedCommits: []commitparser.AnalyzedCommit{
|
||||
{
|
||||
Commit: git.Commit{Message: "fix: blabla"},
|
||||
Type: "fix",
|
||||
Description: "blabla",
|
||||
},
|
||||
},
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
{
|
||||
name: "highest bump (minor)",
|
||||
commits: []git.Commit{
|
||||
{
|
||||
Message: "fix: blabla",
|
||||
},
|
||||
{
|
||||
Message: "feat: foobar",
|
||||
},
|
||||
},
|
||||
expectedCommits: []commitparser.AnalyzedCommit{
|
||||
{
|
||||
Commit: git.Commit{Message: "fix: blabla"},
|
||||
Type: "fix",
|
||||
Description: "blabla",
|
||||
},
|
||||
{
|
||||
Commit: git.Commit{Message: "feat: foobar"},
|
||||
Type: "feat",
|
||||
Description: "foobar",
|
||||
},
|
||||
},
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
|
||||
{
|
||||
name: "highest bump (major)",
|
||||
commits: []git.Commit{
|
||||
{
|
||||
Message: "fix: blabla",
|
||||
},
|
||||
{
|
||||
Message: "feat!: foobar",
|
||||
},
|
||||
},
|
||||
expectedCommits: []commitparser.AnalyzedCommit{
|
||||
{
|
||||
Commit: git.Commit{Message: "fix: blabla"},
|
||||
Type: "fix",
|
||||
Description: "blabla",
|
||||
},
|
||||
{
|
||||
Commit: git.Commit{Message: "feat!: foobar"},
|
||||
Type: "feat",
|
||||
Description: "foobar",
|
||||
BreakingChange: true,
|
||||
},
|
||||
},
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
analyzedCommits, err := NewParser().Analyze(tt.commits)
|
||||
if !tt.wantErr(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
assert.Equal(t, tt.expectedCommits, analyzedCommits)
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue