Skip to content

Commit

Permalink
Merge pull request #214 from kilianw/list_incident_alerts
Browse files Browse the repository at this point in the history
List incident alerts
  • Loading branch information
Scott McAllister authored May 15, 2020
2 parents 137eeb8 + b2c3905 commit d8a84d2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
19 changes: 18 additions & 1 deletion incident.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,26 @@ type ListAlertsResponse struct {
Alerts []IncidentAlert `json:"alerts,omitempty"`
}

// ListIncidentAlertsOptions is the structure used when passing parameters to the ListIncidentAlerts API endpoint.
type ListIncidentAlertsOptions struct {
APIListObject
Statuses []string `url:"statuses,omitempty,brackets"`
SortBy string `url:"sort_by,omitempty"`
Includes []string `url:"include,omitempty,brackets"`
}

// ListIncidentAlerts lists existing alerts for the specified incident.
func (c *Client) ListIncidentAlerts(id string) (*ListAlertsResponse, error) {
resp, err := c.get("/incidents/" + id + "/alerts")
return c.ListIncidentAlertsWithOpts(id, ListIncidentAlertsOptions{})
}

// ListIncidentAlertsWithOpts lists existing alerts for the specified incident.
func (c *Client) ListIncidentAlertsWithOpts(id string, o ListIncidentAlertsOptions) (*ListAlertsResponse, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get("/incidents/" + id + "/alerts?" + v.Encode())
if err != nil {
return nil, err
}
Expand Down
36 changes: 36 additions & 0 deletions incident_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,42 @@ func TestIncident_ListIncidentAlerts(t *testing.T) {
}
testEqual(t, want, res)
}
func TestIncident_ListIncidentAlertsWithOpts(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/incidents/1/alerts", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
w.Write([]byte(`{"alerts": [{"id": "1","summary":"foo"}]}`))
})
var listObj = APIListObject{Limit: 0, Offset: 0, More: false, Total: 0}
var client = &Client{apiEndpoint: server.URL, authToken: "foo", HTTPClient: defaultHTTPClient}
id := "1"

var alertOpts = ListIncidentAlertsOptions{
APIListObject: listObj,
Includes: []string{},
}

res, err := client.ListIncidentAlertsWithOpts(id, alertOpts)

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

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

// CreateIncidentNote
func TestIncident_CreateIncidentNote(t *testing.T) {
Expand Down

0 comments on commit d8a84d2

Please sign in to comment.