Improve notification for a new version, show release notes

This commit is contained in:
Justin Hawkins 2022-07-03 12:12:32 +09:30
parent 451335e17d
commit da970239a5
7 changed files with 82 additions and 41 deletions

View File

@ -30,12 +30,12 @@ func main() {
go func() {
for {
version.UpdateVersionInfo()
version.VersionInfo.UpdateVersionInfo()
time.Sleep(time.Hour * 6)
}
}()
log.Printf("linkwallet version %s starting", version.Is())
log.Printf("linkwallet version %s starting", version.VersionInfo.Local.Tag)
server := web.Create(bmm, cmm)
go bmm.RunQueue()

1
go.mod
View File

@ -37,6 +37,7 @@ require (
github.com/gobwas/glob v0.2.3 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gomarkdown/markdown v0.0.0-20220627144906-e9a81102ebeb
github.com/google/go-github/v44 v44.1.0
github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b
github.com/kennygrant/sanitize v1.2.4 // indirect

2
go.sum
View File

@ -38,6 +38,8 @@ github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaW
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/gomarkdown/markdown v0.0.0-20220627144906-e9a81102ebeb h1:5b/eFaSaKPFG9ygDBaPKkydKU5nFJYk08g9jPIVogMg=
github.com/gomarkdown/markdown v0.0.0-20220627144906-e9a81102ebeb/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=

View File

@ -3,15 +3,16 @@ package version
import (
"context"
"fmt"
"strings"
"sync"
"github.com/google/go-github/v44/github"
"golang.org/x/mod/semver"
)
const Tag = "v0.0.27"
const Tag = "v0.0.28"
var versionInfo struct {
type Info struct {
Local struct {
Tag string
}
@ -23,36 +24,26 @@ var versionInfo struct {
m sync.Mutex
}
var VersionInfo Info
func init() {
versionInfo.Remote.Valid = false
versionInfo.Local.Tag = Tag
VersionInfo.Remote.Valid = false
VersionInfo.Local.Tag = Tag
}
func Is() string {
return versionInfo.Local.Tag
}
func UpgradeAvailable() (bool, string) {
versionInfo.m.Lock()
defer versionInfo.m.Unlock()
if !versionInfo.Remote.Valid {
return false, ""
func (vi *Info) UpgradeAvailable() bool {
vi.m.Lock()
defer vi.m.Unlock()
if !vi.Remote.Valid {
return false
}
if semver.Compare(versionInfo.Local.Tag, versionInfo.Remote.Tag) < 0 {
return true, versionInfo.Remote.Tag
if semver.Compare(vi.Local.Tag, vi.Remote.Tag) < 0 {
return true
}
return false, ""
return false
}
func UpgradeAvailableString() string {
upgrade, ver := UpgradeAvailable()
if upgrade {
return ver
}
return ""
}
func UpdateVersionInfo() {
func (vi *Info) UpdateVersionInfo() {
client := github.NewClient(nil)
rels, _, err := client.Repositories.ListReleases(context.Background(), "tardisx", "linkwallet", nil)
@ -63,16 +54,24 @@ func UpdateVersionInfo() {
return
}
versionInfo.m.Lock()
versionInfo.Remote.Tag = *rels[0].TagName
versionInfo.Remote.Valid = true
versionInfo.UpgradeReleaseNotes = ""
vi.m.Lock()
vi.Remote.Tag = *rels[0].TagName
vi.Remote.Valid = true
vi.UpgradeReleaseNotes = ""
for _, r := range rels {
if semver.Compare(versionInfo.Local.Tag, *r.TagName) < 0 {
versionInfo.UpgradeReleaseNotes += fmt.Sprintf("Version: %s\n\n%s\n\n", *r.TagName, *r.Body)
if semver.Compare(VersionInfo.Local.Tag, *r.TagName) < 0 {
vi.UpgradeReleaseNotes += fmt.Sprintf("*Version %s*\n\n", *r.TagName)
bodyLines := strings.Split(*r.Body, "\n")
for _, l := range bodyLines {
if strings.Index(l, "#") == 0 && strings.Contains(l, "Changelog") {
// do nothing, ignore the changelog heading
} else {
vi.UpgradeReleaseNotes += l + "\n"
}
}
}
}
versionInfo.m.Unlock()
vi.m.Unlock()
}

View File

@ -34,13 +34,12 @@
</div>
<div class="top-bar-right">
<ul class="menu">
{{ if newVersion }}
<li>
<div><a href="https://github.com/tardisx/linkwallet/releases/tag/{{ newVersion }}">{{ newVersion }} available</a></div>
</li>
{{ end }}
<li class="menu-text">
{{ version }}
<a href="/releaseinfo">{{ version.Local.Tag }}
{{ if version.UpgradeAvailable }}
{{ end }}
</a>
</li>
<li>
<a href="https://github.com/tardisx/linkwallet">
@ -64,6 +63,8 @@
{{ template "config.html" . }}
{{ else if eq .page "edit" }}
{{ template "edit.html" . }}
{{ else if eq .page "releaseinfo" }}
{{ template "release_info.html" . }}
{{ end }}
{{/* template "foundation_sample.html" . */}}
</div>

View File

@ -0,0 +1,23 @@
<div class="grid-x grid-padding-x">
<div class="large-12 cell">
<h5>Release info</h5>
{{ if not version.Remote.Valid }}
<p>GitHub version information not yet fetched.</p>
{{ else }}
{{ if version.UpgradeAvailable }}
<p>
A new version is available:
<a href="https://github.com/tardisx/linkwallet/releases/tag/{{ version.Remote.Tag }}">
{{ version.Remote.Tag }}
</a>
(you have {{ version.Local.Tag }}).
</p>
{{ markdown version.UpgradeReleaseNotes }}
{{ else }}
<p>You are currently running the most recent version.</p>
{{ end }}
{{ end }}
</div>

View File

@ -16,6 +16,7 @@ import (
"github.com/tardisx/linkwallet/entity"
"github.com/tardisx/linkwallet/version"
"github.com/gomarkdown/markdown"
"github.com/hako/durafmt"
"github.com/gin-contrib/gzip"
@ -75,7 +76,14 @@ func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
}
// templ := template.Must(template.New("").Funcs(template.FuncMap{"dict": dictHelper}).ParseFS(templateFiles, "templates/*.html"))
templ := template.Must(template.New("").Funcs(template.FuncMap{"nicetime": niceTime, "niceURL": niceURL, "join": strings.Join, "version": version.Is, "newVersion": version.UpgradeAvailableString}).ParseFS(templateFiles, "templates/*.html"))
templ := template.Must(template.New("").Funcs(
template.FuncMap{
"nicetime": niceTime,
"niceURL": niceURL,
"join": strings.Join,
"version": func() *version.Info { return &version.VersionInfo },
"markdown": func(s string) template.HTML { return template.HTML(string(markdown.ToHTML([]byte(s), nil, nil))) },
}).ParseFS(templateFiles, "templates/*.html"))
config, err := cmm.LoadConfig()
if err != nil {
@ -386,6 +394,13 @@ func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
)
})
r.GET("/releaseinfo", func(c *gin.Context) {
meta := gin.H{"page": "releaseinfo", "config": config}
c.HTML(http.StatusOK,
"_layout.html", meta,
)
})
return server
}