Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the Incident endpoint for ResponderRequest #196

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 60 additions & 1 deletion incident.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,4 +380,63 @@ type ListAlertResponse struct {
Alerts []Alert `json:"alerts,omitempty"`
}

/* TODO: Manage Alerts, Get Alert, Create Status Updates, Create Responder Request, */
// IncidentResponders contains details about responders to an incident.
type IncidentResponders struct {
State string `json:"state"`
User APIObject `json:"user"`
Incident APIObject `json:"incident"`
UpdatedAt string `json:"updated_at"`
Message string `json:"message"`
Requester APIObject `json:"requester"`
RequestedAt string `json:"requested_at"`
}

// ResponderRequestResponse
type ResponderRequestResponse struct {
ResponderRequest ResponderRequest `json:"responder_request"`
}

// ResponderRequestTarget specifies an individual target for the responder request.
type ResponderRequestTarget struct {
APIObject
Responders IncidentResponders `json:"incident_responders"`
}

// ResponderRequestTargets is a wrapper for a ResponderRequestTarget.
type ResponderRequestTargets struct {
Target ResponderRequestTarget `json:"responder_request_target"`
}

// ResponderRequestOptions defines the input options for the Create Responder function.
type ResponderRequestOptions struct {
From string `json:"-"`
Message string `json:"message"`
RequesterID string `json:"request_id"`
Targets []ResponderRequestTarget `json:"responder_request_targets"`
}

// ResponderRequest contains the API structure for an incident responder request.
type ResponderRequest struct {
Incident Incident `json:"incident"`
Requester User `json:"requester,omitempty"`
RequestedAt string `json:"request_at,omitempty"`
Message string `json:"message,omitempty"`
Targets ResponderRequestTargets `json:"responder_request_targets"`
}

// ResponderRequest will submit a request to have a responder join an incident.
func (c *Client) ResponderRequest(id string, o ResponderRequestOptions) (*ResponderRequestResponse, error) {
headers := make(map[string]string)
headers["From"] = o.From

resp, err := c.post("/incidents/"+id+"/responder_requests", o, &headers)
if err != nil {
return nil, err
}

result := &ResponderRequestResponse{}
err = json.NewDecoder(resp.Body).Decode(result)
return result, err
}

/* TODO: Manage Alerts, Get Alert, Create Status Updates */
70 changes: 70 additions & 0 deletions incident_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,73 @@ func TestIncident_ListLogEntries(t *testing.T) {
}
testEqual(t, want, res)
}

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

id := "1"
mux.HandleFunc("/incidents/"+id+"/responder_requests", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
w.Write([]byte(`{
"responder_request": {
"requester": {
"id": "PL1JMK5",
"type": "user_reference"
},
"message": "Help",
"responder_request_targets": {
"responder_request_target": {
"id": "PJ25ZYX",
"type": "user_reference",
"incident_responders": {
"state": "pending",
"user": {
"id": "PJ25ZYX"
}
}
}
}
}
}`))

})
var client = &Client{apiEndpoint: server.URL, authToken: "foo", HTTPClient: defaultHTTPClient}
from := "[email protected]"

r := ResponderRequestTarget{}
r.ID = "PJ25ZYX"
r.Type = "user_reference"

input := ResponderRequestOptions{
From: from,
Message: "help",
RequesterID: "PL1JMK5",
Targets: []ResponderRequestTarget{r},
}

user := User{}
user.ID = "PL1JMK5"
user.Type = "user_reference"

target := ResponderRequestTarget{}
target.ID = "PJ25ZYX"
target.Type = "user_reference"
target.Responders.State = "pending"
target.Responders.User.ID = "PJ25ZYX"

want := &ResponderRequestResponse{
ResponderRequest: ResponderRequest{
Incident: Incident{},
Requester: user,
Message: "Help",
Targets: ResponderRequestTargets{target},
},
}
res, err := client.ResponderRequest(id, input)

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