-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[alerting] removes usage of any throughout Alerting Services code #64161
Conversation
* master: (29 commits) [Dashboard] Deangularize navbar, attempt nr. 2 (elastic#61611) refactor action filter creation utils (elastic#62969) Refresh index pattern list before redirecting (elastic#63329) [APM]fixing custom link unit tests (elastic#64045) [Ingest] EPM & Fleet are enabled when Ingest is enabled (elastic#64103) [Alerting] Fixed bug with no possibility to edit the index name after adding (elastic#64033) [Maps] Map settings: min and max zoom (elastic#63714) [kbn-storybook] Use raw loader for text files (elastic#64108) [EPM] /packages/{package} endpoint to support upgrades (elastic#63629) [SIEM] New Platform Saved Objects Registration (elastic#64029) [Endpoint] Hook to handle events needing navigation via Router (elastic#63863) Fixed small issue in clone functionality (elastic#64085) [Endpoint]EMT-146: use ingest agent for status info (elastic#63921) [SIEM] Server NP Followup (elastic#64010) Register uiSettings on New Platform (elastic#64015) [Reporting] Integration polling config with client code (elastic#63754) [Docs]7.7 SIEM doc updates (elastic#63951) [SIEM] [Cases] Tags suggestions (elastic#63878) Include datasource UUID in agent config yaml, adjust overflow height of yaml view (elastic#64027) [DOCS] Add file size setting for Data Visualizer (elastic#64006) ...
const data: { | ||
event_action: ActionParamsType['eventAction']; | ||
dedup_key: string; | ||
payload?: { | ||
summary: string; | ||
source: string; | ||
severity: string; | ||
timestamp?: string; | ||
component?: string; | ||
group?: string; | ||
class?: string; | ||
}; | ||
} = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that this is a change strictly to typing, not actual object, so compile-time addition, not runtime.
value: unknown | ||
): Record<string, unknown> { | ||
if (actionType.validate) { | ||
let name; | ||
try { | ||
switch (key) { | ||
case 'params': | ||
name = 'action params'; | ||
if (actionType.validate.params) { | ||
return actionType.validate.params.validate(value); | ||
} | ||
break; | ||
case 'config': | ||
name = 'action type config'; | ||
if (actionType.validate.config) { | ||
return actionType.validate.config.validate(value); | ||
} | ||
|
||
break; | ||
case 'secrets': | ||
name = 'action type secrets'; | ||
if (actionType.validate.secrets) { | ||
return actionType.validate.secrets.validate(value); | ||
} | ||
break; | ||
default: | ||
// should never happen, but left here for future-proofing | ||
throw new Error(`invalid actionType validate key: ${key}`); | ||
} | ||
} catch (err) { | ||
// we can't really i18n this yet, since the err.message isn't i18n'd itself | ||
throw Boom.badRequest(`error validating ${name}: ${err.message}`); | ||
} | ||
} catch (err) { | ||
// we can't really i18n this yet, since the err.message isn't i18n'd itself | ||
throw Boom.badRequest(`error validating ${name}: ${err.message}`); | ||
} | ||
|
||
// should never happen, but left here for future-proofing | ||
throw new Error(`invalid actionType validate key: ${key}`); | ||
return value as Record<string, unknown>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function was refactored to avoid having to do the as Record<string, unknown>
casting 4 times in a single function, the logic is identical.
// coreMock.createSetup doesn't support Plugin generics | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
await plugin.setup(coreSetup as any, pluginsSetup); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll open an issue with Platform - sadly this piece of generics typing was missed in our core mocks
// This will have to remain `any` until we can extend Action Executors with generics | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
config: Record<string, any>; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
secrets: Record<string, any>; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An issue has been added to address this: #64147
let resolve: (arg: T) => void; | ||
return Object.assign(new Promise<T>(r => (resolve = r)), { | ||
resolve(arg: T) { | ||
return setTimeout(() => resolve(arg), 0); | ||
}, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing an any
in a test that was using resolvable
revealed the typing was wrong here.
export type SerializedConcreteTaskInstance = Omit< | ||
ConcreteTaskInstance, | ||
'state' | 'params' | 'scheduledAt' | 'startedAt' | 'retryAt' | 'runAt' | ||
> & { | ||
state: string; | ||
params: string; | ||
scheduledAt: string; | ||
startedAt: string | null; | ||
retryAt: string | null; | ||
runAt: string; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removing any
revealed that the typing in our serialisation was wrong - this adds a missing type that correctly types the serialised shape.
const c = savedObjectToConcreteTaskInstance( | ||
// The SavedObjects update api forces a Partial on the `attributes` on the response, | ||
// but actually returns the whole object that is passed to it, so as we know we're | ||
// passing in the whole object, this is safe to do. | ||
// This is far from ideal, but unless we change the SavedObjectsClient this is the best we can do | ||
// (updatedSavedObject as unknown) as SavedObject<SerializedConcreteTaskInstance> | ||
{ ...updatedSavedObject, attributes: defaults(updatedSavedObject.attributes, attributes) } | ||
); | ||
return c; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what we discussed on the call yesterday - the typing in SOs basically forces a Partial<>
where it isn't needed.
In case SOs changes it's underlying implementation in the future, we use defaults to ensure we still have all the required fields after update is applied.
Pinging @elastic/kibana-alerting-services (Team:Alerting Services) |
About 1/3 of the way through the review - looks great! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - I think all my comments were about comments :-)
let resolve: (arg: T) => void; | ||
return Object.assign(new Promise<T>(r => (resolve = r)), { | ||
resolve(arg: T) { | ||
return setTimeout(() => resolve(arg), 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need the setTimeout()
here? I doubt it hurts anything, just adds a little more latency, who knows might be useful :-), but I'm curious
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so tbh, I noticed that too, but decided to limit changes in this PR to things related to the removal of any
:)
@@ -29,6 +36,8 @@ declare module 'src/core/server' { | |||
} | |||
|
|||
export interface Services { | |||
// This will have to remain `any` until we can extend Alert Services with generics |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this comment correct? I thought the any
s for callCluster was because callCluster definition, not because of our own generics.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, mistake, good catch :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually... looking at it I've remembered why I marked it that way - this is our exposed interface to the plugins and we could exposed it as unknown
if we wanted to. Exposing as any
allows plugins to avoid explicitly casting, which means they're more likely to drag a lack of typechecking into their code (any
is pervasive, if they even reference this response (in the Promise<any>
) then anything referenced from it will not be type checked, even though it might look like it is.
If we expose as unknown
they would have to explicitly state what type they think it is and it will typecheck properly from there out.
But changing that interface isn't obvious and we'd have to look into it further.
@@ -102,12 +103,14 @@ async function getIndicesFromPattern( | |||
}, | |||
}, | |||
}; | |||
const response = await dataClient.callAsCurrentUser('search', params); | |||
if (response.status === 404 || !response.aggregations) { | |||
const response: SearchResponse<unknown> = await dataClient.callAsCurrentUser('search', params); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ya, this client gets hard to reason about, as there are options to have it return different things on 404's, and what not - it's not even all just the client, ES has such options you can send on requests. Leaving the TODO comment is good for next time we need to look into this particular site ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
@@ -137,3 +140,9 @@ async function getAliasesFromPattern( | |||
|
|||
return result; | |||
} | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like having these little stand-alone interfaces like this, for cases like this.
@@ -5,6 +5,7 @@ | |||
*/ | |||
|
|||
import { reject, isUndefined } from 'lodash'; | |||
import { SearchResponse, Client } from 'elasticsearch'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now I feel slightly embarrassed that I didn't do the basic typing when I wrote most of this code. Thanks for upgrading all this stuff!!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hah nah, don't be silly, we did it to ourselves by not having this rule in place in the first place :)
@@ -3,6 +3,7 @@ | |||
* or more contributor license agreements. Licensed under the Elastic License; | |||
* you may not use this file except in compliance with the Elastic License. | |||
*/ | |||
/* eslint-disable @typescript-eslint/no-explicit-any */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably don't need this anymore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
* master: (70 commits) KQL removes leading zero and breaks query (elastic#62748) [FieldFormats] Cleanup: rename IFieldFormatType -> FieldFormatInstanceType (elastic#64193) [ML] Changes transforms wizard UI text (elastic#64150) [Alerting] change server log action type .log to .server-log in README (elastic#64124) [Metrics UI] Design Refresh: Inventory View, Episode 1 (elastic#64026) chore(NA): reduce siem bundle size using babel-plugin-transfor… (elastic#63269) chore(NA): use core-js instead of babel-polyfill on canvas sha… (elastic#63486) skip flaky suite (elastic#61173) skip flaky suite (elastic#62497) Renamed ilm policy for event log so it is not prefixed with dot (elastic#64262) [eslint] no_restricted_paths config cleanup (elastic#63741) Add Oil Rig Icon from @elastic/maki (elastic#64364) [Maps] Migrate Maps embeddables to NP (elastic#63976) [Ingest] Data streams list page (elastic#64134) chore(NA): add file-loader into jest moduleNameMapper (elastic#64330) [DOCS] Added images to automating report generation (elastic#64333) [SIEM][CASE] Api Integration Tests: Configuration (elastic#63948) Expose ability to check if API Keys are enabled (elastic#63454) [DOCS] Fixes formatting in alerting doc (elastic#64338) [data.search.aggs]: Create agg types function for terms agg. (elastic#63541) ...
💚 Build SucceededHistory
To update your PR or re-run it, just comment with: |
…astic#64161) This removes unneeded use of `any` throughout: 1. alerting 2. alerting_builtin 3. actions 4. task manager 5. event log It also adds a linting rule that will prevent us from adding more `any` in the future unless an explicit exemption is made.
…bana into ingest-node-pipeline/open-flyout-create-edit * 'feature/ingest-node-pipelines' of github.com:elastic/kibana: (116 commits) [Ingest Node Pipelines] More lenient treatment of on-failure value (elastic#64411) Report Deletion via UI- functional test (elastic#64031) Bump handlebars dependency from 4.5.3 to 4.7.6 (elastic#64402) [Uptime] Update TLS settings (elastic#64111) [alerting] removes usage of any throughout Alerting Services code (elastic#64161) [CANVAS] Moves notify to a canvas service (elastic#63268) [Canvas] Misc NP Stuff (elastic#63703) update apm index pattern (elastic#64232) Task/hostlist pagination (elastic#63722) [NP] Vega migration (elastic#63849) Move ensureDefaultIndexPattern into data plugin (elastic#63100) [Fleet] Fix agent status count to not include unenrolled agents (elastic#64106) Migrate graph_workspace saved object registration to Kibana platform (elastic#64157) Index pattern management UI -> TypeScript and New Platform Ready (edit_index_pattern) (elastic#64184) [ML] EuiDataGrid ml/transform components. (elastic#63447) [ML] Moving to kibana capabilities (elastic#64057) Move input_control_vis into NP (elastic#63333) remove reference to local application service in graph (elastic#64288) KQL removes leading zero and breaks query (elastic#62748) [FieldFormats] Cleanup: rename IFieldFormatType -> FieldFormatInstanceType (elastic#64193) ...
Summary
This removes unneeded use of
any
throughout:It also adds a linting rule that will prevent us from adding more
any
in the future unless an explicit exemption is made.closes #64145
Checklist
Delete any items that are not applicable to this PR.
Any text added follows EUI's writing guidelines, uses sentence case text and includes i18n supportDocumentation was added for features that require explanation or tutorialsUnit or functional tests were updated or added to match the most common scenariosThis was checked for keyboard-only and screenreader accessibilityThis renders correctly on smaller devices using a responsive layout. (You can test this in your browserThis was checked for cross-browser compatibility, including a check against IE11For maintainers