Skip to content

Commit

Permalink
add changelog url at bottom
Browse files Browse the repository at this point in the history
  • Loading branch information
HandOfGod94 committed Sep 3, 2023
1 parent 31c454c commit 1b520df
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 16 deletions.
11 changes: 8 additions & 3 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ var (
fromRef string
toRef string
writeTo string
repoURL string
DefaultTimeout = 5 * time.Second
requiredFlags = []string{"base_url", "email_id", "api_token"}
requiredFlags = []string{"base_url", "email_id", "api_token", "repo_url"}
)

var generateCmd = &cobra.Command{
Expand All @@ -35,6 +36,7 @@ gh-jira-changelog generate \
--to="<git-ref>" \
--api_token="<jira-api-token>" \
--email_id="jira-email-id"
--repo_url="https://github.com/<org>/<repo>"
# using config file
# all the jira config such as (base_url, api_token, email_id) can be stored in a config file
Expand Down Expand Up @@ -79,13 +81,15 @@ gh jira-changelog generate --config="<path-to-config-file>.yaml" --from="v0.1.0"
jira.Config{
BaseUrl: viper.GetString("base_url"),
ApiToken: viper.GetString("api_token"),
User: viper.GetString("email_id")},
User: viper.GetString("email_id"),
},
fromRef,
toRef,
repoURL,
)

slog.Info("Generating changelog", "JiraConfig", changelog.JiraConfig,
"From", fromRef, "To", toRef)
"From", fromRef, "To", toRef, "repoURL", repoURL)
changelog.Generate(ctx).Render(writer(writeTo))
},
}
Expand All @@ -111,6 +115,7 @@ 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().StringVar(&writeTo, "write_to", "/dev/stdout", "File stream to write the changelog")
generateCmd.Flags().StringVar(&repoURL, "repo_url", "", "Repo URL. Used to generate diff url. Currently only github is supported")

generateCmd.MarkFlagRequired("from")

Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func init() {
rootCmd.PersistentFlags().String("email_id", "", "email id of the user")
rootCmd.PersistentFlags().StringP("api_token", "t", "", "API token for jira")
rootCmd.PersistentFlags().StringP("log_level", "v", "error", "log level. options: debug, info, warn, error")
rootCmd.PersistentFlags().StringVar(&repoURL, "repo_url", "", "Repo URL. Used to generate diff url. Currently only github is supported")

viper.BindPFlags(rootCmd.PersistentFlags())
}
Expand Down
23 changes: 21 additions & 2 deletions pkg/jira_changelog/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ import (
//go:embed templates/*.tmpl
var changeLogTmpl embed.FS

type Epic = string
type Changes = map[Epic][]jira.Issue

type Changelog struct {
Changes map[string][]jira.Issue
Changes Changes
fromRef string
toRef string
repoURL string
}

func (c *Changelog) Render(w io.Writer) {
slog.Info("rendering changelog")
slog.Info("rendering changelog", "number-of-changes", len(c.Changes))
tmpl, err := template.ParseFS(changeLogTmpl, "templates/changelog.tmpl")
if err != nil {
slog.Error("error parsing template", "error", err)
Expand All @@ -34,3 +40,16 @@ func (c *Changelog) Render(w io.Writer) {

fmt.Fprint(w, resultBuffer.String())
}

func (c *Changelog) URL() string {
return fmt.Sprintf("%s/compare/%s...%s", c.repoURL, c.fromRef, c.toRef)
}

func NewChangelog(fromRef, toRef, repoURL string, changes Changes) *Changelog {
return &Changelog{
Changes: changes,
fromRef: fromRef,
toRef: toRef,
repoURL: repoURL,
}
}
6 changes: 3 additions & 3 deletions pkg/jira_changelog/changelog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestRender(t *testing.T) {
},
},
},
want: `## What has changed?
want: `## What's changed?
### TestEpic
- [TEST-1] foobar is new
Expand All @@ -45,7 +45,7 @@ func TestRender(t *testing.T) {
},
},
},
want: `## What has changed?
want: `## What's changed?
### TestEpic
- [TEST-1] foobar is new
Expand All @@ -71,7 +71,7 @@ func TestRender(t *testing.T) {
},
},
},
want: `## What has changed?
want: `## What's changed?
### TestEpic1
- [TEST-1] foobar is new
Expand Down
14 changes: 9 additions & 5 deletions pkg/jira_changelog/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Generator struct {
JiraConfig jira.Config
fromRef string
toRef string
repoURL string
client jira.Client
}

Expand All @@ -30,13 +31,15 @@ func (c Generator) Generate(ctx context.Context) *Changelog {
commits, err := gitOutput.Commits()
panicIfErr(err)

changelog, err := c.changelogFromCommits(commits)
changes, err := c.changesFromCommits(commits)
panicIfErr(err)

return changelog
slog.Debug("changes fetched", "changes", changes)

return NewChangelog(c.fromRef, c.toRef, c.repoURL, changes)
}

func (c Generator) changelogFromCommits(commits []git.Commit) (*Changelog, error) {
func (c Generator) changesFromCommits(commits []git.Commit) (Changes, error) {
slog.Debug("Total commit messages", "count", len(commits))

jiraIssues := make([]jira.Issue, 0)
Expand All @@ -55,7 +58,7 @@ func (c Generator) changelogFromCommits(commits []git.Commit) (*Changelog, error
slog.Debug("Total jira issues ids", "count", len(jiraIssues))

issuesByEpic := lo.GroupBy(jiraIssues, func(issue jira.Issue) string { return issue.Epic() })
return &Changelog{Changes: issuesByEpic}, nil
return issuesByEpic, nil
}

func (c Generator) fetchJiraIssue(commit git.Commit) (jira.Issue, error) {
Expand All @@ -73,12 +76,13 @@ func (c Generator) fetchJiraIssue(commit git.Commit) (jira.Issue, error) {
return issue, nil
}

func NewGenerator(jiraConfig jira.Config, fromRef, toRef string) *Generator {
func NewGenerator(jiraConfig jira.Config, fromRef, toRef, repoURL string) *Generator {
client := jira.NewClient(jiraConfig)
return &Generator{
jiraConfig,
fromRef,
toRef,
repoURL,
client,
}
}
4 changes: 2 additions & 2 deletions pkg/jira_changelog/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestChangelogFromCommits(t *testing.T) {
}

expected := &Changelog{
Changes: map[string][]jira.Issue{
Changes: Changes{
"Epic1": {
jira.NewIssue("TEST-1234", "Ticket description", "done", "Epic1"),
jira.NewIssue("TEST-12345", "Ticket description of another from same epic", "done", "Epic1"),
Expand All @@ -44,7 +44,7 @@ func TestChangelogFromCommits(t *testing.T) {
generator := Generator{}
generator.client = mockedClient

result, err := generator.changelogFromCommits(commits)
result, err := generator.changesFromCommits(commits)

assert.NoError(t, err)
assert.Equal(t, expected, result)
Expand Down
12 changes: 12 additions & 0 deletions pkg/jira_changelog/git/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ func ExecGitLog(ctx context.Context, fromRef, toRef string) (GitOutput, error) {
return GitOutput(result), nil
}

func FetchGitRemoteURL(ctx context.Context) (string, error) {
cmd := exec.CommandContext(ctx, "git", "remote", "get-url", "origin")
stdout, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("failed to execute git command: %v", err)
}

result := strings.TrimSpace(string(stdout))
result = strings.TrimSuffix(result, ".git")
return result, nil
}

func (gt GitOutput) Commits() ([]Commit, error) {
output := strings.TrimSpace(string(gt))
commits := make([]Commit, 0)
Expand Down
4 changes: 3 additions & 1 deletion pkg/jira_changelog/templates/changelog.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## What has changed?
## What's changed?
{{- range $epic, $changes := .Changes }}

### {{$epic}}
Expand All @@ -14,3 +14,5 @@
{{ end }}

:warning: = Work in Progress. Ensure that these cards don't break things in production.

Full changelog: [{{.URL}}]({{.URL}})

0 comments on commit 1b520df

Please sign in to comment.