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

Use github markdown converter to render links correctly #292

Merged
merged 3 commits into from
Aug 17, 2020
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
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ require (
github.com/spf13/viper v1.7.1
github.com/tcnksm/go-latest v0.0.0-20170313132115-e3007ae9052e
github.com/volkszaehler/mbmd v0.0.0-20200804054214-122815825570
github.com/yuin/goldmark v1.1.32
golang.org/x/net v0.0.0-20200707034311-ab3426394381
golang.org/x/tools v0.0.0-20200717024301-6ddee64345a6
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c
Expand Down
44 changes: 19 additions & 25 deletions server/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import (
"github.com/google/go-github/v32/github"
"github.com/hashicorp/go-version"
latest "github.com/tcnksm/go-latest"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/renderer/html"
)

var instance *updater
Expand Down Expand Up @@ -67,18 +64,18 @@ func (u *updater) checkVersion() {
Val: tag,
}

u.fetchReleaseNotes(res.Current)
u.fetchReleaseNotes()
} else {
u.log.ERROR.Println("version check failed:", err)
}
}

// fetchReleaseNotes retrieves release notes up to semver and sends to client
func (u *updater) fetchReleaseNotes(to string) {
func (u *updater) fetchReleaseNotes() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

if notes, err := releaseNotes(ctx, tag2semver(server.Version), to); err == nil {
if notes, err := releaseNotes(ctx, tag2semver(server.Version)); err == nil {
u.cache <- util.Param{
Key: "releaseNotes",
Val: notes,
Expand All @@ -89,27 +86,19 @@ func (u *updater) fetchReleaseNotes(to string) {
}

// releaseNotes returns github release notes for the (from,to] semver interval
func releaseNotes(ctx context.Context, from, to string) (rendered string, err error) {
var fromVersion, toVersion *version.Version
func releaseNotes(ctx context.Context, from string) (rendered string, err error) {
var fromVersion *version.Version
if fromVersion, err = version.NewVersion(from); err != nil {
return
}
if toVersion, err = version.NewVersion(to); err != nil {
return
}

client := github.NewClient(nil)
releases, _, err := client.Repositories.ListReleases(ctx, server.Owner, server.Repository, nil)
if err != nil {
return
}

notes := bytes.NewBuffer([]byte{})
gm := goldmark.New(
goldmark.WithExtensions(extension.GFM),
goldmark.WithRendererOptions(html.WithHardWraps()),
)

history := bytes.NewBuffer([]byte{})
for _, rel := range releases {
tag := *rel.TagName

Expand All @@ -118,18 +107,23 @@ func releaseNotes(ctx context.Context, from, to string) (rendered string, err er
return
}

if ver.GreaterThan(fromVersion) && ver.LessThanOrEqual(toVersion) {
body := strings.TrimSpace(rel.GetBody())
if body == "" {
continue
}
if ver.LessThanOrEqual(fromVersion) {
continue
}

notes.WriteString(fmt.Sprintf("<h1>%s</h1>\n", tag))
if err = gm.Convert([]byte(body), notes); err != nil {
if notes := strings.TrimSpace(rel.GetBody()); notes != "" {
var md string
if md, _, err = client.Markdown(context.Background(), notes, &github.MarkdownOptions{
Mode: "gfm",
Context: fmt.Sprintf("%s/%s", server.Owner, server.Repository),
}); err != nil {
return
}

history.WriteString(fmt.Sprintf("<h1>%s</h1>\n", tag))
history.WriteString(md)
}
}

return notes.String(), nil
return history.String(), nil
}