Skip to content
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
4 changes: 2 additions & 2 deletions src/commands/report/create-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { pluralize } from '@socketsecurity/registry/lib/words'

import constants from '../../constants'
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
import { getPackageFilesFullScans } from '../../utils/path-resolve'
import { getPackageFilesForScan } from '../../utils/path-resolve'
import { setupSdk } from '../../utils/sdk'

import type { SocketYml } from '@socketsecurity/config'
Expand Down Expand Up @@ -40,7 +40,7 @@ export async function createReport(
cause
})
})
const packagePaths = await getPackageFilesFullScans(
const packagePaths = await getPackageFilesForScan(
cwd,
inputPaths,
supportedFiles,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/report/view-report.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fetchReportData } from './fetch-report-data'
import { formatReportDataOutput } from './format-report-data'
import { getFullScan } from '../scan/get-full-scan'
import { fetchScan } from '../scan/fetch-scan'

import type { components } from '@socketsecurity/sdk/types/api'

Expand All @@ -21,7 +21,7 @@ export async function viewReport(
const result = await fetchReportData(reportId, all, strict)

const artifacts: Array<components['schemas']['SocketArtifact']> | undefined =
await getFullScan('socketdev', reportId)
await fetchScan('socketdev', reportId)

if (result) {
formatReportDataOutput(
Expand Down
92 changes: 70 additions & 22 deletions src/commands/scan/cmd-scan-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import colors from 'yoctocolors-cjs'

import { logger } from '@socketsecurity/registry/lib/logger'

import { createFullScan } from './create-full-scan'
import { handleCreateNewScan } from './handle-create-new-scan'
import { suggestOrgSlug } from './suggest-org-slug'
import { suggestRepoSlug } from './suggest-repo-slug'
import { suggestBranchSlug } from './suggest_branch_slug'
import { suggestTarget } from './suggest_target'
import constants from '../../constants'
import { meowOrExit } from '../../utils/meow-with-subcommands'
import { getFlagListOutput } from '../../utils/output-formatting'
Expand Down Expand Up @@ -142,27 +146,75 @@ async function run(
parentName
})

const [orgSlug = '', ...targets] = cli.input

const { cwd: cwdOverride, dryRun } = cli.flags
const cwd =
cli.flags['cwd'] && cli.flags['cwd'] !== 'process.cwd()'
? String(cli.flags['cwd'])
cwdOverride && cwdOverride !== 'process.cwd()'
? String(cwdOverride)
: process.cwd()
let { branch: branchName, repo: repoName } = cli.flags
let [orgSlug = '', ...targets] = cli.input

// We're going to need an api token to suggest data because those suggestions
// must come from data we already know. Don't error on missing api token yet.
// If the api-token is not set, ignore it for the sake of suggestions.
const apiToken = getDefaultToken()

// If we updated any inputs then we should print the command line to repeat
// the command without requiring user input, as a suggestion.
let updatedInput = false

if (!targets.length && !dryRun) {
const received = await suggestTarget()
targets = received ?? []
updatedInput = true
}

// If the current cwd is unknown and is used as a repo slug anyways, we will
// first need to register the slug before we can use it.
let repoDefaultBranch = ''
// Only do suggestions with an apiToken and when not in dryRun mode
if (apiToken && !dryRun) {
if (!orgSlug) {
const suggestion = await suggestOrgSlug()
if (suggestion) orgSlug = suggestion
updatedInput = true
}

// (Don't bother asking for the rest if we didn't get an org slug above)
if (orgSlug && !repoName) {
const suggestion = await suggestRepoSlug(orgSlug)
if (suggestion) {
repoDefaultBranch = suggestion.defaultBranch
repoName = suggestion.slug
}
updatedInput = true
}

const { branch: branchName, repo: repoName } = cli.flags
// (Don't bother asking for the rest if we didn't get an org/repo above)
if (orgSlug && repoName && !branchName) {
const suggestion = await suggestBranchSlug(repoDefaultBranch)
if (suggestion) branchName = suggestion
updatedInput = true
}
}

const apiToken = getDefaultToken() // This checks if we _can_ suggest anything
if (updatedInput && repoName && branchName && orgSlug && targets?.length) {
logger.error(
'Note: You can invoke this command next time to skip the interactive questions:'
)
logger.error('```')
logger.error(
` socket scan create [other flags...] --repo ${repoName} --branch ${branchName} ${orgSlug} ${targets.join(' ')}`
)
logger.error('```\n')
}

if (!apiToken && (!orgSlug || !repoName || !branchName || !targets.length)) {
// Without api token we cannot recover because we can't request more info
// from the server, to match and help with the current cwd/git status.
//
if (!orgSlug || !repoName || !branchName || !targets.length) {
// Use exit status of 2 to indicate incorrect usage, generally invalid
// options or missing arguments.
// https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
process.exitCode = 2
logger.fail(
stripIndents`
logger.fail(stripIndents`
${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:

- Org name as the first argument ${!orgSlug ? colors.red('(missing!)') : colors.green('(ok)')}
Expand All @@ -171,30 +223,26 @@ async function run(

- Branch name using --branch ${!branchName ? colors.red('(missing!)') : colors.green('(ok)')}

- At least one TARGET (e.g. \`.\` or \`./package.json\`) ${!targets.length ? '(missing)' : colors.green('(ok)')}
- At least one TARGET (e.g. \`.\` or \`./package.json\`) ${!targets.length ? colors.red('(missing)') : colors.green('(ok)')}

(Additionally, no API Token was set so we cannot auto-discover these details)
`
)
${!apiToken ? 'Note: was unable to make suggestions because no API Token was found; this would make the command fail regardless' : ''}
`)
return
}

// Note exiting earlier to skirt a hidden auth requirement
if (cli.flags['dryRun']) {
if (dryRun) {
logger.log(DRY_RUN_BAIL_TEXT)
return
}

await createFullScan({
await handleCreateNewScan({
branchName: branchName as string,
commitHash: (cli.flags['commitHash'] as string) ?? '',
commitMessage: (cli.flags['commitMessage'] as string) ?? '',
committers: (cli.flags['committers'] as string) ?? '',
cwd,
defaultBranch: Boolean(cli.flags['defaultBranch']),
orgSlug,
pendingHead: Boolean(cli.flags['pendingHead']),
pullRequest: (cli.flags['pullRequest'] as number) ?? undefined,
readOnly: Boolean(cli.flags['readOnly']),
repoName: repoName as string,
targets,
Expand Down
10 changes: 5 additions & 5 deletions src/commands/scan/cmd-scan-del.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'

import { logger } from '@socketsecurity/registry/lib/logger'

import { deleteOrgFullScan } from './delete-full-scan'
import { handleDeleteScan } from './handle-delete-scan'
import constants from '../../constants'
import { commonFlags, outputFlags } from '../../flags'
import { meowOrExit } from '../../utils/meow-with-subcommands'
Expand Down Expand Up @@ -51,9 +51,9 @@ async function run(
parentName
})

const [orgSlug = '', fullScanId = ''] = cli.input
const [orgSlug = '', scanId = ''] = cli.input

if (!orgSlug || !fullScanId) {
if (!orgSlug || !scanId) {
// Use exit status of 2 to indicate incorrect usage, generally invalid
// options or missing arguments.
// https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
Expand All @@ -63,7 +63,7 @@ async function run(

- Org name as the first argument ${!orgSlug ? colors.red('(missing!)') : colors.green('(ok)')}

- Full Scan ID to delete as second argument ${!fullScanId ? colors.red('(missing!)') : colors.green('(ok)')}`
- Full Scan ID to delete as second argument ${!scanId ? colors.red('(missing!)') : colors.green('(ok)')}`
)
return
}
Expand All @@ -73,5 +73,5 @@ async function run(
return
}

await deleteOrgFullScan(orgSlug, fullScanId)
await handleDeleteScan(orgSlug, scanId)
}
4 changes: 2 additions & 2 deletions src/commands/scan/cmd-scan-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'

import { logger } from '@socketsecurity/registry/lib/logger'

import { listFullScans } from './list-full-scans'
import { handleListScans } from './handle-list-scans'
import constants from '../../constants'
import { commonFlags, outputFlags } from '../../flags'
import { meowOrExit } from '../../utils/meow-with-subcommands'
Expand Down Expand Up @@ -111,7 +111,7 @@ async function run(
return
}

await listFullScans({
await handleListScans({
direction: String(cli.flags['direction'] || ''),
from_time: String(cli.flags['fromTime'] || ''),
orgSlug,
Expand Down
12 changes: 6 additions & 6 deletions src/commands/scan/cmd-scan-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'

import { logger } from '@socketsecurity/registry/lib/logger'

import { getOrgScanMetadata } from './get-full-scan-metadata'
import { handleOrgScanMetadata } from './handle-scan-metadata'
import constants from '../../constants'
import { commonFlags, outputFlags } from '../../flags'
import { meowOrExit } from '../../utils/meow-with-subcommands'
Expand Down Expand Up @@ -54,9 +54,9 @@ async function run(
parentName
})

const [orgSlug = '', fullScanId = ''] = cli.input
const [orgSlug = '', scanId = ''] = cli.input

if (!orgSlug || !fullScanId) {
if (!orgSlug || !scanId) {
// Use exit status of 2 to indicate incorrect usage, generally invalid
// options or missing arguments.
// https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
Expand All @@ -66,7 +66,7 @@ async function run(

- Org name as the first argument ${!orgSlug ? colors.red('(missing!)') : colors.green('(ok)')}

- Full Scan ID to inspect as second argument ${!fullScanId ? colors.red('(missing!)') : colors.green('(ok)')}`
- Full Scan ID to inspect as second argument ${!scanId ? colors.red('(missing!)') : colors.green('(ok)')}`
)
return
}
Expand All @@ -76,9 +76,9 @@ async function run(
return
}

await getOrgScanMetadata(
await handleOrgScanMetadata(
orgSlug,
fullScanId,
scanId,
cli.flags['json'] ? 'json' : cli.flags['markdown'] ? 'markdown' : 'print'
)
}
12 changes: 6 additions & 6 deletions src/commands/scan/cmd-scan-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'

import { logger } from '@socketsecurity/registry/lib/logger'

import { reportFullScan } from './report-full-scan'
import { handleScanReport } from './handle-scan-report'
import constants from '../../constants'
import { commonFlags, outputFlags } from '../../flags'
import { meowOrExit } from '../../utils/meow-with-subcommands'
Expand Down Expand Up @@ -105,11 +105,11 @@ async function run(
security
} = cli.flags

const [orgSlug = '', fullScanId = '', file = '-'] = cli.input
const [orgSlug = '', scanId = '', file = '-'] = cli.input

if (
!orgSlug ||
!fullScanId ||
!scanId ||
// (!license && !security) ||
(json && markdown)
) {
Expand All @@ -123,7 +123,7 @@ async function run(

- Org name as the first argument ${!orgSlug ? colors.red('(missing!)') : colors.green('(ok)')}

- Full Scan ID to fetch as second argument ${!fullScanId ? colors.red('(missing!)') : colors.green('(ok)')}
- Full Scan ID to fetch as second argument ${!scanId ? colors.red('(missing!)') : colors.green('(ok)')}

- Not both the --json and --markdown flags ${json && markdown ? colors.red('(pick one!)') : colors.green('(ok)')}
`
Expand All @@ -137,9 +137,9 @@ async function run(
return
}

await reportFullScan({
await handleScanReport({
orgSlug,
fullScanId,
scanId: scanId,
includeLicensePolicy: false, // !!license,
includeSecurityPolicy: typeof security === 'boolean' ? security : true,
outputKind: json ? 'json' : markdown ? 'markdown' : 'text',
Expand Down
14 changes: 7 additions & 7 deletions src/commands/scan/cmd-scan-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import colors from 'yoctocolors-cjs'

import { logger } from '@socketsecurity/registry/lib/logger'

import { streamFullScan } from './stream-full-scan'
import { viewFullScan } from './view-full-scan'
import { handleScanView } from './handle-scan-view'
import { streamScan } from './streamScan'
import constants from '../../constants'
import { commonFlags, outputFlags } from '../../flags'
import { meowOrExit } from '../../utils/meow-with-subcommands'
Expand Down Expand Up @@ -57,9 +57,9 @@ async function run(
parentName
})

const [orgSlug = '', fullScanId = '', file = '-'] = cli.input
const [orgSlug = '', scanId = '', file = '-'] = cli.input

if (!orgSlug || !fullScanId) {
if (!orgSlug || !scanId) {
// Use exit status of 2 to indicate incorrect usage, generally invalid
// options or missing arguments.
// https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
Expand All @@ -70,7 +70,7 @@ async function run(

- Org name as the first argument ${!orgSlug ? colors.red('(missing!)') : colors.green('(ok)')}

- Full Scan ID to fetch as second argument ${!fullScanId ? colors.red('(missing!)') : colors.green('(ok)')}
- Full Scan ID to fetch as second argument ${!scanId ? colors.red('(missing!)') : colors.green('(ok)')}
`
)
return
Expand All @@ -82,8 +82,8 @@ async function run(
}

if (cli.flags['json']) {
await streamFullScan(orgSlug, fullScanId, file)
await streamScan(orgSlug, scanId, file)
} else {
await viewFullScan(orgSlug, fullScanId, file)
await handleScanView(orgSlug, scanId, file)
}
}
Loading
Loading