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

Support for multiple Github repos

This commit is contained in:
Chris Cummer 2018-04-19 00:23:34 -07:00
parent e90b62e2e5
commit 66f11e8b8b
5 changed files with 170 additions and 112 deletions

View File

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

View File

@ -5,18 +5,70 @@ import (
) )
func (widget *Widget) display() { 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.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 := " [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 + "\n"
str = str + " [red]Open Pull Requests[white]\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) 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)
}

View File

@ -1,17 +1,68 @@
package github package github
import () import (
"context"
"net/http"
"os"
ghb "github.com/google/go-github/github"
"golang.org/x/oauth2"
)
type GithubRepo struct { type GithubRepo struct {
apiKey string
Name string Name string
Owner string Owner string
PullRequests []*ghb.PullRequest
} }
func NewGithubRepo(owner string, name string) *GithubRepo { func NewGithubRepo(name, owner string) *GithubRepo {
repo := GithubRepo{ repo := GithubRepo{
apiKey: os.Getenv("WTF_GITHUB_TOKEN"),
Name: name, Name: name,
Owner: owner, Owner: owner,
} }
repo.PullRequests, _ = repo.pullRequests()
return &repo 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)
}

View File

@ -1,10 +1,9 @@
package github package github
import ( import (
"fmt"
"time" "time"
ghb "github.com/google/go-github/github" "github.com/gdamore/tcell"
"github.com/olebedev/config" "github.com/olebedev/config"
"github.com/senorprogrammer/wtf/wtf" "github.com/senorprogrammer/wtf/wtf"
) )
@ -14,13 +13,19 @@ var Config *config.Config
type Widget struct { type Widget struct {
wtf.TextWidget wtf.TextWidget
Data []*GithubRepo
Idx int
} }
func NewWidget() *Widget { func NewWidget() *Widget {
widget := Widget{ widget := Widget{
TextWidget: wtf.NewTextWidget(" Github ", "github"), TextWidget: wtf.NewTextWidget(" Github ", "github"),
Idx: 0,
} }
widget.View.SetInputCapture(widget.keyboardIntercept)
return &widget return &widget
} }
@ -31,56 +36,66 @@ func (widget *Widget) Refresh() {
return return
} }
widget.Data = widget.buildRepoCollection(Config.UMap("wtf.mods.github.repositories"))
widget.display() widget.display()
widget.RefreshedAt = time.Now() 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 -------------------- */ /* -------------------- Unexported Functions -------------------- */
func (widget *Widget) prsForReview(prs []*ghb.PullRequest, username string) string { func (widget *Widget) buildRepoCollection(repoData map[string]interface{}) []*GithubRepo {
if len(prs) > 0 { githubColl := []*GithubRepo{}
str := ""
for _, pr := range prs { for name, owner := range repoData {
for _, reviewer := range pr.RequestedReviewers { repo := NewGithubRepo(name, owner.(string))
if *reviewer.Login == username { githubColl = append(githubColl, repo)
str = str + fmt.Sprintf(" [green]%d[white] %s\n", *pr.Number, *pr.Title)
}
}
} }
if str == "" { return githubColl
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
} }
func (widget *Widget) openPRs(prs []*ghb.PullRequest, username string) string { return widget.Data[widget.Idx]
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 == "" { func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
str = " [grey]none[white]\n" switch event.Key() {
case tcell.KeyLeft:
widget.Prev()
return nil
case tcell.KeyRight:
widget.Next()
return nil
default:
return event
} }
return str return event
}
return " [grey]none[white]\n"
}
func (widget *Widget) title() string {
return fmt.Sprintf("[green]%s - %s[white]", Config.UString("wtf.mods.github.owner"), Config.UString("wtf.mods.github.repo"))
} }

View File

@ -68,7 +68,7 @@ func (widget *Widget) contentFrom(deploys []nr.ApplicationDeployment) string {
if (deploy.Revision != "") && wtf.Exclude(revisions, deploy.Revision) { if (deploy.Revision != "") && wtf.Exclude(revisions, deploy.Revision) {
lineColor := "white" lineColor := "white"
if wtf.IsToday(deploy.Timestamp) { if wtf.IsToday(deploy.Timestamp) {
lineColor = "cornflowerblue" lineColor = "lightblue"
} }
str = str + fmt.Sprintf( str = str + fmt.Sprintf(