Skip to content

Commit

Permalink
Download and show release notes (#250)
Browse files Browse the repository at this point in the history
andig authored Jul 29, 2020

Verified

This commit was signed with the committer’s verified signature.
sandy081 Sandeep Somavarapu
1 parent 8b4bd1a commit 728430c
Showing 8 changed files with 274 additions and 149 deletions.
41 changes: 28 additions & 13 deletions assets/index.html
Original file line number Diff line number Diff line change
@@ -58,8 +58,6 @@
<body>

<div id="app">
<version installed="<<.Version>>" id="version-bar"></version>

<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="https://github.com/andig/evcc"><i class="text-primary fas fa-leaf mr-2"></i>evcc</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup"
@@ -78,21 +76,38 @@
</div>
</nav>

<version installed="<<.Version>>" id="version-bar"></version>

<message-toasts></message-toasts>
<router-view></router-view>
</div>

<script type="text/x-template" id="version-template">
<div class="row p-3 bg-warning collapse" ref="bar">
<div class="col-12">
Neue Version verfügbar!
Installiert: {{installed}}.
Verfügbar: {{state.availableVersion}}.
<b><a v-bind:href="'https://github.com/andig/evcc/releases/tag/'+state.availableVersion"
class="text-body pr-2">Download...</a></b>
<button type="button" class="close float-right" style="margin-top: -2px" aria-label="Close" data-toggle="collapse" data-target="#version-bar">
<span aria-hidden="true">&times;</span>
</button>
<div class="collapse" ref="bar">
<div class="row p-3 bg-warning">
<div class="col-12">
Neue Version verfügbar!
Installiert: {{installed}}.
Verfügbar: {{state.availableVersion}}.
<b class="px-3" data-toggle="collapse" data-target="#release-notes" v-if="state.releaseNotes">
<a href="#" class="text-body">
Release notes
<i class="fas fa-chevron-up" v-if="notesShown"></i>
<i class="fas fa-chevron-down" v-else></i>
</a>
</b>
<b class="px-3">
<a v-bind:href="'https://github.com/andig/evcc/releases/tag/'+state.availableVersion" class="text-body">
Download <i class="fas fa-chevron-down"></i>
</a>
</b>
<button type="button" class="close float-right" style="margin-top: -2px" aria-label="Close" data-toggle="collapse" data-target="#version-bar">
<span aria-hidden="true">&times;</span>
</button>
</div>
</div>
<div class="row p-3 bg-light collapse" id="release-notes" ref="notes">
<div class="col-12" v-html="state.releaseNotes"></div>
</div>
</div>
</script>
@@ -113,7 +128,7 @@ <h1 class="display-4 pt-3" v-if="title">{{title}}</h1>
</script>

<div id="toasts">
<div aria-atomic="true" style="position: absolute; top: 4rem; right: 0.5rem; min-height: 200px; min-width: 300px">
<div aria-atomic="true" style="position: absolute; top: 4rem; right: 0.5rem;">
<!-- Position it -->
<message-toast v-for="item in items" v-bind:item="item" :id="'message-id-'+item.id" :key="item.id">
</message-toast>
6 changes: 6 additions & 0 deletions assets/js/app.js
Original file line number Diff line number Diff line change
@@ -181,8 +181,14 @@ Vue.component('version', {
data: function () {
return {
state: store.state,
notesShown: false,
};
},
mounted: function () {
$(this.$refs.notes)
.on('show.bs.collapse', function () { this.notesShown = true; }.bind(this))
.on('hide.bs.collapse', function () { this.notesShown = false; }.bind(this));
},
watch: {
"state.availableVersion": function () {
if (this.installed != "<<.Version>>" && // go template parsed?
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -11,11 +11,13 @@ require (
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
github.com/godbus/dbus/v5 v5.0.3
github.com/golang/mock v1.4.3
github.com/google/go-github/v32 v32.1.0
github.com/gorilla/handlers v1.4.2
github.com/gorilla/mux v1.7.4
github.com/gorilla/websocket v1.4.2
github.com/gregdel/pushover v0.0.0-20200416074932-c8ad547caed4
github.com/grid-x/modbus v0.0.0-20200704184817-73c05928caa1
github.com/hashicorp/go-version v1.2.1
github.com/influxdata/influxdb-client-go v1.4.0
github.com/itchyny/gojq v0.11.0
github.com/joeshaw/carwings v0.0.0-20191118152321-61b46581307a
@@ -32,6 +34,7 @@ require (
github.com/spf13/viper v1.7.0
github.com/tcnksm/go-latest v0.0.0-20170313132115-e3007ae9052e
github.com/volkszaehler/mbmd v0.0.0-20200717102329-c4d965bd1eac
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
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -108,6 +108,8 @@ github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5a
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY=
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
github.com/google/go-github/v32 v32.1.0 h1:GWkQOdXqviCPx7Q7Fj+KyPoGm4SwHRh8rheoPhd27II=
github.com/google/go-github/v32 v32.1.0/go.mod h1:rIEpZD9CTDQwDK9GDrtMTycQNA4JU3qBsCizh3q2WCI=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
@@ -349,6 +351,7 @@ github.com/volkszaehler/mbmd v0.0.0-20200717102329-c4d965bd1eac/go.mod h1:sldLyJ
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32 h1:5tjfNdR2ki3yYQ842+eX2sQHeiwpKJ0RnHO4IYOc4V8=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
259 changes: 131 additions & 128 deletions server/assets.go

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions server/socket.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"encoding/json"
"fmt"
"net/http"
"strings"
@@ -92,12 +93,13 @@ func encode(v interface{}) (string, error) {
case time.Duration:
// must be before stringer to convert to seconds instead of string
s = fmt.Sprintf("%d", int64(val.Seconds()))
case fmt.Stringer, string:
s = fmt.Sprintf("\"%s\"", val)
case float64:
s = fmt.Sprintf("%.3f", val)
default:
s = fmt.Sprintf("%v", val)
// s = fmt.Sprintf("%v", val)
if b, err := json.Marshal(v); err == nil {
s = string(b)
}
}
return s, nil
}
96 changes: 91 additions & 5 deletions server/updater/updater.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package updater

import (
"bytes"
"context"
"fmt"
"strings"
"time"

"github.com/andig/evcc/server"
"github.com/andig/evcc/util"
"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
@@ -15,6 +24,17 @@ type updater struct {
cache chan<- util.Param
}

func tag2semver(tag string) string {
if strings.Count(tag, ".") < 2 {
tag += ".0"
}
return tag
}

func semver2tag(ver string) string {
return strings.TrimSuffix(ver, ".0")
}

// Run regularly checks version
func Run(log *util.Logger, cache chan<- util.Param) {
instance = &updater{
@@ -31,19 +51,85 @@ func Run(log *util.Logger, cache chan<- util.Param) {
// checkVersion validates if updates are available
func (u *updater) checkVersion() {
githubTag := &latest.GithubTag{
Owner: "andig",
Repository: "evcc",
Owner: server.Owner,
Repository: server.Repository,
}

if res, err := latest.Check(githubTag, server.Version); err == nil {
installed := tag2semver(server.Version)
if res, err := latest.Check(githubTag, installed); err == nil {
tag := semver2tag(res.Current)
if res.Outdated {
u.log.INFO.Printf("new version available - please upgrade to %s", res.Current)
u.log.INFO.Printf("new version available - please upgrade to %s", tag)
}

u.cache <- util.Param{
Key: "availableVersion",
Val: res.Current,
Val: tag,
}

u.fetchReleaseNotes(res.Current)
} 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) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

if notes, err := releaseNotes(ctx, tag2semver(server.Version), to); err == nil {
u.cache <- util.Param{
Key: "releaseNotes",
Val: notes,
}
} else {
u.log.WARN.Printf("couldn't download release notes: %v", err)
}
}

// 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
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()),
)

for _, rel := range releases {
tag := *rel.TagName

var ver *version.Version
if ver, err = version.NewVersion(tag2semver(tag)); err != nil {
return
}

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

notes.WriteString(fmt.Sprintf("<h1>%s</h1>\n", tag))
if err = gm.Convert([]byte(body), notes); err != nil {
return
}
}
}

return notes.String(), nil
}
7 changes: 7 additions & 0 deletions server/version.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package server

var (
// Repository name
Repository = "evcc"

// Owner of repository
Owner = "andig"

// Version of executable
Version = "0.0.1-alpha"

// Commit of executable
Commit = "HEAD"
)

0 comments on commit 728430c

Please sign in to comment.