0
0
mirror of https://github.com/thegeeklab/git-sv.git synced 2024-11-12 15:00:39 +00:00
git-sv/sv/validatemessage.go
2020-08-27 22:57:55 -03:00

65 lines
1.7 KiB
Go

package sv
import (
"fmt"
"regexp"
"strings"
)
// ValidateMessageProcessor interface.
type ValidateMessageProcessor interface {
SkipBranch(branch string) bool
Validate(message string) error
Enhance(branch string, message string) (string, error)
}
// NewValidateMessageProcessor ValidateMessageProcessorImpl constructor
func NewValidateMessageProcessor(skipBranches, supportedTypes []string) *ValidateMessageProcessorImpl {
return &ValidateMessageProcessorImpl{
skipBranches: skipBranches,
supportedTypes: supportedTypes,
}
}
// ValidateMessageProcessorImpl process validate message hook.
type ValidateMessageProcessorImpl struct {
skipBranches []string
supportedTypes []string
}
// SkipBranch check if branch should be ignored.
func (p ValidateMessageProcessorImpl) SkipBranch(branch string) bool {
return contains(branch, p.skipBranches)
}
// Validate commit message.
func (p ValidateMessageProcessorImpl) Validate(message string) error {
valid, err := regexp.MatchString("^("+strings.Join(p.supportedTypes, "|")+")(\\(.+\\))?!?: .*$", firstLine(message))
if err != nil {
return err
}
if !valid {
return fmt.Errorf("message should contain type: %v, and should be valid according with conventional commits", p.supportedTypes)
}
return nil
}
// Enhance add metadata on commit message.
func (p ValidateMessageProcessorImpl) Enhance(branch string, message string) (string, error) {
//TODO add issue id (branch format on varenv)
return "", nil
}
func contains(value string, content []string) bool {
for _, v := range content {
if value == v {
return true
}
}
return false
}
func firstLine(value string) string {
return strings.Split(value, "\n")[0]
}