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

refactor: add error handling for parsing messages

issue: #45
This commit is contained in:
hypervtechnics 2022-04-04 08:55:29 +02:00
parent 00c843df2d
commit c6aaac5638
3 changed files with 24 additions and 14 deletions

View File

@ -194,23 +194,33 @@ func parseLogOutput(messageProcessor MessageProcessor, log string) []GitCommitLo
var logs []GitCommitLog var logs []GitCommitLog
for scanner.Scan() { for scanner.Scan() {
if text := strings.TrimSpace(strings.Trim(scanner.Text(), "\"")); text != "" { 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 return logs
} }
func parseCommitLog(messageProcessor MessageProcessor, commit string) GitCommitLog { func parseCommitLog(messageProcessor MessageProcessor, commit string) (GitCommitLog, error) {
content := strings.Split(strings.Trim(commit, "\""), logSeparator) content := strings.Split(strings.Trim(commit, "\""), logSeparator)
timestamp, _ := strconv.Atoi(content[1]) timestamp, _ := strconv.Atoi(content[1])
message, err := messageProcessor.Parse(content[4], content[5])
if err != nil {
return GitCommitLog{}, err
}
return GitCommitLog{ return GitCommitLog{
Date: content[0], Date: content[0],
Timestamp: timestamp, Timestamp: timestamp,
AuthorName: content[2], AuthorName: content[2],
Hash: content[3], 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) { func splitAt(b []byte) func(data []byte, atEOF bool) (advance int, token []byte, err error) {

View File

@ -56,7 +56,7 @@ type MessageProcessor interface {
Enhance(branch string, message string) (string, error) Enhance(branch string, message string) (string, error)
IssueID(branch string) (string, error) IssueID(branch string) (string, error)
Format(msg CommitMessage) (string, string, string) Format(msg CommitMessage) (string, string, string)
Parse(subject, body string) CommitMessage Parse(subject, body string) (CommitMessage, error)
} }
// NewMessageProcessor MessageProcessorImpl constructor. // NewMessageProcessor MessageProcessorImpl constructor.
@ -81,7 +81,7 @@ func (p MessageProcessorImpl) SkipBranch(branch string, detached bool) bool {
// Validate commit message. // Validate commit message.
func (p MessageProcessorImpl) Validate(message string) error { func (p MessageProcessorImpl) Validate(message string) error {
subject, body := splitCommitMessageContent(message) subject, body := splitCommitMessageContent(message)
msg := p.Parse(subject, body) msg, _ := p.Parse(subject, body)
if !regexp.MustCompile(`^[a-z+]+(\(.+\))?!?: .+$`).MatchString(subject) { if !regexp.MustCompile(`^[a-z+]+(\(.+\))?!?: .+$`).MatchString(subject) {
return fmt.Errorf("subject [%s] should be valid according with conventional commits", 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. // Parse a commit message.
func (p MessageProcessorImpl) Parse(subject, body string) CommitMessage { func (p MessageProcessorImpl) Parse(subject, body string) (CommitMessage, error) {
preparedSubject, prepError := p.prepareHeader(subject) preparedSubject, err := p.prepareHeader(subject)
if prepError != nil { if err != nil {
fmt.Println(prepError) return CommitMessage{}, err
} }
commitType, scope, description, hasBreakingChange := parseSubjectMessage(preparedSubject) commitType, scope, description, hasBreakingChange := parseSubjectMessage(preparedSubject)
@ -235,7 +235,7 @@ func (p MessageProcessorImpl) Parse(subject, body string) CommitMessage {
Body: body, Body: body,
IsBreakingChange: hasBreakingChange, IsBreakingChange: hasBreakingChange,
Metadata: metadata, Metadata: metadata,
} }, nil
} }
func (p MessageProcessorImpl) prepareHeader(header string) (string, error) { func (p MessageProcessorImpl) prepareHeader(header string) (string, error) {
@ -256,7 +256,7 @@ func (p MessageProcessorImpl) prepareHeader(header string) (string, error) {
match := regex.FindStringSubmatch(header) match := regex.FindStringSubmatch(header)
if match == nil || len(match) < index { 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 return match[index], nil

View File

@ -411,7 +411,7 @@ func TestMessageProcessorImpl_Parse(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { 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) 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<header>.*)", "Merged PR 123: feat: something", "feat: something", false}, {"matching conventional with selector with group", "Merged PR (\\d+): (?P<header>.*)", "Merged PR 123: feat: something", "feat: something", false},
{"matching non-conventional with selector with group", "Merged PR (\\d+): (?P<header>.*)", "Merged PR 123: something", "something", false}, {"matching non-conventional with selector with group", "Merged PR (\\d+): (?P<header>.*)", "Merged PR 123: something", "something", false},
{"matching non-conventional with selector without group", "Merged PR (\\d+): (.*)", "Merged PR 123: something", "", true}, {"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<header>.*)", "something", "", true}, {"non-matching non-conventional with selector with group", "Merged PR (\\d+): (?P<header>.*)", "something", "something", false},
{"matching non-conventional with invalid regex", "Merged PR (\\d+): (?<header>.*)", "Merged PR 123: something", "", true}, {"matching non-conventional with invalid regex", "Merged PR (\\d+): (?<header>.*)", "Merged PR 123: something", "", true},
} }
for _, tt := range tests { for _, tt := range tests {