From 49ac37ce8f9dc84aabd9da0973bed8089ad85175 Mon Sep 17 00:00:00 2001 From: Gahan Rakholia Date: Sun, 12 Nov 2023 14:42:41 +0530 Subject: [PATCH] add factory function for PR and commit populator --- cmd/generate.go | 7 +++++-- pkg/jira_changelog/generator.go | 16 ++++++++-------- pkg/jira_changelog/generator_test.go | 2 +- .../messages/git_commits_populator.go | 4 ++-- pkg/jira_changelog/messages/populator.go | 16 +++++++++++++++- 5 files changed, 31 insertions(+), 14 deletions(-) diff --git a/cmd/generate.go b/cmd/generate.go index 4118ec6..680ab92 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -18,6 +18,7 @@ var ( fromRef string toRef string writeTo string + usePR bool DefaultTimeout = 10 * time.Second ) @@ -62,11 +63,12 @@ gh jira-changelog generate --config=".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"), @@ -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") diff --git a/pkg/jira_changelog/generator.go b/pkg/jira_changelog/generator.go index 0b54ae1..1f26d6b 100644 --- a/pkg/jira_changelog/generator.go +++ b/pkg/jira_changelog/generator.go @@ -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) diff --git a/pkg/jira_changelog/generator_test.go b/pkg/jira_changelog/generator_test.go index c81922e..6225cb0 100644 --- a/pkg/jira_changelog/generator_test.go +++ b/pkg/jira_changelog/generator_test.go @@ -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 }) diff --git a/pkg/jira_changelog/messages/git_commits_populator.go b/pkg/jira_changelog/messages/git_commits_populator.go index fef815a..ffe1083 100644 --- a/pkg/jira_changelog/messages/git_commits_populator.go +++ b/pkg/jira_changelog/messages/git_commits_populator.go @@ -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) { diff --git a/pkg/jira_changelog/messages/populator.go b/pkg/jira_changelog/messages/populator.go index 33b96f2..7718e2f 100644 --- a/pkg/jira_changelog/messages/populator.go +++ b/pkg/jira_changelog/messages/populator.go @@ -1,6 +1,10 @@ package messages -import "context" +import ( + "context" + + "golang.org/x/exp/slog" +) type Message interface { Message() string @@ -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) + } +}