From 634eac3b76a190ed240a3d8def464b3def628e95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20T=C3=B6lle?= Date: Sun, 8 Sep 2024 21:05:18 +0200 Subject: [PATCH] fix(parser): invalid handling of empty lines (#53) GitLab generates commit messages of the pattern "scope: message\n" if no body is present. This throws up the conventional commits parser we use, and results in the error message "missing a blank line". We now `strings.TrimSpace()` the commit message to avoid this problem. --- .../conventionalcommits/conventionalcommits.go | 3 ++- .../conventionalcommits/conventionalcommits_test.go | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/internal/commitparser/conventionalcommits/conventionalcommits.go b/internal/commitparser/conventionalcommits/conventionalcommits.go index b253354..d11970c 100644 --- a/internal/commitparser/conventionalcommits/conventionalcommits.go +++ b/internal/commitparser/conventionalcommits/conventionalcommits.go @@ -3,6 +3,7 @@ package conventionalcommits import ( "fmt" "log/slog" + "strings" "github.com/leodido/go-conventionalcommits" "github.com/leodido/go-conventionalcommits/parser" @@ -32,7 +33,7 @@ func (c *Parser) Analyze(commits []git.Commit) ([]commitparser.AnalyzedCommit, e analyzedCommits := make([]commitparser.AnalyzedCommit, 0, len(commits)) for _, commit := range commits { - msg, err := c.machine.Parse([]byte(commit.Message)) + 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 diff --git a/internal/commitparser/conventionalcommits/conventionalcommits_test.go b/internal/commitparser/conventionalcommits/conventionalcommits_test.go index bc969de..ddfbaf4 100644 --- a/internal/commitparser/conventionalcommits/conventionalcommits_test.go +++ b/internal/commitparser/conventionalcommits/conventionalcommits_test.go @@ -33,6 +33,19 @@ func TestAnalyzeCommits(t *testing.T) { expectedCommits: []commitparser.AnalyzedCommit{}, wantErr: assert.NoError, }, + { + // GitLab seems to create commits with pattern "scope: message\n" if no body is added. + // This has previously caused a parser error "missing a blank line". + // We added a workaround with `strings.TrimSpace()` and this test make sure that it does not break again. + name: "handles title with new line", + commits: []git.Commit{ + { + Message: "aksdjaklsdjka", + }, + }, + expectedCommits: []commitparser.AnalyzedCommit{}, + wantErr: assert.NoError, + }, { name: "drops unreleasable", commits: []git.Commit{