diff --git a/cmd/generate.go b/cmd/generate.go index ac2c729..e8a67d9 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -10,6 +10,7 @@ import ( "github.com/handofgod94/gh-jira-changelog/pkg/jira_changelog" "github.com/handofgod94/gh-jira-changelog/pkg/jira_changelog/git" "github.com/handofgod94/gh-jira-changelog/pkg/jira_changelog/jira" + "github.com/handofgod94/gh-jira-changelog/pkg/jira_changelog/messages" "github.com/spf13/cobra" "github.com/spf13/viper" "golang.org/x/exp/slog" @@ -62,18 +63,25 @@ gh jira-changelog generate --config=".yaml" --from="v0.1.0" return nil }, - RunE: func(cmd *cobra.Command, args []string) error { + RunE: func(cmd *cobra.Command, args []string) (err error) { ctx, cancel := context.WithTimeout(context.Background(), DefaultTimeout) defer cancel() repoURL := repoURL(ctx) + var populator messages.Populator + if usePR { + populator, err = messages.NewPullRequestPopulator(fromRef, toRef, repoURL) + } else { + populator, err = messages.NewCommitPopulator(fromRef, toRef) + } + changelog := jira_changelog.NewGenerator( jira.NewClient(jira.NewClientOptions(jira.Options{ jira.BaseURL: viper.GetString("base_url"), jira.ApiToken: viper.GetString("api_token"), jira.User: viper.GetString("email_id"), })), - usePR, + populator, fromRef, toRef, repoURL, diff --git a/pkg/jira_changelog/generator.go b/pkg/jira_changelog/generator.go index 8e4e4a2..6851089 100644 --- a/pkg/jira_changelog/generator.go +++ b/pkg/jira_changelog/generator.go @@ -10,30 +10,27 @@ import ( ) type Generator struct { - fromRef string - toRef string - repoURL string - client jira.Client - usePR bool + fromRef string + toRef string + repoURL string + client jira.Client + populator messages.Populator } -func NewGenerator(client jira.Client, usePR bool, fromRef, toRef, repoURL string) *Generator { +func NewGenerator(client jira.Client, populator messages.Populator, fromRef, toRef, repoURL string) *Generator { g := &Generator{ - fromRef: fromRef, - toRef: toRef, - repoURL: repoURL, - client: client, - usePR: usePR, + fromRef: fromRef, + toRef: toRef, + repoURL: repoURL, + client: client, + populator: populator, } return g } func (c *Generator) Generate(ctx context.Context) *Changelog { - populator, err := messages.NewCommitOrPRPopualtor(c.usePR, c.fromRef, c.toRef, c.repoURL) - panicIfErr(err) - - commits, err := populator.Populate(ctx) + commits, err := c.populator.Populate(ctx) panicIfErr(err) issues, err := c.fetchJiraIssues(commits) diff --git a/pkg/jira_changelog/generator_test.go b/pkg/jira_changelog/generator_test.go index f8b5375..0bd73e3 100644 --- a/pkg/jira_changelog/generator_test.go +++ b/pkg/jira_changelog/generator_test.go @@ -11,7 +11,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestFetchJiraIssuesEvent(t *testing.T) { +func TestFetchJiraIssues(t *testing.T) { commits := []messages.Commit{ {Time: time.Now(), Summary: "[TEST-1234] commit message1", Sha: "3245vw"}, {Time: time.Now(), Summary: "[TEST-4546] commit message sample1", Sha: "3245vw"}, @@ -35,7 +35,7 @@ func TestFetchJiraIssuesEvent(t *testing.T) { mockedClient.On("FetchIssue", "TEST-4546").Return(want[1], nil).Twice() mockedClient.On("FetchIssue", "TEST-12345").Return(want[2], nil) - generator := NewGenerator(jira.NewClient(jira.NewClientOptions(nil)), false, "fromRef", "toRef", "http://example-repo.com") + generator := NewGenerator(jira.NewClient(jira.NewClientOptions(nil)), &messages.NoopPopulator{}, "fromRef", "toRef", "http://example-repo.com") generator.client = mockedClient changeMessages := lo.Map(commits, func(commit messages.Commit, i int) messages.Messager { return commit }) diff --git a/pkg/jira_changelog/messages/populator.go b/pkg/jira_changelog/messages/populator.go index e22f29b..206e5e8 100644 --- a/pkg/jira_changelog/messages/populator.go +++ b/pkg/jira_changelog/messages/populator.go @@ -1,10 +1,6 @@ package messages -import ( - "context" - - "golang.org/x/exp/slog" -) +import "context" type Messager interface { Message() string @@ -14,12 +10,8 @@ type Populator interface { Populate(ctx context.Context) ([]Messager, error) } -func NewCommitOrPRPopualtor(usePR bool, fromRef, toRef, repoURL string) (Populator, error) { - if usePR { - slog.Debug("using github PR titles to generate changelog") - return NewPullRequestPopulator(fromRef, toRef, repoURL) - } else { - slog.Debug("using commit messages to generate changelog") - return NewCommitPopulator(fromRef, toRef) - } +type NoopPopulator struct{} + +func (e *NoopPopulator) Populate(ctx context.Context) ([]Messager, error) { + return []Messager{}, nil }