Skip to content

Commit

Permalink
refactor: pull up if-else for PR vs Commit populator
Browse files Browse the repository at this point in the history
  • Loading branch information
HandOfGod94 committed Jan 14, 2024
1 parent 185f55b commit fa41a7a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 32 deletions.
12 changes: 10 additions & 2 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -62,18 +63,25 @@ gh jira-changelog generate --config="<path-to-config-file>.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,
Expand Down
27 changes: 12 additions & 15 deletions pkg/jira_changelog/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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 @@ -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"},
Expand All @@ -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 })
Expand Down
18 changes: 5 additions & 13 deletions pkg/jira_changelog/messages/populator.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package messages

import (
"context"

"golang.org/x/exp/slog"
)
import "context"

type Messager interface {
Message() string
Expand All @@ -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
}

0 comments on commit fa41a7a

Please sign in to comment.