From fb637700b104b63b1e7b326ba010c0cb6238484d Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Thu, 5 Apr 2018 10:05:05 -0700 Subject: [PATCH] Displays Github review requests and PRs --- bamboohr/widget.go | 2 +- config.yml | 5 +++- gcal/widget.go | 5 ++-- git/widget.go | 7 +++-- github/client.go | 63 +++++++++++++++++++++++++++++++++++++++++++++ github/widget.go | 64 ++++++++++++++++++++++++++++++++++++++++++++-- security/client.go | 8 +++--- security/widget.go | 18 +++---------- weather/client.go | 1 + weather/widget.go | 13 +++++++--- wtf.go | 16 ++++++------ wtf/utils.go | 4 +++ 12 files changed, 168 insertions(+), 38 deletions(-) create mode 100644 github/client.go diff --git a/bamboohr/widget.go b/bamboohr/widget.go index 3f0fb04b..4e9eee9d 100644 --- a/bamboohr/widget.go +++ b/bamboohr/widget.go @@ -63,7 +63,7 @@ func (widget *Widget) addView() { func (widget *Widget) contentFrom(items []Item) string { if len(items) == 0 { - return fmt.Sprintf("\n\n\n\n\n\n\n%s", wtf.CenterText("[grey]no one[white]", 52)) + return fmt.Sprintf("\n\n\n\n\n\n\n\n%s", wtf.CenterText("[grey]no one[white]", 50)) } str := "\n" diff --git a/config.yml b/config.yml index f55d8344..e9523bb7 100644 --- a/config.yml +++ b/config.yml @@ -4,6 +4,7 @@ wtf: refreshInterval: 900 url: "https://api.bamboohr.com/api/gateway.php" gcal: + currentIcon: "💥" eventCount: 10 refreshInterval: 300 secretFile: "~/.wtf/gcal/client_secret.json" @@ -12,8 +13,10 @@ wtf: refreshInterval: 8 repository: "/Users/chris/go/src/github.com/senorprogrammer/wtf" github: - refreshInterval: 900 + organization: "BetterOfficeApps" + refreshInterval: 300 repo: "core-api" + username: "senorprogrammer" jira: refreshInterval: 900 opsgenie: diff --git a/gcal/widget.go b/gcal/widget.go index 9d56dbfc..f518dc78 100644 --- a/gcal/widget.go +++ b/gcal/widget.go @@ -99,12 +99,11 @@ func (widget *Widget) dayDivider(event, prevEvent *calendar.Event) string { } func (widget *Widget) eventSummary(event *calendar.Event) string { - summary := event.Summary if widget.eventIsNow(event) { - summary = "🔥 " + summary + return fmt.Sprintf("%s %s", Config.UString("wtf.gcal.currentIcon", "🔸"), event.Summary) } - return summary + return event.Summary } func (widget *Widget) eventTimestamp(event *calendar.Event) string { diff --git a/git/widget.go b/git/widget.go index 90c70e36..2ec815d8 100644 --- a/git/widget.go +++ b/git/widget.go @@ -38,9 +38,9 @@ func NewWidget() *Widget { func (widget *Widget) Refresh() { data := Fetch() - str := fmt.Sprintf("[green]%s[white] [dodgerblue]%s[white]\n", data["repo"][0], data["branch"][0]) + title := fmt.Sprintf("[green]%s[white]\n", data["repo"][0]) - widget.View.SetTitle(fmt.Sprintf(" 🤞 %s ", str)) + widget.View.SetTitle(fmt.Sprintf(" 🤞 %s ", title)) widget.RefreshedAt = time.Now() widget.View.Clear() @@ -63,6 +63,9 @@ func (widget *Widget) addView() { func (widget *Widget) contentFrom(data map[string][]string) string { str := "\n" + str = str + " [red]Branch[white]\n" + str = str + fmt.Sprintf(" %s", data["branch"][0]) + str = str + "\n" str = str + widget.formatChanges(data["changes"]) str = str + "\n" str = str + widget.formatCommits(data["commits"]) diff --git a/github/client.go b/github/client.go new file mode 100644 index 00000000..8fa086cd --- /dev/null +++ b/github/client.go @@ -0,0 +1,63 @@ +package github + +import ( + "context" + "fmt" + "net/http" + "os" + + ghb "github.com/google/go-github/github" + "golang.org/x/oauth2" +) + +type Client struct { + apiKey string +} + +func NewClient() *Client { + client := Client{ + apiKey: os.Getenv("WTF_GITHUB_TOKEN"), + } + + return &client +} + +func (client *Client) PullRequests(orgName string, repoName string) []*ghb.PullRequest { + oauthClient := client.oauthClient() + github := ghb.NewClient(oauthClient) + + opts := &ghb.PullRequestListOptions{} + + prs, _, err := github.PullRequests.List(context.Background(), orgName, repoName, opts) + + if err != nil { + fmt.Printf("Problem in getting pull request information %v\n", err) + os.Exit(1) + } + + return prs +} + +func (client *Client) Repository(orgName string, repoName string) *ghb.Repository { + oauthClient := client.oauthClient() + github := ghb.NewClient(oauthClient) + + repo, _, err := github.Repositories.Get(context.Background(), orgName, repoName) + + if err != nil { + fmt.Printf("Problem in getting repository information %v\n", err) + os.Exit(1) + } + + return repo +} + +/* -------------------- Unexported Functions -------------------- */ + +func (client *Client) oauthClient() *http.Client { + tokenService := oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: client.apiKey}, + ) + + return oauth2.NewClient(context.Background(), tokenService) +} diff --git a/github/widget.go b/github/widget.go index e7626d25..c8b3b897 100644 --- a/github/widget.go +++ b/github/widget.go @@ -5,6 +5,7 @@ import ( "time" "github.com/gdamore/tcell" + ghb "github.com/google/go-github/github" "github.com/olebedev/config" "github.com/rivo/tview" "github.com/senorprogrammer/wtf/wtf" @@ -34,11 +35,25 @@ func NewWidget() *Widget { /* -------------------- Exported Functions -------------------- */ func (widget *Widget) Refresh() { - widget.View.SetTitle(fmt.Sprintf(" %s ", widget.Name)) + client := NewClient() + repo := client.Repository(Config.UString("wtf.github.organization"), Config.UString("wtf.github.repo")) + org := *repo.Organization + prs := client.PullRequests(Config.UString("wtf.github.organization"), Config.UString("wtf.github.repo")) + + title := fmt.Sprintf("[green]%s - %s[white]", *org.Login, *repo.Name) + + widget.View.SetTitle(fmt.Sprintf(" 🤘 %s ", title)) widget.RefreshedAt = time.Now() + str := "\n" + str = str + " [red]Open Review Requests[white]\n" + str = str + widget.prsForReview(prs) + str = str + "\n" + str = str + " [red]Open Pull Requests[white]\n" + str = str + widget.openPRs(prs) + widget.View.Clear() - fmt.Fprintf(widget.View, "%s", "github") + fmt.Fprintf(widget.View, str) } /* -------------------- Unexported Functions -------------------- */ @@ -50,6 +65,51 @@ func (widget *Widget) addView() { view.SetBorderColor(tcell.ColorGray) view.SetDynamicColors(true) view.SetTitle(widget.Name) + view.SetWrap(false) widget.View = view } + +func (widget *Widget) prsForReview(prs []*ghb.PullRequest) string { + if len(prs) > 0 { + str := "" + + for _, pr := range prs { + for _, reviewer := range pr.RequestedReviewers { + if *reviewer.Login == Config.UString("wtf.github.username") { + str = str + fmt.Sprintf(" [green]%d[white] %s\n", *pr.Number, *pr.Title) + } + } + } + + if str == "" { + str = " [grey]none[white]\n" + } + + return str + } + + return " [grey]none[white]\n" +} + +func (widget *Widget) openPRs(prs []*ghb.PullRequest) string { + if len(prs) > 0 { + str := "" + + for _, pr := range prs { + user := *pr.User + + if *user.Login == Config.UString("wtf.github.username") { + str = str + fmt.Sprintf(" [green]%d[white] %s\n", *pr.Number, *pr.Title) + } + } + + if str == "" { + str = " [grey]none[white]\n" + } + + return str + } + + return " [grey]none[white]\n" +} diff --git a/security/client.go b/security/client.go index 87e25278..582c9afe 100644 --- a/security/client.go +++ b/security/client.go @@ -5,10 +5,10 @@ import () func Fetch() map[string]string { data := make(map[string]string) - data["Firewall Enabled"] = FirewallState() - data["Firewall Stealth"] = FirewallStealthState() - data["Wifi Encryption"] = WifiEncryption() - data["Wifi Network"] = WifiName() + data["Enabled"] = FirewallState() + data["Stealth"] = FirewallStealthState() + data["Encryption"] = WifiEncryption() + data["Network"] = WifiName() return data } diff --git a/security/widget.go b/security/widget.go index 227c0dec..5a2c0715 100644 --- a/security/widget.go +++ b/security/widget.go @@ -2,7 +2,7 @@ package security import ( "fmt" - "sort" + //"sort" "time" "github.com/gdamore/tcell" @@ -53,24 +53,14 @@ func (widget *Widget) addView() { view.SetBorderColor(tcell.ColorGray) view.SetDynamicColors(true) view.SetTitle(widget.Name) + view.SetWrap(false) widget.View = view } func (widget *Widget) contentFrom(data map[string]string) string { - str := "\n" - - // Sort the map keys in alphabetical order - var keys []string - for key, _ := range data { - keys = append(keys, key) - } - sort.Strings(keys) - - for _, key := range keys { - val := data[key] - str = str + fmt.Sprintf(" %16s: %s\n", key, val) - } + str := fmt.Sprintf(" Firewall: %s Network: %s\n", data["Enabled"], data["Network"]) + str = str + fmt.Sprintf(" Stealth: %s Crypto: %s\n", data["Stealth"], data["Encryption"]) return str } diff --git a/weather/client.go b/weather/client.go index 467c9477..ecd2e980 100644 --- a/weather/client.go +++ b/weather/client.go @@ -1,6 +1,7 @@ package weather import ( + //"fmt" "os" owm "github.com/briandowns/openweathermap" diff --git a/weather/widget.go b/weather/widget.go index ff06bc60..aa09ab07 100644 --- a/weather/widget.go +++ b/weather/widget.go @@ -70,9 +70,16 @@ func (widget *Widget) contentFrom(data *owm.CurrentWeatherData) string { tempUnit := Config.UString("wtf.weather.tempUnit", "C") - str = str + fmt.Sprintf("%10s: %4.1f° %s\n", "Current", data.Main.Temp, tempUnit) - str = str + fmt.Sprintf("%10s: %4.1f° %s\n", "High", data.Main.TempMax, tempUnit) - str = str + fmt.Sprintf("%10s: %4.1f° %s\n", "Low", data.Main.TempMin, tempUnit) + str = str + fmt.Sprintf("%8s: %4.1f° %s\n", "High", data.Main.TempMax, tempUnit) + str = str + fmt.Sprintf("%8s: [yellow]%4.1f° %s[white]\n", "Current", data.Main.Temp, tempUnit) + str = str + fmt.Sprintf("%8s: %4.1f° %s\n", "Low", data.Main.TempMin, tempUnit) + + str = str + "\n" + str = str + fmt.Sprintf( + " Sunrise: %s Sunset: %s\n", + wtf.UnixTime(int64(data.Sys.Sunrise)).Format("15:04"), + wtf.UnixTime(int64(data.Sys.Sunset)).Format("15:04"), + ) return str } diff --git a/wtf.go b/wtf.go index bb8f120a..f55d3b84 100644 --- a/wtf.go +++ b/wtf.go @@ -83,19 +83,19 @@ func main() { weather.Refresh() grid := tview.NewGrid() - grid.SetRows(9, 9, 9, 9, 9, 6, 3) // How _high_ the row is, in terminal rows - grid.SetColumns(40, 40) // How _wide_ the column is, in terminal columns + grid.SetRows(10, 10, 10, 10, 10, 4) // How _high_ the row is, in terminal rows + grid.SetColumns(37, 37, 37, 37, 37) // How _wide_ the column is, in terminal columns grid.SetBorder(false) grid.AddItem(bamboo.View, 0, 0, 2, 1, 0, 0, false) grid.AddItem(cal.View, 2, 1, 4, 1, 0, 0, false) - grid.AddItem(git.View, 0, 2, 3, 1, 0, 0, false) - grid.AddItem(github.View, 3, 2, 3, 1, 0, 0, false) + grid.AddItem(git.View, 0, 2, 3, 3, 0, 0, false) + grid.AddItem(github.View, 3, 2, 2, 3, 0, 0, false) grid.AddItem(weather.View, 0, 1, 1, 1, 0, 0, false) - grid.AddItem(sec.View, 1, 1, 1, 1, 0, 0, false) - grid.AddItem(opsgenie.View, 2, 0, 3, 1, 0, 0, false) - grid.AddItem(jira.View, 5, 0, 1, 1, 0, 0, false) - grid.AddItem(stat.View, 6, 0, 3, 3, 0, 0, false) + grid.AddItem(sec.View, 5, 0, 1, 1, 0, 0, false) + grid.AddItem(opsgenie.View, 2, 0, 2, 1, 0, 0, false) + grid.AddItem(jira.View, 1, 1, 1, 1, 0, 0, false) + grid.AddItem(stat.View, 5, 2, 3, 3, 0, 0, false) app := tview.NewApplication() diff --git a/wtf/utils.go b/wtf/utils.go index f9d58cf2..fd4c6308 100644 --- a/wtf/utils.go +++ b/wtf/utils.go @@ -35,3 +35,7 @@ func Today() string { localNow := time.Now().Local() return localNow.Format("2006-01-02") } + +func UnixTime(unix int64) time.Time { + return time.Unix(unix, 0) +}