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 #5

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 9 additions & 3 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 @@ -47,7 +48,10 @@ gh-jira-changelog generate --config="<path-to-config-file>.yaml" --from="v0.1.0"

# Using it as GH plugin
# assuming jira plugin installed
gh jira-changelog generate --config="<path-to-config-file>.yaml" --from="v0.1.0" --to="v0.2.0"`,
gh jira-changelog generate --config="<path-to-config-file>.yaml" --from="v0.1.0" --to="v0.2.0"

# using PR titles to generate changelog
gh jira-changelog generate --config="<path-to-config-file>.yaml" --from="v0.1.0" --to="v0.2.0" --use_pr`,
PreRunE: func(cmd *cobra.Command, args []string) error {
apiToken := viper.GetString("api_token")
emailID := viper.GetString("email_id")
Expand All @@ -62,11 +66,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 +105,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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
github.com/stretchr/testify v1.8.3
github.com/whilp/git-urls v1.0.0
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
golang.org/x/oauth2 v0.13.0
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8
github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e h1:BuzhfgfWQbX0dWzYzT1zsORLnHRv3bcRcsaUk0VmXA8=
github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e/go.mod h1:/Tnicc6m/lsJE0irFMA0LfIwTBo4QP7A8IfyIv4zZKI=
github.com/whilp/git-urls v1.0.0 h1:95f6UMWN5FKW71ECsXRUd3FVYiXdrE7aX4NZKcPmIjU=
github.com/whilp/git-urls v1.0.0/go.mod h1:J16SAmobsqc3Qcy98brfl5f5+e0clUvg1krgwk/qCfE=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
32 changes: 17 additions & 15 deletions pkg/jira_changelog/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package jira_changelog

import (
"context"
"fmt"

"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/samber/lo"
"golang.org/x/exp/slog"
)
Expand All @@ -16,23 +15,26 @@ 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 {
commits, err := git.NewCommitPopulator(c.fromRef, c.toRef).Commits(ctx)
populator, err := messages.NewCommitOrPRPopualtor(c.usePR, c.fromRef, c.toRef, c.repoURL)
panicIfErr(err)

commits, err := populator.Populate(ctx)
panicIfErr(err)

issues, err := c.fetchJiraIssues(commits)
Expand All @@ -45,7 +47,7 @@ func (c *Generator) Generate(ctx context.Context) *Changelog {
return NewChangelog(c.fromRef, c.toRef, c.repoURL, issuesByEpic)
}

func (c *Generator) fetchJiraIssues(commits []git.Commit) ([]jira.Issue, error) {
func (c *Generator) fetchJiraIssues(commits []messages.Message) ([]jira.Issue, error) {
slog.Debug("Total commit messages", "count", len(commits))

jiraIssues := make([]jira.Issue, 0)
Expand All @@ -62,17 +64,17 @@ func (c *Generator) fetchJiraIssues(commits []git.Commit) ([]jira.Issue, error)
return lo.Uniq(jiraIssues), nil
}

func (c *Generator) fetchJiraIssue(commit git.Commit) (jira.Issue, error) {
issueId := jira.IssueId(commit.Message)
func (c *Generator) fetchJiraIssue(commit messages.Message) (jira.Issue, error) {
issueId := jira.IssueId(commit.Message())
if issueId == "" {
slog.Warn("commit message does not contain issue jira id of the project", "commit", commit)
return jira.NewIssue("", fmt.Sprintf("%s (%s)", commit.Message, commit.Sha), "done", ""), nil
return jira.NewIssue("", commit.Message(), "done", ""), nil
}

issue, err := c.client.FetchIssue(string(issueId))
if err != nil {
slog.Warn("failed to fetch jira issue", "commit", commit)
return jira.NewIssue("", fmt.Sprintf("%s (%s)", commit.Message, commit.Sha), "done", ""), nil
return jira.NewIssue("", commit.Message(), "done", ""), nil
}
return issue, nil
}
Expand Down
28 changes: 15 additions & 13 deletions pkg/jira_changelog/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,41 @@ import (
"time"

"github.com/handofgod94/gh-jira-changelog/mocks"
"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/samber/lo"
"github.com/stretchr/testify/assert"
)

func TestFetchJiraIssuesEvent(t *testing.T) {
commits := []git.Commit{
{Time: time.Now(), Message: "[TEST-1234] commit message1", Sha: "3245vw"},
{Time: time.Now(), Message: "[TEST-4546] commit message sample1", Sha: "3245vw"},
{Time: time.Now(), Message: "[TEST-1234] commit message2", Sha: "3245vw"},
{Time: time.Now(), Message: "[TEST-4546] commit message sample2", Sha: "3245vw"},
{Time: time.Now(), Message: "[TEST-12345] commit message from same epic", Sha: "3245vw"},
{Time: time.Now(), Message: "[NO-CARD] commit message random", Sha: "3245vw"},
{Time: time.Now(), Message: "foobar commit message random", Sha: "3245vw"},
commits := []messages.Commit{
{Time: time.Now(), Summary: "[TEST-1234] commit message1", Sha: "3245vw"},
{Time: time.Now(), Summary: "[TEST-4546] commit message sample1", Sha: "3245vw"},
{Time: time.Now(), Summary: "[TEST-1234] commit message2", Sha: "3245vw"},
{Time: time.Now(), Summary: "[TEST-4546] commit message sample2", Sha: "3245vw"},
{Time: time.Now(), Summary: "[TEST-12345] commit message from same epic", Sha: "3245vw"},
{Time: time.Now(), Summary: "[NO-CARD] commit message random", Sha: "3245vw"},
{Time: time.Now(), Summary: "foobar commit message random", Sha: "3245vw"},
}

want := []jira.Issue{
jira.NewIssue("TEST-1234", "Ticket description", "done", "Epic1"),
jira.NewIssue("TEST-4546", "Ticket description for 4546 issue", "done", "Epic2"),
jira.NewIssue("TEST-12345", "Ticket description of another card from same epic", "done", "Epic1"),
jira.NewIssue("", "[NO-CARD] commit message random (3245vw)", "done", ""),
jira.NewIssue("", "foobar commit message random (3245vw)", "done", ""),
jira.NewIssue("", "[NO-CARD] commit message random", "done", ""),
jira.NewIssue("", "foobar commit message random", "done", ""),
}

mockedClient := mocks.NewClient(t)
mockedClient.On("FetchIssue", "TEST-1234").Return(want[0], nil).Twice()
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

got, err := generator.fetchJiraIssues(commits)
changeMessages := lo.Map(commits, func(commit messages.Commit, i int) messages.Message { return commit })
got, err := generator.fetchJiraIssues(changeMessages)

assert.NoError(t, err)
assert.Equal(t, len(want), len(got))
Expand Down
68 changes: 0 additions & 68 deletions pkg/jira_changelog/github/pull_request_populator.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,43 +1,53 @@
package git
package messages

import (
"context"
"fmt"
"os/exec"
"time"

"github.com/samber/lo"
)

var _ Populator = &commitPopulator{}
var _ Message = &Commit{}

type Commit struct {
Message string
Summary string
Time time.Time
Sha string
}

func (c Commit) Message() string {
return c.Summary
}

type commitPopulator struct {
fromRef string
toRef string
}

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

func (cpw *commitPopulator) Commits(ctx context.Context) ([]Commit, error) {
func (cpw *commitPopulator) Populate(ctx context.Context) ([]Message, error) {
gitOutput, err := execGitLog(ctx, cpw.fromRef, cpw.toRef)
if err != nil {
return []Commit{}, fmt.Errorf("failed to execute git log. %w", err)
return nil, fmt.Errorf("failed to execute git log. %w", err)
}

commits, err := gitOutput.Commits()
if err != nil {
return []Commit{}, fmt.Errorf("failed to parse output. %w", err)
return nil, fmt.Errorf("failed to parse output. %w", err)
}

return commits, nil
messages := lo.Map(commits, func(commit Commit, i int) Message { return commit })
return messages, nil
}

func execGitLog(ctx context.Context, fromRef, toRef string) (GitOutput, error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package git
package messages

import (
"fmt"
Expand Down Expand Up @@ -37,7 +37,7 @@ func (gt GitOutput) Commits() ([]Commit, error) {
}

commits = append(commits, Commit{
Message: message,
Summary: message,
Time: commitTime,
Sha: sha,
})
Expand Down
Loading