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

Display OpsGenie oncall data for one specific schedule

This commit is contained in:
Chris Cummer 2018-04-02 21:41:46 -07:00 committed by Chris Cummer
parent 62502c24d0
commit 945dfc8db7
6 changed files with 71 additions and 38 deletions

View File

@ -58,7 +58,7 @@ func (widget *Widget) contentFrom(items []Item) string {
return fmt.Sprintf("\n\n\n\n\n\n\n%s", wtf.CenterText("[grey]no one[white]", 52)) return fmt.Sprintf("\n\n\n\n\n\n\n%s", wtf.CenterText("[grey]no one[white]", 52))
} }
str := "" str := "\n"
for _, item := range items { for _, item := range items {
str = str + widget.format(item) str = str + widget.format(item)
} }

View File

@ -9,13 +9,11 @@ import (
type Client struct { type Client struct {
CommitCount int CommitCount int
Repository string
} }
func NewClient() *Client { func NewClient() *Client {
client := Client{ client := Client{
CommitCount: 10, CommitCount: 10,
Repository: "/Users/Chris/Documents/Lendesk/core-api",
} }
return &client return &client
@ -23,7 +21,7 @@ func NewClient() *Client {
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */
func (client *Client) CurrentBranch() string { func (client *Client) Branch() string {
arg := []string{"rev-parse", "--abbrev-ref", "HEAD"} arg := []string{"rev-parse", "--abbrev-ref", "HEAD"}
cmd := exec.Command("git", arg...) cmd := exec.Command("git", arg...)
str := wtf.ExecuteCommand(cmd) str := wtf.ExecuteCommand(cmd)
@ -50,3 +48,11 @@ func (client *Client) Commits() []string {
return data return data
} }
func (client *Client) Repository() string {
arg := []string{"rev-parse", "--show-toplevel"}
cmd := exec.Command("git", arg...)
str := wtf.ExecuteCommand(cmd)
return str
}

View File

@ -7,8 +7,8 @@ func Fetch() map[string][]string {
result := make(map[string][]string) result := make(map[string][]string)
result["repo"] = []string{client.Repository} result["repo"] = []string{client.Repository()}
result["branch"] = []string{client.CurrentBranch()} result["branch"] = []string{client.Branch()}
result["changes"] = client.ChangedFiles() result["changes"] = client.ChangedFiles()
result["commits"] = client.Commits() result["commits"] = client.Commits()

View File

@ -1,33 +1,55 @@
package opsgenie package opsgenie
import ( import (
"encoding/json"
"fmt"
"net/http"
"os" "os"
ogcli "github.com/opsgenie/opsgenie-go-sdk/client"
sch "github.com/opsgenie/opsgenie-go-sdk/schedule"
) )
func Fetch() string { type Data struct {
OnCallRecipients []string `json:"onCallRecipients"`
Parent Parent `json:"_parent"`
}
type OnCallData struct {
Data Data `json:"data"`
Message string `json:"message"`
RequestID string `json:"requestId"`
Took float32 `json:"took"`
}
type Parent struct {
ID string `json:"id"`
Name string `json:"name"`
Enabled bool `json:"enabled"`
}
func Fetch() *OnCallData {
apiKey := os.Getenv("WTF_OPS_GENIE_API_KEY") apiKey := os.Getenv("WTF_OPS_GENIE_API_KEY")
scheduleName := "Oversight"
cli := new(ogcli.OpsGenieClient) url := fmt.Sprintf("https://api.opsgenie.com/v2/schedules/%s/on-calls?scheduleIdentifierType=name&flat=true", scheduleName)
cli.SetAPIKey(apiKey)
scheduler, err := cli.Schedule() req, err := http.NewRequest("GET", url, nil)
if err != nil { if err != nil {
panic(err) panic(err)
} }
request := sch.ListSchedulesRequest{} req.Header.Set("Authorization", fmt.Sprintf("GenieKey %s", apiKey))
response, err := scheduler.List(request)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer resp.Body.Close()
var str string var onCallData OnCallData
for _, schedule := range response.Schedules {
str = str + schedule.Name + "\n" if err := json.NewDecoder(resp.Body).Decode(&onCallData); err != nil {
panic(err)
} }
return "" return &onCallData
} }

View File

@ -1,7 +1,8 @@
package opsgenie package opsgenie
import ( import (
//"fmt" "fmt"
"strings"
"time" "time"
"github.com/rivo/tview" "github.com/rivo/tview"
@ -31,13 +32,13 @@ func NewWidget() *Widget {
/* -------------------- Exported Functions -------------------- */ /* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() { func (widget *Widget) Refresh() {
//data := Fetch() data := Fetch()
widget.View.SetTitle(" OpsGenie ") widget.View.SetTitle(" OpsGenie ")
widget.RefreshedAt = time.Now() widget.RefreshedAt = time.Now()
widget.View.Clear() widget.View.Clear()
//fmt.Fprintf(widget.View, "%s", widget.contentFrom(data)) fmt.Fprintf(widget.View, "%s", widget.contentFrom(data))
} }
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */
@ -52,6 +53,10 @@ func (widget *Widget) addView() {
widget.View = view widget.View = view
} }
func (widget *Widget) contentFrom(data string) string { func (widget *Widget) contentFrom(onCallData *OnCallData) string {
return data str := "\n"
str = str + fmt.Sprintf(" [red]%s[white]\n", onCallData.Data.Parent.Name)
str = str + fmt.Sprintf(" %s\n", strings.Join(onCallData.Data.OnCallRecipients, ", "))
return str
} }

View File

@ -32,7 +32,7 @@ func NewWidget() *Widget {
/* -------------------- Exported Functions -------------------- */ /* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() { func (widget *Widget) Refresh() {
widget.View.SetTitle(" 🎁 Status ") widget.View.SetTitle(" 🎉 Status ")
widget.RefreshedAt = time.Now() widget.RefreshedAt = time.Now()
widget.View.Clear() widget.View.Clear()