Skip to content

Commit

Permalink
Merge pull request #231 from PagerDuty/incident-alerts
Browse files Browse the repository at this point in the history
Adding Get Incident Alert and Manage Incident Alert endpoints
  • Loading branch information
Scott McAllister authored Jul 9, 2020
2 parents e8ee333 + b6a682c commit ab036b7
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 16 deletions.
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"`
}

// 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)
}

0 comments on commit ab036b7

Please sign in to comment.