Skip to content

Commit 11714a2

Browse files
committed
Tweak alert types and add alert fix type
1 parent ace6cae commit 11714a2

File tree

11 files changed

+63
-67
lines changed

11 files changed

+63
-67
lines changed

src/commands/diff-scan/fetch-diff-scan.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import colors from 'yoctocolors-cjs'
22

33
import constants from '../../constants'
4-
import { handleAPIError, handleApiCall, queryAPI } from '../../utils/api'
4+
import { handleApiCall, handleApiError, queryApi } from '../../utils/api'
55
import { AuthError } from '../../utils/errors'
66
import { getDefaultToken } from '../../utils/sdk'
77

@@ -47,15 +47,15 @@ export async function fetchDiffScanWithToken(
4747

4848
spinner.start('Fetching diff-scan...')
4949

50-
const response = await queryAPI(
50+
const response = await queryApi(
5151
`orgs/${orgSlug}/full-scans/diff?before=${encodeURIComponent(before)}&after=${encodeURIComponent(after)}`,
5252
apiToken
5353
)
5454

5555
spinner?.successAndStop('Received diff-scan response')
5656

5757
if (!response.ok) {
58-
const err = await handleAPIError(response.status)
58+
const err = await handleApiError(response.status)
5959
spinner.errorAndStop(
6060
`${colors.bgRed(colors.white(response.statusText))}: ${err}`
6161
)

src/commands/info/output-package-info.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import constants from '@socketsecurity/registry/lib/constants'
55
import { logger } from '@socketsecurity/registry/lib/logger'
66
import { hasKeys } from '@socketsecurity/registry/lib/objects'
77

8-
import { SEVERITY, formatSeverityCount } from '../../utils/alert/severity'
8+
import { ALERT_SEVERITY, formatSeverityCount } from '../../utils/alert/severity'
99
import { ColorOrMarkdown } from '../../utils/color-or-markdown'
1010
import {
1111
getSocketDevAlertUrl,
@@ -32,8 +32,8 @@ function outputPackageIssuesDetails(
3232
) {
3333
const issueDetails = packageData.filter(
3434
d =>
35-
d.value?.severity === SEVERITY.critical ||
36-
d.value?.severity === SEVERITY.high
35+
d.value?.severity === ALERT_SEVERITY.critical ||
36+
d.value?.severity === ALERT_SEVERITY.high
3737
)
3838
const uniqueIssueDetails = issueDetails.reduce((acc, issue) => {
3939
const { type } = issue

src/commands/scan/fetch-report-data.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'
33
import { logger } from '@socketsecurity/registry/lib/logger'
44

55
import constants from '../../constants'
6-
import { handleAPIError, handleApiCall, queryAPI } from '../../utils/api'
6+
import { handleApiCall, handleApiError, queryApi } from '../../utils/api'
77
import { AuthError } from '../../utils/errors'
88
import { getDefaultToken, setupSdk } from '../../utils/sdk'
99

@@ -102,7 +102,7 @@ export async function fetchReportData(
102102
] = await Promise.all([
103103
(async () => {
104104
try {
105-
const response = await queryAPI(
105+
const response = await queryApi(
106106
`orgs/${orgSlug}/full-scans/${encodeURIComponent(fullScanId)}`,
107107
apiToken
108108
)
@@ -111,7 +111,7 @@ export async function fetchReportData(
111111
updateProgress()
112112

113113
if (!response.ok) {
114-
const err = await handleAPIError(response.status)
114+
const err = await handleApiError(response.status)
115115
logger.fail(
116116
`${colors.bgRed(colors.white(response.statusText))}: Fetch error: ${err}`
117117
)

src/commands/scan/get-full-scan.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'
33
import { logger } from '@socketsecurity/registry/lib/logger'
44

55
import constants from '../../constants'
6-
import { handleAPIError, queryAPI } from '../../utils/api'
6+
import { handleApiError, queryApi } from '../../utils/api'
77
import { AuthError } from '../../utils/errors'
88
import { getDefaultToken } from '../../utils/sdk'
99

@@ -25,15 +25,15 @@ export async function getFullScan(
2525

2626
spinner.start('Fetching full-scan...')
2727

28-
const response = await queryAPI(
28+
const response = await queryApi(
2929
`orgs/${orgSlug}/full-scans/${encodeURIComponent(fullScanId)}`,
3030
apiToken
3131
)
3232

3333
spinner.stop('Fetch complete.')
3434

3535
if (!response.ok) {
36-
const err = await handleAPIError(response.status)
36+
const err = await handleApiError(response.status)
3737
logger.fail(
3838
`${colors.bgRed(colors.white(response.statusText))}: Fetch error: ${err}`
3939
)

src/commands/threat-feed/get-threat-feed.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import TableWidget from 'blessed-contrib/lib/widget/table'
1010
import { logger } from '@socketsecurity/registry/lib/logger'
1111

1212
import constants from '../../constants'
13-
import { queryAPI } from '../../utils/api'
13+
import { queryApi } from '../../utils/api'
1414
import { AuthError } from '../../utils/errors'
1515
import { getDefaultToken } from '../../utils/sdk'
1616

@@ -90,7 +90,7 @@ async function getThreatFeedWithToken({
9090

9191
spinner.start('Fetching Threat Feed data...')
9292

93-
const response = await queryAPI(`threat-feed?${queryParams}`, apiToken)
93+
const response = await queryApi(`threat-feed?${queryParams}`, apiToken)
9494
const data = (await response.json()) as {
9595
results: ThreatResult[]
9696
nextPage: string

src/constants.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ type IPC = Readonly<{
5757
type Constants = Remap<
5858
Omit<typeof registryConstants, 'Symbol(kInternalsSymbol)' | 'ENV' | 'IPC'> & {
5959
readonly 'Symbol(kInternalsSymbol)': Internals
60-
readonly ALERT_FIX_TYPE_CVE: 'cve'
61-
readonly ALERT_FIX_TYPE_UPGRADE: 'upgrade'
6260
readonly ALERT_TYPE_CRITICAL_CVE: 'criticalCVE'
6361
readonly ALERT_TYPE_CVE: 'cve'
6462
readonly ALERT_TYPE_MEDIUM_CVE: 'mediumCVE'
@@ -128,8 +126,6 @@ type Constants = Remap<
128126
}
129127
>
130128

131-
const ALERT_FIX_TYPE_CVE = 'cve'
132-
const ALERT_FIX_TYPE_UPGRADE = 'upgrade'
133129
const ALERT_TYPE_CRITICAL_CVE = 'criticalCVE'
134130
const ALERT_TYPE_CVE = 'cve'
135131
const ALERT_TYPE_MEDIUM_CVE = 'mediumCVE'
@@ -303,8 +299,6 @@ const lazyZshRcPath = () =>
303299

304300
const constants = createConstantsObject(
305301
{
306-
ALERT_FIX_TYPE_CVE,
307-
ALERT_FIX_TYPE_UPGRADE,
308302
ALERT_TYPE_CRITICAL_CVE,
309303
ALERT_TYPE_CVE,
310304
ALERT_TYPE_MEDIUM_CVE,

src/utils/alert/fix.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export enum ALERT_FIX_TYPE {
2+
cve = 'cve',
3+
upgrade = 'upgrade'
4+
}

src/utils/alert/rules.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,29 +185,35 @@ export async function uxLookup(
185185
const sockSdk = await setupSdk(getPublicToken())
186186
const orgResult = await sockSdk.getOrganizations()
187187
if (!orgResult.success) {
188+
if (orgResult.status === 429) {
189+
throw new Error(
190+
`API token quota exceeded: ${orgResult.error}`
191+
)
192+
}
188193
throw new Error(
189-
`Failed to fetch Socket organization info: ${orgResult.error.message}`
194+
`Failed to fetch Socket organization info: ${orgResult.error}`
190195
)
191196
}
197+
const { organizations } = orgResult.data
192198
const orgs: Array<
193-
Exclude<(typeof orgResult.data.organizations)[string], undefined>
199+
Exclude<(typeof organizations)[string], undefined>
194200
> = []
195-
for (const org of Object.values(orgResult.data.organizations)) {
201+
for (const org of Object.values(organizations)) {
196202
if (org) {
197203
orgs.push(org)
198204
}
199205
}
200-
const result = await sockSdk.postSettings(
206+
const settingsResult = await sockSdk.postSettings(
201207
orgs.map(org => ({ organization: org.id }))
202208
)
203-
if (!result.success) {
209+
if (!settingsResult.success) {
204210
throw new Error(
205-
`Failed to fetch API key settings: ${result.error.message}`
211+
`Failed to fetch API key settings: ${settingsResult.error}`
206212
)
207213
}
208214
return {
209215
orgs,
210-
settings: result.data
216+
settings: settingsResult.data
211217
}
212218
} catch (e) {
213219
const cause = isObject(e) && 'cause' in e ? e['cause'] : undefined

src/utils/alert/severity.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ import { stringJoinWithSeparateFinalSeparator } from '../strings'
33

44
import type { SocketSdkReturnType } from '@socketsecurity/sdk'
55

6+
export enum ALERT_SEVERITY {
7+
critical = 'critical',
8+
high = 'high',
9+
middle = 'middle',
10+
low = 'low'
11+
}
12+
613
export type SocketSdkAlertList =
714
SocketSdkReturnType<'getIssuesByNPMPackage'>['data']
815

@@ -12,20 +19,9 @@ export type SocketSdkAlert = SocketSdkAlertList[number]['value'] extends
1219
? U
1320
: never
1421

15-
export enum SEVERITY {
16-
critical = 'critical',
17-
high = 'high',
18-
middle = 'middle',
19-
low = 'low'
20-
}
21-
2222
// Ordered from most severe to least.
23-
const SEVERITIES_BY_ORDER: Array<SocketSdkAlert['severity']> = [
24-
'critical',
25-
'high',
26-
'middle',
27-
'low'
28-
]
23+
const SEVERITIES_BY_ORDER: ReadonlyArray<SocketSdkAlert['severity']> =
24+
Object.freeze(['critical', 'high', 'middle', 'low'])
2925

3026
function getDesiredSeverities(
3127
lowestToInclude: SocketSdkAlert['severity'] | undefined
@@ -66,8 +62,9 @@ export function getSeverityCount(
6662
if (!value) {
6763
continue
6864
}
69-
if (severityCount[value.severity] !== undefined) {
70-
severityCount[value.severity] += 1
65+
const { severity } = value
66+
if (severityCount[severity] !== undefined) {
67+
severityCount[severity] += 1
7168
}
7269
}
7370
return severityCount

src/utils/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export async function handleApiCall<T>(
5050
return result
5151
}
5252

53-
export async function handleAPIError(code: number) {
53+
export async function handleApiError(code: number) {
5454
if (code === 400) {
5555
return 'One of the options passed might be incorrect.'
5656
} else if (code === 403) {
@@ -72,7 +72,7 @@ function getDefaultApiBaseUrl(): string | undefined {
7272
return isNonEmptyString(baseUrl) ? baseUrl : undefined
7373
}
7474

75-
export async function queryAPI(path: string, apiToken: string) {
75+
export async function queryApi(path: string, apiToken: string) {
7676
const API_V0_URL = getDefaultApiBaseUrl()
7777
return await fetch(`${API_V0_URL}/${path}`, {
7878
method: 'GET',

0 commit comments

Comments
 (0)