From fc44c1c6fd1f77c0fc025ac402bfba9630d83104 Mon Sep 17 00:00:00 2001 From: Scott McAllister Date: Thu, 25 Jun 2020 17:49:08 -0700 Subject: [PATCH 1/2] adding Get Incident Alert and Manage Incident Alert endpoints --- incident.go | 59 +++++++++++++++++++++++++++++------------ incident_test.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 16 deletions(-) diff --git a/incident.go b/incident.go index d37ba2a5..a8955113 100644 --- a/incident.go +++ b/incident.go @@ -3,6 +3,7 @@ package pagerduty import ( "encoding/json" "fmt" + "net/http" "github.com/google/go-querystring/query" ) @@ -80,6 +81,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. @@ -105,6 +107,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) @@ -263,6 +271,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 @@ -386,21 +404,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"` @@ -460,4 +463,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 */ diff --git a/incident_test.go b/incident_test.go index 5eac9dac..3444efa6 100644 --- a/incident_test.go +++ b/incident_test.go @@ -597,3 +597,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) +} From b6a682c4a68cbdc6c69f3f23757f6c07fa30718e Mon Sep 17 00:00:00 2001 From: Scott McAllister Date: Fri, 26 Jun 2020 15:46:57 -0700 Subject: [PATCH 2/2] restoring firsttriggerlogentry --- incident.go | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/incident.go b/incident.go index ee2d2db2..7739b088 100644 --- a/incident.go +++ b/incident.go @@ -65,29 +65,29 @@ type FirstTriggerLogEntry struct { // Incident is a normalized, de-duplicated event generated by a PagerDuty integration. type Incident struct { APIObject - IncidentNumber uint `json:"incident_number,omitempty"` - Title string `json:"title,omitempty"` - Description string `json:"description,omitempty"` - CreatedAt string `json:"created_at,omitempty"` - PendingActions []PendingAction `json:"pending_actions,omitempty"` - IncidentKey string `json:"incident_key,omitempty"` - Service APIObject `json:"service,omitempty"` - Assignments []Assignment `json:"assignments,omitempty"` - Acknowledgements []Acknowledgement `json:"acknowledgements,omitempty"` - LastStatusChangeAt string `json:"last_status_change_at,omitempty"` - LastStatusChangeBy APIObject `json:"last_status_change_by,omitempty"` - FirstTriggerLogEntry APIObject `json:"first_trigger_log_entry,omitempty"` - EscalationPolicy APIObject `json:"escalation_policy,omitempty"` - Teams []APIObject `json:"teams,omitempty"` - Priority *Priority `json:"priority,omitempty"` - Urgency string `json:"urgency,omitempty"` - Status string `json:"status,omitempty"` - Id string `json:"id,omitempty"` - ResolveReason ResolveReason `json:"resolve_reason,omitempty"` - AlertCounts AlertCounts `json:"alert_counts,omitempty"` - Body IncidentBody `json:"body,omitempty"` - IsMergeable bool `json:"is_mergeable,omitempty"` - ConferenceBridge *ConferenceBridge `json:"conference_bridge,omitempty"` + IncidentNumber uint `json:"incident_number,omitempty"` + Title string `json:"title,omitempty"` + Description string `json:"description,omitempty"` + CreatedAt string `json:"created_at,omitempty"` + PendingActions []PendingAction `json:"pending_actions,omitempty"` + IncidentKey string `json:"incident_key,omitempty"` + Service APIObject `json:"service,omitempty"` + Assignments []Assignment `json:"assignments,omitempty"` + Acknowledgements []Acknowledgement `json:"acknowledgements,omitempty"` + LastStatusChangeAt string `json:"last_status_change_at,omitempty"` + LastStatusChangeBy APIObject `json:"last_status_change_by,omitempty"` + FirstTriggerLogEntry FirstTriggerLogEntry `json:"first_trigger_log_entry,omitempty"` + EscalationPolicy APIObject `json:"escalation_policy,omitempty"` + Teams []APIObject `json:"teams,omitempty"` + Priority *Priority `json:"priority,omitempty"` + Urgency string `json:"urgency,omitempty"` + Status string `json:"status,omitempty"` + Id string `json:"id,omitempty"` + ResolveReason ResolveReason `json:"resolve_reason,omitempty"` + 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.