This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
/
polling.go
92 lines (75 loc) · 2.04 KB
/
polling.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package polling
/**
* Note: This only works with the Bitbucket API 1.0
*/
import (
"fmt"
"github.com/coreos/go-semver/semver"
"github.com/getlantern/systray"
"github.com/michaelsanford/bittray/config"
"github.com/tidwall/gjson"
"io/ioutil"
"math/rand"
"net/http"
"time"
)
var pInterval = config.GetPollingInterval()
// Poll retrieves pull request data from Bitbucket at a given interval
func Poll() <-chan int8 {
items := make(chan int8, 1)
user, url := config.GetConfig()
endpoint := url + "/rest/api/1.0/dashboard/pull-requests?state=OPEN&role=REVIEWER&participantStatus=UNAPPROVED"
pass, ok, _ := config.AskPass()
if !ok {
systray.Quit()
}
ticker := time.NewTicker(pInterval)
client := &http.Client{}
req, _ := http.NewRequest("GET", endpoint, nil)
req.SetBasicAuth(user, pass)
go func(items chan int8) {
cBackOffDelay := int(10)
for ; true; <-ticker.C {
resp, _ := client.Do(req)
if resp != nil {
if resp.StatusCode == 200 {
bodyText, _ := ioutil.ReadAll(resp.Body)
items <- int8(gjson.Get(string(bodyText), "size").Uint())
cBackOffDelay = 10
} else if resp.StatusCode == 401 || resp.StatusCode == 403 {
ticker.Stop()
items <- int8(-2)
} else if resp.StatusCode == 429 {
items <- int8(-3)
cBackOffDelay = backOff(cBackOffDelay)
}
} else {
items <- int8(-1)
}
}
}(items)
return items
}
// CheckForUpdate queries the GitHub repo's latest release tag for an update
func CheckForUpdate() (available bool, latestTagName string, err error) {
vCurrent := semver.New(config.CurrentVersionTag)
resp, err := http.Get(config.GhAPI)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
json, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
latestTagName = gjson.Get(string(json), "tag_name").Str
vLatest := semver.New(latestTagName)
available = vCurrent.LessThan(*vLatest)
return
}
func backOff(backOffDelay int) int {
time.Sleep(time.Second * time.Duration(backOffDelay))
return rand.Intn(backOffDelay) + backOffDelay
}