Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tailor output to look better on a GitHub releases page #104

Merged
merged 4 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we now need to remove trailing newlines?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now there is a footer and it became noticeable that we have a large amount of whitespace before the footer -- this makes the whitespace between the content and footer smaller

}

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