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

WTF-400 VictorOps extracted to new config format

This commit is contained in:
Chris Cummer 2019-04-16 20:40:44 -07:00
parent eaa8825aa5
commit d0faa3cb40
4 changed files with 42 additions and 23 deletions

View File

@ -307,7 +307,8 @@ func makeWidget(app *tview.Application, pages *tview.Pages, widgetName string) w
settings := twitter.NewSettingsFromYAML(wtf.Config) settings := twitter.NewSettingsFromYAML(wtf.Config)
widget = twitter.NewWidget(app, pages, settings) widget = twitter.NewWidget(app, pages, settings)
case "victorops": case "victorops":
widget = victorops.NewWidget(app) settings := victorops.NewSettingsFromYAML(wtf.Config)
widget = victorops.NewWidget(app, settings)
case "weather": case "weather":
widget = weather.NewWidget(app, pages) widget = weather.NewWidget(app, pages)
case "zendesk": case "zendesk":

View File

@ -4,34 +4,20 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"os"
"strings" "strings"
"github.com/wtfutil/wtf/logger" "github.com/wtfutil/wtf/logger"
"github.com/wtfutil/wtf/wtf"
) )
// Fetch gets the current oncall users // Fetch gets the current oncall users
func Fetch() ([]OnCallTeam, error) { func Fetch(apiID, apiKey string) ([]OnCallTeam, error) {
scheduleURL := "https://api.victorops.com/api-public/v1/oncall/current" scheduleURL := "https://api.victorops.com/api-public/v1/oncall/current"
response, err := victorOpsRequest(scheduleURL, apiID(), apiKey()) response, err := victorOpsRequest(scheduleURL, apiID, apiKey)
return response, err return response, err
} }
/* ---------------- Unexported Functions ---------------- */ /* ---------------- Unexported Functions ---------------- */
func apiID() string {
return wtf.Config.UString(
"wtf.mods.victorops.apiID",
os.Getenv("WTF_VICTOROPS_API_ID"),
)
}
func apiKey() string {
return wtf.Config.UString(
"wtf.mods.victorops.apiKey",
os.Getenv("WTF_VICTOROPS_API_KEY"),
)
}
func victorOpsRequest(url string, apiID string, apiKey string) ([]OnCallTeam, error) { func victorOpsRequest(url string, apiID string, apiKey string) ([]OnCallTeam, error) {
req, err := http.NewRequest("GET", url, nil) req, err := http.NewRequest("GET", url, nil)

View File

@ -0,0 +1,30 @@
package victorops
import (
"os"
"github.com/olebedev/config"
"github.com/wtfutil/wtf/cfg"
)
type Settings struct {
common *cfg.Common
apiID string
apiKey string
team string
}
func NewSettingsFromYAML(ymlConfig *config.Config) *Settings {
localConfig, _ := ymlConfig.Get("wtf.mods.victorops")
settings := Settings{
common: cfg.NewCommonSettingsFromYAML(ymlConfig),
apiID: localConfig.UString("apiID", os.Getenv("WTF_VICTOROPS_API_ID")),
apiKey: localConfig.UString("apiKey", os.Getenv("WTF_VICTOROPS_API_KEY")),
team: localConfig.UString("team"),
}
return &settings
}

View File

@ -19,11 +19,13 @@ const HelpText = `
// Widget contains text info // Widget contains text info
type Widget struct { type Widget struct {
wtf.TextWidget wtf.TextWidget
teams []OnCallTeam teams []OnCallTeam
settings *Settings
} }
// NewWidget creates a new widget // NewWidget creates a new widget
func NewWidget(app *tview.Application) *Widget { func NewWidget(app *tview.Application, settings *Settings) *Widget {
widget := Widget{ widget := Widget{
TextWidget: wtf.NewTextWidget(app, "VictorOps - OnCall", "victorops", true), TextWidget: wtf.NewTextWidget(app, "VictorOps - OnCall", "victorops", true),
} }
@ -40,7 +42,7 @@ func (widget *Widget) Refresh() {
return return
} }
teams, err := Fetch() teams, err := Fetch(widget.settings.apiID, widget.settings.apiKey)
widget.View.SetTitle(widget.ContextualTitle(widget.Name())) widget.View.SetTitle(widget.ContextualTitle(widget.Name()))
if err != nil { if err != nil {
@ -64,10 +66,10 @@ func (widget *Widget) display() {
} }
func (widget *Widget) contentFrom(teams []OnCallTeam) string { func (widget *Widget) contentFrom(teams []OnCallTeam) string {
teamToDisplay := wtf.Config.UString("wtf.mods.victorops.team")
var str string var str string
for _, team := range teams { for _, team := range teams {
if len(teamToDisplay) > 0 && teamToDisplay != team.Slug { if len(widget.settings.team) > 0 && widget.settings.team != team.Slug {
continue continue
} }