Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,7 @@
!/src/commands/scan/output-create-new-scan.test.mts
!/src/commands/scan/output-delete-scan.mts
!/src/commands/scan/output-diff-scan.mts
!/src/commands/scan/output-diff-scan.test.mts
!/src/commands/scan/output-list-scans.mts
!/src/commands/scan/output-scan-config-result.mts
!/src/commands/scan/output-scan-github.mts
Expand Down
10 changes: 10 additions & 0 deletions src/commands/scan/cmd-scan-diff.mts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ async function run(
description:
'Force override the organization slug, overrides the default org from config',
},
strictBlocking: {
type: 'boolean',
default: false,
description:
'Fail when unchanged blocking alerts are present in the scan diff',
},
},
help: (command, config) => `
Usage
Expand All @@ -87,6 +93,7 @@ async function run(
Examples
$ ${command} aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 aaa1aa1a-aaaa-1111-1a1a-1111111a11a1
$ ${command} aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 aaa1aa1a-aaaa-1111-1a1a-1111111a11a1 --json
$ ${command} aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 aaa1aa1a-aaaa-1111-1a1a-1111111a11a1 --strict-blocking
`,
}

Expand All @@ -107,13 +114,15 @@ async function run(
json,
markdown,
org: orgFlag,
strictBlocking,
} = cli.flags as {
depth: number
dryRun: boolean
file: string
json: boolean
markdown: boolean
org: string
strictBlocking: boolean
}

const interactive = !!cli.flags['interactive']
Expand Down Expand Up @@ -185,5 +194,6 @@ async function run(
orgSlug,
outputKind,
file,
strictBlocking,
})
}
4 changes: 3 additions & 1 deletion src/commands/scan/cmd-scan-diff.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ describe('socket scan diff', async () => {
--json Output as JSON
--markdown Output as Markdown
--org Force override the organization slug, overrides the default org from config
--strict-blocking Fail when unchanged blocking alerts are present in the scan diff

Examples
$ socket scan diff aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 aaa1aa1a-aaaa-1111-1a1a-1111111a11a1
$ socket scan diff aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 aaa1aa1a-aaaa-1111-1a1a-1111111a11a1 --json"
$ socket scan diff aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 aaa1aa1a-aaaa-1111-1a1a-1111111a11a1 --json
$ socket scan diff aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 aaa1aa1a-aaaa-1111-1a1a-1111111a11a1 --strict-blocking"
`,
)
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
Expand Down
3 changes: 3 additions & 0 deletions src/commands/scan/handle-diff-scan.mts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ export async function handleDiffScan({
id2,
orgSlug,
outputKind,
strictBlocking,
}: {
depth: number
file: string
id1: string
id2: string
orgSlug: string
outputKind: OutputKind
strictBlocking: boolean
}): Promise<void> {
const data = await fetchDiffScan({
id1,
Expand All @@ -28,5 +30,6 @@ export async function handleDiffScan({
depth,
file,
outputKind,
strictBlocking,
})
}
24 changes: 24 additions & 0 deletions src/commands/scan/output-diff-scan.mts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ export async function outputDiffScan(
depth,
file,
outputKind,
strictBlocking,
}: {
depth: number
file: string
outputKind: OutputKind
strictBlocking: boolean
},
): Promise<void> {
if (!result.ok) {
Expand All @@ -51,6 +53,28 @@ export async function outputDiffScan(
return
}

const changedArtifacts = [
...result.data.artifacts.added,
...result.data.artifacts.updated,
...result.data.artifacts.replaced,
]
if (
changedArtifacts.some(artifact =>
artifact.alerts?.some(alert => alert.action === 'error'),
)
) {
process.exitCode = 1
}

if (strictBlocking) {
const unchangedBlocking = (result.data.artifacts.unchanged ?? []).filter(
artifact => artifact.alerts?.some(alert => alert.action === 'error'),
)
if (unchangedBlocking.length > 0) {
process.exitCode = 1
}
}
Comment thread
jdalton marked this conversation as resolved.
Resolved

const dashboardUrl = result.data.diff_report_url
const dashboardMessage = dashboardUrl
? `\n View this diff scan in the Socket dashboard: ${colors.cyan(dashboardUrl)}`
Expand Down
84 changes: 84 additions & 0 deletions src/commands/scan/output-diff-scan.test.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { afterEach, describe, expect, it, vi } from 'vitest'

import { outputDiffScan } from './output-diff-scan.mts'

vi.mock('../../utils/logger.mts', () => ({
logger: { error: vi.fn(), fail: vi.fn(), info: vi.fn(), log: vi.fn() },
}))

const diff = {
after: { id: 'after' },
artifacts: {
added: [],
removed: [],
replaced: [],
unchanged: [
{
type: 'npm',
name: 'left-pad',
version: '1.0.0',
alerts: [{ action: 'error', type: 'test-alert', key: 'alert-1' }],
},
],
updated: [],
},
before: { id: 'before' },
directDependenciesChanged: false,
diff_report_url: '',
}

describe('outputDiffScan strict blocking', () => {
afterEach(() => {
process.exitCode = 0
vi.clearAllMocks()
})

it('fails for new blocking alerts in ordinary diff mode', async () => {
const changed = structuredClone(diff)
changed.artifacts.added.push({
type: 'npm',
name: 'new-package',
version: '1.0.0',
alerts: [{ action: 'error', type: 'test-alert', key: 'alert-2' }],
})
await outputDiffScan({ ok: true, data: changed } as never, {
depth: 0,
file: '',
outputKind: 'json',
strictBlocking: false,
})
expect(process.exitCode).toBe(1)
})

it('fails when an unchanged alert blocks under strict mode', async () => {
await outputDiffScan({ ok: true, data: diff } as never, {
depth: 0,
file: '',
outputKind: 'json',
strictBlocking: true,
})
expect(process.exitCode).toBe(1)
})

it('does not fail on unchanged blocking alerts by default', async () => {
await outputDiffScan({ ok: true, data: diff } as never, {
depth: 0,
file: '',
outputKind: 'json',
strictBlocking: false,
})
expect(process.exitCode).toBe(0)
})

it('does not fail when unchanged alerts are nonblocking', async () => {
const nonBlocking = structuredClone(diff)
nonBlocking.artifacts.unchanged[0]!.alerts![0]!.action = 'warn'
await outputDiffScan({ ok: true, data: nonBlocking } as never, {
depth: 0,
file: '',
outputKind: 'json',
strictBlocking: true,
})
expect(process.exitCode).toBe(0)
})
})
Loading
Loading