Skip to content
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
4 changes: 3 additions & 1 deletion cmd/sync_ooo_to_gcal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ func (e *Event) Run(ctx context.Context) {
return
}

env, err := core.FilterByCreatedAt(respBytes, createdStartT, createdEndT)
// TODO: Revisit the naming of the time window variables now that filtering
// includes both request creation and status changes.
env, err := core.FilterByActivity(respBytes, createdStartT, createdEndT)
if err != nil {
core.Die("filter: %v", err)
}
Expand Down
1 change: 1 addition & 0 deletions core/clockify.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type ClockifyRequest struct {

Status struct {
StatusType string `json:"statusType"`
ChangedAt string `json:"changedAt"`
} `json:"status"`
Comment thread
dacut marked this conversation as resolved.
}

Expand Down
6 changes: 4 additions & 2 deletions core/clockify_fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package core
import "time"

func makeRequest(id, tz, start, end string) ClockifyRequest {
return makeRequestWithCreatedAt(
return makeRequestWithActivityTimestamps(
id,
tz,
start,
end,
time.Date(2025, 12, 1, 12, 0, 0, 0, time.UTC),
time.Date(2025, 12, 1, 12, 0, 0, 0, time.UTC),
)
}

func makeRequestWithCreatedAt(id, tz, start, end string, createdAt time.Time) ClockifyRequest {
func makeRequestWithActivityTimestamps(id, tz, start, end string, createdAt time.Time, statusChangedAt time.Time) ClockifyRequest {
var r ClockifyRequest

r.ID = id
Expand All @@ -23,6 +24,7 @@ func makeRequestWithCreatedAt(id, tz, start, end string, createdAt time.Time) Cl

r.TimeOffPeriod.Period.Start = start
r.TimeOffPeriod.Period.End = end
r.Status.ChangedAt = statusChangedAt.Format(time.RFC3339)
Comment thread
dacut marked this conversation as resolved.

return r
}
Expand Down
88 changes: 50 additions & 38 deletions core/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ type rawClockifyEnvelope struct {
Requests []json.RawMessage `json:"requests"`
}

type createdOnly struct {
type requestTimestamps struct {
CreatedAt string `json:"createdAt"`
Status struct {
ChangedAt string `json:"changedAt"`
} `json:"status"`
Comment on lines +16 to +18

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there ever a case where we won't send this in a request? If so, change this to:

Status *struct {
    ChangedAt string `json:"changedAt"`
} `json:"status,omitempty"`

Note the change to a pointer type and the omitempty directive.

If not, leave as-is.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the clockify responses I've seen, the status object is always present, but status.changedAt might be null. Because of that, I think it probably makes sense to leave Status as-is.

}
Comment thread
dacut marked this conversation as resolved.

func ParseRawClockifyEnvelope(respBytes []byte) (rawClockifyEnvelope, error) {
Expand All @@ -23,65 +26,74 @@ func ParseRawClockifyEnvelope(respBytes []byte) (rawClockifyEnvelope, error) {
return env, nil
}

// Filters raw request payloads by createdAt in [start, end].
func FilterRawRequestsByCreatedAt(
rawRequests []json.RawMessage,
start, end time.Time,
) []json.RawMessage {

filtered := make([]json.RawMessage, 0, len(rawRequests))
// ParseClockifyRequests converts valid raw request payloads into ClockifyRequest structs and skips malformed JSON entries, logging each unmarshal error via log.Printf.
func ParseClockifyRequests(rawRequests []json.RawMessage) []ClockifyRequest {
requests := make([]ClockifyRequest, 0, len(rawRequests))

for _, raw := range rawRequests {
var c createdOnly
if err := json.Unmarshal(raw, &c); err != nil {
continue // skip if no createdAt
}

ct, err := ParseFlexibleRFC3339(c.CreatedAt)
if err != nil {
var r ClockifyRequest
if err := json.Unmarshal(raw, &r); err != nil {
log.Printf("skipping bad request: %v", err)
continue
}
ct = ct.UTC()

if ct.Before(start) {
continue
}
if !ct.Before(end) { // exclusive
continue
}
requests = append(requests, r)
}

return requests
}

filtered = append(filtered, raw)
func isTimestampInWindow(
value string,
start, end time.Time,
) bool {
timestamp, err := ParseFlexibleRFC3339(value)
if err != nil {
return false
}
return filtered

timestamp = timestamp.UTC()

return !timestamp.Before(start) && timestamp.Before(end)
}

// ParseClockifyRequests converts valid raw request payloads into ClockifyRequest structs and skips malformed JSON entries, logging each unmarshal error via log.Printf.
func ParseClockifyRequests(rawRequests []json.RawMessage) []ClockifyRequest {
requests := make([]ClockifyRequest, 0, len(rawRequests))
// Filters raw requests that were created or had their status updated in [start, end).
func FilterRawRequestsByActivity(
rawRequests []json.RawMessage,
start, end time.Time,
) []json.RawMessage {
filtered := make([]json.RawMessage, 0, len(rawRequests))

for _, raw := range rawRequests {
var r ClockifyRequest
if err := json.Unmarshal(raw, &r); err != nil {
log.Printf("skipping bad request: %v", err)
var timestamps requestTimestamps
if err := json.Unmarshal(raw, &timestamps); err != nil {
continue
}

requests = append(requests, r)
createdInWindow := isTimestampInWindow(timestamps.CreatedAt, start, end)

statusUpdatedInWindow := isTimestampInWindow(timestamps.Status.ChangedAt, start, end)

if createdInWindow || statusUpdatedInWindow {
filtered = append(filtered, raw)
}
}

return requests
return filtered
}

// Filter a raw Clockify response for out-of-office requests that were created within the given time span.
func FilterByCreatedAt(respBytes []byte, createdStart, createdEnd time.Time) (ClockifyEnvelope, error) {
// FilterByActivity returns Clockify requests that were created or had their
// status updated within the given time span.
func FilterByActivity(
respBytes []byte,
start, end time.Time,
) (ClockifyEnvelope, error) {
rawEnv, err := ParseRawClockifyEnvelope(respBytes)
if err != nil {
return ClockifyEnvelope{}, err
}

filtered := FilterRawRequestsByCreatedAt(rawEnv.Requests, createdStart, createdEnd)
filtered := FilterRawRequestsByActivity(rawEnv.Requests, start, end)

return ClockifyEnvelope{
Requests: ParseClockifyRequests(filtered),
}, nil
return ClockifyEnvelope{Requests: ParseClockifyRequests(filtered)}, nil
}
141 changes: 102 additions & 39 deletions core/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,55 +38,85 @@ func mustRawMessage(t *testing.T, v any) json.RawMessage {
return json.RawMessage(b)
}

func TestFilterRawRequestsByCreatedAt(t *testing.T) {
func TestFilterRawRequestsByActivity(t *testing.T) {
rangeStart := time.Date(2025, 12, 1, 0, 0, 0, 0, time.UTC)
rangeEnd := time.Date(2025, 12, 3, 0, 0, 0, 0, time.UTC)

timeoffStart := "2025-12-10T00:00:00Z"
timeoffEnd := "2025-12-10T23:59:59Z"
timeZone := "America/New_York"

beforeStart := mustRawMessage(t, makeRequestWithCreatedAt(
"before-start",
timeZone,
timeoffStart,
timeoffEnd,
time.Date(2025, 11, 30, 23, 59, 59, 0, time.UTC),
))

atStart := mustRawMessage(t, makeRequestWithCreatedAt(
"at-start",
timeZone,
timeoffStart,
timeoffEnd,
time.Date(2025, 12, 1, 0, 0, 0, 0, time.UTC),
))

withinRange := mustRawMessage(t, makeRequestWithCreatedAt(
"within-range",
timeZone,
timeoffStart,
timeoffEnd,
time.Date(2025, 12, 2, 12, 0, 0, 0, time.UTC),
))

atEnd := mustRawMessage(t, makeRequestWithCreatedAt(
"at-end",
timeZone,
timeoffStart,
timeoffEnd,
time.Date(2025, 12, 3, 0, 0, 0, 0, time.UTC),
))
beforeRange := time.Date(2025, 11, 30, 23, 59, 59, 0, time.UTC)
atStart := time.Date(2025, 12, 1, 0, 0, 0, 0, time.UTC)
withinRange := time.Date(2025, 12, 2, 12, 0, 0, 0, time.UTC)
atEnd := time.Date(2025, 12, 3, 0, 0, 0, 0, time.UTC)

rawRequests := []json.RawMessage{
beforeStart,
atStart,
withinRange,
atEnd,
mustRawMessage(t, makeRequestWithActivityTimestamps(
"created-before-status-before",
timeZone,
timeoffStart,
timeoffEnd,
beforeRange,
beforeRange,
)),
mustRawMessage(t, makeRequestWithActivityTimestamps(
"created-at-start",
timeZone,
timeoffStart,
timeoffEnd,
atStart,
beforeRange,
)),
mustRawMessage(t, makeRequestWithActivityTimestamps(
"status-at-start",
timeZone,
timeoffStart,
timeoffEnd,
beforeRange,
atStart,
)),
mustRawMessage(t, makeRequestWithActivityTimestamps(
"created-within-range",
timeZone,
timeoffStart,
timeoffEnd,
withinRange,
beforeRange,
)),
mustRawMessage(t, makeRequestWithActivityTimestamps(
"status-within-range",
timeZone,
timeoffStart,
timeoffEnd,
beforeRange,
withinRange,
)),
mustRawMessage(t, makeRequestWithActivityTimestamps(
"created-at-end",
timeZone,
timeoffStart,
timeoffEnd,
atEnd,
beforeRange,
)),
mustRawMessage(t, makeRequestWithActivityTimestamps(
"status-at-end",
timeZone,
timeoffStart,
timeoffEnd,
beforeRange,
atEnd,
)),
}

got := FilterRawRequestsByCreatedAt(rawRequests, rangeStart, rangeEnd)
got := FilterRawRequestsByActivity(
rawRequests,
rangeStart,
rangeEnd,
)

require.Len(t, got, 2)
require.Len(t, got, 4)

var gotIDs []string
for _, raw := range got {
Expand All @@ -95,10 +125,43 @@ func TestFilterRawRequestsByCreatedAt(t *testing.T) {
gotIDs = append(gotIDs, r.ID)
}

wantIDs := []string{"at-start", "within-range"}
wantIDs := []string{
"created-at-start",
"status-at-start",
"created-within-range",
"status-within-range",
}

assert.Equal(t, wantIDs, gotIDs)
}

func TestFilterRawRequestsByActivity_AllowsNullStatusChangedAt(t *testing.T) {
rangeStart := time.Date(2025, 12, 1, 0, 0, 0, 0, time.UTC)
rangeEnd := time.Date(2025, 12, 3, 0, 0, 0, 0, time.UTC)

rawRequest := json.RawMessage(`{
"id": "status-null-created-in-range",
"createdAt": "2025-12-02T12:00:00Z",
"status": {
"changedAt": null,
"statusType": "APPROVED"
}
}`)

got := FilterRawRequestsByActivity(
[]json.RawMessage{rawRequest},
rangeStart,
rangeEnd,
)

require.Len(t, got, 1)

var request ClockifyRequest
require.NoError(t, json.Unmarshal(got[0], &request))

assert.Equal(t, "status-null-created-in-range", request.ID)
}

func TestParseClockifyRequests(t *testing.T) {
timeoffStart := "2025-12-10T00:00:00Z"
timeoffEnd := "2025-12-10T23:59:59Z"
Expand Down