Skip to content

Commit

Permalink
fix: handle commit message format errors (#69)
Browse files Browse the repository at this point in the history
Signed-off-by: Nico <rainbowstack@gmail.com>
  • Loading branch information
bluebrown authored Oct 9, 2024
1 parent 2f43914 commit 1971e78
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
15 changes: 10 additions & 5 deletions task/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{}{}
}
Expand Down
2 changes: 1 addition & 1 deletion task/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1971e78

Please sign in to comment.