From 1971e784126c2d8e0fa1c42b184cecef559f2444 Mon Sep 17 00:00:00 2001 From: Nico Braun <39703898+bluebrown@users.noreply.github.com> Date: Wed, 9 Oct 2024 10:15:35 +0200 Subject: [PATCH] fix: handle commit message format errors (#69) Signed-off-by: Nico --- task/handler.go | 15 ++++++++++----- task/handler_test.go | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/task/handler.go b/task/handler.go index dd761fc..8f67d65 100644 --- a/task/handler.go +++ b/task/handler.go @@ -45,9 +45,9 @@ func KoboldHandler(ctx context.Context, cache string, g model.TaskGroup, runner g.DestBranch.Valid = true } - msg, err = GetCommitMessage(changes) + msg, err = commitMessage(changes) if err != nil { - return nil, fmt.Errorf("unable to get commit message: %w", err) + return nil, fmt.Errorf("get commit message: %w", err) } if err := git.Publish(ctx, cache, g.DestBranch.String, msg); err != nil { @@ -67,17 +67,22 @@ func KoboldHandler(ctx context.Context, cache string, g model.TaskGroup, runner return warnings, nil } -func GetCommitMessage(changes []krm.Change) (string, error) { +func commitMessage(changes []krm.Change) (string, error) { seen := make(map[string]struct{}) msg := strings.Builder{} - msg.WriteString("chore(kobold):\n") + if _, err := msg.WriteString("chore(kobold):\n"); err != nil { + return "", fmt.Errorf("write header: %w", err) + } for _, change := range changes { if _, ok := seen[change.Repo]; ok { continue } - msg.WriteString(fmt.Sprintf(" * %s: %s\n", change.Repo, change.Description)) + + if _, err := msg.WriteString(fmt.Sprintf(" * %s: %s\n", change.Repo, change.Description)); err != nil { + return "", fmt.Errorf("write change: %w", err) + } seen[change.Repo] = struct{}{} } diff --git a/task/handler_test.go b/task/handler_test.go index 88edc81..27cfc8e 100644 --- a/task/handler_test.go +++ b/task/handler_test.go @@ -66,7 +66,7 @@ func TestGetCommitMessage(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := GetCommitMessage(tt.args.changes) + got, err := commitMessage(tt.args.changes) if (err != nil) != tt.wantErr { t.Errorf("GetCommitMessage() error = %v, wantErr %v", err, tt.wantErr) return