mirror of
https://github.com/thegeeklab/git-sv.git
synced 2024-11-21 22:10:39 +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 {
|
return func(c *cli.Context) error {
|
||||||
|
|
||||||
ctype, err := promptType()
|
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 {
|
return func(c *cli.Context) error {
|
||||||
branch := git.Branch()
|
branch := git.Branch()
|
||||||
if validateMessageProcessor.SkipBranch(branch) {
|
if messageProcessor.SkipBranch(branch) {
|
||||||
warn("commit message validation skipped, branch in ignore list...")
|
warn("commit message validation skipped, branch in ignore list...")
|
||||||
return nil
|
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())
|
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())
|
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 {
|
if err != nil {
|
||||||
warn("could not enhance commit message, %s", err.Error())
|
warn("could not enhance commit message, %s", err.Error())
|
||||||
return nil
|
return nil
|
||||||
|
@ -23,7 +23,7 @@ func main() {
|
|||||||
semverProcessor := sv.NewSemVerCommitsProcessor(cfg.IncludeUnknownTypeAsPatch, cfg.MajorVersionTypes, cfg.MinorVersionTypes, cfg.PatchVersionTypes)
|
semverProcessor := sv.NewSemVerCommitsProcessor(cfg.IncludeUnknownTypeAsPatch, cfg.MajorVersionTypes, cfg.MinorVersionTypes, cfg.PatchVersionTypes)
|
||||||
releasenotesProcessor := sv.NewReleaseNoteProcessor(cfg.ReleaseNotesTags)
|
releasenotesProcessor := sv.NewReleaseNoteProcessor(cfg.ReleaseNotesTags)
|
||||||
outputFormatter := sv.NewOutputFormatter()
|
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 := cli.NewApp()
|
||||||
app.Name = "sv"
|
app.Name = "sv"
|
||||||
@ -76,13 +76,13 @@ func main() {
|
|||||||
Name: "commit",
|
Name: "commit",
|
||||||
Aliases: []string{"cmt"},
|
Aliases: []string{"cmt"},
|
||||||
Usage: "execute git commit with convetional commit message helper",
|
Usage: "execute git commit with convetional commit message helper",
|
||||||
Action: commitHandler(cfg, git, validateMessageProcessor),
|
Action: commitHandler(cfg, git, messageProcessor),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "validate-commit-message",
|
Name: "validate-commit-message",
|
||||||
Aliases: []string{"vcm"},
|
Aliases: []string{"vcm"},
|
||||||
Usage: "use as prepare-commit-message hook to validate message",
|
Usage: "use as prepare-commit-message hook to validate message",
|
||||||
Action: validateCommitMessageHandler(git, validateMessageProcessor),
|
Action: validateCommitMessageHandler(git, messageProcessor),
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.StringFlag{Name: "path", Required: true, Usage: "git working directory"},
|
&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"},
|
&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"
|
const breakingChangeKey = "BREAKING CHANGE"
|
||||||
|
|
||||||
// ValidateMessageProcessor interface.
|
// MessageProcessor interface.
|
||||||
type ValidateMessageProcessor interface {
|
type MessageProcessor interface {
|
||||||
SkipBranch(branch string) bool
|
SkipBranch(branch string) bool
|
||||||
Validate(message string) error
|
Validate(message string) error
|
||||||
Enhance(branch string, message string) (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)
|
Format(ctype, scope, subject, body, issue, breakingChanges string) (string, string, string)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewValidateMessageProcessor ValidateMessageProcessorImpl constructor
|
// NewMessageProcessor MessageProcessorImpl constructor
|
||||||
func NewValidateMessageProcessor(skipBranches, supportedTypes []string, issueKeyName, branchIssueRegex, issueRegex string) *ValidateMessageProcessorImpl {
|
func NewMessageProcessor(skipBranches, supportedTypes []string, issueKeyName, branchIssueRegex, issueRegex string) *MessageProcessorImpl {
|
||||||
return &ValidateMessageProcessorImpl{
|
return &MessageProcessorImpl{
|
||||||
skipBranches: skipBranches,
|
skipBranches: skipBranches,
|
||||||
supportedTypes: supportedTypes,
|
supportedTypes: supportedTypes,
|
||||||
issueKeyName: issueKeyName,
|
issueKeyName: issueKeyName,
|
||||||
@ -29,8 +29,8 @@ func NewValidateMessageProcessor(skipBranches, supportedTypes []string, issueKey
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ValidateMessageProcessorImpl process validate message hook.
|
// MessageProcessorImpl process validate message hook.
|
||||||
type ValidateMessageProcessorImpl struct {
|
type MessageProcessorImpl struct {
|
||||||
skipBranches []string
|
skipBranches []string
|
||||||
supportedTypes []string
|
supportedTypes []string
|
||||||
issueKeyName string
|
issueKeyName string
|
||||||
@ -39,12 +39,12 @@ type ValidateMessageProcessorImpl struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SkipBranch check if branch should be ignored.
|
// 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)
|
return contains(branch, p.skipBranches)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate commit message.
|
// 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))
|
valid, err := regexp.MatchString("^("+strings.Join(p.supportedTypes, "|")+")(\\(.+\\))?!?: .*$", firstLine(message))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -56,7 +56,7 @@ func (p ValidateMessageProcessorImpl) Validate(message string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Enhance add metadata on commit message.
|
// 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) {
|
if p.branchIssueRegex == "" || p.issueKeyName == "" || hasIssueID(message, p.issueKeyName) {
|
||||||
return "", nil //enhance disabled
|
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
|
// 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)
|
r, err := regexp.Compile(p.branchIssueRegex)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("could not compile issue regex: %s, error: %v", p.branchIssueRegex, err.Error())
|
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
|
// 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
|
var header strings.Builder
|
||||||
header.WriteString(ctype)
|
header.WriteString(ctype)
|
||||||
if scope != "" {
|
if scope != "" {
|
@ -45,8 +45,8 @@ BREAKING CHANGE: refactor to use JavaScript features not available in Node 6.`
|
|||||||
|
|
||||||
// multiline samples end
|
// multiline samples end
|
||||||
|
|
||||||
func TestValidateMessageProcessorImpl_Validate(t *testing.T) {
|
func TestMessageProcessorImpl_Validate(t *testing.T) {
|
||||||
p := NewValidateMessageProcessor([]string{"develop", "master"}, []string{"feat", "fix"}, "jira", branchIssueRegex, issueRegex)
|
p := NewMessageProcessor([]string{"develop", "master"}, []string{"feat", "fix"}, "jira", branchIssueRegex, issueRegex)
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
@ -72,14 +72,14 @@ func TestValidateMessageProcessorImpl_Validate(t *testing.T) {
|
|||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
if err := p.Validate(tt.message); (err != nil) != tt.wantErr {
|
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) {
|
func TestMessageProcessorImpl_Enhance(t *testing.T) {
|
||||||
p := NewValidateMessageProcessor([]string{"develop", "master"}, []string{"feat", "fix"}, "jira", branchIssueRegex, issueRegex)
|
p := NewMessageProcessor([]string{"develop", "master"}, []string{"feat", "fix"}, "jira", branchIssueRegex, issueRegex)
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
@ -101,11 +101,11 @@ func TestValidateMessageProcessorImpl_Enhance(t *testing.T) {
|
|||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
got, err := p.Enhance(tt.branch, tt.message)
|
got, err := p.Enhance(tt.branch, tt.message)
|
||||||
if (err != nil) != tt.wantErr {
|
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
|
return
|
||||||
}
|
}
|
||||||
if got != tt.want {
|
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