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

[WTF-59] Somewhat better seperation of concerns in the Github code

This commit is contained in:
Chris Cummer 2018-05-26 14:31:09 -07:00
parent c20c8fdb9d
commit 53e5e5fbbd
3 changed files with 82 additions and 76 deletions

View File

@ -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)
}

View File

@ -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
}

View File

@ -26,9 +26,10 @@ type Widget struct {
wtf.TextWidget
app *tview.Application
Data []*GithubRepo
Idx int
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 {