From 8ea624efa3e562e68233c4ab4b839c5395540cca Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Tue, 1 Dec 2020 23:52:15 -0300 Subject: [PATCH] refactor: rename ValidateMessageProcessor to MessageProcessor --- cmd/git-sv/handlers.go | 10 ++++---- cmd/git-sv/main.go | 6 ++--- sv/{validatemessage.go => message.go} | 24 +++++++++---------- ...alidatemessage_test.go => message_test.go} | 14 +++++------ 4 files changed, 27 insertions(+), 27 deletions(-) rename sv/{validatemessage.go => message.go} (78%) rename sv/{validatemessage_test.go => message_test.go} (87%) diff --git a/cmd/git-sv/handlers.go b/cmd/git-sv/handlers.go index 9136183..a9f5319 100644 --- a/cmd/git-sv/handlers.go +++ b/cmd/git-sv/handlers.go @@ -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 diff --git a/cmd/git-sv/main.go b/cmd/git-sv/main.go index 4b6b37d..cf0760b 100644 --- a/cmd/git-sv/main.go +++ b/cmd/git-sv/main.go @@ -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"}, diff --git a/sv/validatemessage.go b/sv/message.go similarity index 78% rename from sv/validatemessage.go rename to sv/message.go index f9776b0..25b75e2 100644 --- a/sv/validatemessage.go +++ b/sv/message.go @@ -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 != "" { diff --git a/sv/validatemessage_test.go b/sv/message_test.go similarity index 87% rename from sv/validatemessage_test.go rename to sv/message_test.go index a9024fa..3735039 100644 --- a/sv/validatemessage_test.go +++ b/sv/message_test.go @@ -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) } }) }