diff --git a/plugin/commit.go b/plugin/commit.go index 4b1d77b..99958dc 100644 --- a/plugin/commit.go +++ b/plugin/commit.go @@ -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,6 +213,8 @@ 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"), @@ -218,8 +222,8 @@ func prevFromContext(c *cli.Context) Commit { 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"), @@ -227,3 +231,16 @@ func prevFromContext(c *cli.Context) Commit { }, } } + +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 "", "" +} diff --git a/plugin/commit_test.go b/plugin/commit_test.go new file mode 100644 index 0000000..053cb9b --- /dev/null +++ b/plugin/commit_test.go @@ -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"]) + } + } +}