From 2f95f9bcdb272a2d21fb9698b247208ab321edec Mon Sep 17 00:00:00 2001 From: Gahan Rakholia Date: Sun, 14 Jan 2024 11:31:34 +0530 Subject: [PATCH] refactor: use struct directly instead of constructor function more dsl-ish --- cmd/generate.go | 14 +++++++------- pkg/jira_changelog/generator.go | 28 ++++++++-------------------- pkg/jira_changelog/generator_test.go | 4 ++-- 3 files changed, 17 insertions(+), 29 deletions(-) diff --git a/cmd/generate.go b/cmd/generate.go index e8a67d9..c981aa7 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -75,17 +75,17 @@ gh jira-changelog generate --config=".yaml" --from="v0.1.0" populator, err = messages.NewCommitPopulator(fromRef, toRef) } - changelog := jira_changelog.NewGenerator( - jira.NewClient(jira.NewClientOptions(jira.Options{ + changelog := jira_changelog.Generator{ + Client: jira.NewClient(jira.NewClientOptions(jira.Options{ jira.BaseURL: viper.GetString("base_url"), jira.ApiToken: viper.GetString("api_token"), jira.User: viper.GetString("email_id"), })), - populator, - fromRef, - toRef, - repoURL, - ) + Populator: populator, + FromRef: fromRef, + ToRef: toRef, + RepoURL: repoURL, + } slog.Info("Generating changelog", "From", fromRef, "To", toRef, "repoURL", repoURL) changelog.Generate(ctx).Render(writer(writeTo)) diff --git a/pkg/jira_changelog/generator.go b/pkg/jira_changelog/generator.go index 6851089..9eefe6d 100644 --- a/pkg/jira_changelog/generator.go +++ b/pkg/jira_changelog/generator.go @@ -10,27 +10,15 @@ import ( ) type Generator struct { - fromRef string - toRef string - repoURL string - client jira.Client - populator messages.Populator -} - -func NewGenerator(client jira.Client, populator messages.Populator, fromRef, toRef, repoURL string) *Generator { - g := &Generator{ - fromRef: fromRef, - toRef: toRef, - repoURL: repoURL, - client: client, - populator: populator, - } - - return g + FromRef string + ToRef string + RepoURL string + Client jira.Client + Populator messages.Populator } func (c *Generator) Generate(ctx context.Context) *Changelog { - commits, err := c.populator.Populate(ctx) + commits, err := c.Populator.Populate(ctx) panicIfErr(err) issues, err := c.fetchJiraIssues(commits) @@ -40,7 +28,7 @@ func (c *Generator) Generate(ctx context.Context) *Changelog { slog.Debug("Total epics", "count", len(issuesByEpic)) - return NewChangelog(c.fromRef, c.toRef, c.repoURL, issuesByEpic) + return NewChangelog(c.FromRef, c.ToRef, c.RepoURL, issuesByEpic) } func (c *Generator) fetchJiraIssues(commits []messages.Messager) ([]jira.Issue, error) { @@ -67,7 +55,7 @@ func (c *Generator) fetchJiraIssue(commit messages.Messager) (jira.Issue, error) return jira.NewIssue("", commit.Message(), "done", ""), nil } - issue, err := c.client.FetchIssue(string(issueId)) + issue, err := c.Client.FetchIssue(string(issueId)) if err != nil { slog.Warn("failed to fetch jira issue", "commit", commit) return jira.NewIssue("", commit.Message(), "done", ""), nil diff --git a/pkg/jira_changelog/generator_test.go b/pkg/jira_changelog/generator_test.go index 0bd73e3..5c8021b 100644 --- a/pkg/jira_changelog/generator_test.go +++ b/pkg/jira_changelog/generator_test.go @@ -35,8 +35,8 @@ func TestFetchJiraIssues(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)), &messages.NoopPopulator{}, "fromRef", "toRef", "http://example-repo.com") - generator.client = mockedClient + generator := Generator{} + generator.Client = mockedClient changeMessages := lo.Map(commits, func(commit messages.Commit, i int) messages.Messager { return commit }) got, err := generator.fetchJiraIssues(changeMessages)