mirror of
https://github.com/thegeeklab/git-sv.git
synced 2024-11-10 02:10:38 +00:00
refactor: rename ValidateMessageProcessor to MessageProcessor
This commit is contained in:
parent
9947c06b72
commit
8ea624efa3
@ -189,7 +189,7 @@ func tagHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor) func(c *c
|
||||
}
|
||||
}
|
||||
|
||||
func commitHandler(cfg Config, git sv.Git, messageProcessor sv.ValidateMessageProcessor) func(c *cli.Context) error {
|
||||
func commitHandler(cfg Config, git sv.Git, messageProcessor sv.MessageProcessor) func(c *cli.Context) error {
|
||||
return func(c *cli.Context) error {
|
||||
|
||||
ctype, err := promptType()
|
||||
@ -294,10 +294,10 @@ func changelogHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, rnP
|
||||
}
|
||||
}
|
||||
|
||||
func validateCommitMessageHandler(git sv.Git, validateMessageProcessor sv.ValidateMessageProcessor) func(c *cli.Context) error {
|
||||
func validateCommitMessageHandler(git sv.Git, messageProcessor sv.MessageProcessor) func(c *cli.Context) error {
|
||||
return func(c *cli.Context) error {
|
||||
branch := git.Branch()
|
||||
if validateMessageProcessor.SkipBranch(branch) {
|
||||
if messageProcessor.SkipBranch(branch) {
|
||||
warn("commit message validation skipped, branch in ignore list...")
|
||||
return nil
|
||||
}
|
||||
@ -309,11 +309,11 @@ func validateCommitMessageHandler(git sv.Git, validateMessageProcessor sv.Valida
|
||||
return fmt.Errorf("failed to read commit message, error: %s", err.Error())
|
||||
}
|
||||
|
||||
if err := validateMessageProcessor.Validate(commitMessage); err != nil {
|
||||
if err := messageProcessor.Validate(commitMessage); err != nil {
|
||||
return fmt.Errorf("invalid commit message, error: %s", err.Error())
|
||||
}
|
||||
|
||||
msg, err := validateMessageProcessor.Enhance(branch, commitMessage)
|
||||
msg, err := messageProcessor.Enhance(branch, commitMessage)
|
||||
if err != nil {
|
||||
warn("could not enhance commit message, %s", err.Error())
|
||||
return nil
|
||||
|
@ -23,7 +23,7 @@ func main() {
|
||||
semverProcessor := sv.NewSemVerCommitsProcessor(cfg.IncludeUnknownTypeAsPatch, cfg.MajorVersionTypes, cfg.MinorVersionTypes, cfg.PatchVersionTypes)
|
||||
releasenotesProcessor := sv.NewReleaseNoteProcessor(cfg.ReleaseNotesTags)
|
||||
outputFormatter := sv.NewOutputFormatter()
|
||||
validateMessageProcessor := sv.NewValidateMessageProcessor(cfg.ValidateMessageSkipBranches, cfg.CommitMessageTypes, cfg.IssueKeyName, cfg.BranchIssueRegex, cfg.IssueRegex)
|
||||
messageProcessor := sv.NewMessageProcessor(cfg.ValidateMessageSkipBranches, cfg.CommitMessageTypes, cfg.IssueKeyName, cfg.BranchIssueRegex, cfg.IssueRegex)
|
||||
|
||||
app := cli.NewApp()
|
||||
app.Name = "sv"
|
||||
@ -76,13 +76,13 @@ func main() {
|
||||
Name: "commit",
|
||||
Aliases: []string{"cmt"},
|
||||
Usage: "execute git commit with convetional commit message helper",
|
||||
Action: commitHandler(cfg, git, validateMessageProcessor),
|
||||
Action: commitHandler(cfg, git, messageProcessor),
|
||||
},
|
||||
{
|
||||
Name: "validate-commit-message",
|
||||
Aliases: []string{"vcm"},
|
||||
Usage: "use as prepare-commit-message hook to validate message",
|
||||
Action: validateCommitMessageHandler(git, validateMessageProcessor),
|
||||
Action: validateCommitMessageHandler(git, messageProcessor),
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{Name: "path", Required: true, Usage: "git working directory"},
|
||||
&cli.StringFlag{Name: "file", Required: true, Usage: "name of the file that contains the commit log message"},
|
||||
|
@ -9,8 +9,8 @@ import (
|
||||
|
||||
const breakingChangeKey = "BREAKING CHANGE"
|
||||
|
||||
// ValidateMessageProcessor interface.
|
||||
type ValidateMessageProcessor interface {
|
||||
// MessageProcessor interface.
|
||||
type MessageProcessor interface {
|
||||
SkipBranch(branch string) bool
|
||||
Validate(message string) error
|
||||
Enhance(branch string, message string) (string, error)
|
||||
@ -18,9 +18,9 @@ type ValidateMessageProcessor interface {
|
||||
Format(ctype, scope, subject, body, issue, breakingChanges string) (string, string, string)
|
||||
}
|
||||
|
||||
// NewValidateMessageProcessor ValidateMessageProcessorImpl constructor
|
||||
func NewValidateMessageProcessor(skipBranches, supportedTypes []string, issueKeyName, branchIssueRegex, issueRegex string) *ValidateMessageProcessorImpl {
|
||||
return &ValidateMessageProcessorImpl{
|
||||
// NewMessageProcessor MessageProcessorImpl constructor
|
||||
func NewMessageProcessor(skipBranches, supportedTypes []string, issueKeyName, branchIssueRegex, issueRegex string) *MessageProcessorImpl {
|
||||
return &MessageProcessorImpl{
|
||||
skipBranches: skipBranches,
|
||||
supportedTypes: supportedTypes,
|
||||
issueKeyName: issueKeyName,
|
||||
@ -29,8 +29,8 @@ func NewValidateMessageProcessor(skipBranches, supportedTypes []string, issueKey
|
||||
}
|
||||
}
|
||||
|
||||
// ValidateMessageProcessorImpl process validate message hook.
|
||||
type ValidateMessageProcessorImpl struct {
|
||||
// MessageProcessorImpl process validate message hook.
|
||||
type MessageProcessorImpl struct {
|
||||
skipBranches []string
|
||||
supportedTypes []string
|
||||
issueKeyName string
|
||||
@ -39,12 +39,12 @@ type ValidateMessageProcessorImpl struct {
|
||||
}
|
||||
|
||||
// SkipBranch check if branch should be ignored.
|
||||
func (p ValidateMessageProcessorImpl) SkipBranch(branch string) bool {
|
||||
func (p MessageProcessorImpl) SkipBranch(branch string) bool {
|
||||
return contains(branch, p.skipBranches)
|
||||
}
|
||||
|
||||
// Validate commit message.
|
||||
func (p ValidateMessageProcessorImpl) Validate(message string) error {
|
||||
func (p MessageProcessorImpl) Validate(message string) error {
|
||||
valid, err := regexp.MatchString("^("+strings.Join(p.supportedTypes, "|")+")(\\(.+\\))?!?: .*$", firstLine(message))
|
||||
if err != nil {
|
||||
return err
|
||||
@ -56,7 +56,7 @@ func (p ValidateMessageProcessorImpl) Validate(message string) error {
|
||||
}
|
||||
|
||||
// Enhance add metadata on commit message.
|
||||
func (p ValidateMessageProcessorImpl) Enhance(branch string, message string) (string, error) {
|
||||
func (p MessageProcessorImpl) Enhance(branch string, message string) (string, error) {
|
||||
if p.branchIssueRegex == "" || p.issueKeyName == "" || hasIssueID(message, p.issueKeyName) {
|
||||
return "", nil //enhance disabled
|
||||
}
|
||||
@ -79,7 +79,7 @@ func (p ValidateMessageProcessorImpl) Enhance(branch string, message string) (st
|
||||
}
|
||||
|
||||
// IssueID try to extract issue id from branch, return empty if not found
|
||||
func (p ValidateMessageProcessorImpl) IssueID(branch string) (string, error) {
|
||||
func (p MessageProcessorImpl) IssueID(branch string) (string, error) {
|
||||
r, err := regexp.Compile(p.branchIssueRegex)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("could not compile issue regex: %s, error: %v", p.branchIssueRegex, err.Error())
|
||||
@ -93,7 +93,7 @@ func (p ValidateMessageProcessorImpl) IssueID(branch string) (string, error) {
|
||||
}
|
||||
|
||||
// Format format commit message to header, body and footer
|
||||
func (p ValidateMessageProcessorImpl) Format(ctype, scope, subject, body, issue, breakingChanges string) (string, string, string) {
|
||||
func (p MessageProcessorImpl) Format(ctype, scope, subject, body, issue, breakingChanges string) (string, string, string) {
|
||||
var header strings.Builder
|
||||
header.WriteString(ctype)
|
||||
if scope != "" {
|
@ -45,8 +45,8 @@ BREAKING CHANGE: refactor to use JavaScript features not available in Node 6.`
|
||||
|
||||
// multiline samples end
|
||||
|
||||
func TestValidateMessageProcessorImpl_Validate(t *testing.T) {
|
||||
p := NewValidateMessageProcessor([]string{"develop", "master"}, []string{"feat", "fix"}, "jira", branchIssueRegex, issueRegex)
|
||||
func TestMessageProcessorImpl_Validate(t *testing.T) {
|
||||
p := NewMessageProcessor([]string{"develop", "master"}, []string{"feat", "fix"}, "jira", branchIssueRegex, issueRegex)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@ -72,14 +72,14 @@ func TestValidateMessageProcessorImpl_Validate(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := p.Validate(tt.message); (err != nil) != tt.wantErr {
|
||||
t.Errorf("ValidateMessageProcessorImpl.Validate() error = %v, wantErr %v", err, tt.wantErr)
|
||||
t.Errorf("MessageProcessorImpl.Validate() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateMessageProcessorImpl_Enhance(t *testing.T) {
|
||||
p := NewValidateMessageProcessor([]string{"develop", "master"}, []string{"feat", "fix"}, "jira", branchIssueRegex, issueRegex)
|
||||
func TestMessageProcessorImpl_Enhance(t *testing.T) {
|
||||
p := NewMessageProcessor([]string{"develop", "master"}, []string{"feat", "fix"}, "jira", branchIssueRegex, issueRegex)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@ -101,11 +101,11 @@ func TestValidateMessageProcessorImpl_Enhance(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := p.Enhance(tt.branch, tt.message)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ValidateMessageProcessorImpl.Enhance() error = %v, wantErr %v", err, tt.wantErr)
|
||||
t.Errorf("MessageProcessorImpl.Enhance() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Errorf("ValidateMessageProcessorImpl.Enhance() = %v, want %v", got, tt.want)
|
||||
t.Errorf("MessageProcessorImpl.Enhance() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user