2024-07-12 15:32:35 +02:00
|
|
|
package rp
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"github.com/leodido/go-conventionalcommits"
|
|
|
|
|
"github.com/leodido/go-conventionalcommits/parser"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type AnalyzedCommit struct {
|
|
|
|
|
Commit
|
2024-08-01 23:00:56 +02:00
|
|
|
Type string
|
|
|
|
|
Description string
|
|
|
|
|
Scope *string
|
|
|
|
|
BreakingChange bool
|
2024-07-12 15:32:35 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-08 19:01:44 +02:00
|
|
|
type CommitParser interface {
|
|
|
|
|
Analyze(commits []Commit) ([]AnalyzedCommit, error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ConventionalCommitsParser struct {
|
|
|
|
|
machine conventionalcommits.Machine
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewConventionalCommitsParser() *ConventionalCommitsParser {
|
2024-07-12 15:32:35 +02:00
|
|
|
parserMachine := parser.NewMachine(
|
|
|
|
|
parser.WithBestEffort(),
|
|
|
|
|
parser.WithTypes(conventionalcommits.TypesConventional),
|
|
|
|
|
)
|
|
|
|
|
|
2024-08-08 19:01:44 +02:00
|
|
|
return &ConventionalCommitsParser{
|
|
|
|
|
machine: parserMachine,
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-12 15:32:35 +02:00
|
|
|
|
2024-08-08 19:01:44 +02:00
|
|
|
func (c *ConventionalCommitsParser) AnalyzeCommits(commits []Commit) ([]AnalyzedCommit, error) {
|
|
|
|
|
analyzedCommits := make([]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
|
|
|
|
|
analyzedCommits = append(analyzedCommits, 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
|
|
|
}
|