Commit 1a5e67c8 authored by Pablo Carranza's avatar Pablo Carranza 🦄

Some reshaping of things so they make some sense

parent 8278bff3
......@@ -3,28 +3,30 @@ package main
import (
"flag"
"fmt"
"github.com/xanzy/go-gitlab"
"log"
"time"
"github.com/xanzy/go-gitlab"
"gitlab.com/gl-infra/oncall-robot-assistant/oncall"
)
func main() {
cfgFile := flag.String("config", "./.settings.json", "the configuration file")
flag.Parse()
config, err := readConfig(*cfgFile)
config, err := oncall.ReadConfig(*cfgFile)
if err != nil {
log.Fatalln(err)
}
// get current primary
primaryNow, err := config.getOncallPerson("primary", "current")
primaryNow, err := oncall.GetOncallPerson(config, "primary", "current")
if err != nil {
log.Fatalln(err)
}
// get current secondary
secondaryNow, err := config.getOncallPerson("secondary", "current")
secondaryNow, err := oncall.GetOncallPerson(config, "secondary", "current")
if err != nil {
log.Fatalln(err)
}
......@@ -33,13 +35,13 @@ func main() {
fmt.Printf("sec oncall now: %s\n", secondaryNow)
// get next primary
primaryNext, err := config.getOncallPerson("primary", "next")
primaryNext, err := oncall.GetOncallPerson(config, "primary", "next")
if err != nil {
log.Fatalln(err)
}
// get next secondary
secondaryNext, err := config.getOncallPerson("secondary", "next")
secondaryNext, err := oncall.GetOncallPerson(config, "secondary", "next")
if err != nil {
log.Fatalln(err)
}
......
package oncall
import (
"encoding/json"
"errors"
"io/ioutil"
"os"
)
// Config : map the json fields on the config file
type Config struct {
GitLabToken string `json:"gitlab_token"`
PDToken string `json:"pd_token"`
OncallPriID string `json:"oncall_pri_id"`
OncallSecID string `json:"oncall_sec_id"`
OncallShadowID string `json:"oncall_shadow_id"`
OncallStartTime string `json:"oncall_start_time"`
OncallEndTime string `json:"oncall_end_time"`
}
// readConfig will attempt to read the different configuration
// parameters from a JSON formatted file.
func ReadConfig(f string) (*Config, error) {
if _, err := os.Stat(f); os.IsNotExist(err) {
return nil, errors.New(err.Error())
}
content, err := ioutil.ReadFile(f)
if err != nil {
return nil, errors.New(err.Error())
}
var cfg Config
json.Unmarshal(content, &cfg)
return &cfg, nil
}
package main
package oncall
import (
"testing"
......@@ -6,7 +6,7 @@ import (
func TestReadConfig(t *testing.T) {
filename := "invalid-cfg.json"
_, err := readConfig(filename)
_, err := ReadConfig(filename)
if err == nil {
t.Fatalf("could not read configuration file %s", filename)
}
......
package main
package oncall
import (
"encoding/json"
"errors"
"fmt"
"github.com/PagerDuty/go-pagerduty"
"io/ioutil"
"os"
"strconv"
"time"
pagerduty "github.com/PagerDuty/go-pagerduty"
)
// Config : map the json fields on the config file
type Config struct {
GitLabToken string `json:"gitlab_token"`
PDToken string `json:"pd_token"`
OncallPriID string `json:"oncall_pri_id"`
OncallSecID string `json:"oncall_sec_id"`
OncallShadowID string `json:"oncall_shadow_id"`
OncallStartTime string `json:"oncall_start_time"`
OncallEndTime string `json:"oncall_end_time"`
}
// GetOncallPerson returns the person who happens to be on call
func GetOncallPerson(c *Config, shift string, period string) (string, error) {
// if we ask for current oncall period, there is no offset to apply.
// otherwise offset = 1 because we will run this the day before
// the rotation changes.
var offset int
if period == "current" {
offset = 0
} else {
offset = 5
}
// readConfig will attempt to read the different configuration
// parameters from a JSON formatted file.
func readConfig(f string) (*Config, error) {
if _, err := os.Stat(f); os.IsNotExist(err) {
return nil, errors.New(err.Error())
// switch oncall shift type
switch shift {
case "primary":
shift = c.OncallPriID
case "secondary":
shift = c.OncallSecID
}
content, err := ioutil.ReadFile(f)
var options pagerduty.ListOnCallUsersOptions
options.Since, options.Until = getOncallDates(c, offset)
client := pagerduty.NewClient(c.PDToken)
oncall, err := client.ListOnCallUsers(shift, options)
if err != nil {
return nil, errors.New(err.Error())
return "", err
}
var cfg Config
json.Unmarshal(content, &cfg)
return &cfg, nil
return oncall[0].Name, nil
}
// getOncallDates return formatted dates for the current or future oncall shift
func (c Config) getOncallDates(offset int) (start string, end string) {
// get current time
// getOncallDates returns formatted dates as string for the current or future oncall shift
func getOncallDates(c *Config, offset int) (start string, end string) {
t := time.Now()
// get month
year := strconv.Itoa(t.Year())
var month string
if t.Month() < 10 {
......@@ -64,32 +62,3 @@ func (c Config) getOncallDates(offset int) (start string, end string) {
end = fmt.Sprintf("%s-%s-%sT%sZ", year, month, day, c.OncallEndTime)
return start, end
}
// getOncall
func (c Config) getOncallPerson(shift string, period string) (string, error) {
// if we ask for current oncall period, there is no offset to apply.
// otherwise offset = 1 because we will run this the day before
// the rotation changes.
var offset int
if period == "current" {
offset = 0
} else {
offset = 5
}
// switch oncall shift type
switch shift {
case "primary":
shift = c.OncallPriID
case "secondary":
shift = c.OncallSecID
}
var options pagerduty.ListOnCallUsersOptions
options.Since, options.Until = c.getOncallDates(offset)
client := pagerduty.NewClient(c.PDToken)
oncall, err := client.ListOnCallUsers(shift, options)
if err != nil {
return "", err
}
return oncall[0].Name, nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment