diff --git a/main.go b/main.go index 7433cd6c..d7088082 100644 --- a/main.go +++ b/main.go @@ -182,42 +182,43 @@ func makeWidget(app *tview.Application, pages *tview.Pages, widgetName string) w // Always in alphabetical order switch widgetName { case "bamboohr": - cfg := bamboohr.NewSettingsFromYAML(wtf.Config) - widget = bamboohr.NewWidget(app, cfg) + settings := bamboohr.NewSettingsFromYAML(wtf.Config) + widget = bamboohr.NewWidget(app, settings) case "bargraph": widget = bargraph.NewWidget(app) case "bittrex": - cfg := bittrex.NewSettingsFromYAML(wtf.Config) - widget = bittrex.NewWidget(app, cfg) + settings := bittrex.NewSettingsFromYAML(wtf.Config) + widget = bittrex.NewWidget(app, settings) case "blockfolio": - cfg := blockfolio.NewSettingsFromYAML(wtf.Config) - widget = blockfolio.NewWidget(app, cfg) + settings := blockfolio.NewSettingsFromYAML(wtf.Config) + widget = blockfolio.NewWidget(app, settings) case "circleci": - cfg := circleci.NewSettingsFromYAML(wtf.Config) - widget = circleci.NewWidget(app, cfg) + settings := circleci.NewSettingsFromYAML(wtf.Config) + widget = circleci.NewWidget(app, settings) case "clocks": - cfg := clocks.NewSettingsFromYAML(wtf.Config) - widget = clocks.NewWidget(app, cfg) + settings := clocks.NewSettingsFromYAML(wtf.Config) + widget = clocks.NewWidget(app, settings) case "cmdrunner": - cfg := cmdrunner.NewSettingsFromYAML(wtf.Config) - widget = cmdrunner.NewWidget(app, cfg) + settings := cmdrunner.NewSettingsFromYAML(wtf.Config) + widget = cmdrunner.NewWidget(app, settings) case "cryptolive": - cfg := cryptolive.NewSettingsFromYAML(wtf.Config) - widget = cryptolive.NewWidget(app, cfg) + settings := cryptolive.NewSettingsFromYAML(wtf.Config) + widget = cryptolive.NewWidget(app, settings) case "datadog": - cfg := datadog.NewSettingsFromYAML(wtf.Config) - widget = datadog.NewWidget(app, cfg) + settings := datadog.NewSettingsFromYAML(wtf.Config) + widget = datadog.NewWidget(app, settings) case "gcal": - cfg := gcal.NewSettingsFromYAML(wtf.Config) - widget = gcal.NewWidget(app, cfg) + settings := gcal.NewSettingsFromYAML(wtf.Config) + widget = gcal.NewWidget(app, settings) case "gerrit": - cfg := gerrit.NewSettingsFromYAML(wtf.Config) - widget = gerrit.NewWidget(app, pages, cfg) + settings := gerrit.NewSettingsFromYAML(wtf.Config) + widget = gerrit.NewWidget(app, pages, settings) case "git": - cfg := git.NewSettingsFromYAML(wtf.Config) - widget = git.NewWidget(app, pages, cfg) + settings := git.NewSettingsFromYAML(wtf.Config) + widget = git.NewWidget(app, pages, settings) case "github": - widget = github.NewWidget(app, pages) + settings := github.NewSettingsFromYAML(wtf.Config) + widget = github.NewWidget(app, pages, settings) case "gitlab": widget = gitlab.NewWidget(app, pages) case "gitter": diff --git a/modules/github/display.go b/modules/github/display.go index 556f38a3..1819efa2 100644 --- a/modules/github/display.go +++ b/modules/github/display.go @@ -21,16 +21,16 @@ func (widget *Widget) display() { 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 + widget.displayMyReviewRequests(repo, widget.settings.username) str = str + "\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) } func (widget *Widget) displayMyPullRequests(repo *GithubRepo, username string) string { - prs := repo.myPullRequests(username) + prs := repo.myPullRequests(username, widget.settings.enableStatus) if len(prs) == 0 { return " [grey]none[white]\n" @@ -38,7 +38,7 @@ func (widget *Widget) displayMyPullRequests(repo *GithubRepo, username string) s str := "" 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 @@ -74,10 +74,6 @@ 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] ", @@ -85,8 +81,8 @@ var mergeIcons = map[string]string{ "blocked": "[red]✖[white] ", } -func mergeString(pr *github.PullRequest) string { - if !showStatus() { +func (widget *Widget) mergeString(pr *github.PullRequest) string { + if !widget.settings.enableStatus { return "" } if str, ok := mergeIcons[pr.GetMergeableState()]; ok { diff --git a/modules/github/github_repo.go b/modules/github/github_repo.go index 1860de61..65636795 100644 --- a/modules/github/github_repo.go +++ b/modules/github/github_repo.go @@ -3,7 +3,6 @@ package github import ( "context" "net/http" - "os" ghb "github.com/google/go-github/github" "github.com/wtfutil/wtf/wtf" @@ -21,13 +20,15 @@ type GithubRepo struct { RemoteRepo *ghb.Repository } -func NewGithubRepo(name, owner string) *GithubRepo { +func NewGithubRepo(name, owner, apiKey, baseURL, uploadURL string) *GithubRepo { repo := GithubRepo{ Name: name, Owner: owner, - } - repo.loadAPICredentials() + apiKey: apiKey, + baseURL: baseURL, + uploadURL: uploadURL, + } return &repo } @@ -94,25 +95,8 @@ func (repo *GithubRepo) githubClient() (*ghb.Client, error) { 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 { +func (repo *GithubRepo) myPullRequests(username string, showStatus bool) []*ghb.PullRequest { prs := []*ghb.PullRequest{} 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) } diff --git a/modules/github/settings.go b/modules/github/settings.go new file mode 100644 index 00000000..14fd0ee1 --- /dev/null +++ b/modules/github/settings.go @@ -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 +} diff --git a/modules/github/widget.go b/modules/github/widget.go index 2bafc598..69e4f136 100644 --- a/modules/github/widget.go +++ b/modules/github/widget.go @@ -26,17 +26,19 @@ type Widget struct { GithubRepos []*GithubRepo 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{ HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText), 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.View.SetInputCapture(widget.keyboardIntercept) @@ -78,7 +80,14 @@ func (widget *Widget) buildRepoCollection(repoData map[string]interface{}) []*Gi githubRepos := []*GithubRepo{} 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) }