forked from kubeshop/botkube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade.go
55 lines (48 loc) · 1.34 KB
/
upgrade.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package controller
import (
"context"
"fmt"
"os"
"time"
"github.com/google/go-github/github"
"github.com/infracloudio/botkube/pkg/config"
log "github.com/infracloudio/botkube/pkg/logging"
)
var (
notified = false
botkubeUpgradeMsg = "Newer version (%s) of BotKube is available :tada:. Please upgrade BotKube backend.\n" +
"Visit botkube.io for more info."
)
func checkRelease(c *config.Config) {
ctx := context.Background()
client := github.NewClient(nil)
release, _, err := client.Repositories.GetLatestRelease(ctx, "infracloudio", "botkube")
if err == nil {
log.Logger.Debugf(fmt.Sprintf("Upgrade notifier:: latest release info=%+v", release))
if len(os.Getenv("BOTKUBE_VERSION")) == 0 || release.TagName == nil {
return
}
// Send notification if newer version available
if len(os.Getenv("BOTKUBE_VERSION")) > 0 && os.Getenv("BOTKUBE_VERSION") != *release.TagName {
sendMessage(c, fmt.Sprintf(botkubeUpgradeMsg, *release.TagName))
notified = true
}
}
}
// UpgradeNotifier checks if newer version for BotKube backend available and notifies user
func UpgradeNotifier(c *config.Config) {
// Check at startup
checkRelease(c)
ticker := time.NewTicker(24 * time.Hour)
defer ticker.Stop()
for {
select {
case <-ticker.C:
if notified == true {
return
}
// Check periodically
checkRelease(c)
}
}
}