Repository navigation
[Fix] Validate the resolution time against the event start date - #129
Conversation
There was a problem hiding this comment.
Summary
Adds a 400 in checkPatchData (internal/api/v2/v2.go:1281-1286) rejecting resolved patches whose update_date is earlier than the stored start_date, with a new sentinel error (internal/api/errors/incident.go:42-44) and table-driven tests (internal/api/v2/v2_test.go:250-298). The logic itself looks correct:
- Comparison uses
time.Time.Before, which compares instants (timezone-safe), and the handler normalizes to UTC atv2.go:2237. - Equal timestamps are accepted, matching the stated intent; the nil-
StartDatecase is skipped; other statuses are untouched — all covered by tests. - The check is scoped to
event.TypeIncident, so maintenance/info events are unaffected.
Findings
Critical
- Failing CI check:
go-test-accfailed on the head commit (fd13050). All other checks (build,go-test, lint, CodeQL, GitGuardian) passed, so this is not a compile/unit issue — it is the acceptance suite. I cannot run the suite in this environment, so I can't tell whether the failure is caused by this PR (e.g., an acceptance test that PATCHes a resolved incident with anupdate_datebeforestart_datenow gets a 400 instead of 200) or is pre-existing/flaky onmain. This must be resolved before merge: re-rungo-test-acc, and if it is caused by the new validation, update the affected acceptance test to expect 400 (or to use anupdate_date≥start_date).
Suggestion (non-blocking)
internal/api/v2/v2.go:1281-1286: the new check runs beforecheckPatchDataForIncident, so re-patching an already-resolved incident with anotherresolvedstatus and a pre-startupdate_datereturnsErrIncidentPatchResolvedBeforeStartinstead of the existing closed-status error. Harmless (both are 400s with clear messages), but if a distinct "closed incident" message is preferred for that case, move the check insidecheckPatchDataForIncidentafter the closed-status guard.- The error message says "update_date must not be earlier than start_date" — accurate for the request field; no change needed, just noting it names the request field while the stored counterpart is
end_date(consistent with the comment, so fine).
Verdict
request_changes — the code change is correct and well-tested, but the failing go-test-acc check on the head commit must be re-run and explained (or the acceptance test updated) before this can merge.
There was a problem hiding this comment.
Summary
Fixes #115: checkPatchData now rejects patches whose end_date precedes the stored start_date (all event types) and whose update_date precedes start_date for a resolved incident, preventing inverted availability windows. Unit tests cover acceptance of equal timestamps, rejection of earlier ones, and the "no start date → no check" case. CI: build, check (golangci-lint), go-test, ent-codegen, GitGuardian all green; go-test-acc and the Go CodeQL analysis were still in progress at review time.
Overall the logic is correct and well-tested; two items below are worth addressing/confirming.
Findings
Warning — possible nil dereference in the integration-test cleanup
tests/v2_system_incident_test.go:872 — the new line
endDate := inc.StartDate.Add(time.Millisecond).UTC()dereferences inc.StartDate for every incident with EndDate == nil. The db.Incident model uses a pointer *time.Time (the API code in checkPatchData explicitly guards with stored.StartDate != nil), so a legacy/open record without a start date turns this into a test panic instead of the previous, always-safe time.Now().Add(-time.Hour). If any such row can exist in the integration DB, the test suite will crash. Fix: guard it, e.g.
if inc.StartDate == nil {
endDate := time.Now().UTC()
} else {
endDate = inc.StartDate.Add(time.Millisecond).UTC()
}(or require.NotNil(t, inc.StartDate) if the test data is guaranteed to always have one).
Suggestion — end_date check compares against the stored start date
internal/api/v2/v2.go:1279 — the new check compares incoming.EndDate with *stored.StartDate. If a patch can also modify start_date (the existing ErrIncidentPatchOpenedStartDate error suggests start-date changes are allowed in some closed-incident flows), then a patch that moves the start date forward past the incoming end date would be rejected even though the final stored window would be valid. If start_date is never writable in the same request, ignore this; otherwise compare against incoming.StartDate when it is present.
Suggestion — resolved check depends on UpdateDate never being zero
internal/api/v2/v2.go:1287 — UpdateDate is a non-pointer time.Time, and the zero time is always before start_date. The guard currently only holds because update_date is binding:"required" in PatchIncidentData (v2.go:1120). That's fine for the HTTP path, but if checkPatchData is ever called from a non-HTTP caller with a partially-filled struct, resolved patches would be falsely rejected. Consider documenting the precondition or making UpdateDate a *time.Time (larger refactor, optional).
Nothing else blocking: the equal-timestamps-accepted behavior and the type-scoped checks match the stated intent, and the new errors are surfaced with clear messages.
There was a problem hiding this comment.
Summary
This PR adds two validations to checkPatchData (internal/api/v2/v2.go):
- Reject any
end_dateearlier than the storedstart_date(all event types). - For
incidenttype, reject a patch toresolvedstatus whoseupdate_dateis earlier than the storedstart_date(sinceupdateFieldsstores the resolution time asend_date).
It also fixes the system-incident test cleanup to close seed incidents at start_date + 1ms instead of an hour in the past. New tests in internal/api/v2/v2_test.go cover both paths, and CI (build, go-test, go-test-acc, CodeQL) is green.
The end_date check and the cleanup change look correct and well-tested.
Findings
⚠️ Warning — update_date check may misfire when update_date is omitted
internal/api/v2/v2.go (in checkPatchData, incident branch):
if incoming.Status == event.IncidentResolved && stored.StartDate != nil &&
incoming.UpdateDate.Before(*stored.StartDate) {
return apiErrors.ErrIncidentPatchResolvedBeforeStart
}PatchIncidentData.EndDate is a *time.Time (so it has an explicit nil guard), but UpdateDate appears to be a value time.Time — the new tests construct it as PatchIncidentData{Status: tc.status, UpdateDate: tc.updateDate} with a plain time.Time, and there is no != nil guard here. If a caller transitions an incident to resolved without supplying update_date, UpdateDate will be the zero time (0001-01-01T00:00:00Z), which is before any real start_date, so the patch would be rejected with ErrIncidentPatchResolvedBeforeStart.
Why it matters: this turns a legitimate "resolve now" patch (relying on the server/updateFields to fill in the resolution time) into a spurious 400. The end_date check explicitly handles the "field absent" case; this one does not.
Fix: guard the same way as the end_date check — either make UpdateDate a pointer and require non-nil before comparing, or skip the check when UpdateDate is zero. Confirm whether update_date is actually mandatory on a resolve transition; if it is guaranteed to be present by the API contract (and that guarantee is enforced elsewhere), note that in a comment, but a defensive nil/zero guard is safer.
Suggestion
The end_date guard and the resolved-incident guard both re-check stored.StartDate != nil inline. That's fine as written, just flagging for consistency if you refactor.
Verdict
approve — the change is correct, tested, and CI is green. Please verify the update_date zero-value concern above before relying on it in production; if update_date can be omitted on a resolve, add a nil/zero guard (that would warrant request_changes if confirmed to break a real code path).
There was a problem hiding this comment.
Summary
The fix is sound: checkPatchData now rejects end_date values earlier than the stored start_date for all event types, and rejects resolved-incident patches whose update_date precedes the start date, which is exactly the inverted-availability-window bug in #115 (updateFields stores UpdateDate as EndDate for resolved incidents — v2.go:1351-1352). Equal timestamps stay valid, no clamping, and both paths are covered by new unit tests. CI is fully green (build, go-test, go-test-acc, lint, CodeQL).
One edge case remains, reported below as a warning.
Warnings
1. The end_date check uses the old start date when a patch moves start_date too
File: internal/api/v2/v2.go:1279
The new check compares incoming.EndDate only against stored.StartDate. But patches are allowed to change start_date as well (status incident_changed per checkPatchDataForIncident, v2.go:1303, and updateFields overwrites stored.StartDate at v2.go:1324-1325). So a patch that sets start_date = Feb 1 and end_date = Jan 15 on an event stored with start_date = Jan 1 passes the new check (Jan 15 ≥ Jan 1) and still persists an inverted window — the exact bug this PR is meant to close.
Fix: compare against the effective start date:
effStart := stored.StartDate
if incoming.StartDate != nil {
effStart = incoming.StartDate
}
if incoming.EndDate != nil && effStart != nil && incoming.EndDate.Before(*effStart) {
return apiErrors.ErrIncidentPatchEndDateBeforeStart
}Suggestions
tests/v2_system_incident_test.go:872—endDate := inc.StartDate.Add(time.Millisecond).UTC()dereferences a pointer.db.Incident.StartDateis nullable (the new check at v2.go:1279 explicitly guardsstored.StartDate != nil), so if an event without a start date ever reachescleanupOpenIncidents, the test panics instead of failing cleanly. Guard the nil case (fall back totime.Now().UTC()or skip closing that event).internal/api/errors/incident.go:42— the message "update_date must not be earlier than start_date for a resolved event" reads generic, but the check (v2.go:1286) only applies to incident type. Minor wording nit.- Robustness — the resolved-incident check relies on
UpdateDatebeing non-zero. That holds today because the field isbinding:"required"(v2.go:1120), but a zerotime.Timewould be treated as "before start" and rejected; a!incoming.UpdateDate.IsZero()guard would makecheckPatchDatasafe for any future non-HTTP caller.
Verdict
Approve: the primary fix is correct and well-tested; the warning above is a secondary edge case worth a follow-up (or a small fix in this PR).
…d-update-date # Conflicts: # internal/api/v2/v2.go # internal/api/v2/v2_test.go
There was a problem hiding this comment.
Review: [Fix] Validate the resolution time against the event start date
Summary. Adds two validations to checkPatchData (internal/api/v2/v2.go): a generic rejection of end_date < stored.start_date, and a rejection of a resolved-incident patch whose update_date < stored.start_date. The system-incident test cleanup now closes seed incidents at start_date + 1ms instead of now − 1h. The core logic is sound:
UpdateDateistime.Timewithbinding:"required"(v2.go:1175), so the newincoming.UpdateDate.Before(*stored.StartDate)check cannot trip on a zero time.time.Beforecompares instants, so the UTC normalization atv2.go:2313keeps the comparison safe regardless of input timezone.updateFieldsstoringstored.EndDate = &income.UpdateDate(lines 1419/1425) confirms the comment about the resolution time becomingend_date— the guard covers the exact inversion path.- Equal timestamps remain accepted, matching the PR description.
Findings
Suggestion — compare against the effective start date, not only the stored one. internal/api/v2/v2.go:1340 and :1354 both compare the incoming end_date/update_date against stored.StartDate. If a patch can also move start_date (the presence of ErrIncidentPatchOpenedStartDate implies start_date is patchable on closed events), a patch that shifts start_date later while keeping an end_date/update_date that only satisfies the old start date would still invert the window after updateFields. If start_date is mutable in patches, both checks should compare against the effective start (incoming start if provided and valid, else stored). If start_date is in fact never patchable, no change is needed — please confirm.
Suggestion — test cleanup assumption. tests/v2_system_incident_test.go:872: inc.StartDate.Add(time.Millisecond) assumes StartDate is always populated for open seed incidents. This is true for API-created events (start_date is binding:"required"), but if any fixture ever creates an incident without a start date, EndDate would silently become 1970-01-01T00:00:00.001. A require.NotZero(t, inc.StartDate) (or the old now-1h fallback) would make the test self-documenting.
Note — CI. build, go-test, go-test-acc and the check/analysis jobs were still in_progress at review time; the PR description claims go build/vet/test pass, which I could not independently verify (no build tooling in this environment).
Verdict
approve — no correctness, security, or data-loss issues found in the diff; the two suggestions above are worth confirming but do not block the change.
updateFieldsstores a caller-suppliedend_datefor maintenance and info events, and the resolution time for a resolved incident, without checking it againststart_date, so a patch could invert the availability window.checkPatchDatanow rejects anyend_dateearlier than the storedstart_dateon every type, and a resolved patch whoseupdate_dateprecedes it; equal timestamps stay valid and nothing is clamped. The system-incident cleanup closed seed incidents an hour in the past and now closes them after their start date, matching the other integration cleanups.Verified with
go build ./... && go vet ./... && go test ./internal/....Closes #115