Skip to content

Commit

Permalink
Tailor output to look better on a GitHub releases page (#104)
Browse files Browse the repository at this point in the history
* cleanup markdown format

Signed-off-by: Alex Goodman <[email protected]>

* allow for title templating and default to version value

Signed-off-by: Alex Goodman <[email protected]>

* remove conventional commit prefixes from changelog lines

Signed-off-by: Alex Goodman <[email protected]>

---------

Signed-off-by: Alex Goodman <[email protected]>
  • Loading branch information
wagoodman authored Sep 18, 2023
1 parent 2ddcf57 commit 24b308b
Show file tree
Hide file tree
Showing 10 changed files with 366 additions and 89 deletions.
2 changes: 2 additions & 0 deletions .chronicle.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
log:
level: trace

title: ''
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

[TestMarkdownPresenter_Present - 1]
# v0.19.1

### Bug Fixes

- Redirect cursor hide/show to stderr [[#456](https://github.com/anchore/syft/pull/456)]

### Added Features

- added feature [[#457](https://github.com/anchore/syft/pull/457) @wagoodman]
- another added feature

### Breaking Changes

- breaking change [[#458](https://github.com/anchore/syft/pull/458) [#450](https://github.com/anchore/syft/issues/450) @wagoodman]

**[(Full Changelog)](https://github.com/anchore/syft/compare/v0.19.0...v0.19.1)**

---

[TestMarkdownPresenter_Present_NoTitle - 1]
### Bug Fixes

- Redirect cursor hide/show to stderr [[#456](https://github.com/anchore/syft/pull/456)]

**[(Full Changelog)](https://github.com/anchore/syft/compare/v0.19.0...v0.19.1)**

---

[TestMarkdownPresenter_Present_NoChanges - 1]
# Changelog

**[(Full Changelog)](https://github.com/anchore/syft/compare/v0.19.0...v0.19.1)**

---
74 changes: 63 additions & 11 deletions chronicle/release/format/markdown/presenter.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
package markdown

import (
"bytes"
"fmt"
"io"
"strings"
"text/template"

"github.com/leodido/go-conventionalcommits"
cc "github.com/leodido/go-conventionalcommits/parser"
"github.com/wagoodman/go-presenter"

"github.com/anchore/chronicle/chronicle/release"
"github.com/anchore/chronicle/chronicle/release/change"
)

const (
markdownHeaderTemplate = `# {{.Title}}
markdownHeaderTemplate = `{{if .Title }}# {{.Title}}
## [{{.Version}}]({{.VCSReferenceURL}}) ({{ .Date.Format "2006-01-02" }})
{{ end }}{{if .Changes }}{{ formatChangeSections .Changes }}
[Full Changelog]({{.VCSChangesURL}})
{{ formatChangeSections .Changes }}
{{ end }}**[(Full Changelog)]({{.VCSChangesURL}})**
`
)

Expand Down Expand Up @@ -54,6 +56,17 @@ func NewMarkdownPresenter(config Config) (*Presenter, error) {
return nil, fmt.Errorf("unable to parse markdown presenter template: %w", err)
}

titleTemplater, err := template.New("title").Funcs(funcMap).Parse(config.Title)
if err != nil {
return nil, fmt.Errorf("unable to parse markdown presenter title template: %w", err)
}

buf := bytes.Buffer{}
if err := titleTemplater.Execute(&buf, config); err != nil {
return nil, fmt.Errorf("unable to template title: %w", err)
}
p.config.Title = buf.String()

p.templater = templater

return &p, nil
Expand All @@ -71,7 +84,7 @@ func (m Presenter) formatChangeSections(changes change.Changes) string {
result += formatChangeSection(section.Title, summaries) + "\n"
}
}
return result
return strings.TrimRight(result, "\n")
}

func formatChangeSection(title string, summaries []change.Change) string {
Expand All @@ -83,14 +96,53 @@ func formatChangeSection(title string, summaries []change.Change) string {
}

func formatSummary(summary change.Change) string {
result := fmt.Sprintf("- %s", summary.Text)
result := removeConventionalCommitPrefix(strings.TrimSpace(summary.Text))
result = fmt.Sprintf("- %s", result)
if endsWithPunctuation(result) {
result = result[:len(result)-1]
}

var refs string
for _, ref := range summary.References {
if ref.URL == "" {
result += fmt.Sprintf(" [%s]", ref.Text)
} else {
result += fmt.Sprintf(" [[%s](%s)]", ref.Text, ref.URL)
switch {
case ref.URL == "":
refs += fmt.Sprintf(" %s", ref.Text)
case strings.HasPrefix(ref.Text, "@") && strings.HasPrefix(ref.URL, "https://github.com/"):
// the github release page will automatically show all contributors as a footer. However, if you
// embed the contributor's github handle in a link, then this feature will not work.
refs += fmt.Sprintf(" %s", ref.Text)
default:
refs += fmt.Sprintf(" [%s](%s)", ref.Text, ref.URL)
}
}

refs = strings.TrimSpace(refs)
if refs != "" {
result += fmt.Sprintf(" [%s]", refs)
}

return result + "\n"
}

func endsWithPunctuation(s string) bool {
if len(s) == 0 {
return false
}
return strings.Contains("!.?", s[len(s)-1:]) //nolint:gocritic
}

func removeConventionalCommitPrefix(s string) string {
res, err := cc.NewMachine(cc.WithTypes(conventionalcommits.TypesConventional)).Parse([]byte(s))
if err != nil || res == nil || (res != nil && !res.Ok()) {
// probably not a conventional commit
return s
}

// conventional commits always have a prefix and the message starts after the first ":"
fields := strings.SplitN(s, ":", 2)
if len(fields) == 2 {
return strings.TrimSpace(fields[1])
}

return s
}
Loading

0 comments on commit 24b308b

Please sign in to comment.