Skip to content

Commit

Permalink
refactor: use struct directly instead of constructor function
Browse files Browse the repository at this point in the history
more dsl-ish
  • Loading branch information
HandOfGod94 committed Jan 14, 2024
1 parent fa41a7a commit 2f95f9b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 29 deletions.
14 changes: 7 additions & 7 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,17 @@ gh jira-changelog generate --config="<path-to-config-file>.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))
Expand Down
28 changes: 8 additions & 20 deletions pkg/jira_changelog/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/jira_changelog/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2f95f9b

Please sign in to comment.