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

Displays Github review requests and PRs

This commit is contained in:
Chris Cummer
2018-04-05 10:05:05 -07:00
committed by Chris Cummer
parent 32b395a19e
commit fb637700b1
12 changed files with 168 additions and 38 deletions

63
github/client.go Normal file
View File

@@ -0,0 +1,63 @@
package github
import (
"context"
"fmt"
"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(orgName string, repoName string) []*ghb.PullRequest {
oauthClient := client.oauthClient()
github := ghb.NewClient(oauthClient)
opts := &ghb.PullRequestListOptions{}
prs, _, err := github.PullRequests.List(context.Background(), orgName, repoName, opts)
if err != nil {
fmt.Printf("Problem in getting pull request information %v\n", err)
os.Exit(1)
}
return prs
}
func (client *Client) Repository(orgName string, repoName string) *ghb.Repository {
oauthClient := client.oauthClient()
github := ghb.NewClient(oauthClient)
repo, _, err := github.Repositories.Get(context.Background(), orgName, repoName)
if err != nil {
fmt.Printf("Problem in getting repository information %v\n", err)
os.Exit(1)
}
return repo
}
/* -------------------- 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,6 +5,7 @@ import (
"time"
"github.com/gdamore/tcell"
ghb "github.com/google/go-github/github"
"github.com/olebedev/config"
"github.com/rivo/tview"
"github.com/senorprogrammer/wtf/wtf"
@@ -34,11 +35,25 @@ func NewWidget() *Widget {
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
widget.View.SetTitle(fmt.Sprintf(" %s ", widget.Name))
client := NewClient()
repo := client.Repository(Config.UString("wtf.github.organization"), Config.UString("wtf.github.repo"))
org := *repo.Organization
prs := client.PullRequests(Config.UString("wtf.github.organization"), Config.UString("wtf.github.repo"))
title := fmt.Sprintf("[green]%s - %s[white]", *org.Login, *repo.Name)
widget.View.SetTitle(fmt.Sprintf(" 🤘 %s ", title))
widget.RefreshedAt = time.Now()
str := "\n"
str = str + " [red]Open Review Requests[white]\n"
str = str + widget.prsForReview(prs)
str = str + "\n"
str = str + " [red]Open Pull Requests[white]\n"
str = str + widget.openPRs(prs)
widget.View.Clear()
fmt.Fprintf(widget.View, "%s", "github")
fmt.Fprintf(widget.View, str)
}
/* -------------------- Unexported Functions -------------------- */
@@ -50,6 +65,51 @@ func (widget *Widget) addView() {
view.SetBorderColor(tcell.ColorGray)
view.SetDynamicColors(true)
view.SetTitle(widget.Name)
view.SetWrap(false)
widget.View = view
}
func (widget *Widget) prsForReview(prs []*ghb.PullRequest) string {
if len(prs) > 0 {
str := ""
for _, pr := range prs {
for _, reviewer := range pr.RequestedReviewers {
if *reviewer.Login == Config.UString("wtf.github.username") {
str = str + fmt.Sprintf(" [green]%d[white] %s\n", *pr.Number, *pr.Title)
}
}
}
if str == "" {
str = " [grey]none[white]\n"
}
return str
}
return " [grey]none[white]\n"
}
func (widget *Widget) openPRs(prs []*ghb.PullRequest) string {
if len(prs) > 0 {
str := ""
for _, pr := range prs {
user := *pr.User
if *user.Login == Config.UString("wtf.github.username") {
str = str + fmt.Sprintf(" [green]%d[white] %s\n", *pr.Number, *pr.Title)
}
}
if str == "" {
str = " [grey]none[white]\n"
}
return str
}
return " [grey]none[white]\n"
}