Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for pr based changelog #3

Merged
merged 3 commits into from
Nov 12, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
add factory function for PR and commit populator
  • Loading branch information
HandOfGod94 committed Nov 12, 2023
commit 4e590e246769dcc04847ea3cb513ba31c5500e28
7 changes: 5 additions & 2 deletions cmd/generate.go
Original file line number Diff line number Diff line change
@@ -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="<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"),
@@ -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")
16 changes: 8 additions & 8 deletions pkg/jira_changelog/generator.go
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion pkg/jira_changelog/generator_test.go
Original file line number Diff line number Diff line change
@@ -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 })
4 changes: 2 additions & 2 deletions pkg/jira_changelog/messages/git_commits_populator.go
Original file line number Diff line number Diff line change
@@ -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) {
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
@@ -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)
}
}