2020-08-28 01:57:55 +00:00
|
|
|
package sv
|
|
|
|
|
|
|
|
import (
|
2020-09-01 01:28:54 +00:00
|
|
|
"bufio"
|
2020-08-28 01:57:55 +00:00
|
|
|
"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
|
2020-09-01 01:28:54 +00:00
|
|
|
func NewValidateMessageProcessor(skipBranches, supportedTypes []string, issueKeyName, branchIssueRegex string) *ValidateMessageProcessorImpl {
|
2020-08-28 01:57:55 +00:00
|
|
|
return &ValidateMessageProcessorImpl{
|
2020-09-01 01:28:54 +00:00
|
|
|
skipBranches: skipBranches,
|
|
|
|
supportedTypes: supportedTypes,
|
|
|
|
issueKeyName: issueKeyName,
|
|
|
|
branchIssueRegex: branchIssueRegex,
|
2020-08-28 01:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateMessageProcessorImpl process validate message hook.
|
|
|
|
type ValidateMessageProcessorImpl struct {
|
2020-09-01 01:28:54 +00:00
|
|
|
skipBranches []string
|
|
|
|
supportedTypes []string
|
|
|
|
issueKeyName string
|
|
|
|
branchIssueRegex string
|
2020-08-28 01:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2020-09-01 01:28:54 +00:00
|
|
|
if p.branchIssueRegex == "" || p.issueKeyName == "" || hasIssueID(message, p.issueKeyName) {
|
|
|
|
return "", nil //enhance disabled
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := regexp.Compile(p.branchIssueRegex)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not compile issue regex: %s, error: %v", p.branchIssueRegex, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
groups := r.FindStringSubmatch(branch)
|
|
|
|
if len(groups) != 4 {
|
2020-12-01 22:43:19 +00:00
|
|
|
return "", fmt.Errorf("could not find issue id using configured regex")
|
2020-09-01 01:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
footer := fmt.Sprintf("%s: %s", p.issueKeyName, groups[2])
|
|
|
|
|
|
|
|
if !hasFooter(message) {
|
|
|
|
return "\n" + footer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return footer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasFooter(message string) bool {
|
|
|
|
r := regexp.MustCompile("^[a-zA-Z-]+: .*|^[a-zA-Z-]+ #.*|^BREAKING CHANGE: .*")
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(strings.NewReader(message))
|
|
|
|
lines := 0
|
|
|
|
for scanner.Scan() {
|
|
|
|
if lines > 0 && r.MatchString(scanner.Text()) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
lines++
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasIssueID(message, issueKeyName string) bool {
|
|
|
|
r := regexp.MustCompile(fmt.Sprintf("(?m)^%s: .+$", issueKeyName))
|
|
|
|
return r.MatchString(message)
|
2020-08-28 01:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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]
|
|
|
|
}
|