-
Notifications
You must be signed in to change notification settings - Fork 0
/
updatenotifier.go
75 lines (65 loc) · 2.05 KB
/
updatenotifier.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"io/ioutil"
"net/http"
"regexp"
"time"
)
// updateNotifierLatestCommitRegexp contains the regular expression that
// extracts the latest commit sha1 from the github page of the bot.
var updateNotifierLatestCommitRegexp *regexp.Regexp
// init initializes the regexp.
func init() {
updateNotifierLatestCommitRegexp = regexp.MustCompile("commit-tease-sha.*([0-9a-f]{40})")
}
// initUpdateNotifier sets default values for the update notifier.
func (b *bot) initUpdateNotifier() {
if b.IRC.UpdateNotifierMsg == "" {
b.IRC.UpdateNotifierMsg = "new version of the bot is available (<version>)"
}
}
// updateNotifierHandler downloads the HTML source of the main github page and
// extracts the latest commit, if the currently running version of the bot
// isn't equal to the latest commit we'll send a message to all users in the
// update notifier names array.
func (b *bot) updateNotifierHandler() {
// Return early if we don't have anyone to notify.
if len(b.IRC.UpdateNotifierNames) == 0 {
return
}
for {
// Let's sleep for an hour before we perform the check.
time.Sleep(1 * time.Hour)
// Download the HTML source of the bot repo.
res, err := http.Get("https://github.com/osm/bot")
if err != nil {
b.logger.Printf("updateNotifier: %v", err)
return
}
// Read the body.
defer res.Body.Close()
data, err := ioutil.ReadAll(res.Body)
if err != nil {
b.logger.Printf("updateNotifier: %v", err)
return
}
// Try to extract the latest commit.
matches := updateNotifierLatestCommitRegexp.FindStringSubmatch(string(data))
if matches == nil {
b.logger.Printf("updateNotifier: nothing found")
return
}
// The version has changed, so let's notify everyone that needs to
// know. We don't want to keep notifying, so we'll end the
// goroutine after the notification has been sent.
version := matches[1][0:8]
if version != VERSION {
for _, name := range b.IRC.UpdateNotifierNames {
b.privmsgpht(b.IRC.UpdateNotifierMsg, name, map[string]string{
"<version>": version,
})
return
}
}
}
}