Skip to content

Commit

Permalink
Mark Type struct field deprecated, for incident creation + management
Browse files Browse the repository at this point in the history
We should not expect consumers to set this value, as there is only a single
value that is valid (`incident`) based on how we use the API. As such, let's
deprecate the field and just set it in the methods that need it.

This also updates the tests to assert that we're automatically setting it
appropriately.

Closes #390
  • Loading branch information
theckman committed Nov 16, 2021
1 parent d4bd360 commit 8faf322
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 5 deletions.
24 changes: 23 additions & 1 deletion incident.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ type createIncidentResponse struct {

// CreateIncidentOptions is the structure used when POSTing to the CreateIncident API endpoint.
type CreateIncidentOptions struct {
// Type is the type of API object this is.
//
// Deprecated: Because the Type field can only have the value of "incident"
// when creating an incident, the CreateIncident* methods always set this
// value to "incident". Any other value will be overwritten. This will be
// removed in v2.0.0.
Type string `json:"type"`
Title string `json:"title"`
Service *APIReference `json:"service"`
Expand All @@ -173,7 +179,15 @@ type CreateIncidentOptions struct {

// ManageIncidentsOptions is the structure used when PUTing updates to incidents to the ManageIncidents func
type ManageIncidentsOptions struct {
ID string `json:"id"`
ID string `json:"id"`

// Type is the type of API object this is.
//
// Deprecated: Because the Type field can only have the value of "incident"
// or "incident_reference" when managing an incident, the CreateIncident*
// methods always set this value to "incident" because this struct is not an
// incident_reference. Any other value will be overwritten. This will be
// removed in v2.0.0.
Type string `json:"type"`
Status string `json:"status,omitempty"`
Title string `json:"title,omitempty"`
Expand Down Expand Up @@ -206,6 +220,9 @@ func (c *Client) CreateIncidentWithContext(ctx context.Context, from string, o *
"From": from,
}

// see: https://github.com/PagerDuty/go-pagerduty/issues/390
o.Type = "incident"

d := map[string]*CreateIncidentOptions{
"incident": o,
}
Expand Down Expand Up @@ -234,6 +251,11 @@ func (c *Client) ManageIncidents(from string, incidents []ManageIncidentsOptions
// ManageIncidentsWithContext acknowledges, resolves, escalates, or reassigns
// one or more incidents.
func (c *Client) ManageIncidentsWithContext(ctx context.Context, from string, incidents []ManageIncidentsOptions) (*ListIncidentsResponse, error) {
// see: https://github.com/PagerDuty/go-pagerduty/issues/390
for i := range incidents {
incidents[i].Type = "incident"
}

d := map[string][]ManageIncidentsOptions{
"incidents": incidents,
}
Expand Down
72 changes: 68 additions & 4 deletions incident_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pagerduty
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"testing"
Expand Down Expand Up @@ -45,13 +46,38 @@ func TestIncident_Create(t *testing.T) {
defer teardown()

input := &CreateIncidentOptions{
Type: "incident",
Title: "foo",
Urgency: "low",
}

mux.HandleFunc("/incidents", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")

body, err := ioutil.ReadAll(r.Body)
testErrCheck(t, "ioutil.ReadAll", "", err)

fmt.Println(string(body))

got := make(map[string]CreateIncidentOptions)
testErrCheck(t, "json.Unmarshal()", "", json.Unmarshal(body, &got))

o, ok := got["incident"]
if !ok {
t.Fatal("map does not have an incident key")
}

if o.Type != "incident" {
t.Errorf("o.Type = %q, want %q", o.Type, "incident")
}

if o.Title != "foo" {
t.Errorf("o.Foo = %q, want %q", o.Title, "foo")
}

if o.Urgency != "low" {
t.Errorf("o.Urgency = %q, want %q", o.Urgency, "low")
}

_, _ = w.Write([]byte(`{"incident": {"title": "foo", "id": "1", "urgency": "low"}}`))
})
client := defaultTestClient(server.URL, "foo")
Expand All @@ -76,18 +102,56 @@ func TestIncident_Manage_status(t *testing.T) {
setup()
defer teardown()

wantFrom := "foo@bar.com"

mux.HandleFunc("/incidents", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")

if gotFrom := r.Header.Get("From"); gotFrom != wantFrom {
t.Errorf("From HTTP header = %q, want %q", gotFrom, wantFrom)
}

body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}

var data map[string][]ManageIncidentsOptions
testErrCheck(t, "json.Unmarshal()", "", json.Unmarshal(body, &data))

if len(data["incidents"]) == 0 {
t.Fatalf("no incidents, expect 1")
}

const (
wantType = "incident"
wantID = "1"
wantStatus = "acknowledged"
)

inc := data["incidents"][0]

if inc.Type != wantType {
t.Errorf("inc.Type = %q, want %q", inc.Type, wantType)
}

if inc.ID != wantID {
t.Errorf("inc.ID = %q, want %q", inc.ID, wantID)
}

if inc.Status != wantStatus {
t.Errorf("inc.Status = %q, want %q", inc.Status, wantStatus)
}

_, _ = w.Write([]byte(`{"incidents": [{"title": "foo", "id": "1", "status": "acknowledged"}]}`))
})

listObj := APIListObject{Limit: 0, Offset: 0, More: false, Total: 0}
client := defaultTestClient(server.URL, "foo")
from := "foo@bar.com"

input := []ManageIncidentsOptions{
{
ID: "1",
Type: "incident",
Status: "acknowledged",
},
}
Expand All @@ -104,7 +168,7 @@ func TestIncident_Manage_status(t *testing.T) {
},
},
}
res, err := client.ManageIncidents(from, input)
res, err := client.ManageIncidents(wantFrom, input)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 8faf322

Please sign in to comment.