mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
96
modules/github/display.go
Normal file
96
modules/github/display.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package github
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/google/go-github/github"
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
|
||||
func (widget *Widget) display() {
|
||||
repo := widget.currentGithubRepo()
|
||||
if repo == nil {
|
||||
widget.View.SetText(" GitHub repo data is unavailable ")
|
||||
return
|
||||
}
|
||||
|
||||
widget.View.SetTitle(widget.ContextualTitle(fmt.Sprintf("%s - %s", widget.Name, 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 + widget.displayMyReviewRequests(repo, wtf.Config.UString("wtf.mods.github.username"))
|
||||
str = str + "\n"
|
||||
str = str + " [red]My Pull Requests[white]\n"
|
||||
str = str + widget.displayMyPullRequests(repo, wtf.Config.UString("wtf.mods.github.username"))
|
||||
|
||||
widget.View.SetText(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(" %s[green]%4d[white] %s\n", mergeString(pr), *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)
|
||||
}
|
||||
|
||||
func showStatus() bool {
|
||||
return wtf.Config.UBool("wtf.mods.github.enableStatus", false)
|
||||
}
|
||||
|
||||
var mergeIcons = map[string]string{
|
||||
"dirty": "[red]![white] ",
|
||||
"clean": "[green]✔[white] ",
|
||||
"unstable": "[red]✖[white] ",
|
||||
"blocked": "[red]✖[white] ",
|
||||
}
|
||||
|
||||
func mergeString(pr *github.PullRequest) string {
|
||||
if !showStatus() {
|
||||
return ""
|
||||
}
|
||||
if str, ok := mergeIcons[pr.GetMergeableState()]; ok {
|
||||
return str
|
||||
}
|
||||
return "? "
|
||||
}
|
||||
203
modules/github/github_repo.go
Normal file
203
modules/github/github_repo.go
Normal file
@@ -0,0 +1,203 @@
|
||||
package github
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
ghb "github.com/google/go-github/github"
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
type GithubRepo struct {
|
||||
apiKey string
|
||||
baseURL string
|
||||
uploadURL string
|
||||
|
||||
Name string
|
||||
Owner string
|
||||
PullRequests []*ghb.PullRequest
|
||||
RemoteRepo *ghb.Repository
|
||||
}
|
||||
|
||||
func NewGithubRepo(name, owner string) *GithubRepo {
|
||||
repo := GithubRepo{
|
||||
Name: name,
|
||||
Owner: owner,
|
||||
}
|
||||
|
||||
repo.loadAPICredentials()
|
||||
|
||||
return &repo
|
||||
}
|
||||
|
||||
func (repo *GithubRepo) Open() {
|
||||
wtf.OpenFile(*repo.RemoteRepo.HTMLURL)
|
||||
}
|
||||
|
||||
// Refresh reloads the github data via the Github API
|
||||
func (repo *GithubRepo) Refresh() {
|
||||
repo.PullRequests, _ = repo.loadPullRequests()
|
||||
repo.RemoteRepo, _ = repo.loadRemoteRepository()
|
||||
}
|
||||
|
||||
/* -------------------- Counts -------------------- */
|
||||
|
||||
func (repo *GithubRepo) IssueCount() int {
|
||||
if repo.RemoteRepo == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
return *repo.RemoteRepo.OpenIssuesCount
|
||||
}
|
||||
|
||||
func (repo *GithubRepo) PullRequestCount() int {
|
||||
return len(repo.PullRequests)
|
||||
}
|
||||
|
||||
func (repo *GithubRepo) StarCount() int {
|
||||
if repo.RemoteRepo == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
return *repo.RemoteRepo.StargazersCount
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func (repo *GithubRepo) isGitHubEnterprise() bool {
|
||||
if len(repo.baseURL) > 0 {
|
||||
if len(repo.uploadURL) == 0 {
|
||||
repo.uploadURL = repo.baseURL
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (repo *GithubRepo) oauthClient() *http.Client {
|
||||
tokenService := oauth2.StaticTokenSource(
|
||||
&oauth2.Token{AccessToken: repo.apiKey},
|
||||
)
|
||||
|
||||
return oauth2.NewClient(context.Background(), tokenService)
|
||||
}
|
||||
|
||||
func (repo *GithubRepo) githubClient() (*ghb.Client, error) {
|
||||
oauthClient := repo.oauthClient()
|
||||
|
||||
if repo.isGitHubEnterprise() {
|
||||
return ghb.NewEnterpriseClient(repo.baseURL, repo.uploadURL, oauthClient)
|
||||
}
|
||||
|
||||
return ghb.NewClient(oauthClient), nil
|
||||
}
|
||||
|
||||
func (repo *GithubRepo) loadAPICredentials() {
|
||||
repo.apiKey = wtf.Config.UString(
|
||||
"wtf.mods.github.apiKey",
|
||||
os.Getenv("WTF_GITHUB_TOKEN"),
|
||||
)
|
||||
|
||||
repo.baseURL = wtf.Config.UString(
|
||||
"wtf.mods.github.baseURL",
|
||||
os.Getenv("WTF_GITHUB_BASE_URL"),
|
||||
)
|
||||
|
||||
repo.uploadURL = wtf.Config.UString(
|
||||
"wtf.mods.github.uploadURL",
|
||||
os.Getenv("WTF_GITHUB_UPLOAD_URL"),
|
||||
)
|
||||
}
|
||||
|
||||
// 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 {
|
||||
prs = append(prs, pr)
|
||||
}
|
||||
}
|
||||
|
||||
if showStatus() {
|
||||
prs = repo.individualPRs(prs)
|
||||
}
|
||||
|
||||
return prs
|
||||
}
|
||||
|
||||
// individualPRs takes a list of pull requests (presumably returned from
|
||||
// github.PullRequests.List) and fetches them individually to get more detailed
|
||||
// status info on each. see: https://developer.github.com/v3/git/#checking-mergeability-of-pull-requests
|
||||
func (repo *GithubRepo) individualPRs(prs []*ghb.PullRequest) []*ghb.PullRequest {
|
||||
github, err := repo.githubClient()
|
||||
if err != nil {
|
||||
return prs
|
||||
}
|
||||
|
||||
var ret []*ghb.PullRequest
|
||||
for i := range prs {
|
||||
pr, _, err := github.PullRequests.Get(context.Background(), repo.Owner, repo.Name, prs[i].GetNumber())
|
||||
if err != nil {
|
||||
// worst case, just keep the original one
|
||||
ret = append(ret, prs[i])
|
||||
} else {
|
||||
ret = append(ret, pr)
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// 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 {
|
||||
prs = append(prs, pr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return prs
|
||||
}
|
||||
|
||||
func (repo *GithubRepo) loadPullRequests() ([]*ghb.PullRequest, error) {
|
||||
github, err := repo.githubClient()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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) loadRemoteRepository() (*ghb.Repository, error) {
|
||||
github, err := repo.githubClient()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
repository, _, err := github.Repositories.Get(context.Background(), repo.Owner, repo.Name)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return repository, nil
|
||||
}
|
||||
137
modules/github/widget.go
Normal file
137
modules/github/widget.go
Normal file
@@ -0,0 +1,137 @@
|
||||
package github
|
||||
|
||||
import (
|
||||
"github.com/gdamore/tcell"
|
||||
"github.com/rivo/tview"
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
|
||||
const HelpText = `
|
||||
Keyboard commands for GitHub:
|
||||
|
||||
/: Show/hide this help window
|
||||
h: Previous git repository
|
||||
l: Next git repository
|
||||
r: Refresh the data
|
||||
|
||||
arrow left: Previous git repository
|
||||
arrow right: Next git repository
|
||||
|
||||
return: Open the selected repository in a browser
|
||||
`
|
||||
|
||||
type Widget struct {
|
||||
wtf.HelpfulWidget
|
||||
wtf.TextWidget
|
||||
|
||||
GithubRepos []*GithubRepo
|
||||
Idx int
|
||||
}
|
||||
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
TextWidget: wtf.NewTextWidget(app, "GitHub", "github", true),
|
||||
|
||||
Idx: 0,
|
||||
}
|
||||
|
||||
widget.GithubRepos = widget.buildRepoCollection(wtf.Config.UMap("wtf.mods.github.repositories"))
|
||||
|
||||
widget.HelpfulWidget.SetView(widget.View)
|
||||
widget.View.SetInputCapture(widget.keyboardIntercept)
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) Refresh() {
|
||||
for _, repo := range widget.GithubRepos {
|
||||
repo.Refresh()
|
||||
}
|
||||
|
||||
widget.display()
|
||||
}
|
||||
|
||||
func (widget *Widget) Next() {
|
||||
widget.Idx = widget.Idx + 1
|
||||
if widget.Idx == len(widget.GithubRepos) {
|
||||
widget.Idx = 0
|
||||
}
|
||||
|
||||
widget.display()
|
||||
}
|
||||
|
||||
func (widget *Widget) Prev() {
|
||||
widget.Idx = widget.Idx - 1
|
||||
if widget.Idx < 0 {
|
||||
widget.Idx = len(widget.GithubRepos) - 1
|
||||
}
|
||||
|
||||
widget.display()
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) buildRepoCollection(repoData map[string]interface{}) []*GithubRepo {
|
||||
githubRepos := []*GithubRepo{}
|
||||
|
||||
for name, owner := range repoData {
|
||||
repo := NewGithubRepo(name, owner.(string))
|
||||
githubRepos = append(githubRepos, repo)
|
||||
}
|
||||
|
||||
return githubRepos
|
||||
}
|
||||
|
||||
func (widget *Widget) currentGithubRepo() *GithubRepo {
|
||||
if len(widget.GithubRepos) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if widget.Idx < 0 || widget.Idx >= len(widget.GithubRepos) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return widget.GithubRepos[widget.Idx]
|
||||
}
|
||||
|
||||
func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
|
||||
switch string(event.Rune()) {
|
||||
case "/":
|
||||
widget.ShowHelp()
|
||||
return nil
|
||||
case "h":
|
||||
widget.Prev()
|
||||
return nil
|
||||
case "l":
|
||||
widget.Next()
|
||||
return nil
|
||||
case "r":
|
||||
widget.Refresh()
|
||||
return nil
|
||||
}
|
||||
|
||||
switch event.Key() {
|
||||
case tcell.KeyEnter:
|
||||
widget.openRepo()
|
||||
return nil
|
||||
case tcell.KeyLeft:
|
||||
widget.Prev()
|
||||
return nil
|
||||
case tcell.KeyRight:
|
||||
widget.Next()
|
||||
return nil
|
||||
default:
|
||||
return event
|
||||
}
|
||||
}
|
||||
|
||||
func (widget *Widget) openRepo() {
|
||||
repo := widget.currentGithubRepo()
|
||||
|
||||
if repo != nil {
|
||||
repo.Open()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user