diff --git a/github/display.go b/github/display.go index a4bf1499..a3732369 100644 --- a/github/display.go +++ b/github/display.go @@ -18,15 +18,59 @@ func (widget *Widget) display() { widget.View.SetTitle(fmt.Sprintf(" Github: %s ", widget.title(repo))) str := wtf.SigilStr(len(widget.GithubRepos), widget.Idx, widget.View) + "\n" + str = str + " [red]Stats[white]\n" + str = str + widget.displayStats(repo) + str = str + "\n" str = str + " [red]Open Review Requests[white]\n" - str = str + repo.pullRequetsForMeToReview(Config.UString("wtf.mods.github.username")) + str = str + widget.displayMyReviewRequests(repo, Config.UString("wtf.mods.github.username")) str = str + "\n" str = str + " [red]My Pull Requests[white]\n" - str = str + repo.myPullRequests(Config.UString("wtf.mods.github.username")) + str = str + widget.displayMyPullRequests(repo, Config.UString("wtf.mods.github.username")) fmt.Fprintf(widget.View, str) } +func (widget *Widget) displayMyPullRequests(repo *GithubRepo, username string) string { + prs := repo.myPullRequests(username) + + if len(prs) == 0 { + return " [grey]none[white]\n" + } + + str := "" + for _, pr := range prs { + str = str + fmt.Sprintf(" [green]%4d[white] %s\n", *pr.Number, *pr.Title) + } + + return str +} + +func (widget *Widget) displayMyReviewRequests(repo *GithubRepo, username string) string { + prs := repo.myReviewRequests(username) + + if len(prs) == 0 { + return " [grey]none[white]\n" + } + + str := "" + for _, pr := range prs { + str = str + fmt.Sprintf(" [green]%4d[white] %s\n", *pr.Number, *pr.Title) + } + + return str +} + +func (widget *Widget) displayStats(repo *GithubRepo) string { + str := fmt.Sprintf( + " PRs: %d Issues: %d Stars: %d\n", + repo.PullRequestCount(), + repo.IssueCount(), + repo.StarCount(), + ) + + 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 f8768dbd..df3644d2 100644 --- a/github/github_repo.go +++ b/github/github_repo.go @@ -2,7 +2,6 @@ package github import ( "context" - "fmt" "net/http" "os" @@ -16,6 +15,7 @@ type GithubRepo struct { Name string Owner string PullRequests []*ghb.PullRequest + RemoteRepo *ghb.Repository } func NewGithubRepo(name, owner string) *GithubRepo { @@ -25,22 +25,27 @@ func NewGithubRepo(name, owner string) *GithubRepo { Owner: owner, } - repo.PullRequests, _ = repo.allPullRequests() - return &repo } -func (repo *GithubRepo) Repository() (*ghb.Repository, error) { - oauthClient := repo.oauthClient() - github := ghb.NewClient(oauthClient) +// Refresh reloads the github data via the Github API +func (repo *GithubRepo) Refresh() { + repo.PullRequests, _ = repo.loadPullRequests() + repo.RemoteRepo, _ = repo.loadRemoteRepository() +} - repository, _, err := github.Repositories.Get(context.Background(), repo.Owner, repo.Name) +/* -------------------- Counts -------------------- */ - if err != nil { - return nil, err - } +func (repo *GithubRepo) IssueCount() int { + return *repo.RemoteRepo.OpenIssuesCount +} - return repository, nil +func (repo *GithubRepo) PullRequestCount() int { + return len(repo.PullRequests) +} + +func (repo *GithubRepo) StarCount() int { + return *repo.RemoteRepo.StargazersCount } /* -------------------- Unexported Functions -------------------- */ @@ -53,53 +58,38 @@ 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 := "" +// myPullRequests returns a list of pull requests created by username on this repo +func (repo *GithubRepo) myPullRequests(username string) []*ghb.PullRequest { + prs := []*ghb.PullRequest{} 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) + prs = append(prs, pr) } } - if str == "" { - return " [grey]none[white]\n" - } - - return str + return prs } -// 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 := "" +// myReviewRequests returns a list of pull requests for which username has been +// requested to do a code review +func (repo *GithubRepo) myReviewRequests(username string) []*ghb.PullRequest { + prs := []*ghb.PullRequest{} 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) + prs = append(prs, pr) } } } - if str == "" { - return " [grey]none[white]\n" - } - - return str + return prs } -func (repo *GithubRepo) allPullRequests() ([]*ghb.PullRequest, error) { +func (repo *GithubRepo) loadPullRequests() ([]*ghb.PullRequest, error) { oauthClient := repo.oauthClient() github := ghb.NewClient(oauthClient) @@ -113,3 +103,16 @@ func (repo *GithubRepo) allPullRequests() ([]*ghb.PullRequest, error) { return prs, nil } + +func (repo *GithubRepo) loadRemoteRepository() (*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 +} diff --git a/github/widget.go b/github/widget.go index 535b6c5e..ad186310 100644 --- a/github/widget.go +++ b/github/widget.go @@ -25,8 +25,8 @@ const HelpText = ` type Widget struct { wtf.TextWidget - app *tview.Application - pages *tview.Pages + app *tview.Application + pages *tview.Pages GithubRepos []*GithubRepo Idx int @@ -55,7 +55,9 @@ func (widget *Widget) Refresh() { return } - // TODO: Tell all the Github repos to refresh their data + for _, repo := range widget.GithubRepos { + repo.Refresh() + } widget.UpdateRefreshedAt() widget.display()