diff --git a/sv/git.go b/sv/git.go index 4fb4ee0..6c2e9b2 100644 --- a/sv/git.go +++ b/sv/git.go @@ -194,23 +194,33 @@ func parseLogOutput(messageProcessor MessageProcessor, log string) []GitCommitLo var logs []GitCommitLog for scanner.Scan() { if text := strings.TrimSpace(strings.Trim(scanner.Text(), "\"")); text != "" { - logs = append(logs, parseCommitLog(messageProcessor, text)) + log, err := parseCommitLog(messageProcessor, text) + // Ignore errors occuring during parsing + if err == nil { + logs = append(logs, log) + } } } return logs } -func parseCommitLog(messageProcessor MessageProcessor, commit string) GitCommitLog { +func parseCommitLog(messageProcessor MessageProcessor, commit string) (GitCommitLog, error) { content := strings.Split(strings.Trim(commit, "\""), logSeparator) timestamp, _ := strconv.Atoi(content[1]) + message, err := messageProcessor.Parse(content[4], content[5]) + + if err != nil { + return GitCommitLog{}, err + } + return GitCommitLog{ Date: content[0], Timestamp: timestamp, AuthorName: content[2], Hash: content[3], - Message: messageProcessor.Parse(content[4], content[5]), - } + Message: message, + }, nil } func splitAt(b []byte) func(data []byte, atEOF bool) (advance int, token []byte, err error) { diff --git a/sv/message.go b/sv/message.go index 1c96e52..2d9852f 100644 --- a/sv/message.go +++ b/sv/message.go @@ -56,7 +56,7 @@ type MessageProcessor interface { Enhance(branch string, message string) (string, error) IssueID(branch string) (string, error) Format(msg CommitMessage) (string, string, string) - Parse(subject, body string) CommitMessage + Parse(subject, body string) (CommitMessage, error) } // NewMessageProcessor MessageProcessorImpl constructor. @@ -81,7 +81,7 @@ func (p MessageProcessorImpl) SkipBranch(branch string, detached bool) bool { // Validate commit message. func (p MessageProcessorImpl) Validate(message string) error { subject, body := splitCommitMessageContent(message) - msg := p.Parse(subject, body) + msg, _ := p.Parse(subject, body) if !regexp.MustCompile(`^[a-z+]+(\(.+\))?!?: .+$`).MatchString(subject) { return fmt.Errorf("subject [%s] should be valid according with conventional commits", subject) @@ -202,11 +202,11 @@ func (p MessageProcessorImpl) Format(msg CommitMessage) (string, string, string) } // Parse a commit message. -func (p MessageProcessorImpl) Parse(subject, body string) CommitMessage { - preparedSubject, prepError := p.prepareHeader(subject) +func (p MessageProcessorImpl) Parse(subject, body string) (CommitMessage, error) { + preparedSubject, err := p.prepareHeader(subject) - if prepError != nil { - fmt.Println(prepError) + if err != nil { + return CommitMessage{}, err } commitType, scope, description, hasBreakingChange := parseSubjectMessage(preparedSubject) @@ -235,7 +235,7 @@ func (p MessageProcessorImpl) Parse(subject, body string) CommitMessage { Body: body, IsBreakingChange: hasBreakingChange, Metadata: metadata, - } + }, nil } func (p MessageProcessorImpl) prepareHeader(header string) (string, error) { @@ -256,7 +256,7 @@ func (p MessageProcessorImpl) prepareHeader(header string) (string, error) { 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 header, nil } return match[index], nil diff --git a/sv/message_test.go b/sv/message_test.go index 243c6df..a7f4079 100644 --- a/sv/message_test.go +++ b/sv/message_test.go @@ -411,7 +411,7 @@ func TestMessageProcessorImpl_Parse(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := NewMessageProcessor(tt.cfg, newBranchCfg(false)).Parse(tt.subject, tt.body); !reflect.DeepEqual(got, tt.want) { + if got, err := NewMessageProcessor(tt.cfg, newBranchCfg(false)).Parse(tt.subject, tt.body); !reflect.DeepEqual(got, tt.want) && err == nil { t.Errorf("MessageProcessorImpl.Parse() = %v, want %v", got, tt.want) } }) @@ -534,7 +534,7 @@ func Test_prepareHeader(t *testing.T) { {"matching conventional with selector with group", "Merged PR (\\d+): (?P
.*)", "Merged PR 123: feat: something", "feat: something", false}, {"matching non-conventional with selector with group", "Merged PR (\\d+): (?P
.*)", "Merged PR 123: something", "something", false}, {"matching non-conventional with selector without group", "Merged PR (\\d+): (.*)", "Merged PR 123: something", "", true}, - {"non-matching non-conventional with selector with group", "Merged PR (\\d+): (?P
.*)", "something", "", true}, + {"non-matching non-conventional with selector with group", "Merged PR (\\d+): (?P
.*)", "something", "something", false}, {"matching non-conventional with invalid regex", "Merged PR (\\d+): (?
.*)", "Merged PR 123: something", "", true}, } for _, tt := range tests {