Skip to content

Commit

Permalink
add new notes output to get command
Browse files Browse the repository at this point in the history
  • Loading branch information
h0tw1r3 committed May 6, 2024
1 parent 2ed3c46 commit e74d47d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 5 deletions.
17 changes: 16 additions & 1 deletion cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
)

var printLatest bool
var printNotes bool
var printVersion string

// getCmd retrieves a local changelog and prints it to stdout
Expand All @@ -35,6 +36,7 @@ This command is useful for creating and updating Release notes in GitHub.
`,
RunE: func(command *cobra.Command, args []string) error {
fileName := configuration.Config.FileName
tmplSrc := writer.TmplSrcChangelog

var changelog changelog.Changelog
var err error
Expand All @@ -43,16 +45,22 @@ This command is useful for creating and updating Release notes in GitHub.
changelog, err = get.GetLatest(fileName)
} else if printVersion != "" {
changelog, err = get.GetVersion(fileName, printVersion)
} else if printNotes {
err = fmt.Errorf("notes only supported with latest or version")
} else {
changelog, err = get.GetAll(fileName)
}

if printNotes {
tmplSrc = writer.TmplSrcNotes
}

if err != nil {
return err
}

var buf bytes.Buffer
if err := writer.Write(&buf, changelog); err != nil {
if err := writer.Write(&buf, tmplSrc, changelog); err != nil {
return err
}

Expand All @@ -77,5 +85,12 @@ func init() {
"Prints a specific version from the changelog to stdout.",
)

getCmd.Flags().BoolVar(
&printNotes,
"notes",
false,
"Format output as release notes.",
)

getCmd.Flags().SortFlags = false
}
2 changes: 1 addition & 1 deletion cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var newCmd = &cobra.Command{
return err
}

if err := writer.Write(f, changelog); err != nil {
if err := writer.Write(f, writer.TmplSrcChangelog, changelog); err != nil {
return err
}

Expand Down
54 changes: 52 additions & 2 deletions internal/writer/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/chelnak/gh-changelog/pkg/changelog"
)

var tmplSrc = `<!-- markdownlint-disable MD024 -->
const tmplChangelog = `<!-- markdownlint-disable MD024 -->
# Changelog
All notable changes to this project will be documented in this file.
Expand Down Expand Up @@ -79,7 +79,57 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
{{- end}}
`

func Write(writer io.Writer, changelog changelog.Changelog) error {
const tmplNotes = `{{range .GetEntries }}
{{- if .Security }}
### Security
{{range .Security}}
- {{.}}
{{- end}}
{{end}}
{{- if .Changed }}
### Changed
{{range .Changed}}
- {{.}}
{{- end}}
{{end}}
{{- if .Removed }}
### Removed
{{range .Removed}}
- {{.}}
{{- end}}
{{end}}
{{- if .Deprecated }}
### Deprecated
{{range .Deprecated}}
- {{.}}
{{- end}}
{{end}}
{{- if .Added }}
### Added
{{range .Added}}
- {{.}}
{{- end}}
{{end}}
{{- if .Fixed }}
### Fixed
{{range .Fixed}}
- {{.}}
{{- end}}
{{end}}
{{- if .Other }}
### Other
{{range .Other}}
- {{.}}
{{- end}}
{{end}}
{{- end}}`

const (
TmplSrcChangelog = tmplChangelog
TmplSrcNotes = tmplNotes
)

func Write(writer io.Writer, tmplSrc string, changelog changelog.Changelog) error {
tmpl, err := template.New("changelog").Funcs(template.FuncMap{
"getFirstCommit": func() string {
git := gitclient.NewGitClient(exec.Command)
Expand Down
10 changes: 9 additions & 1 deletion internal/writer/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func Test_ItWritesOutAChangelogInTheCorrectFormat(t *testing.T) {
mockChangelog.AddUnreleased([]string{"Unreleased 1", "Unreleased 2"})

var buf bytes.Buffer
err := writer.Write(&buf, mockChangelog)
err := writer.Write(&buf, writer.TmplSrcChangelog, mockChangelog)

assert.NoError(t, err)

Expand All @@ -58,4 +58,12 @@ func Test_ItWritesOutAChangelogInTheCorrectFormat(t *testing.T) {
assert.Regexp(t, "### Other", buf.String())
assert.Regexp(t, "- Other 1", buf.String())
assert.Regexp(t, "- Other 2", buf.String())

buf.Reset()
err = writer.Write(&buf, writer.TmplSrcNotes, mockChangelog)

assert.NoError(t, err)

assert.NotRegexp(t, regexp.MustCompile(`## \[v1.0.0\]\(https:\/\/github.com\/repo-owner\/repo-name\/tree\/v1.0.0\)`), buf.String())
assert.NotRegexp(t, regexp.MustCompile(`\[Full Changelog\]\(https:\/\/github.com\/repo-owner\/repo-name\/compare\/v0.9.0\.\.\.v1.0.0\)`), buf.String())
}

0 comments on commit e74d47d

Please sign in to comment.