| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {Alert} from 'isl/src/types'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | /** Given raw json output from `sl config`, parse Alerts */ |
| b69ab31 | | | 11 | export function parseAlerts(rawConfigs: Array<{name: string; value: unknown}>): Array<Alert> { |
| b69ab31 | | | 12 | // we get back configs with their keys as prefixes |
| b69ab31 | | | 13 | // [ {name: "alerts.S11111.title", value: "alert 1"}, {name: "alerts.S22222.title", value: "alert 2"}, ...] |
| b69ab31 | | | 14 | const alertMap = new Map<string, Partial<Alert>>(); |
| b69ab31 | | | 15 | for (const entry of Object.values(rawConfigs)) { |
| b69ab31 | | | 16 | const {name, value} = entry; |
| b69ab31 | | | 17 | const [, key, suffix] = name.split('.'); |
| b69ab31 | | | 18 | if (!suffix) { |
| b69ab31 | | | 19 | continue; |
| b69ab31 | | | 20 | } |
| b69ab31 | | | 21 | const existing = alertMap.get(key) ?? {key}; |
| b69ab31 | | | 22 | (existing as {[key: string]: unknown})[suffix] = |
| b69ab31 | | | 23 | suffix === 'show-in-isl' ? value == 'true' : value; |
| b69ab31 | | | 24 | alertMap.set(key, existing); |
| b69ab31 | | | 25 | } |
| b69ab31 | | | 26 | |
| b69ab31 | | | 27 | return [...alertMap.values()].filter( |
| b69ab31 | | | 28 | (entry): entry is Alert => |
| b69ab31 | | | 29 | !!entry.title && |
| b69ab31 | | | 30 | !!entry.description && |
| b69ab31 | | | 31 | !!entry.severity && |
| b69ab31 | | | 32 | !!entry.url && |
| b69ab31 | | | 33 | entry['show-in-isl'] === true, |
| b69ab31 | | | 34 | ); |
| b69ab31 | | | 35 | } |