-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
# Backport This will backport the following commits from `main` to `8.x`: - [Convert timestamp before passing to validation (#192379)](#192379) <!--- Backport version: 9.4.3 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Ersin Erdal","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-09-13T21:48:05Z","message":"Convert timestamp before passing to validation (#192379)\n\nResolves: #186114\r\n\r\nThis PR adds a utility function to `stack_connectors` plugin to convert\r\ngiven date string or number (epoch) to proper type before passing to\r\nDate object or validation functions.","sha":"48de1a57e726f570feefe37e211d0b79033b1b75","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:ResponseOps","v9.0.0","v8.16.0"],"title":"Convert timestamp before passing to validation","number":192379,"url":"https://github.com/elastic/kibana/pull/192379","mergeCommit":{"message":"Convert timestamp before passing to validation (#192379)\n\nResolves: #186114\r\n\r\nThis PR adds a utility function to `stack_connectors` plugin to convert\r\ngiven date string or number (epoch) to proper type before passing to\r\nDate object or validation functions.","sha":"48de1a57e726f570feefe37e211d0b79033b1b75"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/192379","number":192379,"mergeCommit":{"message":"Convert timestamp before passing to validation (#192379)\n\nResolves: #186114\r\n\r\nThis PR adds a utility function to `stack_connectors` plugin to convert\r\ngiven date string or number (epoch) to proper type before passing to\r\nDate object or validation functions.","sha":"48de1a57e726f570feefe37e211d0b79033b1b75"}},{"branch":"8.x","label":"v8.16.0","branchLabelMappingKey":"^v8.16.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: Ersin Erdal <[email protected]>
- Loading branch information
1 parent
85e99bb
commit eef194b
Showing
4 changed files
with
94 additions
and
14 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
x-pack/plugins/stack_connectors/server/connector_types/lib/convert_timestamp.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { convertTimestamp } from './convert_timestamp'; | ||
|
||
describe('convert_timestamp', () => { | ||
const stringDate = '2024-09-06T11:10:24.681Z'; | ||
const stringDateWithSlash = '2019/05/15'; | ||
const stringDateWithDot = '10.12.1979'; | ||
const anyString = 'asdfgh'; | ||
const anyStringWithNumber = '123asdfghjkl'; | ||
const epochDate = 1725880672; | ||
const stringifiedEpochDate = '1725880672'; | ||
|
||
it('should return a string date as it is', () => { | ||
expect(convertTimestamp(stringDate)).toBe(stringDate); | ||
}); | ||
|
||
it('should return any string as it is', () => { | ||
expect(convertTimestamp(anyString)).toBe(anyString); | ||
}); | ||
|
||
it('should return any string date with slash as it is', () => { | ||
expect(convertTimestamp(stringDateWithSlash)).toBe(stringDateWithSlash); | ||
}); | ||
|
||
it('should return any string date with dot as it is', () => { | ||
expect(convertTimestamp(stringDateWithDot)).toBe(stringDateWithDot); | ||
}); | ||
|
||
it('should return any string with some numbers in it as it is', () => { | ||
expect(convertTimestamp(anyStringWithNumber)).toBe(anyStringWithNumber); | ||
}); | ||
|
||
it('should return a number if the input is a stringified number', () => { | ||
expect(convertTimestamp('12345678')).toBe(12345678); | ||
}); | ||
|
||
it('should return an epoch date as it is', () => { | ||
expect(convertTimestamp(epochDate)).toBe(epochDate); | ||
}); | ||
|
||
it('should return a stringified epoch date as number', () => { | ||
expect(convertTimestamp(stringifiedEpochDate)).toBe(epochDate); | ||
}); | ||
|
||
it('should return null if timestamp is not passed', () => { | ||
expect(convertTimestamp()).toBe(null); | ||
}); | ||
|
||
it('should return null if timestamp is null', () => { | ||
expect(convertTimestamp(null)).toBe(null); | ||
}); | ||
}); |
26 changes: 26 additions & 0 deletions
26
x-pack/plugins/stack_connectors/server/connector_types/lib/convert_timestamp.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export function convertTimestamp(timestamp?: string | number | null): string | number | null { | ||
if (timestamp) { | ||
if (typeof timestamp === 'string') { | ||
const trimmedTimestamp = timestamp.trim(); | ||
if (trimmedTimestamp.length > 0) { | ||
const parsedTimestamp = parseInt(trimmedTimestamp, 10); | ||
|
||
if (!isNaN(parsedTimestamp) && JSON.stringify(parsedTimestamp) === trimmedTimestamp) { | ||
return parsedTimestamp; // return converted epoch | ||
} | ||
return trimmedTimestamp; // return string | ||
} | ||
} | ||
if (typeof timestamp === 'number') { | ||
return timestamp; // return epoch | ||
} | ||
} | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters