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

Merge pull request #366 from GaboFDC/gf_fix_opsgenie_api

Use new api format
This commit is contained in:
Chris Cummer 2019-02-11 03:18:21 -08:00 committed by GitHub
commit 75db34a270
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 18 deletions

View File

@ -10,10 +10,10 @@ import (
) )
type OnCallResponse struct { type OnCallResponse struct {
OnCallData []OnCallData `json:"data"` OnCallData OnCallData `json:"data"`
Message string `json:"message"` Message string `json:"message"`
RequestID string `json:"requestId"` RequestID string `json:"requestId"`
Took float32 `json:"took"` Took float32 `json:"took"`
} }
type OnCallData struct { type OnCallData struct {
@ -29,12 +29,17 @@ type Parent struct {
/* -------------------- Exported Functions -------------------- */ /* -------------------- Exported Functions -------------------- */
func Fetch() (*OnCallResponse, error) { func Fetch(scheduleIdentifierType string, schedules []string) ([]*OnCallResponse, error) {
scheduleUrl := "https://api.opsgenie.com/v2/schedules/on-calls?flat=true" agregatedResponses := []*OnCallResponse{}
for _, sched := range schedules {
response, err := opsGenieRequest(scheduleUrl, apiKey()) scheduleUrl := fmt.Sprintf("https://api.opsgenie.com/v2/schedules/%s/on-calls?scheduleIdentifierType=%s&flat=true", sched, scheduleIdentifierType)
response, err := opsGenieRequest(scheduleUrl, apiKey())
return response, err agregatedResponses = append(agregatedResponses, response)
if err != nil {
return nil, err
}
}
return agregatedResponses, nil
} }
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */

View File

@ -23,8 +23,10 @@ func NewWidget(app *tview.Application) *Widget {
/* -------------------- Exported Functions -------------------- */ /* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() { func (widget *Widget) Refresh() {
data, err := Fetch() data, err := Fetch(
wtf.Config.UString("wtf.mods.opsgenie.scheduleIdentifierType"),
getSchedules(),
)
widget.View.SetTitle(widget.ContextualTitle(widget.Name)) widget.View.SetTitle(widget.ContextualTitle(widget.Name))
var content string var content string
@ -41,24 +43,42 @@ func (widget *Widget) Refresh() {
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */
func (widget *Widget) contentFrom(onCallResponse *OnCallResponse) string { func getSchedules() []string {
// see if schedule is set to a single string
configPath := "wtf.mods.opsgenie.schedule"
singleSchedule, err := wtf.Config.String(configPath)
if err == nil {
return []string{singleSchedule}
}
// else, assume list
scheduleList := wtf.Config.UList(configPath)
var ret []string
for _, schedule := range scheduleList {
if str, ok := schedule.(string); ok {
ret = append(ret, str)
}
}
return ret
}
func (widget *Widget) contentFrom(onCallResponses []*OnCallResponse) string {
str := "" str := ""
displayEmpty := wtf.Config.UBool("wtf.mods.opsgenie.displayEmpty", true) displayEmpty := wtf.Config.UBool("wtf.mods.opsgenie.displayEmpty", true)
for _, data := range onCallResponse.OnCallData { for _, data := range onCallResponses {
if (len(data.Recipients) == 0) && (displayEmpty == false) { if (len(data.OnCallData.Recipients) == 0) && (displayEmpty == false) {
continue continue
} }
var msg string var msg string
if len(data.Recipients) == 0 { if len(data.OnCallData.Recipients) == 0 {
msg = " [gray]no one[white]\n\n" msg = " [gray]no one[white]\n\n"
} else { } else {
msg = fmt.Sprintf(" %s\n\n", strings.Join(wtf.NamesFromEmails(data.Recipients), ", ")) msg = fmt.Sprintf(" %s\n\n", strings.Join(wtf.NamesFromEmails(data.OnCallData.Recipients), ", "))
} }
str = str + widget.cleanScheduleName(data.Parent.Name) str = str + widget.cleanScheduleName(data.OnCallData.Parent.Name)
str = str + msg str = str + msg
} }