2024-08-31 15:23:21 +02:00
|
|
|
package conventionalcommits
|
2024-07-12 15:32:35 +02:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"github.com/leodido/go-conventionalcommits"
|
|
|
|
|
"github.com/leodido/go-conventionalcommits/parser"
|
|
|
|
|
|
2024-08-31 15:23:21 +02:00
|
|
|
"github.com/apricote/releaser-pleaser/internal/commitparser"
|
|
|
|
|
"github.com/apricote/releaser-pleaser/internal/git"
|
|
|
|
|
)
|
2024-08-08 19:01:44 +02:00
|
|
|
|
2024-08-31 15:23:21 +02:00
|
|
|
type Parser struct {
|
2024-08-08 19:01:44 +02:00
|
|
|
machine conventionalcommits.Machine
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-31 15:23:21 +02:00
|
|
|
func NewParser() *Parser {
|
2024-07-12 15:32:35 +02:00
|
|
|
parserMachine := parser.NewMachine(
|
|
|
|
|
parser.WithBestEffort(),
|
|
|
|
|
parser.WithTypes(conventionalcommits.TypesConventional),
|
|
|
|
|
)
|
|
|
|
|
|
2024-08-31 15:23:21 +02:00
|
|
|
return &Parser{
|
2024-08-08 19:01:44 +02:00
|
|
|
machine: parserMachine,
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-12 15:32:35 +02:00
|
|
|
|
2024-08-31 15:23:21 +02:00
|
|
|
func (c *Parser) Analyze(commits []git.Commit) ([]commitparser.AnalyzedCommit, error) {
|
|
|
|
|
analyzedCommits := make([]commitparser.AnalyzedCommit, 0, len(commits))
|
2024-07-12 15:32:35 +02:00
|
|
|
|
|
|
|
|
for _, commit := range commits {
|
2024-08-08 19:01:44 +02:00
|
|
|
msg, err := c.machine.Parse([]byte(commit.Message))
|
2024-07-12 15:32:35 +02:00
|
|
|
if err != nil {
|
2024-08-08 19:01:44 +02:00
|
|
|
return nil, fmt.Errorf("failed to parse message of commit %q: %w", commit.Hash, err)
|
2024-07-12 15:32:35 +02:00
|
|
|
}
|
|
|
|
|
conventionalCommit, ok := msg.(*conventionalcommits.ConventionalCommit)
|
|
|
|
|
if !ok {
|
2024-08-08 19:01:44 +02:00
|
|
|
return nil, fmt.Errorf("unable to get ConventionalCommit from parser result: %T", msg)
|
2024-07-12 15:32:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
commitVersionBump := conventionalCommit.VersionBump(conventionalcommits.DefaultStrategy)
|
|
|
|
|
if commitVersionBump > conventionalcommits.UnknownVersion {
|
|
|
|
|
// We only care about releasable commits
|
2024-08-31 15:23:21 +02:00
|
|
|
analyzedCommits = append(analyzedCommits, commitparser.AnalyzedCommit{
|
2024-08-01 23:00:56 +02:00
|
|
|
Commit: commit,
|
|
|
|
|
Type: conventionalCommit.Type,
|
|
|
|
|
Description: conventionalCommit.Description,
|
|
|
|
|
Scope: conventionalCommit.Scope,
|
|
|
|
|
BreakingChange: conventionalCommit.IsBreakingChange(),
|
2024-07-12 15:32:35 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-08 19:01:44 +02:00
|
|
|
return analyzedCommits, nil
|
2024-07-12 15:32:35 +02:00
|
|
|
}
|