Skip to content

Commit

Permalink
client: update on call schedule to send effective_at (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonehusin authored May 7, 2024
1 parent 1ba3b72 commit fcc96c8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
5 changes: 5 additions & 0 deletions firehydrant/on_call_schedules.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type UpdateOnCallScheduleRequest struct {
Name string `json:"name"`
Description string `json:"description"`
MemberIDs []string `json:"member_ids,omitempty"`
EffectiveAt string `json:"effective_at,omitempty"`
}

type OnCallScheduleRestriction struct {
Expand Down Expand Up @@ -115,6 +116,10 @@ func (c *RESTOnCallSchedulesClient) Update(ctx context.Context, teamID, id strin
onCallScheduleResponse := &OnCallScheduleResponse{}
apiError := &APIError{}

if updateReq.EffectiveAt == "" {
updateReq.EffectiveAt = time.Now().Format(time.RFC3339)
}

response, err := c.restClient().Patch(fmt.Sprintf("teams/%s/on_call_schedules/%s", teamID, id)).BodyJSON(updateReq).Receive(onCallScheduleResponse, apiError)
if err != nil {
return nil, errors.Wrap(err, "could not update on-call schedule")
Expand Down
47 changes: 47 additions & 0 deletions firehydrant/on_call_schedules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package firehydrant

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"
)

func expectedOnCallScheduleResponse() *OnCallScheduleResponse {
Expand Down Expand Up @@ -97,3 +99,48 @@ func TestOnCallSchedulesGet(t *testing.T) {
t.Fatalf("response mismatch: expected '%+v', got: '%+v'", expectedResponse, res)
}
}

func TestOnCallScheduleUpdateHasEffectiveAt(t *testing.T) {
var requestPath string
var requestBody map[string]any
h := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
requestPath = req.URL.Path
if err := json.NewDecoder(req.Body).Decode(&requestBody); err != nil {
t.Fatalf("error unmarshalling request body: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
}
w.Write([]byte(expectedOnCallScheduleResponseJSON()))
})

ts := httptest.NewServer(h)
defer ts.Close()

c, err := NewRestClient("test-token-very-authorized", WithBaseURL(ts.URL))
if err != nil {
t.Fatalf("Received error initializing API client: %s", err.Error())
return
}
updateReq := UpdateOnCallScheduleRequest{
Name: "A pleasant on-call schedule",
Description: "Managed by Terraform. Contact @platform-eng for changes.",
MemberIDs: []string{"77779528-690b-4161-84ca-312e932c626e"},
}
if _, err := c.OnCallSchedules().Update(context.TODO(), "team-id", "schedule-id", updateReq); err != nil {
t.Fatalf("error retrieving on-call schedule: %s", err.Error())
}

if expected := "/teams/team-id/on_call_schedules/schedule-id"; expected != requestPath {
t.Fatalf("request path mismatch: expected '%s', got: '%s'", expected, requestPath)
}
effectiveAt := requestBody["effective_at"].(string)
if effectiveAt == "" {
t.Fatalf("expected effective_at to be set")
}
e, err := time.Parse(time.RFC3339, effectiveAt)
if err != nil {
t.Fatalf("error parsing effective_at: %s", err.Error())
}
if dur := time.Since(e); dur > time.Minute {
t.Fatalf("expected effective_at to be now-ish, found %s ago", dur)
}
}

0 comments on commit fcc96c8

Please sign in to comment.