Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Get Incident Alert and Manage Incident Alert endpoints #231

Merged
merged 3 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 43 additions & 16 deletions incident.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pagerduty
import (
"encoding/json"
"fmt"
"net/http"

"github.com/google/go-querystring/query"
)
Expand Down Expand Up @@ -86,6 +87,7 @@ type Incident struct {
AlertCounts AlertCounts `json:"alert_counts,omitempty"`
Body IncidentBody `json:"body,omitempty"`
IsMergeable bool `json:"is_mergeable,omitempty"`
ConferenceBridge *ConferenceBridge `json:"conference_bridge,omitempty"`
}

// ListIncidentsResponse is the response structure when calling the ListIncident API endpoint.
Expand All @@ -111,6 +113,12 @@ type ListIncidentsOptions struct {
Includes []string `url:"include,omitempty,brackets"`
}

// ConferenceBridge is a struct for the conference_bridge object on an incident
type ConferenceBridge struct {
ConferenceNumber string `json:"conference_number,omitempty"`
ConferenceURL string `json:"conference_url,omitempty"`
}

// ListIncidents lists existing incidents.
func (c *Client) ListIncidents(o ListIncidentsOptions) (*ListIncidentsResponse, error) {
v, err := query.Values(o)
Expand Down Expand Up @@ -269,6 +277,16 @@ type IncidentAlert struct {
Integration APIObject `json:"integration,omitempty"`
}

// IncidentAlertResponse is the response of a sincle incident alert
type IncidentAlertResponse struct {
IncidentAlert *IncidentAlert `json:"alert,omitempty"`
}

// IncidentAlertList is the generic structure of a list of alerts
type IncidentAlertList struct {
Alerts []IncidentAlert `json:"alerts,omitempty"`
}

// ListAlertsResponse is the response structure when calling the ListAlert API endpoint.
type ListAlertsResponse struct {
APIListObject
Expand Down Expand Up @@ -392,21 +410,6 @@ func (c *Client) ListIncidentLogEntries(id string, o ListIncidentLogEntriesOptio
return &result, c.decodeJSON(resp, &result)
}

// Alert is a list of all of the alerts that happened to an incident.
type Alert struct {
APIObject
Service APIObject `json:"service,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
Status string `json:"status,omitempty"`
AlertKey string `json:"alert_key,omitempty"`
Incident APIObject `json:"incident,omitempty"`
}

type ListAlertResponse struct {
APIListObject
Alerts []Alert `json:"alerts,omitempty"`
}
Comment on lines -396 to -408
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly here -- guessing impact is low if these aren't referenced anymore?


// IncidentResponders contains details about responders to an incident.
type IncidentResponders struct {
State string `json:"state"`
Expand Down Expand Up @@ -466,4 +469,28 @@ func (c *Client) ResponderRequest(id string, o ResponderRequestOptions) (*Respon
return result, err
}

/* TODO: Manage Alerts, Get Alert, Create Status Updates */
// GetIncidentAlert
func (c *Client) GetIncidentAlert(incidentID, alertID string) (*IncidentAlertResponse, *http.Response, error) {
resp, err := c.get("/incidents/" + incidentID + "/alerts/" + alertID)
if err != nil {
return nil, nil, err
}

result := &IncidentAlertResponse{}
err = json.NewDecoder(resp.Body).Decode(result)
return result, resp, err
}

// ManageIncidentAlerts
func (c *Client) ManageIncidentAlerts(incidentID string, alerts *IncidentAlertList) (*ListAlertsResponse, *http.Response, error) {
headers := make(map[string]string)

resp, err := c.put("/incidents/"+incidentID+"/alerts/", alerts, &headers)
if err != nil {
return nil, nil, err
}
var result ListAlertsResponse
return &result, resp, c.decodeJSON(resp, &result)
}

/* TODO: Create Status Updates */
68 changes: 68 additions & 0 deletions incident_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,3 +601,71 @@ func TestIncident_ResponderRequest(t *testing.T) {
}
testEqual(t, want, res)
}

func TestIncident_GetAlert(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/incidents/1/alerts/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
w.Write([]byte(`{"alert": {"id": "1"}}`))
})

var client = &Client{apiEndpoint: server.URL, authToken: "foo", HTTPClient: defaultHTTPClient}

incidentID := "1"
alertID := "1"
res, _, err := client.GetIncidentAlert(incidentID, alertID)

want := &IncidentAlertResponse{
IncidentAlert: &IncidentAlert{
APIObject: APIObject{
ID: "1",
},
},
}

if err != nil {
t.Fatal(err)
}
testEqual(t, want, res)
}
func TestIncident_ManageAlerts(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/incidents/1/alerts/", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
w.Write([]byte(`{"alerts": [{"id": "1"}]}`))
})

var client = &Client{apiEndpoint: server.URL, authToken: "foo", HTTPClient: defaultHTTPClient}

incidentID := "1"

input := &IncidentAlertList{
Alerts: []IncidentAlert{
{
APIObject: APIObject{
ID: "1",
},
},
},
}
res, _, err := client.ManageIncidentAlerts(incidentID, input)

want := &ListAlertsResponse{
Alerts: []IncidentAlert{
{
APIObject: APIObject{
ID: "1",
},
},
},
}

if err != nil {
t.Fatal(err)
}
testEqual(t, want, res)
}