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

bug: update on call schedule to send effective_at #154

Merged
merged 1 commit into from
May 7, 2024
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
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)
}
}
Loading