From 53e5e5fbbdec681753ea0824356c558cd0b38033 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Sat, 26 May 2018 14:31:09 -0700 Subject: [PATCH] [WTF-59] Somewhat better seperation of concerns in the Github code --- github/display.go | 54 +++-------------------------- github/github_repo.go | 79 ++++++++++++++++++++++++++++++++++--------- github/widget.go | 25 ++++++++------ 3 files changed, 82 insertions(+), 76 deletions(-) diff --git a/github/display.go b/github/display.go index 6215b75b..a4bf1499 100644 --- a/github/display.go +++ b/github/display.go @@ -9,7 +9,7 @@ import ( func (widget *Widget) display() { widget.View.Clear() - repo := widget.currentData() + repo := widget.currentGithubRepo() if repo == nil { fmt.Fprintf(widget.View, "%s", " Github repo data is unavailable (1)") return @@ -17,60 +17,16 @@ func (widget *Widget) display() { widget.View.SetTitle(fmt.Sprintf(" Github: %s ", widget.title(repo))) - str := wtf.SigilStr(len(widget.Data), widget.Idx, widget.View) + "\n" + str := wtf.SigilStr(len(widget.GithubRepos), widget.Idx, widget.View) + "\n" str = str + " [red]Open Review Requests[white]\n" - str = str + widget.prsForReview(repo, Config.UString("wtf.mods.github.username")) + str = str + repo.pullRequetsForMeToReview(Config.UString("wtf.mods.github.username")) str = str + "\n" - str = str + " [red]Open Pull Requests[white]\n" - str = str + widget.openPRs(repo, Config.UString("wtf.mods.github.username")) + str = str + " [red]My Pull Requests[white]\n" + str = str + repo.myPullRequests(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]%4d[white] %s\n", *pr.Number, *pr.Title) - } - } - - if str == "" { - return " [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 == "" { - return " [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 ff17f6e7..f8768dbd 100644 --- a/github/github_repo.go +++ b/github/github_repo.go @@ -2,6 +2,7 @@ package github import ( "context" + "fmt" "net/http" "os" @@ -24,26 +25,11 @@ func NewGithubRepo(name, owner string) *GithubRepo { Owner: owner, } - repo.PullRequests, _ = repo.pullRequests() + repo.PullRequests, _ = repo.allPullRequests() 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) @@ -66,3 +52,64 @@ func (repo *GithubRepo) oauthClient() *http.Client { return oauth2.NewClient(context.Background(), tokenService) } + +// TODO: This should return a slice of pull requests and let Display handle the output +func (repo *GithubRepo) myPullRequests(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]%4d[white] %s\n", *pr.Number, *pr.Title) + } + } + + if str == "" { + return " [grey]none[white]\n" + } + + return str +} + +// TODO: This should return a slice of pull requests and let Display handle the output +func (repo *GithubRepo) pullRequetsForMeToReview(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 == "" { + return " [grey]none[white]\n" + } + + return str +} + +func (repo *GithubRepo) allPullRequests() ([]*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 +} diff --git a/github/widget.go b/github/widget.go index 2ff10861..535b6c5e 100644 --- a/github/widget.go +++ b/github/widget.go @@ -25,10 +25,11 @@ const HelpText = ` type Widget struct { wtf.TextWidget - app *tview.Application - Data []*GithubRepo - Idx int - pages *tview.Pages + app *tview.Application + pages *tview.Pages + + GithubRepos []*GithubRepo + Idx int } func NewWidget(app *tview.Application, pages *tview.Pages) *Widget { @@ -40,6 +41,8 @@ func NewWidget(app *tview.Application, pages *tview.Pages) *Widget { pages: pages, } + widget.GithubRepos = widget.buildRepoCollection(Config.UMap("wtf.mods.github.repositories")) + widget.View.SetInputCapture(widget.keyboardIntercept) return &widget @@ -52,7 +55,7 @@ func (widget *Widget) Refresh() { return } - widget.Data = widget.buildRepoCollection(Config.UMap("wtf.mods.github.repositories")) + // TODO: Tell all the Github repos to refresh their data widget.UpdateRefreshedAt() widget.display() @@ -60,7 +63,7 @@ func (widget *Widget) Refresh() { func (widget *Widget) Next() { widget.Idx = widget.Idx + 1 - if widget.Idx == len(widget.Data) { + if widget.Idx == len(widget.GithubRepos) { widget.Idx = 0 } @@ -70,7 +73,7 @@ func (widget *Widget) Next() { func (widget *Widget) Prev() { widget.Idx = widget.Idx - 1 if widget.Idx < 0 { - widget.Idx = len(widget.Data) - 1 + widget.Idx = len(widget.GithubRepos) - 1 } widget.display() @@ -89,16 +92,16 @@ func (widget *Widget) buildRepoCollection(repoData map[string]interface{}) []*Gi return githubRepos } -func (widget *Widget) currentData() *GithubRepo { - if len(widget.Data) == 0 { +func (widget *Widget) currentGithubRepo() *GithubRepo { + if len(widget.GithubRepos) == 0 { return nil } - if widget.Idx < 0 || widget.Idx >= len(widget.Data) { + if widget.Idx < 0 || widget.Idx >= len(widget.GithubRepos) { return nil } - return widget.Data[widget.Idx] + return widget.GithubRepos[widget.Idx] } func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {