| | | 1 | | package plugins |
| | | 2 | | |
| | | 3 | | import ( |
| | | 4 | | "context" |
| | | 5 | | "fmt" |
| | | 6 | | "strings" |
| | | 7 | | |
| | | 8 | | "github.com/jedi-knights/go-semantic-release/internal/domain" |
| | | 9 | | "github.com/jedi-knights/go-semantic-release/internal/ports" |
| | | 10 | | ) |
| | | 11 | | |
| | | 12 | | // Compile-time interface compliance checks. |
| | | 13 | | var ( |
| | | 14 | | _ ports.Plugin = (*LintPlugin)(nil) |
| | | 15 | | _ ports.VerifyReleasePlugin = (*LintPlugin)(nil) |
| | | 16 | | ) |
| | | 17 | | |
| | | 18 | | // LintPlugin implements VerifyReleasePlugin by linting commit messages. |
| | | 19 | | type LintPlugin struct { |
| | | 20 | | linter ports.CommitLinter |
| | | 21 | | logger ports.Logger |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | // NewLintPlugin creates a commit linting plugin. |
| | | 25 | | func NewLintPlugin(linter ports.CommitLinter, logger ports.Logger) *LintPlugin { |
| | | 26 | | return &LintPlugin{linter: linter, logger: logger} |
| | | 27 | | } |
| | | 28 | | |
| | 1 | 29 | | func (p *LintPlugin) Name() string { return "commit-lint" } |
| | | 30 | | |
| | | 31 | | // VerifyRelease lints all commits and returns an error if any have error-severity violations. |
| | 5 | 32 | | func (p *LintPlugin) VerifyRelease(_ context.Context, rc *domain.ReleaseContext) error { |
| | 5 | 33 | | var allErrors []string |
| | 5 | 34 | | |
| | 5 | 35 | | for i := range rc.Commits { |
| | 5 | 36 | | violations := p.linter.Lint(rc.Commits[i]) |
| | 3 | 37 | | for _, v := range violations { |
| | 3 | 38 | | msg := fmt.Sprintf("%s (%s): %s [%s]", rc.Commits[i].Hash[:minInt(7, len(rc.Commits[i].Hash))], v.Severity, v.Mess |
| | 2 | 39 | | if v.Severity == domain.LintError { |
| | 2 | 40 | | allErrors = append(allErrors, msg) |
| | 1 | 41 | | } else { |
| | 1 | 42 | | p.logger.Warn("lint warning", "commit", rc.Commits[i].Hash, "rule", v.Rule, "message", v.Message) |
| | 1 | 43 | | } |
| | | 44 | | } |
| | | 45 | | } |
| | | 46 | | |
| | 2 | 47 | | if len(allErrors) > 0 { |
| | 2 | 48 | | return fmt.Errorf("commit lint errors:\n %s", strings.Join(allErrors, "\n ")) |
| | 2 | 49 | | } |
| | 3 | 50 | | return nil |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | func minInt(a, b int) int { |
| | | 54 | | if a < b { |
| | | 55 | | return a |
| | | 56 | | } |
| | | 57 | | return b |
| | | 58 | | } |