Skip to content

Commit cf4ef17

Browse files
committed
Apply handle pattern to scan
1 parent ace6cae commit cf4ef17

34 files changed

+724
-568
lines changed

src/commands/report/create-report.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { pluralize } from '@socketsecurity/registry/lib/words'
33

44
import constants from '../../constants'
55
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
6-
import { getPackageFilesFullScans } from '../../utils/path-resolve'
6+
import { getPackageFilesForScan } from '../../utils/path-resolve'
77
import { setupSdk } from '../../utils/sdk'
88

99
import type { SocketYml } from '@socketsecurity/config'
@@ -40,7 +40,7 @@ export async function createReport(
4040
cause
4141
})
4242
})
43-
const packagePaths = await getPackageFilesFullScans(
43+
const packagePaths = await getPackageFilesForScan(
4444
cwd,
4545
inputPaths,
4646
supportedFiles,

src/commands/report/view-report.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { fetchReportData } from './fetch-report-data'
22
import { formatReportDataOutput } from './format-report-data'
3-
import { getFullScan } from '../scan/get-full-scan'
3+
import { fetchScan } from '../scan/fetch-scan'
44

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

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

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

2626
if (result) {
2727
formatReportDataOutput(

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

Lines changed: 74 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ import colors from 'yoctocolors-cjs'
55

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

8-
import { createFullScan } from './create-full-scan'
8+
import { handleCreateNewScan } from './handle-create-new-scan'
9+
import { suggestOrgSlug } from './suggest-org-slug'
10+
import { suggestRepoSlug } from './suggest-repo-slug'
11+
import { suggestBranchSlug } from './suggest_branch_slug'
12+
import { suggestTarget } from './suggest_target'
913
import constants from '../../constants'
1014
import { meowOrExit } from '../../utils/meow-with-subcommands'
1115
import { getFlagListOutput } from '../../utils/output-formatting'
12-
import { getDefaultToken } from '../../utils/sdk'
16+
import { getDefaultToken, setupSdk } from '../../utils/sdk'
1317

1418
import type { CliCommandConfig } from '../../utils/meow-with-subcommands'
1519

@@ -142,27 +146,78 @@ async function run(
142146
parentName
143147
})
144148

145-
const [orgSlug = '', ...targets] = cli.input
146-
149+
const { cwd: cwdOverride, dryRun } = cli.flags
147150
const cwd =
148-
cli.flags['cwd'] && cli.flags['cwd'] !== 'process.cwd()'
149-
? String(cli.flags['cwd'])
151+
cwdOverride && cwdOverride !== 'process.cwd()'
152+
? String(cwdOverride)
150153
: process.cwd()
154+
let { branch: branchName, repo: repoName } = cli.flags
155+
let [orgSlug = '', ...targets] = cli.input
156+
157+
// We're going to need an api token to suggest data because those suggestions
158+
// must come from data we already know. Don't error on missing api token yet.
159+
// If the api-token is not set, ignore it for the sake of suggestions.
160+
const apiToken = getDefaultToken()
161+
162+
// If we updated any inputs then we should print the command line to repeat
163+
// the command without requiring user input, as a suggestion.
164+
let updatedInput = false
165+
166+
if (!targets.length && !dryRun) {
167+
const received = await suggestTarget()
168+
targets = received ?? []
169+
updatedInput = true
170+
}
171+
172+
// If the current cwd is unknown and is used as a repo slug anyways, we will
173+
// first need to register the slug before we can use it.
174+
let repoDefaultBranch = ''
175+
176+
// Only do suggestions with an apiToken and when not in dryRun mode
177+
if (apiToken && !dryRun) {
178+
const socketSdk = await setupSdk()
179+
180+
if (!orgSlug) {
181+
const suggestion = await suggestOrgSlug(socketSdk)
182+
if (suggestion) orgSlug = suggestion
183+
updatedInput = true
184+
}
185+
186+
// (Don't bother asking for the rest if we didn't get an org slug above)
187+
if (orgSlug && !repoName) {
188+
const suggestion = await suggestRepoSlug(socketSdk, orgSlug)
189+
if (suggestion) {
190+
repoDefaultBranch = suggestion.defaultBranch
191+
repoName = suggestion.slug
192+
}
193+
updatedInput = true
194+
}
151195

152-
const { branch: branchName, repo: repoName } = cli.flags
196+
// (Don't bother asking for the rest if we didn't get an org/repo above)
197+
if (orgSlug && repoName && !branchName) {
198+
const suggestion = await suggestBranchSlug(repoDefaultBranch)
199+
if (suggestion) branchName = suggestion
200+
updatedInput = true
201+
}
202+
}
153203

154-
const apiToken = getDefaultToken() // This checks if we _can_ suggest anything
204+
if (updatedInput) {
205+
logger.error(
206+
'Note: You can invoke this command next time to skip the interactive questions:'
207+
)
208+
logger.error('```')
209+
logger.error(
210+
` socket scan create [other flags...] --repo ${repoName} --branch ${branchName} ${orgSlug} ${targets.join(' ')}`
211+
)
212+
logger.error('```')
213+
}
155214

156-
if (!apiToken && (!orgSlug || !repoName || !branchName || !targets.length)) {
157-
// Without api token we cannot recover because we can't request more info
158-
// from the server, to match and help with the current cwd/git status.
159-
//
215+
if (!orgSlug || !repoName || !branchName || !targets.length) {
160216
// Use exit status of 2 to indicate incorrect usage, generally invalid
161217
// options or missing arguments.
162218
// https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
163219
process.exitCode = 2
164-
logger.fail(
165-
stripIndents`
220+
logger.fail(stripIndents`
166221
${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:
167222
168223
- Org name as the first argument ${!orgSlug ? colors.red('(missing!)') : colors.green('(ok)')}
@@ -171,30 +226,26 @@ async function run(
171226
172227
- Branch name using --branch ${!branchName ? colors.red('(missing!)') : colors.green('(ok)')}
173228
174-
- At least one TARGET (e.g. \`.\` or \`./package.json\`) ${!targets.length ? '(missing)' : colors.green('(ok)')}
229+
- At least one TARGET (e.g. \`.\` or \`./package.json\`) ${!targets.length ? colors.red('(missing)') : colors.green('(ok)')}
175230
176-
(Additionally, no API Token was set so we cannot auto-discover these details)
177-
`
178-
)
231+
${!apiToken ? 'Note: was unable to make suggestions because no API Token was found; this would make the command fail regardless' : ''}
232+
`)
179233
return
180234
}
181235

182236
// Note exiting earlier to skirt a hidden auth requirement
183-
if (cli.flags['dryRun']) {
237+
if (dryRun) {
184238
logger.log(DRY_RUN_BAIL_TEXT)
185239
return
186240
}
187241

188-
await createFullScan({
242+
await handleCreateNewScan({
189243
branchName: branchName as string,
190-
commitHash: (cli.flags['commitHash'] as string) ?? '',
191244
commitMessage: (cli.flags['commitMessage'] as string) ?? '',
192-
committers: (cli.flags['committers'] as string) ?? '',
193245
cwd,
194246
defaultBranch: Boolean(cli.flags['defaultBranch']),
195247
orgSlug,
196248
pendingHead: Boolean(cli.flags['pendingHead']),
197-
pullRequest: (cli.flags['pullRequest'] as number) ?? undefined,
198249
readOnly: Boolean(cli.flags['readOnly']),
199250
repoName: repoName as string,
200251
targets,

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'
33

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

6-
import { deleteOrgFullScan } from './delete-full-scan'
6+
import { handleDeleteScan } from './handle-delete-scan'
77
import constants from '../../constants'
88
import { commonFlags, outputFlags } from '../../flags'
99
import { meowOrExit } from '../../utils/meow-with-subcommands'
@@ -51,9 +51,9 @@ async function run(
5151
parentName
5252
})
5353

54-
const [orgSlug = '', fullScanId = ''] = cli.input
54+
const [orgSlug = '', scanId = ''] = cli.input
5555

56-
if (!orgSlug || !fullScanId) {
56+
if (!orgSlug || !scanId) {
5757
// Use exit status of 2 to indicate incorrect usage, generally invalid
5858
// options or missing arguments.
5959
// https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
@@ -63,7 +63,7 @@ async function run(
6363
6464
- Org name as the first argument ${!orgSlug ? colors.red('(missing!)') : colors.green('(ok)')}
6565
66-
- Full Scan ID to delete as second argument ${!fullScanId ? colors.red('(missing!)') : colors.green('(ok)')}`
66+
- Full Scan ID to delete as second argument ${!scanId ? colors.red('(missing!)') : colors.green('(ok)')}`
6767
)
6868
return
6969
}
@@ -73,5 +73,5 @@ async function run(
7373
return
7474
}
7575

76-
await deleteOrgFullScan(orgSlug, fullScanId)
76+
await handleDeleteScan(orgSlug, scanId)
7777
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'
33

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

6-
import { listFullScans } from './list-full-scans'
6+
import { handleListScans } from './handle-list-scans'
77
import constants from '../../constants'
88
import { commonFlags, outputFlags } from '../../flags'
99
import { meowOrExit } from '../../utils/meow-with-subcommands'
@@ -111,7 +111,7 @@ async function run(
111111
return
112112
}
113113

114-
await listFullScans({
114+
await handleListScans({
115115
direction: String(cli.flags['direction'] || ''),
116116
from_time: String(cli.flags['fromTime'] || ''),
117117
orgSlug,

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'
33

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

6-
import { getOrgScanMetadata } from './get-full-scan-metadata'
6+
import { handleOrgScanMetadata } from './handle-scan-metadata'
77
import constants from '../../constants'
88
import { commonFlags, outputFlags } from '../../flags'
99
import { meowOrExit } from '../../utils/meow-with-subcommands'
@@ -54,9 +54,9 @@ async function run(
5454
parentName
5555
})
5656

57-
const [orgSlug = '', fullScanId = ''] = cli.input
57+
const [orgSlug = '', scanId = ''] = cli.input
5858

59-
if (!orgSlug || !fullScanId) {
59+
if (!orgSlug || !scanId) {
6060
// Use exit status of 2 to indicate incorrect usage, generally invalid
6161
// options or missing arguments.
6262
// https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
@@ -66,7 +66,7 @@ async function run(
6666
6767
- Org name as the first argument ${!orgSlug ? colors.red('(missing!)') : colors.green('(ok)')}
6868
69-
- Full Scan ID to inspect as second argument ${!fullScanId ? colors.red('(missing!)') : colors.green('(ok)')}`
69+
- Full Scan ID to inspect as second argument ${!scanId ? colors.red('(missing!)') : colors.green('(ok)')}`
7070
)
7171
return
7272
}
@@ -76,9 +76,9 @@ async function run(
7676
return
7777
}
7878

79-
await getOrgScanMetadata(
79+
await handleOrgScanMetadata(
8080
orgSlug,
81-
fullScanId,
81+
scanId,
8282
cli.flags['json'] ? 'json' : cli.flags['markdown'] ? 'markdown' : 'print'
8383
)
8484
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'
33

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

6-
import { reportFullScan } from './report-full-scan'
6+
import { handleScanReport } from './handle-scan-report'
77
import constants from '../../constants'
88
import { commonFlags, outputFlags } from '../../flags'
99
import { meowOrExit } from '../../utils/meow-with-subcommands'
@@ -105,11 +105,11 @@ async function run(
105105
security
106106
} = cli.flags
107107

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

110110
if (
111111
!orgSlug ||
112-
!fullScanId ||
112+
!scanId ||
113113
// (!license && !security) ||
114114
(json && markdown)
115115
) {
@@ -123,7 +123,7 @@ async function run(
123123
124124
- Org name as the first argument ${!orgSlug ? colors.red('(missing!)') : colors.green('(ok)')}
125125
126-
- Full Scan ID to fetch as second argument ${!fullScanId ? colors.red('(missing!)') : colors.green('(ok)')}
126+
- Full Scan ID to fetch as second argument ${!scanId ? colors.red('(missing!)') : colors.green('(ok)')}
127127
128128
- Not both the --json and --markdown flags ${json && markdown ? colors.red('(pick one!)') : colors.green('(ok)')}
129129
`
@@ -137,9 +137,9 @@ async function run(
137137
return
138138
}
139139

140-
await reportFullScan({
140+
await handleScanReport({
141141
orgSlug,
142-
fullScanId,
142+
scanId: scanId,
143143
includeLicensePolicy: false, // !!license,
144144
includeSecurityPolicy: typeof security === 'boolean' ? security : true,
145145
outputKind: json ? 'json' : markdown ? 'markdown' : 'text',

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import colors from 'yoctocolors-cjs'
33

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

6-
import { streamFullScan } from './stream-full-scan'
7-
import { viewFullScan } from './view-full-scan'
6+
import { handleScanView } from './handle-scan-view'
7+
import { streamScan } from './streamScan'
88
import constants from '../../constants'
99
import { commonFlags, outputFlags } from '../../flags'
1010
import { meowOrExit } from '../../utils/meow-with-subcommands'
@@ -57,9 +57,9 @@ async function run(
5757
parentName
5858
})
5959

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

62-
if (!orgSlug || !fullScanId) {
62+
if (!orgSlug || !scanId) {
6363
// Use exit status of 2 to indicate incorrect usage, generally invalid
6464
// options or missing arguments.
6565
// https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
@@ -70,7 +70,7 @@ async function run(
7070
7171
- Org name as the first argument ${!orgSlug ? colors.red('(missing!)') : colors.green('(ok)')}
7272
73-
- Full Scan ID to fetch as second argument ${!fullScanId ? colors.red('(missing!)') : colors.green('(ok)')}
73+
- Full Scan ID to fetch as second argument ${!scanId ? colors.red('(missing!)') : colors.green('(ok)')}
7474
`
7575
)
7676
return
@@ -82,8 +82,8 @@ async function run(
8282
}
8383

8484
if (cli.flags['json']) {
85-
await streamFullScan(orgSlug, fullScanId, file)
85+
await streamScan(orgSlug, scanId, file)
8686
} else {
87-
await viewFullScan(orgSlug, fullScanId, file)
87+
await handleScanView(orgSlug, scanId, file)
8888
}
8989
}

0 commit comments

Comments
 (0)