Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
Gracefully handle error when polling
Browse files Browse the repository at this point in the history
Resolves #15
  • Loading branch information
michaelsanford committed Feb 17, 2019
1 parent 3c33d34 commit 0ad81fc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
15 changes: 10 additions & 5 deletions polling/polling.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
const pollIntervalSec = 10

// Poll retrieves pull request data from Bitbucket at a given interval
func Poll() <-chan uint8 {
items := make(chan uint8)
func Poll() <-chan int8 {
items := make(chan int8)

user, url := credentials.GetConfig()
endpoint := url + "/rest/api/1.0/dashboard/pull-requests?state=OPEN&role=REVIEWER&participantStatus=UNAPPROVED"
Expand All @@ -33,11 +33,16 @@ func Poll() <-chan uint8 {
req, _ := http.NewRequest("GET", endpoint, nil)
req.SetBasicAuth(user, pass)

go func(items chan uint8) {
go func(items chan int8) {
for ; true; <-ticker.C {
resp, _ := client.Do(req)
bodyText, _ := ioutil.ReadAll(resp.Body)
items <- uint8(gjson.Get(string(bodyText), "size").Uint())

if resp != nil && resp.StatusCode == 200 {
bodyText, _ := ioutil.ReadAll(resp.Body)
items <- int8(gjson.Get(string(bodyText), "size").Uint())
} else {
items <- int8(-1)
}
}
}(items)

Expand Down
19 changes: 15 additions & 4 deletions tray/tray.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,36 @@ func onReady() {

systray.SetIcon(icon.Lock)
systray.SetTitle("Bittray")
systray.SetTooltip("Loading...")
systray.SetTooltip("Locked")

mQuit := systray.AddMenuItem("Quit", "Quit Bittray")
mReset := systray.AddMenuItem("Reset", "Reset Bittray to factory defaults")
systray.AddSeparator()
mStash := systray.AddMenuItem("Go to BitBucket", "Review your open Pull Requests")

go func() {
warned := false

for count := range polling.Poll() {
if count > 0 {
warned = false
var plural string
if count > 1 {
plural = "s"
}
systray.SetTooltip(fmt.Sprintf("%d PR%s waiting...", count, plural))
systray.SetIcon(icon.Alarm)
} else {
systray.SetTooltip("Pull Request queue clear!")
systray.SetTooltip(fmt.Sprintf("%d PR%s waiting...", count, plural))
} else if count == 0 {
warned = false
systray.SetIcon(icon.Checkmark)
systray.SetTooltip("Pull Request queue clear!")
} else if count == -1 {
if !warned {
warned = true
systray.SetIcon(icon.Lock)
systray.SetTooltip("Locked")
dlgs.Error("Bitbucket Error", "There was a problem contacting the API")
}
}
}
}()
Expand Down

0 comments on commit 0ad81fc

Please sign in to comment.