Skip to content

Make scan report also return cresult interface with --json #504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions src/commands/scan/generate-report.mts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { CResult } from '../../types.mts'
import type { Spinner } from '@socketsecurity/registry/lib/spinner'
import type { SocketSdkReturnType } from '@socketsecurity/sdk'
import type { components } from '@socketsecurity/sdk/types/api'
Expand Down Expand Up @@ -29,6 +30,8 @@ export type ReportLeafNode = {
manifest: string[]
}

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

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

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

const report = short
? { healthy }
: {
healthy,
orgSlug,
scanId,
options: { fold, reportLevel },
alerts: violations
}
if (short) {
return {
ok: true,
data: { healthy }
}
}

return report
const report = {
healthy,
orgSlug,
scanId,
options: { fold, reportLevel },
alerts: violations
}

if (!healthy) {
return {
ok: true,
message:
'The report contains at least one alert that violates the policies set by your organization',
data: report
}
}

return {
ok: true,
data: report
}
}

function createLeaf(
Expand Down
Loading
Loading