From df120330ab485c91083f89ffb75a1146821003be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20T=C3=B6lle?= Date: Fri, 15 Nov 2024 15:31:20 +0100 Subject: [PATCH] fix: use commits with slightly invalid messages Fixes a bug where commits with messages that do not strictly conform to conventional commits spec would be ignored. This could easily happen while parsing footers like "Closes #xx" which would start the footer section, while continuing with the body afterwards. This solution has two downsides: - these warnings are not surfaced to the user. - If a `BREAKING CHANGE` footer exists after the parsing issue it is ignored --- .../conventionalcommits/conventionalcommits.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/internal/commitparser/conventionalcommits/conventionalcommits.go b/internal/commitparser/conventionalcommits/conventionalcommits.go index d11970c..e00af34 100644 --- a/internal/commitparser/conventionalcommits/conventionalcommits.go +++ b/internal/commitparser/conventionalcommits/conventionalcommits.go @@ -35,8 +35,12 @@ func (c *Parser) Analyze(commits []git.Commit) ([]commitparser.AnalyzedCommit, e for _, commit := range commits { msg, err := c.machine.Parse([]byte(strings.TrimSpace(commit.Message))) if err != nil { - c.logger.Warn("failed to parse message of commit, skipping", "commit.hash", commit.Hash, "err", err) - continue + if msg == nil { + c.logger.Warn("failed to parse message of commit, skipping", "commit.hash", commit.Hash, "err", err) + continue + } + + c.logger.Warn("failed to parse message of commit fully, trying to use as much as possible", "commit.hash", commit.Hash, "err", err) } conventionalCommit, ok := msg.(*conventionalcommits.ConventionalCommit) @@ -44,6 +48,12 @@ func (c *Parser) Analyze(commits []git.Commit) ([]commitparser.AnalyzedCommit, e return nil, fmt.Errorf("unable to get ConventionalCommit from parser result: %T", msg) } + if conventionalCommit.Type == "" { + // Parsing broke before getting the type, can not use the commit + c.logger.Warn("commit type was not parsed, skipping", "commit.hash", commit.Hash, "err", err) + continue + } + commitVersionBump := conventionalCommit.VersionBump(conventionalcommits.DefaultStrategy) if commitVersionBump > conventionalcommits.UnknownVersion { // We only care about releasable commits