diff --git a/polling/polling.go b/polling/polling.go index cf1aff8..f469bc0 100644 --- a/polling/polling.go +++ b/polling/polling.go @@ -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" @@ -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) diff --git a/tray/tray.go b/tray/tray.go index ad89ebc..ce65887 100644 --- a/tray/tray.go +++ b/tray/tray.go @@ -19,7 +19,7 @@ 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") @@ -27,17 +27,28 @@ func onReady() { 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") + } } } }()