Skip to content

Commit 5039ea5

Browse files
authored
Apply handle pattern to diff-scan (#375)
1 parent 6bbc8f5 commit 5039ea5

File tree

4 files changed

+124
-89
lines changed

4 files changed

+124
-89
lines changed

src/commands/diff-scan/cmd-diff-scan-get.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import colors from 'yoctocolors-cjs'
22

33
import { logger } from '@socketsecurity/registry/lib/logger'
44

5-
import { getDiffScan } from './get-diff-scan'
5+
import { handleDiffScan } from './handle-diff-scan'
66
import constants from '../../constants'
77
import { commonFlags } from '../../flags'
88
import { meowOrExit } from '../../utils/meow-with-subcommands'
@@ -85,8 +85,8 @@ async function run(
8585
parentName
8686
})
8787

88-
const before = String(cli.flags['before'] || '')
89-
const after = String(cli.flags['after'] || '')
88+
const { after, before, depth, file, json, markdown } = cli.flags
89+
9090
const [orgSlug = ''] = cli.input
9191

9292
if (!before || !after || cli.input.length < 1) {
@@ -107,12 +107,12 @@ async function run(
107107
return
108108
}
109109

110-
await getDiffScan({
111-
outputJson: Boolean(cli.flags['json']),
112-
before,
113-
after,
114-
depth: Number(cli.flags['depth']),
110+
await handleDiffScan({
111+
before: String(before || ''),
112+
after: String(after || ''),
113+
depth: Number(depth),
115114
orgSlug,
116-
file: String(cli.flags['file'] || '')
115+
outputKind: json ? 'json' : markdown ? 'markdown' : 'text',
116+
file: String(file || '')
117117
})
118118
}
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import colors from 'yoctocolors-cjs'
2+
3+
import constants from '../../constants'
4+
import { handleAPIError, handleApiCall, queryAPI } from '../../utils/api'
5+
import { AuthError } from '../../utils/errors'
6+
import { getDefaultToken } from '../../utils/sdk'
7+
8+
import type { SocketSdkReturnType } from '@socketsecurity/sdk'
9+
10+
export async function fetchDiffScan({
11+
after,
12+
before,
13+
orgSlug
14+
}: {
15+
after: string
16+
before: string
17+
orgSlug: string
18+
}): Promise<SocketSdkReturnType<'GetOrgDiffScan'>['data'] | undefined> {
19+
const apiToken = getDefaultToken()
20+
if (!apiToken) {
21+
throw new AuthError(
22+
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
23+
)
24+
}
25+
26+
return await fetchDiffScanWithToken(apiToken, {
27+
after,
28+
before,
29+
orgSlug
30+
})
31+
}
32+
33+
export async function fetchDiffScanWithToken(
34+
apiToken: string,
35+
{
36+
after,
37+
before,
38+
orgSlug
39+
}: {
40+
after: string
41+
before: string
42+
orgSlug: string
43+
}
44+
): Promise<SocketSdkReturnType<'GetOrgDiffScan'>['data'] | undefined> {
45+
// Lazily access constants.spinner.
46+
const { spinner } = constants
47+
48+
spinner.start('Fetching diff-scan...')
49+
50+
const response = await queryAPI(
51+
`orgs/${orgSlug}/full-scans/diff?before=${encodeURIComponent(before)}&after=${encodeURIComponent(after)}`,
52+
apiToken
53+
)
54+
55+
spinner?.successAndStop('Received diff-scan response')
56+
57+
if (!response.ok) {
58+
const err = await handleAPIError(response.status)
59+
spinner.errorAndStop(
60+
`${colors.bgRed(colors.white(response.statusText))}: ${err}`
61+
)
62+
return
63+
}
64+
65+
const result = await handleApiCall(
66+
(await response.json()) as Promise<
67+
SocketSdkReturnType<'GetOrgDiffScan'>['data']
68+
>,
69+
'Deserializing json'
70+
)
71+
72+
return result
73+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { fetchDiffScan } from './fetch-diff-scan'
2+
import { outputDiffScan } from './output-diff-scan'
3+
4+
export async function handleDiffScan({
5+
after,
6+
before,
7+
depth,
8+
file,
9+
orgSlug,
10+
outputKind
11+
}: {
12+
after: string
13+
before: string
14+
depth: number
15+
file: string
16+
orgSlug: string
17+
outputKind: 'json' | 'markdown' | 'text'
18+
}): Promise<void> {
19+
const data = await fetchDiffScan({
20+
after,
21+
before,
22+
orgSlug
23+
})
24+
if (!data) return
25+
26+
await outputDiffScan(data, {
27+
depth,
28+
file,
29+
outputKind
30+
})
31+
}

src/commands/diff-scan/get-diff-scan.ts src/commands/diff-scan/output-diff-scan.ts

+11-80
Original file line numberDiff line numberDiff line change
@@ -5,98 +5,29 @@ import colors from 'yoctocolors-cjs'
55

66
import { logger } from '@socketsecurity/registry/lib/logger'
77

8-
import constants from '../../constants'
9-
import { handleAPIError, handleApiCall, queryAPI } from '../../utils/api'
10-
import { AuthError } from '../../utils/errors'
11-
import { getDefaultToken } from '../../utils/sdk'
12-
138
import type { SocketSdkReturnType } from '@socketsecurity/sdk'
149

15-
export async function getDiffScan({
16-
after,
17-
before,
18-
depth,
19-
file,
20-
orgSlug,
21-
outputJson
22-
}: {
23-
after: string
24-
before: string
25-
depth: number
26-
file: string
27-
orgSlug: string
28-
outputJson: boolean
29-
}): Promise<void> {
30-
const apiToken = getDefaultToken()
31-
if (!apiToken) {
32-
throw new AuthError(
33-
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
34-
)
35-
}
36-
37-
await getDiffScanWithToken({
38-
after,
39-
before,
10+
export async function outputDiffScan(
11+
result: SocketSdkReturnType<'GetOrgDiffScan'>['data'],
12+
{
4013
depth,
4114
file,
42-
orgSlug,
43-
outputJson,
44-
apiToken
45-
})
46-
}
47-
export async function getDiffScanWithToken({
48-
after,
49-
apiToken,
50-
before,
51-
depth,
52-
file,
53-
orgSlug,
54-
outputJson
55-
}: {
56-
after: string
57-
apiToken: string
58-
depth: number
59-
before: string
60-
file: string
61-
orgSlug: string
62-
outputJson: boolean
63-
}): Promise<void> {
64-
// Lazily access constants.spinner.
65-
const { spinner } = constants
66-
67-
spinner.start('Getting diff scan...')
68-
69-
const response = await queryAPI(
70-
`orgs/${orgSlug}/full-scans/diff?before=${encodeURIComponent(before)}&after=${encodeURIComponent(after)}`,
71-
apiToken
72-
)
73-
74-
if (!response.ok) {
75-
const err = await handleAPIError(response.status)
76-
spinner.errorAndStop(
77-
`${colors.bgRed(colors.white(response.statusText))}: ${err}`
78-
)
79-
return
15+
outputKind
16+
}: {
17+
depth: number
18+
file: string
19+
outputKind: 'json' | 'markdown' | 'text'
8020
}
81-
82-
const result = await handleApiCall(
83-
(await response.json()) as Promise<
84-
SocketSdkReturnType<'GetOrgDiffScan'>['data']
85-
>,
86-
'Deserializing json'
87-
)
88-
89-
spinner.stop()
90-
91-
const dashboardUrl = (result as any)?.['diff_report_url']
21+
): Promise<void> {
22+
const dashboardUrl = result.diff_report_url
9223
const dashboardMessage = dashboardUrl
9324
? `\n View this diff scan in the Socket dashboard: ${colors.cyan(dashboardUrl)}`
9425
: ''
9526

9627
// When forcing json, or dumping to file, serialize to string such that it
9728
// won't get truncated. The only way to dump the full raw JSON to stdout is
9829
// to use `--json --file -` (the dash is a standard notation for stdout)
99-
if (outputJson || file) {
30+
if (outputKind === 'json' || file) {
10031
let json
10132
try {
10233
json = JSON.stringify(result, null, 2)

0 commit comments

Comments
 (0)