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

WTF-400 GitHub extracted to new config format

This commit is contained in:
Chris Cummer 2019-04-14 16:29:13 -07:00
parent 095041be61
commit 6c22ad6d27
5 changed files with 86 additions and 60 deletions

47
main.go
View File

@ -182,42 +182,43 @@ func makeWidget(app *tview.Application, pages *tview.Pages, widgetName string) w
// Always in alphabetical order // Always in alphabetical order
switch widgetName { switch widgetName {
case "bamboohr": case "bamboohr":
cfg := bamboohr.NewSettingsFromYAML(wtf.Config) settings := bamboohr.NewSettingsFromYAML(wtf.Config)
widget = bamboohr.NewWidget(app, cfg) widget = bamboohr.NewWidget(app, settings)
case "bargraph": case "bargraph":
widget = bargraph.NewWidget(app) widget = bargraph.NewWidget(app)
case "bittrex": case "bittrex":
cfg := bittrex.NewSettingsFromYAML(wtf.Config) settings := bittrex.NewSettingsFromYAML(wtf.Config)
widget = bittrex.NewWidget(app, cfg) widget = bittrex.NewWidget(app, settings)
case "blockfolio": case "blockfolio":
cfg := blockfolio.NewSettingsFromYAML(wtf.Config) settings := blockfolio.NewSettingsFromYAML(wtf.Config)
widget = blockfolio.NewWidget(app, cfg) widget = blockfolio.NewWidget(app, settings)
case "circleci": case "circleci":
cfg := circleci.NewSettingsFromYAML(wtf.Config) settings := circleci.NewSettingsFromYAML(wtf.Config)
widget = circleci.NewWidget(app, cfg) widget = circleci.NewWidget(app, settings)
case "clocks": case "clocks":
cfg := clocks.NewSettingsFromYAML(wtf.Config) settings := clocks.NewSettingsFromYAML(wtf.Config)
widget = clocks.NewWidget(app, cfg) widget = clocks.NewWidget(app, settings)
case "cmdrunner": case "cmdrunner":
cfg := cmdrunner.NewSettingsFromYAML(wtf.Config) settings := cmdrunner.NewSettingsFromYAML(wtf.Config)
widget = cmdrunner.NewWidget(app, cfg) widget = cmdrunner.NewWidget(app, settings)
case "cryptolive": case "cryptolive":
cfg := cryptolive.NewSettingsFromYAML(wtf.Config) settings := cryptolive.NewSettingsFromYAML(wtf.Config)
widget = cryptolive.NewWidget(app, cfg) widget = cryptolive.NewWidget(app, settings)
case "datadog": case "datadog":
cfg := datadog.NewSettingsFromYAML(wtf.Config) settings := datadog.NewSettingsFromYAML(wtf.Config)
widget = datadog.NewWidget(app, cfg) widget = datadog.NewWidget(app, settings)
case "gcal": case "gcal":
cfg := gcal.NewSettingsFromYAML(wtf.Config) settings := gcal.NewSettingsFromYAML(wtf.Config)
widget = gcal.NewWidget(app, cfg) widget = gcal.NewWidget(app, settings)
case "gerrit": case "gerrit":
cfg := gerrit.NewSettingsFromYAML(wtf.Config) settings := gerrit.NewSettingsFromYAML(wtf.Config)
widget = gerrit.NewWidget(app, pages, cfg) widget = gerrit.NewWidget(app, pages, settings)
case "git": case "git":
cfg := git.NewSettingsFromYAML(wtf.Config) settings := git.NewSettingsFromYAML(wtf.Config)
widget = git.NewWidget(app, pages, cfg) widget = git.NewWidget(app, pages, settings)
case "github": case "github":
widget = github.NewWidget(app, pages) settings := github.NewSettingsFromYAML(wtf.Config)
widget = github.NewWidget(app, pages, settings)
case "gitlab": case "gitlab":
widget = gitlab.NewWidget(app, pages) widget = gitlab.NewWidget(app, pages)
case "gitter": case "gitter":

View File

@ -21,16 +21,16 @@ func (widget *Widget) display() {
str = str + widget.displayStats(repo) str = str + widget.displayStats(repo)
str = str + "\n" str = str + "\n"
str = str + " [red]Open Review Requests[white]\n" str = str + " [red]Open Review Requests[white]\n"
str = str + widget.displayMyReviewRequests(repo, wtf.Config.UString("wtf.mods.github.username")) str = str + widget.displayMyReviewRequests(repo, widget.settings.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 + widget.displayMyPullRequests(repo, wtf.Config.UString("wtf.mods.github.username")) str = str + widget.displayMyPullRequests(repo, widget.settings.username)
widget.View.SetText(str) widget.View.SetText(str)
} }
func (widget *Widget) displayMyPullRequests(repo *GithubRepo, username string) string { func (widget *Widget) displayMyPullRequests(repo *GithubRepo, username string) string {
prs := repo.myPullRequests(username) prs := repo.myPullRequests(username, widget.settings.enableStatus)
if len(prs) == 0 { if len(prs) == 0 {
return " [grey]none[white]\n" return " [grey]none[white]\n"
@ -38,7 +38,7 @@ func (widget *Widget) displayMyPullRequests(repo *GithubRepo, username string) s
str := "" str := ""
for _, pr := range prs { for _, pr := range prs {
str = str + fmt.Sprintf(" %s[green]%4d[white] %s\n", mergeString(pr), *pr.Number, *pr.Title) str = str + fmt.Sprintf(" %s[green]%4d[white] %s\n", widget.mergeString(pr), *pr.Number, *pr.Title)
} }
return str return str
@ -74,10 +74,6 @@ 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)
} }
func showStatus() bool {
return wtf.Config.UBool("wtf.mods.github.enableStatus", false)
}
var mergeIcons = map[string]string{ var mergeIcons = map[string]string{
"dirty": "[red]![white] ", "dirty": "[red]![white] ",
"clean": "[green]✔[white] ", "clean": "[green]✔[white] ",
@ -85,8 +81,8 @@ var mergeIcons = map[string]string{
"blocked": "[red]✖[white] ", "blocked": "[red]✖[white] ",
} }
func mergeString(pr *github.PullRequest) string { func (widget *Widget) mergeString(pr *github.PullRequest) string {
if !showStatus() { if !widget.settings.enableStatus {
return "" return ""
} }
if str, ok := mergeIcons[pr.GetMergeableState()]; ok { if str, ok := mergeIcons[pr.GetMergeableState()]; ok {

View File

@ -3,7 +3,6 @@ package github
import ( import (
"context" "context"
"net/http" "net/http"
"os"
ghb "github.com/google/go-github/github" ghb "github.com/google/go-github/github"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/wtf"
@ -21,13 +20,15 @@ type GithubRepo struct {
RemoteRepo *ghb.Repository RemoteRepo *ghb.Repository
} }
func NewGithubRepo(name, owner string) *GithubRepo { func NewGithubRepo(name, owner, apiKey, baseURL, uploadURL string) *GithubRepo {
repo := GithubRepo{ repo := GithubRepo{
Name: name, Name: name,
Owner: owner, Owner: owner,
}
repo.loadAPICredentials() apiKey: apiKey,
baseURL: baseURL,
uploadURL: uploadURL,
}
return &repo return &repo
} }
@ -94,25 +95,8 @@ func (repo *GithubRepo) githubClient() (*ghb.Client, error) {
return ghb.NewClient(oauthClient), nil 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 // myPullRequests returns a list of pull requests created by username on this repo
func (repo *GithubRepo) myPullRequests(username string) []*ghb.PullRequest { func (repo *GithubRepo) myPullRequests(username string, showStatus bool) []*ghb.PullRequest {
prs := []*ghb.PullRequest{} prs := []*ghb.PullRequest{}
for _, pr := range repo.PullRequests { for _, pr := range repo.PullRequests {
@ -123,7 +107,7 @@ func (repo *GithubRepo) myPullRequests(username string) []*ghb.PullRequest {
} }
} }
if showStatus() { if showStatus {
prs = repo.individualPRs(prs) prs = repo.individualPRs(prs)
} }

View File

@ -0,0 +1,36 @@
package github
import (
"os"
"github.com/olebedev/config"
"github.com/wtfutil/wtf/cfg"
)
type Settings struct {
common *cfg.Common
apiKey string
baseURL string
enableStatus bool
repositories map[string]interface{}
uploadURL string
username string
}
func NewSettingsFromYAML(ymlConfig *config.Config) *Settings {
localConfig, _ := ymlConfig.Get("wtf.mods.github")
settings := Settings{
common: cfg.NewCommonSettingsFromYAML(ymlConfig),
apiKey: localConfig.UString("apiKey", os.Getenv("WTF_GITHUB_TOKEN")),
baseURL: localConfig.UString("baseURL", os.Getenv("WTF_GITHUB_BASE_URL")),
enableStatus: localConfig.UBool("enableStatus", false),
repositories: localConfig.UMap("repositories"),
uploadURL: localConfig.UString("uploadURL", os.Getenv("WTF_GITHUB_UPLOAD_URL")),
username: localConfig.UString("username"),
}
return &settings
}

View File

@ -26,17 +26,19 @@ type Widget struct {
GithubRepos []*GithubRepo GithubRepos []*GithubRepo
Idx int Idx int
settings *Settings
} }
func NewWidget(app *tview.Application, pages *tview.Pages) *Widget { func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
widget := Widget{ widget := Widget{
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText), HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
TextWidget: wtf.NewTextWidget(app, "GitHub", "github", true), TextWidget: wtf.NewTextWidget(app, "GitHub", "github", true),
Idx: 0, Idx: 0,
settings: settings,
} }
widget.GithubRepos = widget.buildRepoCollection(wtf.Config.UMap("wtf.mods.github.repositories")) widget.GithubRepos = widget.buildRepoCollection(widget.settings.repositories)
widget.HelpfulWidget.SetView(widget.View) widget.HelpfulWidget.SetView(widget.View)
widget.View.SetInputCapture(widget.keyboardIntercept) widget.View.SetInputCapture(widget.keyboardIntercept)
@ -78,7 +80,14 @@ func (widget *Widget) buildRepoCollection(repoData map[string]interface{}) []*Gi
githubRepos := []*GithubRepo{} githubRepos := []*GithubRepo{}
for name, owner := range repoData { for name, owner := range repoData {
repo := NewGithubRepo(name, owner.(string)) repo := NewGithubRepo(
name,
owner.(string),
widget.settings.apiKey,
widget.settings.baseURL,
widget.settings.uploadURL,
)
githubRepos = append(githubRepos, repo) githubRepos = append(githubRepos, repo)
} }