Skip to content

Commit afbbf91

Browse files
authored
Make scan report also return cresult interface with --json (#504)
1 parent 7699ef7 commit afbbf91

File tree

4 files changed

+549
-417
lines changed

4 files changed

+549
-417
lines changed

src/commands/scan/generate-report.mts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { CResult } from '../../types.mts'
12
import type { Spinner } from '@socketsecurity/registry/lib/spinner'
23
import type { SocketSdkReturnType } from '@socketsecurity/sdk'
34
import type { components } from '@socketsecurity/sdk/types/api'
@@ -29,6 +30,8 @@ export type ReportLeafNode = {
2930
manifest: string[]
3031
}
3132

33+
// Note: The returned cresult will only be ok:false when the generation
34+
// failed. It won't reflect the healthy state.
3235
export function generateReport(
3336
scan: Array<components['schemas']['SocketArtifact']>,
3437
securityPolicy: SocketSdkReturnType<'getOrgSecurityPolicy'>['data'],
@@ -47,7 +50,7 @@ export function generateReport(
4750
short?: boolean | undefined
4851
spinner?: Spinner | undefined
4952
}
50-
): ScanReport | ShortScanReport {
53+
): CResult<ScanReport | { healthy: boolean }> {
5154
const now = Date.now()
5255

5356
spinner?.start('Generating report...')
@@ -199,17 +202,34 @@ export function generateReport(
199202

200203
spinner?.successAndStop(`Generated reported in ${Date.now() - now} ms`)
201204

202-
const report = short
203-
? { healthy }
204-
: {
205-
healthy,
206-
orgSlug,
207-
scanId,
208-
options: { fold, reportLevel },
209-
alerts: violations
210-
}
205+
if (short) {
206+
return {
207+
ok: true,
208+
data: { healthy }
209+
}
210+
}
211211

212-
return report
212+
const report = {
213+
healthy,
214+
orgSlug,
215+
scanId,
216+
options: { fold, reportLevel },
217+
alerts: violations
218+
}
219+
220+
if (!healthy) {
221+
return {
222+
ok: true,
223+
message:
224+
'The report contains at least one alert that violates the policies set by your organization',
225+
data: report
226+
}
227+
}
228+
229+
return {
230+
ok: true,
231+
data: report
232+
}
213233
}
214234

215235
function createLeaf(

0 commit comments

Comments
 (0)