Skip to content

Commit 1c5b07a

Browse files
authored
[scan report] Add --short flag for CI type of use cases (#365)
1 parent b611563 commit 1c5b07a

File tree

4 files changed

+64
-28
lines changed

4 files changed

+64
-28
lines changed

src/commands/scan/cmd-scan-report.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ describe('socket scan report', async () => {
2828
--markdown Output result as markdown
2929
--reportLevel Which policy level alerts should be reported
3030
--security Report the security policy status. Default: true
31+
--short Report only the healthy status
3132
3233
This consumes 1 quota unit plus 1 for each of the requested policy types.
3334
@@ -43,6 +44,8 @@ describe('socket scan report', async () => {
4344
By default only the warn and error policy level alerts are reported. You can
4445
override this and request more ('defer' < 'ignore' < 'monitor' < 'warn' < 'error')
4546
47+
Short responses: JSON: \`{healthy:bool}\`, markdown: \`healthy = bool\`, text: \`OK/ERR\`
48+
4649
Examples
4750
$ socket scan report FakeOrg 000aaaa1-0000-0a0a-00a0-00a0000000a0 --json --fold=version"
4851
`

src/commands/scan/cmd-scan-report.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ const config: CliCommandConfig = {
3434
default: 'warn',
3535
description: 'Which policy level alerts should be reported'
3636
},
37+
short: {
38+
type: 'boolean',
39+
default: false,
40+
description: 'Report only the healthy status'
41+
},
3742
// license: {
3843
// type: 'boolean',
3944
// default: true,
@@ -66,6 +71,8 @@ const config: CliCommandConfig = {
6671
By default only the warn and error policy level alerts are reported. You can
6772
override this and request more ('defer' < 'ignore' < 'monitor' < 'warn' < 'error')
6873
74+
Short responses: JSON: \`{healthy:bool}\`, markdown: \`healthy = bool\`, text: \`OK/ERR\`
75+
6976
Examples
7077
$ ${command} FakeOrg 000aaaa1-0000-0a0a-00a0-00a0000000a0 --json --fold=version
7178
`
@@ -138,6 +145,7 @@ async function run(
138145
outputKind: json ? 'json' : markdown ? 'markdown' : 'text',
139146
filePath: file,
140147
fold: fold as 'none' | 'file' | 'pkg' | 'version',
148+
short: !!cli.flags['short'],
141149
reportLevel: reportLevel as
142150
| 'warn'
143151
| 'error'

src/commands/scan/generate-report.ts

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ type PackageMap = Map<string, ReportLeafNode | VersionMap>
1212
type EcoMap = Map<string, ReportLeafNode | PackageMap>
1313
export type ViolationsMap = Map<string, EcoMap>
1414

15+
export interface ShortScanReport {
16+
healthy: boolean
17+
}
1518
export interface ScanReport {
1619
orgSlug: string
1720
scanId: string
@@ -35,14 +38,16 @@ export function generateReport(
3538
fold,
3639
orgSlug,
3740
reportLevel,
38-
scanId
41+
scanId,
42+
short
3943
}: {
40-
orgSlug: string
41-
scanId: string
4244
fold: 'pkg' | 'version' | 'file' | 'none'
45+
orgSlug: string
4346
reportLevel: 'defer' | 'ignore' | 'monitor' | 'warn' | 'error'
47+
scanId: string
48+
short: boolean
4449
}
45-
): ScanReport {
50+
): ScanReport | ShortScanReport {
4651
const now = Date.now()
4752

4853
// Lazily access constants.spinner.
@@ -95,20 +100,22 @@ export function generateReport(
95100
switch (action) {
96101
case 'error': {
97102
healthy = false
98-
addAlert(
99-
artifact,
100-
violations,
101-
fold,
102-
ecosystem,
103-
pkgName,
104-
version,
105-
alert,
106-
action
107-
)
103+
if (!short) {
104+
addAlert(
105+
artifact,
106+
violations,
107+
fold,
108+
ecosystem,
109+
pkgName,
110+
version,
111+
alert,
112+
action
113+
)
114+
}
108115
break
109116
}
110117
case 'warn': {
111-
if (reportLevel !== 'error') {
118+
if (!short && reportLevel !== 'error') {
112119
addAlert(
113120
artifact,
114121
violations,
@@ -123,7 +130,7 @@ export function generateReport(
123130
break
124131
}
125132
case 'monitor': {
126-
if (reportLevel !== 'warn' && reportLevel !== 'error') {
133+
if (!short && reportLevel !== 'warn' && reportLevel !== 'error') {
127134
addAlert(
128135
artifact,
129136
violations,
@@ -140,6 +147,7 @@ export function generateReport(
140147

141148
case 'ignore': {
142149
if (
150+
!short &&
143151
reportLevel !== 'warn' &&
144152
reportLevel !== 'error' &&
145153
reportLevel !== 'monitor'
@@ -160,7 +168,7 @@ export function generateReport(
160168

161169
case 'defer': {
162170
// Not sure but ignore for now. Defer to later ;)
163-
if (reportLevel === 'defer') {
171+
if (!short && reportLevel === 'defer') {
164172
addAlert(
165173
artifact,
166174
violations,
@@ -186,13 +194,15 @@ export function generateReport(
186194

187195
spinner.successAndStop(`Generated reported in ${Date.now() - now} ms`)
188196

189-
const report = {
190-
healthy,
191-
orgSlug,
192-
scanId,
193-
options: { fold, reportLevel },
194-
alerts: violations
195-
}
197+
const report = short
198+
? { healthy }
199+
: {
200+
healthy,
201+
orgSlug,
202+
scanId,
203+
options: { fold, reportLevel },
204+
alerts: violations
205+
}
196206

197207
return report
198208
}

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export async function reportFullScan({
1818
includeSecurityPolicy,
1919
orgSlug,
2020
outputKind,
21-
reportLevel
21+
reportLevel,
22+
short
2223
}: {
2324
orgSlug: string
2425
fullScanId: string
@@ -28,6 +29,7 @@ export async function reportFullScan({
2829
filePath: string
2930
fold: 'pkg' | 'version' | 'file' | 'none'
3031
reportLevel: 'defer' | 'ignore' | 'monitor' | 'warn' | 'error'
32+
short: boolean
3133
}): Promise<void> {
3234
logger.error(
3335
'output:',
@@ -67,15 +69,22 @@ export async function reportFullScan({
6769
orgSlug,
6870
scanId: fullScanId,
6971
fold,
72+
short,
7073
reportLevel
7174
}
7275
)
7376

77+
if (!scanReport.healthy) {
78+
process.exitCode = 1
79+
}
80+
7481
if (
7582
outputKind === 'json' ||
7683
(outputKind === 'text' && filePath && filePath.endsWith('.json'))
7784
) {
78-
const json = toJsonReport(scanReport)
85+
const json = short
86+
? JSON.stringify(scanReport)
87+
: toJsonReport(scanReport as ScanReport)
7988

8089
if (filePath && filePath !== '-') {
8190
logger.log('Writing json report to', filePath)
@@ -87,7 +96,9 @@ export async function reportFullScan({
8796
}
8897

8998
if (outputKind === 'markdown' || (filePath && filePath.endsWith('.md'))) {
90-
const md = toMarkdownReport(scanReport)
99+
const md = short
100+
? `healthy = ${scanReport.healthy}`
101+
: toMarkdownReport(scanReport as ScanReport)
91102

92103
if (filePath && filePath !== '-') {
93104
logger.log('Writing markdown report to', filePath)
@@ -98,7 +109,11 @@ export async function reportFullScan({
98109
return
99110
}
100111

101-
logger.dir(scanReport, { depth: null })
112+
if (short) {
113+
logger.log(scanReport.healthy ? 'OK' : 'ERR')
114+
} else {
115+
logger.dir(scanReport, { depth: null })
116+
}
102117
}
103118

104119
export function toJsonReport(report: ScanReport): string {

0 commit comments

Comments
 (0)