Improve fail reasons, fix api crashes
- Add options about TLS verification - Fix crashes when cachet presents IDs as a string - Improve fail reasons
This commit is contained in:
@@ -7,15 +7,15 @@ import (
|
||||
|
||||
// Incident Cachet data model
|
||||
type Incident struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Message string `json:"message"`
|
||||
Status int `json:"status"` // 4?
|
||||
HumanStatus string `json:"human_status"`
|
||||
Component *Component `json:"-"`
|
||||
ComponentID *int `json:"component_id"`
|
||||
CreatedAt int `json:"created_at"`
|
||||
UpdatedAt int `json:"updated_at"`
|
||||
ID json.Number `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Message string `json:"message"`
|
||||
Status json.Number `json:"status"` // 4?
|
||||
HumanStatus string `json:"human_status"`
|
||||
Component *Component `json:"-"`
|
||||
ComponentID *json.Number `json:"component_id"`
|
||||
CreatedAt *string `json:"created_at"`
|
||||
UpdatedAt *string `json:"updated_at"`
|
||||
}
|
||||
|
||||
// IncidentData is a response when creating/updating an incident
|
||||
@@ -40,6 +40,7 @@ func GetIncidents() []Incident {
|
||||
err = json.Unmarshal(body, &data)
|
||||
if err != nil {
|
||||
Logger.Printf("Cannot parse incidents: %v\n", err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return data.Incidents
|
||||
@@ -47,17 +48,19 @@ func GetIncidents() []Incident {
|
||||
|
||||
// Send - Create or Update incident
|
||||
func (incident *Incident) Send() {
|
||||
jsonBytes, err := json.Marshal(incident)
|
||||
if err != nil {
|
||||
Logger.Printf("Cannot encode incident: %v\n", err)
|
||||
return
|
||||
}
|
||||
jsonBytes, _ := json.Marshal(map[string]interface{}{
|
||||
"name": incident.Name,
|
||||
"message": incident.Message,
|
||||
"status": incident.Status,
|
||||
"component_id": incident.ComponentID,
|
||||
"notify": true,
|
||||
})
|
||||
|
||||
requestType := "POST"
|
||||
requestURL := "/incidents"
|
||||
if incident.ID > 0 {
|
||||
if len(incident.ID) > 0 {
|
||||
requestType = "PUT"
|
||||
requestURL += "/" + strconv.Itoa(incident.ID)
|
||||
requestURL += "/" + string(incident.ID)
|
||||
}
|
||||
|
||||
resp, body, err := makeRequest(requestType, requestURL, jsonBytes)
|
||||
@@ -71,7 +74,7 @@ func (incident *Incident) Send() {
|
||||
var data IncidentData
|
||||
err = json.Unmarshal(body, &data)
|
||||
if err != nil {
|
||||
Logger.Println("Cannot parse incident body.")
|
||||
Logger.Println("Cannot parse incident body.", string(body))
|
||||
panic(err)
|
||||
} else {
|
||||
incident.ID = data.Incident.ID
|
||||
@@ -89,7 +92,7 @@ func (incident *Incident) GetSimilarIncidentID() {
|
||||
incidents := GetIncidents()
|
||||
|
||||
for _, inc := range incidents {
|
||||
if incident.Name == inc.Name && incident.Message == inc.Message && incident.Status == inc.Status && incident.HumanStatus == inc.HumanStatus {
|
||||
if incident.Name == inc.Name && incident.Message == inc.Message && incident.Status == inc.Status {
|
||||
incident.ID = inc.ID
|
||||
Logger.Printf("Updated incident id to %v\n", inc.ID)
|
||||
break
|
||||
@@ -98,7 +101,7 @@ func (incident *Incident) GetSimilarIncidentID() {
|
||||
}
|
||||
|
||||
func (incident *Incident) fetchComponent() error {
|
||||
_, body, err := makeRequest("GET", "/components/" + strconv.Itoa(*incident.ComponentID), nil)
|
||||
_, body, err := makeRequest("GET", "/components/"+string(*incident.ComponentID), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -106,7 +109,7 @@ func (incident *Incident) fetchComponent() error {
|
||||
var data ComponentData
|
||||
err = json.Unmarshal(body, &data)
|
||||
if err != nil {
|
||||
Logger.Println("Cannot parse component body.")
|
||||
Logger.Println("Cannot parse component body. %v", string(body))
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -116,7 +119,7 @@ func (incident *Incident) fetchComponent() error {
|
||||
}
|
||||
|
||||
func (incident *Incident) UpdateComponent() {
|
||||
if incident.ComponentID == nil || *incident.ComponentID == 0 {
|
||||
if incident.ComponentID == nil || len(*incident.ComponentID) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -128,22 +131,23 @@ func (incident *Incident) UpdateComponent() {
|
||||
}
|
||||
}
|
||||
|
||||
switch incident.Status {
|
||||
status, _ := strconv.Atoi(string(incident.Status))
|
||||
switch status {
|
||||
case 1, 2, 3:
|
||||
if incident.Component.Status == 3 {
|
||||
incident.Component.Status = 4
|
||||
if incident.Component.Status == "3" {
|
||||
incident.Component.Status = "4"
|
||||
} else {
|
||||
incident.Component.Status = 3
|
||||
incident.Component.Status = "3"
|
||||
}
|
||||
case 4:
|
||||
incident.Component.Status = 1
|
||||
incident.Component.Status = "1"
|
||||
}
|
||||
|
||||
jsonBytes, _ := json.Marshal(map[string]interface{}{
|
||||
"status": incident.Component.Status,
|
||||
})
|
||||
|
||||
resp, _, err := makeRequest("PUT", "/components/" + strconv.Itoa(incident.Component.ID), jsonBytes)
|
||||
resp, _, err := makeRequest("PUT", "/components/"+string(incident.Component.ID), jsonBytes)
|
||||
if err != nil || resp.StatusCode != 200 {
|
||||
Logger.Printf("Could not update component: (resp code %d) %v", resp.StatusCode, err)
|
||||
return
|
||||
@@ -152,24 +156,24 @@ func (incident *Incident) UpdateComponent() {
|
||||
|
||||
// SetInvestigating sets status to Investigating
|
||||
func (incident *Incident) SetInvestigating() {
|
||||
incident.Status = 1
|
||||
incident.Status = "1"
|
||||
incident.HumanStatus = "Investigating"
|
||||
}
|
||||
|
||||
// SetIdentified sets status to Identified
|
||||
func (incident *Incident) SetIdentified() {
|
||||
incident.Status = 2
|
||||
incident.Status = "2"
|
||||
incident.HumanStatus = "Identified"
|
||||
}
|
||||
|
||||
// SetWatching sets status to Watching
|
||||
func (incident *Incident) SetWatching() {
|
||||
incident.Status = 3
|
||||
incident.Status = "3"
|
||||
incident.HumanStatus = "Watching"
|
||||
}
|
||||
|
||||
// SetFixed sets status to Fixed
|
||||
func (incident *Incident) SetFixed() {
|
||||
incident.Status = 4
|
||||
incident.Status = "4"
|
||||
incident.HumanStatus = "Fixed"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user