Skip to content

Commit

Permalink
fix: fix commit message split and add test (#22)
Browse files Browse the repository at this point in the history
xoxys authored Sep 3, 2023
1 parent 1d057f0 commit 6f5c8d5
Showing 2 changed files with 99 additions and 4 deletions.
25 changes: 21 additions & 4 deletions plugin/commit.go
Original file line number Diff line number Diff line change
@@ -130,6 +130,8 @@ func currFlags(category string) []cli.Flag {
}

func currFromContext(c *cli.Context) Commit {
commitTitle, commitDesc := splitMessage(c.String("commit.message"))

return Commit{
URL: c.String("commit.url"),
SHA: c.String("commit.sha"),
@@ -141,8 +143,8 @@ func currFromContext(c *cli.Context) Commit {
Branch: c.String("commit.branch"),
Tag: c.String("commit.tag"),
Message: c.String("commit.message"),
Title: strings.Split(c.String("commit.message"), "\n")[0],
Description: strings.Split(c.String("commit.message"), "\n")[1],
Title: commitTitle,
Description: commitDesc,
Author: Author{
Name: c.String("commit.author.name"),
Email: c.String("commit.author.email"),
@@ -211,19 +213,34 @@ func prevFlags(category string) []cli.Flag {
}

func prevFromContext(c *cli.Context) Commit {
commitTitle, commitDesc := splitMessage(c.String("commit.message"))

return Commit{
URL: c.String("prev.commit.url"),
SHA: c.String("prev.commit.sha"),
Ref: c.String("prev.commit.ref"),
Refspec: c.String("prev.commit.refspec"),
Branch: c.String("prev.commit.branch"),
Message: c.String("prev.commit.message"),
Title: strings.Split(c.String("commit.message"), "\n")[0],
Description: strings.Split(c.String("commit.message"), "\n")[1],
Title: commitTitle,
Description: commitDesc,
Author: Author{
Name: c.String("prev.commit.author.name"),
Email: c.String("prev.commit.author.email"),
Avatar: c.String("prev.commit.author.avatar"),
},
}
}

func splitMessage(message string) (string, string) {
//nolint:gomnd
switch parts := strings.SplitN(message, "\n", 2); len(parts) {
case 1:
return parts[0], ""
//nolint:gomnd
case 2:
return parts[0], parts[1]
}

return "", ""
}
78 changes: 78 additions & 0 deletions plugin/commit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package plugin

import (
"context"
"testing"

"github.com/urfave/cli/v2"
)

func Test_currFromContext(t *testing.T) {
tests := []struct {
envs map[string]string
want map[string]string
}{
{
envs: map[string]string{
"CI_COMMIT_MESSAGE": "",
},
want: map[string]string{
"title": "",
"desc": "",
"message": "",
},
},
{
envs: map[string]string{
"CI_COMMIT_MESSAGE": "test_title\ntest_desc",
},
want: map[string]string{
"title": "test_title",
"desc": "test_desc",
"message": "test_title\ntest_desc",
},
},
{
envs: map[string]string{
"CI_COMMIT_MESSAGE": "test_title\ntest_desc\nadditional",
},
want: map[string]string{
"title": "test_title",
"desc": "test_desc\nadditional",
"message": "test_title\ntest_desc\nadditional",
},
},
}

for _, tt := range tests {
for key, value := range tt.envs {
t.Setenv(key, value)
}

options := Options{
Name: "dummy",
Execute: func(ctx context.Context) error { return nil },
}

got := New(options)
got.App.Action = func(ctx *cli.Context) error {
got.Metadata = MetadataFromContext(ctx)

return nil
}

_ = got.App.Run([]string{"dummy"})

if got.Metadata.Curr.Message != tt.want["message"] {
t.Errorf("got = %q, want = %q", got.Metadata.Curr.Message, tt.want["message"])
}

if got.Metadata.Curr.Title != tt.want["title"] {
t.Errorf("got = %q, want = %q", got.Metadata.Curr.Title, tt.want["title"])
}

if got.Metadata.Curr.Description != tt.want["desc"] {
t.Errorf("got = %q, want = %q", got.Metadata.Curr.Description, tt.want["desc"])
}
}
}

0 comments on commit 6f5c8d5

Please sign in to comment.