Skip to content

Commit

Permalink
Merge pull request #153 from h0tw1r3/feature/notes
Browse files Browse the repository at this point in the history
  • Loading branch information
chelnak committed Jun 11, 2024
2 parents 2ed3c46 + b7776a0 commit 3bd1f40
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 5 deletions.
44 changes: 43 additions & 1 deletion cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,32 @@ import (
"github.com/spf13/cobra"
)

type outputEnum string

const (
outputStandard outputEnum = "standard"
outputNotes outputEnum = "notes"
)

func (e *outputEnum) String() string {
return string(*e)
}

func (e *outputEnum) Set(v string) error {
switch v {
case string(outputStandard), string(outputNotes):
*e = outputEnum(v)
return nil
default:
return fmt.Errorf(`must be one of %s or %s`, outputStandard, outputNotes)
}
}

func (e *outputEnum) Type() string {
return "outputEnum"
}

var outputTemplate = outputStandard
var printLatest bool
var printVersion string

Expand All @@ -36,23 +62,33 @@ This command is useful for creating and updating Release notes in GitHub.
RunE: func(command *cobra.Command, args []string) error {
fileName := configuration.Config.FileName

var tmplSrc string
var changelog changelog.Changelog
var err error

if printLatest {
changelog, err = get.GetLatest(fileName)
} else if printVersion != "" {
changelog, err = get.GetVersion(fileName, printVersion)
} else if outputTemplate == outputNotes {
err = fmt.Errorf("notes output only supported with latest or version")
} else {
changelog, err = get.GetAll(fileName)
}

switch outputTemplate {
case outputStandard:
tmplSrc = writer.TmplSrcStandard
case outputNotes:
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 +113,11 @@ func init() {
"Prints a specific version from the changelog to stdout.",
)

getCmd.Flags().Var(
&outputTemplate,
"output",
fmt.Sprintf(`Output template. allowed: "%s" or "%s"`, outputStandard, outputNotes),
)

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.TmplSrcStandard, 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 tmplStandard = `<!-- 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 (
TmplSrcStandard = tmplStandard
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.TmplSrcStandard, 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 3bd1f40

Please sign in to comment.