1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

[WTF-59] Closes #59. Github widget now displays some repo stats

This commit is contained in:
Chris Cummer 2018-05-26 15:12:26 -07:00
parent 53e5e5fbbd
commit 374173ebea
3 changed files with 92 additions and 43 deletions

View File

@ -18,15 +18,59 @@ func (widget *Widget) display() {
widget.View.SetTitle(fmt.Sprintf(" Github: %s ", widget.title(repo))) widget.View.SetTitle(fmt.Sprintf(" Github: %s ", widget.title(repo)))
str := wtf.SigilStr(len(widget.GithubRepos), widget.Idx, widget.View) + "\n" 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 + " [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 + "\n"
str = str + " [red]My Pull Requests[white]\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) 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 { func (widget *Widget) title(repo *GithubRepo) string {
return fmt.Sprintf("[green]%s - %s[white]", repo.Owner, repo.Name) return fmt.Sprintf("[green]%s - %s[white]", repo.Owner, repo.Name)
} }

View File

@ -2,7 +2,6 @@ package github
import ( import (
"context" "context"
"fmt"
"net/http" "net/http"
"os" "os"
@ -16,6 +15,7 @@ type GithubRepo struct {
Name string Name string
Owner string Owner string
PullRequests []*ghb.PullRequest PullRequests []*ghb.PullRequest
RemoteRepo *ghb.Repository
} }
func NewGithubRepo(name, owner string) *GithubRepo { func NewGithubRepo(name, owner string) *GithubRepo {
@ -25,22 +25,27 @@ func NewGithubRepo(name, owner string) *GithubRepo {
Owner: owner, Owner: owner,
} }
repo.PullRequests, _ = repo.allPullRequests()
return &repo return &repo
} }
func (repo *GithubRepo) Repository() (*ghb.Repository, error) { // Refresh reloads the github data via the Github API
oauthClient := repo.oauthClient() func (repo *GithubRepo) Refresh() {
github := ghb.NewClient(oauthClient) repo.PullRequests, _ = repo.loadPullRequests()
repo.RemoteRepo, _ = repo.loadRemoteRepository()
repository, _, err := github.Repositories.Get(context.Background(), repo.Owner, repo.Name)
if err != nil {
return nil, err
} }
return repository, nil /* -------------------- Counts -------------------- */
func (repo *GithubRepo) IssueCount() int {
return *repo.RemoteRepo.OpenIssuesCount
}
func (repo *GithubRepo) PullRequestCount() int {
return len(repo.PullRequests)
}
func (repo *GithubRepo) StarCount() int {
return *repo.RemoteRepo.StargazersCount
} }
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */
@ -53,53 +58,38 @@ func (repo *GithubRepo) oauthClient() *http.Client {
return oauth2.NewClient(context.Background(), tokenService) return oauth2.NewClient(context.Background(), tokenService)
} }
// TODO: This should return a slice of pull requests and let Display handle the output // myPullRequests returns a list of pull requests created by username on this repo
func (repo *GithubRepo) myPullRequests(username string) string { func (repo *GithubRepo) myPullRequests(username string) []*ghb.PullRequest {
if len(repo.PullRequests) == 0 { prs := []*ghb.PullRequest{}
return " [grey]none[white]\n"
}
str := ""
for _, pr := range repo.PullRequests { for _, pr := range repo.PullRequests {
user := *pr.User user := *pr.User
if *user.Login == username { if *user.Login == username {
str = str + fmt.Sprintf(" [green]%4d[white] %s\n", *pr.Number, *pr.Title) prs = append(prs, pr)
} }
} }
if str == "" { return prs
return " [grey]none[white]\n"
} }
return 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 {
// TODO: This should return a slice of pull requests and let Display handle the output prs := []*ghb.PullRequest{}
func (repo *GithubRepo) pullRequetsForMeToReview(username string) string {
if len(repo.PullRequests) == 0 {
return " [grey]none[white]\n"
}
str := ""
for _, pr := range repo.PullRequests { for _, pr := range repo.PullRequests {
for _, reviewer := range pr.RequestedReviewers { for _, reviewer := range pr.RequestedReviewers {
if *reviewer.Login == username { if *reviewer.Login == username {
str = str + fmt.Sprintf(" [green]%d[white] %s\n", *pr.Number, *pr.Title) prs = append(prs, pr)
} }
} }
} }
if str == "" { return prs
return " [grey]none[white]\n"
} }
return str func (repo *GithubRepo) loadPullRequests() ([]*ghb.PullRequest, error) {
}
func (repo *GithubRepo) allPullRequests() ([]*ghb.PullRequest, error) {
oauthClient := repo.oauthClient() oauthClient := repo.oauthClient()
github := ghb.NewClient(oauthClient) github := ghb.NewClient(oauthClient)
@ -113,3 +103,16 @@ func (repo *GithubRepo) allPullRequests() ([]*ghb.PullRequest, error) {
return prs, nil 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
}

View File

@ -55,7 +55,9 @@ func (widget *Widget) Refresh() {
return return
} }
// TODO: Tell all the Github repos to refresh their data for _, repo := range widget.GithubRepos {
repo.Refresh()
}
widget.UpdateRefreshedAt() widget.UpdateRefreshedAt()
widget.display() widget.display()