diff --git a/github/client.go b/github/client.go deleted file mode 100644 index 33fd5adc..00000000 --- a/github/client.go +++ /dev/null @@ -1,60 +0,0 @@ -package github - -import ( - "context" - "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(owner string, repoName string) ([]*ghb.PullRequest, error) { - oauthClient := client.oauthClient() - github := ghb.NewClient(oauthClient) - - opts := &ghb.PullRequestListOptions{} - - prs, _, err := github.PullRequests.List(context.Background(), owner, repoName, opts) - - if err != nil { - return nil, err - } - - return prs, nil -} - -func (client *Client) Repository(owner string, repoName string) (*ghb.Repository, error) { - oauthClient := client.oauthClient() - github := ghb.NewClient(oauthClient) - - repo, _, err := github.Repositories.Get(context.Background(), owner, repoName) - - if err != nil { - return nil, err - } - - return repo, nil -} - -/* -------------------- 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/display.go b/github/display.go index 0b0a24a0..71304626 100644 --- a/github/display.go +++ b/github/display.go @@ -5,18 +5,70 @@ import ( ) func (widget *Widget) display() { - client := NewClient() - prs, _ := client.PullRequests(Config.UString("wtf.mods.github.owner"), Config.UString("wtf.mods.github.repo")) - widget.View.Clear() - widget.View.SetTitle(fmt.Sprintf(" Github: %s ", widget.title())) + repo := widget.currentData() + if repo == nil { + fmt.Fprintf(widget.View, "%s", " Github repo data is unavailable (1)") + return + } + + widget.View.SetTitle(fmt.Sprintf(" Github: %s ", widget.title(repo))) str := " [red]Open Review Requests[white]\n" - str = str + widget.prsForReview(prs, Config.UString("wtf.mods.github.username")) + str = str + widget.prsForReview(repo, Config.UString("wtf.mods.github.username")) str = str + "\n" str = str + " [red]Open Pull Requests[white]\n" - str = str + widget.openPRs(prs, Config.UString("wtf.mods.github.username")) + str = str + widget.openPRs(repo, Config.UString("wtf.mods.github.username")) fmt.Fprintf(widget.View, str) } + +func (widget *Widget) openPRs(repo *GithubRepo, username string) string { + if len(repo.PullRequests) == 0 { + return " [grey]none[white]\n" + } + + str := "" + + for _, pr := range repo.PullRequests { + user := *pr.User + + if *user.Login == username { + str = str + fmt.Sprintf(" [green]%d[white] %s\n", *pr.Number, *pr.Title) + } + } + + if str == "" { + str = " [grey]none[white]\n" + } + + return str +} + +func (widget *Widget) prsForReview(repo *GithubRepo, username string) string { + if len(repo.PullRequests) > 0 { + return " [grey]none[white]\n" + } + + str := "" + + for _, pr := range repo.PullRequests { + for _, reviewer := range pr.RequestedReviewers { + if *reviewer.Login == username { + str = str + fmt.Sprintf(" [green]%d[white] %s\n", *pr.Number, *pr.Title) + } + } + } + + if str == "" { + str = " [grey]none[white]\n" + } + + return str +} + + +func (widget *Widget) title(repo *GithubRepo) string { + return fmt.Sprintf("[green]%s - %s[white]", repo.Owner, repo.Name) +} diff --git a/github/github_repo.go b/github/github_repo.go index f9e26963..ff17f6e7 100644 --- a/github/github_repo.go +++ b/github/github_repo.go @@ -1,17 +1,68 @@ package github -import () +import ( + "context" + "net/http" + "os" + + ghb "github.com/google/go-github/github" + "golang.org/x/oauth2" +) type GithubRepo struct { - Name string - Owner string + apiKey string + + Name string + Owner string + PullRequests []*ghb.PullRequest } -func NewGithubRepo(owner string, name string) *GithubRepo { +func NewGithubRepo(name, owner string) *GithubRepo { repo := GithubRepo{ - Name: name, - Owner: owner, + apiKey: os.Getenv("WTF_GITHUB_TOKEN"), + Name: name, + Owner: owner, } + repo.PullRequests, _ = repo.pullRequests() + return &repo } + +func (repo *GithubRepo) pullRequests() ([]*ghb.PullRequest, error) { + oauthClient := repo.oauthClient() + github := ghb.NewClient(oauthClient) + + opts := &ghb.PullRequestListOptions{} + + prs, _, err := github.PullRequests.List(context.Background(), repo.Owner, repo.Name, opts) + + if err != nil { + return nil, err + } + + return prs, nil +} + +func (repo *GithubRepo) Repository() (*ghb.Repository, error) { + oauthClient := repo.oauthClient() + github := ghb.NewClient(oauthClient) + + repository, _, err := github.Repositories.Get(context.Background(), repo.Owner, repo.Name) + + if err != nil { + return nil, err + } + + return repository, nil +} + +/* -------------------- Unexported Functions -------------------- */ + +func (repo *GithubRepo) oauthClient() *http.Client { + tokenService := oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: repo.apiKey}, + ) + + return oauth2.NewClient(context.Background(), tokenService) +} diff --git a/github/widget.go b/github/widget.go index cae355e2..1f59ee28 100644 --- a/github/widget.go +++ b/github/widget.go @@ -1,10 +1,9 @@ package github import ( - "fmt" "time" - ghb "github.com/google/go-github/github" + "github.com/gdamore/tcell" "github.com/olebedev/config" "github.com/senorprogrammer/wtf/wtf" ) @@ -14,13 +13,19 @@ var Config *config.Config type Widget struct { wtf.TextWidget + + Data []*GithubRepo + Idx int } func NewWidget() *Widget { widget := Widget{ TextWidget: wtf.NewTextWidget(" Github ", "github"), + Idx: 0, } + widget.View.SetInputCapture(widget.keyboardIntercept) + return &widget } @@ -31,56 +36,66 @@ func (widget *Widget) Refresh() { return } + widget.Data = widget.buildRepoCollection(Config.UMap("wtf.mods.github.repositories")) + widget.display() widget.RefreshedAt = time.Now() } +func (widget *Widget) Next() { + widget.Idx = widget.Idx + 1 + if widget.Idx == len(widget.Data) { + widget.Idx = 0 + } + + widget.display() +} + +func (widget *Widget) Prev() { + widget.Idx = widget.Idx - 1 + if widget.Idx < 0 { + widget.Idx = len(widget.Data) - 1 + } + + widget.display() +} + /* -------------------- Unexported Functions -------------------- */ -func (widget *Widget) prsForReview(prs []*ghb.PullRequest, username string) string { - if len(prs) > 0 { - str := "" +func (widget *Widget) buildRepoCollection(repoData map[string]interface{}) []*GithubRepo { + githubColl := []*GithubRepo{} - for _, pr := range prs { - for _, reviewer := range pr.RequestedReviewers { - if *reviewer.Login == username { - str = str + fmt.Sprintf(" [green]%d[white] %s\n", *pr.Number, *pr.Title) - } - } - } - - if str == "" { - str = " [grey]none[white]\n" - } - - return str + for name, owner := range repoData { + repo := NewGithubRepo(name, owner.(string)) + githubColl = append(githubColl, repo) } - return " [grey]none[white]\n" + return githubColl } -func (widget *Widget) openPRs(prs []*ghb.PullRequest, username string) string { - if len(prs) > 0 { - str := "" - - for _, pr := range prs { - user := *pr.User - - if *user.Login == username { - str = str + fmt.Sprintf(" [green]%d[white] %s\n", *pr.Number, *pr.Title) - } - } - - if str == "" { - str = " [grey]none[white]\n" - } - - return str +func (widget *Widget) currentData() *GithubRepo { + if len(widget.Data) == 0 { + return nil } - return " [grey]none[white]\n" + if widget.Idx < 0 || widget.Idx >= len(widget.Data) { + return nil + } + + return widget.Data[widget.Idx] } -func (widget *Widget) title() string { - return fmt.Sprintf("[green]%s - %s[white]", Config.UString("wtf.mods.github.owner"), Config.UString("wtf.mods.github.repo")) +func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey { + switch event.Key() { + case tcell.KeyLeft: + widget.Prev() + return nil + case tcell.KeyRight: + widget.Next() + return nil + default: + return event + } + + return event } diff --git a/newrelic/widget.go b/newrelic/widget.go index 4b8a06f2..06929982 100644 --- a/newrelic/widget.go +++ b/newrelic/widget.go @@ -68,7 +68,7 @@ func (widget *Widget) contentFrom(deploys []nr.ApplicationDeployment) string { if (deploy.Revision != "") && wtf.Exclude(revisions, deploy.Revision) { lineColor := "white" if wtf.IsToday(deploy.Timestamp) { - lineColor = "cornflowerblue" + lineColor = "lightblue" } str = str + fmt.Sprintf(