Skip to content

Commit

Permalink
Add missing required parameeter to ManageIncidentAlerts
Browse files Browse the repository at this point in the history
When making a call to the REST API[1] endpoint to manage alerts for incidents,
an HTTP header ("From") indicating who made the request is required. As a
result, these methods may not have ever worked properly.

So while we're breaking the API, let's remove the deprecated non-context version
to have fewer breaking changes in v2.

Closes #338

[1] https://developer.pagerduty.com/api-reference/b3A6Mjc0ODE0NA-manage-alerts
  • Loading branch information
theckman committed Nov 9, 2021
1 parent ad98290 commit 52e8c50
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
13 changes: 5 additions & 8 deletions incident.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,15 +632,12 @@ func (c *Client) GetIncidentAlertWithContext(ctx context.Context, incidentID, al
}

// ManageIncidentAlerts allows you to manage the alerts of an incident.
//
// Deprecated: Use ManageIncidentAlertsWithContext instead.
func (c *Client) ManageIncidentAlerts(incidentID string, alerts *IncidentAlertList) (*ListAlertsResponse, error) {
return c.ManageIncidentAlertsWithContext(context.Background(), incidentID, alerts)
}
func (c *Client) ManageIncidentAlerts(ctx context.Context, incidentID, from string, alerts *IncidentAlertList) (*ListAlertsResponse, error) {
h := map[string]string{
"From": from,
}

// ManageIncidentAlertsWithContext allows you to manage the alerts of an incident.
func (c *Client) ManageIncidentAlertsWithContext(ctx context.Context, incidentID string, alerts *IncidentAlertList) (*ListAlertsResponse, error) {
resp, err := c.put(ctx, "/incidents/"+incidentID+"/alerts/", alerts, nil)
resp, err := c.put(ctx, "/incidents/"+incidentID+"/alerts/", alerts, h)
if err != nil {
return nil, err
}
Expand Down
10 changes: 8 additions & 2 deletions incident_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pagerduty

import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -708,12 +709,17 @@ func TestIncident_GetAlert(t *testing.T) {
testEqual(t, want, res)
}

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

from := "pagerduty@example.com"

mux.HandleFunc("/incidents/1/alerts/", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
if hdr := r.Header.Get("From"); hdr != "pagerduty@example.com" {
t.Errorf("From header = %q, want %q", hdr, from)
}
_, _ = w.Write([]byte(`{"alerts": [{"id": "1"}]}`))
})

Expand All @@ -730,7 +736,7 @@ func TestIncident_ManageAlerts(t *testing.T) {
},
},
}
res, err := client.ManageIncidentAlerts(incidentID, input)
res, err := client.ManageIncidentAlerts(context.Background(), incidentID, from, input)

want := &ListAlertsResponse{
Alerts: []IncidentAlert{
Expand Down

0 comments on commit 52e8c50

Please sign in to comment.