-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathget-diff-scan.ts
More file actions
75 lines (65 loc) · 1.75 KB
/
get-diff-scan.ts
File metadata and controls
75 lines (65 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import fs from 'node:fs'
import util from 'node:util'
import colors from 'yoctocolors-cjs'
import { Spinner } from '@socketsecurity/registry/lib/spinner'
import { handleAPIError, queryAPI } from '../../utils/api'
export async function getDiffScan(
{
after,
before,
file,
orgSlug,
outputJson
}: {
outputJson: boolean
outputMarkdown: boolean
before: string
after: string
preview: boolean
orgSlug: string
file: string
},
apiToken: string
): Promise<void> {
const spinnerText = 'Getting diff scan... \n'
const spinner = new Spinner({ text: spinnerText }).start()
const response = await queryAPI(
`${orgSlug}/full-scans/diff?before=${before}&after=${after}&preview`,
apiToken
)
const data = await response.json()
if (!response.ok) {
const err = await handleAPIError(response.status)
spinner.errorAndStop(
`${colors.bgRed(colors.white(response.statusText))}: ${err}`
)
return
}
spinner.stop()
if (file && !outputJson) {
fs.writeFile(file, JSON.stringify(data), err => {
err
? console.error(err)
: console.log(`Data successfully written to ${file}`)
})
return
}
if (outputJson) {
console.log(`\n Diff scan result: \n`)
console.log(
util.inspect(data, { showHidden: false, depth: null, colors: true })
)
console.log(
`\n View this diff scan in the Socket dashboard: ${colors.cyan((data as any)?.['diff_report_url'])}`
)
return
}
console.log('Diff scan result:')
console.log(data)
console.log(
`\n 📝 To display the detailed report in the terminal, use the --json flag \n`
)
console.log(
`\n View this diff scan in the Socket dashboard: ${colors.cyan((data as any)?.['diff_report_url'])}`
)
}