Skip to content

fix(audit-log): parse occurredAt from --file into a Date - #271

Open
Mohith26 wants to merge 1 commit into
workos:mainfrom
Mohith26:fix/audit-log-file-occurred-at
Open

Mohith26 wants to merge 1 commit into
workos:mainfrom
Mohith26:fix/audit-log-file-occurred-at

Conversation

@Mohith26

@Mohith26 Mohith26 commented Nov 3, 2026

Copy link
Copy Markdown

Fixes #254.

workos audit-log create-event <org> --file event.json passed the parsed JSON straight to the SDK, so occurredAt stayed a string and serializeCreateAuditLogEventOptions failed on event.occurredAt.toISOString() before any request went out. The flag path already turned --occurred-at into a Date.

This adds a small parseOccurredAt helper used by both paths:

  • a string (or number) from the file or --occurred-at becomes a Date
  • missing / empty defaults to now, same as the flag path today
  • an unparseable value throws Invalid occurredAt: ... instead of reaching the SDK and failing with a RangeError from toISOString()

The existing "creates event from file" test asserted the string was passed through unchanged, so I updated it to expect a Date and added tests for a file without occurredAt and for invalid values from the file and the flag.

Testing:

  • vitest run src/commands/audit-log.spec.ts: the 4 new/updated tests fail on main and pass with the change; full vitest run is green (185 files, 3499 tests)
  • tsc --noEmit, oxlint and oxfmt --check pass
  • Ran runAuditLogCreateEvent with the JSON file from the issue against a local HTTP server: on main it fails with event.occurredAt.toISOString is not a function; with the change the server receives "occurred_at":"2026-09-28T08:53:08.000Z"

AI-assisted; I reproduced the bug and ran the checks above myself.

@greptile-apps

greptile-apps Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 4/5

[Low impact] The change stays within audit-event creation in the CLI.

The PR appears safe to merge, with non-blocking improvements to timestamp type checks and tests.

Findings

  1. P2 Booleans become unrelated event times ▶
  2. P2 Tests miss time and message ▶
Fix with agent prompt
### Issue 1
src/commands/audit-log.ts:28-29
`parseOccurredAt` accepts boolean values from an event file. The type cast does not change the value at runtime, so `false` becomes the Unix epoch and `true` becomes one millisecond later. Both pass the date check and reach `createEvent`.

Reject values other than strings and numbers after handling the allowed empty values, so malformed input cannot silently become an unrelated event time.

### Issue 2
src/commands/audit-log.spec.ts:110-112
The default-time test accepts any valid `Date`, including a fixed date in 1970. The new invalid-input tests check only that something throws, not that the user sees `Invalid occurredAt`. These tests could stay green if either promised behavior breaks.

Freeze the clock and compare the default time exactly. Also check `stderrOutput` for the intended error text; the shared handler prints that text before throwing a separate `CliExit`.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

Converts file-based occurredAt values into Date objects before calling the SDK and shares that helper with the flag path.

  • Audit event timestamps become valid dates before events are created.

Reviews (1) · Last reviewed commit: "fix(audit-log): parse occurredAt from --..." · Reviewed by Greptile

Comment thread src/commands/audit-log.ts
Comment on lines +28 to +29
const date = new Date(value as string | number | Date);
if (Number.isNaN(date.getTime())) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Booleans become unrelated event times

parseOccurredAt accepts boolean values from an event file. The type cast does not change the value at runtime, so false becomes the Unix epoch and true becomes one millisecond later. Both pass the date check and reach createEvent.

Reject values other than strings and numbers after handling the allowed empty values, so malformed input cannot silently become an unrelated event time.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/commands/audit-log.ts
Line: 28-29

Comment:
**Booleans become unrelated event times**

`parseOccurredAt` accepts boolean values from an event file. The type cast does not change the value at runtime, so `false` becomes the Unix epoch and `true` becomes one millisecond later. Both pass the date check and reach `createEvent`.

Reject values other than strings and numbers after handling the allowed empty values, so malformed input cannot silently become an unrelated event time.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Comment on lines +110 to +112
const [, event] = mockSdk.auditLogs.createEvent.mock.calls[0];
expect(event.occurredAt).toBeInstanceOf(Date);
expect(Number.isNaN(event.occurredAt.getTime())).toBe(false);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Tests miss time and message

The default-time test accepts any valid Date, including a fixed date in 1970. The new invalid-input tests check only that something throws, not that the user sees Invalid occurredAt. These tests could stay green if either promised behavior breaks.

Freeze the clock and compare the default time exactly. Also check stderrOutput for the intended error text; the shared handler prints that text before throwing a separate CliExit.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/commands/audit-log.spec.ts
Line: 110-112

Comment:
**Tests miss time and message**

The default-time test accepts any valid `Date`, including a fixed date in 1970. The new invalid-input tests check only that something throws, not that the user sees `Invalid occurredAt`. These tests could stay green if either promised behavior breaks.

Freeze the clock and compare the default time exactly. Also check `stderrOutput` for the intended error text; the shared handler prints that text before throwing a separate `CliExit`.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

audit-log create-event --file does not parse occurredAt into a Date

1 participant