0
0
mirror of https://github.com/thegeeklab/git-sv.git synced 2024-11-24 11:10:39 +00:00

chore: include feedback from pull request review

issue: #45
This commit is contained in:
hypervtechnics 2022-03-31 15:53:53 +02:00
parent b1b47747c1
commit cde390c838
3 changed files with 32 additions and 11 deletions

View File

@ -120,7 +120,7 @@ branches: # Git branches config.
commit-message:
types: [build, ci, chore, docs, feat, fix, perf, refactor, revert, style, test] # Supported commit types.
selector: '' # You can put in a regex here to select only a certain part of the commit message.
header-selector: '' # You can put in a regex here to select only a certain part of the commit message. Please define a regex group 'header'.
scope:
# Define supported scopes, if blank, scope will not be validated, if not, only scope listed will be valid.
# Don't forget to add "" on your list if you need to define scopes and keep it optional.

View File

@ -5,7 +5,7 @@ package sv
// CommitMessageConfig config a commit message.
type CommitMessageConfig struct {
Types []string `yaml:"types,flow"`
MessageSelector string `yaml:"selector"`
HeaderSelector string `yaml:"header-selector"`
Scope CommitMessageScopeConfig `yaml:"scope"`
Footer map[string]CommitMessageFooterConfig `yaml:"footer"`
Issue CommitMessageIssueConfig `yaml:"issue"`

View File

@ -11,7 +11,7 @@ const (
breakingChangeFooterKey = "BREAKING CHANGE"
breakingChangeMetadataKey = "breaking-change"
issueMetadataKey = "issue"
messageRegexGroupName = "message"
messageRegexGroupName = "header"
)
// CommitMessage is a message using conventional commits.
@ -203,16 +203,13 @@ func (p MessageProcessorImpl) Format(msg CommitMessage) (string, string, string)
// Parse a commit message.
func (p MessageProcessorImpl) Parse(subject, body string) CommitMessage {
filteredSubject := subject
if p.messageCfg.MessageSelector != "" {
subjectRegex := regexp.MustCompile(p.messageCfg.MessageSelector)
subjectMessageIndex := subjectRegex.SubexpIndex(messageRegexGroupName)
subjectMatch := subjectRegex.FindStringSubmatch(subject)
preparedSubject, prepError := p.prepareHeader(subject)
filteredSubject = subjectMatch[subjectMessageIndex]
if prepError != nil {
fmt.Println(prepError)
}
commitType, scope, description, hasBreakingChange := parseSubjectMessage(filteredSubject)
commitType, scope, description, hasBreakingChange := parseSubjectMessage(preparedSubject)
metadata := make(map[string]string)
for key, mdCfg := range p.messageCfg.Footer {
@ -241,6 +238,30 @@ func (p MessageProcessorImpl) Parse(subject, body string) CommitMessage {
}
}
func (p MessageProcessorImpl) prepareHeader(header string) (string, error) {
if p.messageCfg.HeaderSelector == "" {
return header, nil
}
regex, err := regexp.Compile(p.messageCfg.HeaderSelector)
if err != nil {
return "", fmt.Errorf("invalid regex on header-selector %s, error: %s", p.messageCfg.HeaderSelector, err.Error())
}
index := regex.SubexpIndex(messageRegexGroupName)
if index < 0 {
return "", fmt.Errorf("could not find %s regex group on header-selector regex", messageRegexGroupName)
}
match := regex.FindStringSubmatch(header)
if match == nil || len(match) < index {
return "", fmt.Errorf("could not find %s regex group in match result for '%s'", messageRegexGroupName, header)
}
return match[index], nil
}
func parseSubjectMessage(message string) (string, string, string, bool) {
regex := regexp.MustCompile(`([a-z]+)(\((.*)\))?(!)?: (.*)`)
result := regex.FindStringSubmatch(message)