Skip to content

Commit

Permalink
add factory function for PR and commit populator
Browse files Browse the repository at this point in the history
  • Loading branch information
HandOfGod94 committed Nov 12, 2023
1 parent cb763fe commit 49ac37c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
7 changes: 5 additions & 2 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (
fromRef string
toRef string
writeTo string
usePR bool
DefaultTimeout = 10 * time.Second
)

Expand Down Expand Up @@ -62,11 +63,12 @@ gh jira-changelog generate --config="<path-to-config-file>.yaml" --from="v0.1.0"
defer cancel()

changelog := jira_changelog.NewGenerator(
jira.NewContext(jira.Options{
jira.NewClient(jira.NewContext(jira.Options{
jira.BaseURL: viper.GetString("base_url"),
jira.ApiToken: viper.GetString("api_token"),
jira.User: viper.GetString("email_id"),
}),
})),
usePR,
fromRef,
toRef,
viper.GetString("repo_url"),
Expand Down Expand Up @@ -100,6 +102,7 @@ func writer(writeTo string) io.Writer {
func init() {
generateCmd.Flags().StringVar(&fromRef, "from", "", "Git ref to start from")
generateCmd.Flags().StringVar(&toRef, "to", "main", "Git ref to end at")
generateCmd.Flags().BoolVar(&usePR, "use_pr", false, "use PR titles to generate changelog. Note: only works if used as gh plugin")
generateCmd.Flags().StringVar(&writeTo, "write_to", "/dev/stdout", "File stream to write the changelog")

generateCmd.MarkFlagRequired("from")
Expand Down
16 changes: 8 additions & 8 deletions pkg/jira_changelog/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ type Generator struct {
toRef string
repoURL string
client jira.Client
usePR bool
}

func NewGenerator(jiraCtx *jira.Context, fromRef, toRef, repoURL string) *Generator {
client := jira.NewClient(jiraCtx)
func NewGenerator(client jira.Client, usePR bool, fromRef, toRef, repoURL string) *Generator {
g := &Generator{
JiraConfig: jiraCtx,
fromRef: fromRef,
toRef: toRef,
repoURL: repoURL,
client: client,
fromRef: fromRef,
toRef: toRef,
repoURL: repoURL,
client: client,
usePR: usePR,
}

return g
}

func (c *Generator) Generate(ctx context.Context) *Changelog {
populator, err := messages.NewPullRequestPopulator(c.fromRef, c.toRef, c.repoURL)
populator, err := messages.NewCommitOrPRPopualtor(c.usePR, c.fromRef, c.toRef, c.repoURL)
panicIfErr(err)

commits, err := populator.Populate(ctx)
Expand Down
2 changes: 1 addition & 1 deletion pkg/jira_changelog/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.NewContext(nil), "fromRef", "toRef", "http://example-repo.com")
generator := NewGenerator(jira.NewClient(jira.NewContext(nil)), false, "fromRef", "toRef", "http://example-repo.com")
generator.client = mockedClient

changeMessages := lo.Map(commits, func(commit messages.Commit, i int) messages.Message { return commit })
Expand Down
4 changes: 2 additions & 2 deletions pkg/jira_changelog/messages/git_commits_populator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ type commitPopulator struct {
toRef string
}

func NewCommitPopulator(fromRef, toRef string) Populator {
func NewCommitPopulator(fromRef, toRef string) (Populator, error) {
cpw := &commitPopulator{
fromRef: fromRef,
toRef: toRef,
}
return cpw
return cpw, nil
}

func (cpw *commitPopulator) Populate(ctx context.Context) ([]Message, error) {
Expand Down
16 changes: 15 additions & 1 deletion pkg/jira_changelog/messages/populator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package messages

import "context"
import (
"context"

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

type Message interface {
Message() string
Expand All @@ -9,3 +13,13 @@ type Message interface {
type Populator interface {
Populate(ctx context.Context) ([]Message, 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)
}
}

0 comments on commit 49ac37c

Please sign in to comment.