diff --git a/README.md b/README.md index 210c0dd..726d651 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/sv/config.go b/sv/config.go index 64437cd..264755b 100644 --- a/sv/config.go +++ b/sv/config.go @@ -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"` diff --git a/sv/message.go b/sv/message.go index 57d1dad..1c96e52 100644 --- a/sv/message.go +++ b/sv/message.go @@ -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) - - filteredSubject = subjectMatch[subjectMessageIndex] + preparedSubject, prepError := p.prepareHeader(subject) + + 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)