Skip to content

[Fix] Validate the effective interval when patching an incident - #137

Merged
Aloento merged 3 commits into
mainfrom
fix/issue-103-event-date-validation
Dec 1, 2026
Merged

Aloento merged 3 commits into
mainfrom
fix/issue-103-event-date-validation

Conversation

@Aloento

@Aloento Aloento commented Dec 1, 2026

Copy link
Copy Markdown
Member

checkPatchData compared an incoming end_date with the stored start_date only, so a closed incident patched with changed could move both dates at once and still end up with end_date <= start_date, which produces negative downtime in /v2/availability. The incident path now validates the window the patch leaves behind: start_date and end_date fall back to the stored values, update_date is the end of a resolved event, and equal dates are rejected. Maintenance and info patches keep the existing per-field check.

Verification: gofmt -l internal/api/v2, go test ./internal/... -count 1. Integration tests were not run because Docker is not available on the development host.

Closes #103

An incident patch compared the incoming end_date with the stored start_date
only, so a closed incident moving both dates at once could still end up with
end_date <= start_date. Validate the window the patch leaves behind, and keep
update_date as the end of a resolved event.
A resolution exactly at the effective start left a zero-length window that
the availability query drops. Require update_date to be later than the
start, and word both patch interval errors to say so.
ecosquad-autoreview[bot]
ecosquad-autoreview Bot previously approved these changes Dec 1, 2026

@ecosquad-autoreview ecosquad-autoreview Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Summary

Fixes #103: checkPatchData now validates the effective interval a patch leaves behind for incidents, instead of comparing only the incoming end_date against the stored start_date. A new checkIncidentPatchInterval (v2.go:1364) falls back to stored values for each date, treats update_date as the end of a resolved event, and rejects inverted or zero-length windows. Tests are thorough: unit cases for closed/open incidents, edge cases (equal dates, both dates moved at once, no dates), and three HTTP handler tests asserting the exact 400 error payloads.

The change is correct as I can trace it:

  • Resolved branch (v2.go:1370-1377): updateFields unconditionally stores stored.EndDate = &income.UpdateDate for resolved incidents (v2.go:1446-1447), so checking update_date against the effective start is exactly the right validation; an incoming end_date on a resolved patch is ignored here, consistent with it being discarded.
  • Non-resolved branch (v2.go:1379-1389): end := stored.EndDate falls back correctly; nil start or nil end (open incident) skips the check, which is fine.
  • UpdateDate is binding:"required" (v2.go:1175), so the resolved branch can't hit the zero-time case.
  • The pre-existing per-field check for maintenance/info (v2.go:1345-1347) is untouched, as described.
  • Ordering is safe: checkIncidentPatchInterval runs before checkPatchDataForIncident, and both return apiErrors errors that the handler maps to 400.

Findings

Critical

None found.

Warning

None — the code works as intended.

Suggestions

  1. Maintenance/info patches can still invert the window (out of scope, but worth a follow-up) — v2.go:1345-1347 still only compares incoming.EndDate with the stored StartDate. A closed maintenance patched with changed can move start_date after end_date in one request, producing the same negative-downtime class of bug this PR fixes for incidents. The PR intentionally scopes to incidents; consider a follow-up issue applying the same effective-window check to other types.

  2. Equal dates are now rejected even for patches that touch no dates — v2.go:1387 uses !end.After(*start), so an incident whose stored window already has end_date == start_date will have a status-only changed patch rejected. This is stricter than the old behavior (which only rejected end < start for the incoming field) and may reject patches against pre-existing zero-length windows. The PR description says "equal dates are rejected," so this appears intentional — just flagging it in case any such rows exist.

  3. CI not yet green at review time — go-test, build, golangci-lint and the analyzer runs were still queued when I reviewed, so I could not confirm the new tests pass in CI. The code is consistent with the existing test helpers (initRouterWithStoredEvent, error messages), so I expect it to be fine, but worth confirming the checks land green before merging.

Verdict

Approve — the fix is correct and well-tested; the items above are follow-ups, not blockers.

@ecosquad-autoreview ecosquad-autoreview Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Summary

The PR fixes a real gap: checkPatchData previously compared an incoming end_date only against the stored start_date, so a closed incident patched with changed could move both dates at once and leave end_date <= start_date, producing negative downtime in /v2/availability. The new checkIncidentPatchInterval (internal/api/v2/v2.go:1364) validates the effective window left behind by the patch, with fallbacks to stored values, and rejects equal dates for resolved events. The error-message wording change in internal/api/errors/incident.go matches the new semantics. Tests are thorough for the interval logic (internal/api/v2/v2_patch_interval_test.go), including the previously-bug "both dates edited into an inverted interval" case.

No critical issues found. Verdict: approve, with the notes below.

Findings

Suggestion / open questions

  1. Strict inequality is a behavior change for clients sending equal timestamps (internal/api/v2/v2.go:1375, 1387). !end.After(*start) / !incoming.UpdateDate.After(*start) now reject end_date == start_date and update_date == start_date, which were previously accepted. This is intentional and documented, but it turns zero-duration incidents (start and resolution in the same second) into 400s. Worth confirming no frontend or integration flow can legitimately produce same-second windows; if so, they will start failing at patch time.

  2. Verify updateFields only assigns end_date = update_date for resolved patches (internal/api/v2/v2.go:1443–1447). checkIncidentPatchInterval's resolved branch (line 1369–1379) validates only update_date > start and trusts the comment that "updateFields stores the resolution time as end_date". If stored.EndDate = &income.UpdateDate is also applied for a changed patch on a closed incident, the interval check (which uses stored.EndDate / incoming end_date) would miss an inverted window introduced via update_date. I could not confirm the surrounding condition in updateFields, so please double-check that branch covers only resolved status.

  3. Resolved branch skips the check when start == nil (internal/api/v2/v2.go:1375). start_date is required on create, so this should be unreachable for incidents, but the silent skip means a malformed stored record with start_date IS NULL would pass any resolution. A defensive rejection (or a log) would be safer than silently accepting.

  4. PatchIncidentData.UpdateDate is dereferenced unconditionally in the resolved branch. If update_date is not binding-required in PatchIncidentData, a client omitting it would send zero time and be rejected with "must be after start_date" — correct outcome, confusing message. Confirm the field is binding:"required".

Process note

CI was still in progress at review time (go-test, go-test-acc, build all in_progress); the author's note that integration tests were not run locally (no Docker) is fine, but the head commit's CI results should be green before merge.

The patch interval error is shared with maintenance and info, which still
accept end_date equal to start_date. Keep their message unchanged and give
incidents the stricter wording.

@ecosquad-autoreview ecosquad-autoreview Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Summary

The fix is well scoped and the logic in checkIncidentPatchInterval (internal/api/v2/v2.go:1361-1388) is correct for the reported case: it validates the effective window (incoming ?? stored for each date) instead of only the incoming end_date against the stored start_date, so a changed patch on a closed incident can no longer invert the interval and produce negative downtime in /v2/availability. The resolved branch correctly treats update_date as the effective end and now rejects update_date == start_date (intentional, tests updated in v2_test.go:437). The new tests cover the closed-incident date edits, open-incident resolution ordering, and two full handler round-trips.

No blocking issues found in the diff. Findings below are minor.

Findings

Suggestion — same bug class remains for maintenance/info patches

internal/api/v2/v2.go:1348-1350 (the old per-field check kept for non-incident types). If a maintenance event patch can move start_date past the (stored or incoming) end_date, the same inversion can be persisted, since only incoming.EndDate < stored.StartDate is checked. The PR description says this is deliberately out of scope; if maintenance patches can edit both dates, the same effective window check should be extended to TypeMaintenance in a follow-up.

Suggestion — misleading error when update_date is omitted on a resolved patch

internal/api/v2/v2/v2.go:1370-1376. If a patch with status: resolved omits update_date, the zero-value time fails !incoming.UpdateDate.After(*start) and the client gets "update_date must be after start_date" even though the field was absent. I could not verify whether the handler or binding makes update_date required for resolved patches (PatchIncidentData binding wasn't visible in the reviewed sections); if it is not required, add an explicit "update_date is required for a resolved event" check before the interval comparison.

Note — verification

  • CI go-test, build, and lint runs are still queued for the head commit, so the unit tests have not yet run in CI; the PR notes integration tests were not run (no Docker). Please make sure go-test passes before merging.
  • The resolved branch of checkIncidentPatchInterval returns nil before any end-date check, trusting that updateFields (v2.go:1417) stores update_date as end_date for resolved events. This assumption predates the change, but if updateFields is ever changed, this check silently stops applying — the existing comment documents it, which is good.

Verdict

Approve: the change fixes the reported inversion bug correctly, the error semantics change (equal dates rejected) is intentional and tested, and the remaining items are follow-ups.

@Aloento
Aloento merged commit ae8a0fd into main Dec 1, 2026
14 checks passed
@Aloento
Aloento deleted the fix/issue-103-event-date-validation branch December 1, 2026 12:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[events] Incident patch accepts end_date before start_date

1 participant