Refactor based on goreportcard.com

This commit is contained in:
Matej Kramny
2015-03-20 20:40:16 +01:00
parent 8e17ebceef
commit 3d0b0c88e1
7 changed files with 55 additions and 38 deletions

View File

@@ -6,26 +6,30 @@ import (
"encoding/json"
)
// Cachet Incident data model
type Incident struct {
Id int `json:"id"`
ID int `json:"id"`
Name string `json:"name"`
Message string `json:"message"`
Status int `json:"status"`// 4?
Human_status string `json:"human_status"`
HumanStatus string `json:"human_status"`
Component *Component `json:"component"`
Component_id *int `json:"component_id"`
Created_at int `json:"created_at"`
Updated_at int `json:"updated_at"`
ComponentId *int `json:"component_id"`
CreatedAt int `json:"created_at"`
UpdatedAt int `json:"updated_at"`
}
// Response when creating/updating an incident
type IncidentData struct {
Incident Incident `json:"data"`
}
// from API /incidents
type IncidentList struct {
Incidents []Incident `json:"data"`
}
// Get list of incidents
func GetIncidents() []Incident {
_, body, err := makeRequest("GET", "/incidents", nil)
if err != nil {
@@ -36,12 +40,12 @@ func GetIncidents() []Incident {
err = json.Unmarshal(body, &data)
if err != nil {
fmt.Println("Cannot parse incidents.")
panic(err)
}
return data.Incidents
}
// Create or Update incident
func (incident *Incident) Send() {
jsonBytes, err := json.Marshal(incident)
if err != nil {
@@ -49,13 +53,13 @@ func (incident *Incident) Send() {
}
requestType := "POST"
requestUrl := "/incidents"
if incident.Id > 0 {
requestURL := "/incidents"
if incident.ID > 0 {
requestType = "PUT"
requestUrl = "/incidents/" + strconv.Itoa(incident.Id)
requestURL = "/incidents/" + strconv.Itoa(incident.ID)
}
resp, body, err := makeRequest(requestType, requestUrl, jsonBytes)
resp, body, err := makeRequest(requestType, requestURL, jsonBytes)
if err != nil {
panic(err)
}
@@ -68,44 +72,50 @@ func (incident *Incident) Send() {
fmt.Println("Cannot parse incident body.")
panic(err)
} else {
incident.Id = data.Incident.Id
incident.ID = data.Incident.ID
}
fmt.Println("ID:"+strconv.Itoa(incident.Id))
fmt.Println("ID:"+strconv.Itoa(incident.ID))
if resp.StatusCode != 200 {
fmt.Println("Could not create/update incident!")
}
}
// Get the same incident.
// Updates incident.ID
func (incident *Incident) GetSimilarIncidentId() {
incidents := GetIncidents()
for _, inc := range incidents {
if incident.Name == inc.Name && incident.Message == inc.Message && incident.Status == inc.Status && incident.Human_status == inc.Human_status {
incident.Id = inc.Id
fmt.Printf("Updated incident id to %v\n", inc.Id)
if incident.Name == inc.Name && incident.Message == inc.Message && incident.Status == inc.Status && incident.HumanStatus == inc.HumanStatus {
incident.ID = inc.ID
fmt.Printf("Updated incident id to %v\n", inc.ID)
break
}
}
}
// Set status to Investigating
func (incident *Incident) SetInvestigating() {
incident.Status = 1
incident.Human_status = "Investigating"
incident.HumanStatus = "Investigating"
}
// Set status to Identified
func (incident *Incident) SetIdentified() {
incident.Status = 2
incident.Human_status = "Identified"
incident.HumanStatus = "Identified"
}
// Set status to Watching
func (incident *Incident) SetWatching() {
incident.Status = 3
incident.Human_status = "Watching"
incident.HumanStatus = "Watching"
}
// Set status to Fixed
func (incident *Incident) SetFixed() {
incident.Status = 4
incident.Human_status = "Fixed"
incident.HumanStatus = "Fixed"
}