Skip to content

Commit

Permalink
feat(publish): Add dry-run mode
Browse files Browse the repository at this point in the history
  • Loading branch information
aexvir authored and kodiakhq[bot] committed Nov 28, 2019
1 parent 13c196b commit a25a1bd
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/get_commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func getCommits(repo *history.Git) ([]plumbing.Hash, error) {
return nil, err
}

commits := make([]plumbing.Hash, 0)
var commits []plumbing.Hash

// If previous tag is not available, or both tags are the same,
// provide empty hash so that all commits are iterated.
Expand Down
30 changes: 23 additions & 7 deletions cmd/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ var publishCmd = &cobra.Command{
debug = true
}

dryRun := false
if cmd.Flag("dry-run").Value.String() == "true" {
dryRun = true
}

repo, err := history.OpenGit(".", debug)

if err != nil {
Expand Down Expand Up @@ -60,6 +65,23 @@ var publishCmd = &cobra.Command{
)
}

sections := text.SplitSections(parsedCommits)

releaseNotes := text.ReleaseNotes{
Simple: simpleOutput,
}

content := releaseNotes.Generate(sections, dryRun)

if err != nil {
return err
}

if dryRun {
fmt.Println("my job here is done...")
return nil
}

viper.AutomaticEnv()

var releaseService *releaser.Releaser
Expand Down Expand Up @@ -107,13 +129,7 @@ var publishCmd = &cobra.Command{
return errors.New("Missing release service, please consult documentation on required env vars")
}

sections := text.SplitSections(parsedCommits)

releaseNotes := text.ReleaseNotes{
Simple: simpleOutput,
}

err = releaseService.Release(releaseNotes.Generate(sections))
err = releaseService.Release(content)

if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ var rootCmd = &cobra.Command{
// Verbose is used to allow verbose/debug output for any given command
var Verbose bool

// DryRun is used for showing the changelog without actually publishing it
var DryRun bool

func init() {
rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")
rootCmd.PersistentFlags().BoolVarP(&DryRun, "dry-run", "d", false, "dry run")
}

// Execute just executes the rootCmd for Cobra
Expand Down
1 change: 0 additions & 1 deletion internal/releaser/releaser.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type Releaser struct {
// Options are used to initialize releaser
type Options struct {
Provider Provider
DryRun bool
Token string
Owner string
Repo string
Expand Down
7 changes: 6 additions & 1 deletion internal/text/release_notes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package text

import (
"fmt"
"strings"
)

Expand All @@ -10,7 +11,7 @@ type ReleaseNotes struct {
}

// Generate generates the output mentioned in the expected-output.md
func (r *ReleaseNotes) Generate(sections map[string][]Commit) string {
func (r *ReleaseNotes) Generate(sections map[string][]Commit, dryRun bool) string {
builder := strings.Builder{}
// Extra lines at the start to make sure formatting starts correctly
builder.WriteString("\n\n")
Expand All @@ -31,5 +32,9 @@ func (r *ReleaseNotes) Generate(sections map[string][]Commit) string {
builder.WriteString(r.buildSection("others", sections["others"]))
}

if dryRun {
fmt.Print(builder.String())
}

return builder.String()
}
6 changes: 3 additions & 3 deletions internal/text/release_notes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestReleaseNotes(t *testing.T) {
"others": []Commit{Commit{Category: "other", Scope: "", Heading: "merge master in something"}, Commit{Category: "bs", Scope: "", Heading: "random"}},
}

releaseNotes := notes.Generate(sections)
releaseNotes := notes.Generate(sections, false)

assert.Equal(t, expected+"\n", "test heading"+releaseNotes)
}
Expand All @@ -55,7 +55,7 @@ func TestReleaseNotesSimple(t *testing.T) {
"others": []Commit{Commit{Category: "other", Scope: "", Heading: "merge master in something"}, Commit{Category: "bs", Scope: "", Heading: "random"}},
}

releaseNotes := notes.Generate(sections)
releaseNotes := notes.Generate(sections, false)

assert.Equal(t, expected+"\n", "test heading"+releaseNotes)
}
Expand All @@ -68,7 +68,7 @@ func TestReleaseNotesWithMissingSections(t *testing.T) {
"features": []Commit{Commit{Heading: "ci test"}},
}

releaseNotes := notes.Generate(sections)
releaseNotes := notes.Generate(sections, false)

assert.Equal(t, expected, releaseNotes)
}

0 comments on commit a25a1bd

Please sign in to comment.