Skip to content

Commit 4e6c2e0

Browse files
pvdzjdalton
authored andcommitted
Apply handle pattern to scan (#379)
* Apply handle pattern to scan * Improve suggestion handling
1 parent 17fd85b commit 4e6c2e0

36 files changed

+734
-583
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: 70 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ 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'
@@ -142,27 +146,75 @@ 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+
// Only do suggestions with an apiToken and when not in dryRun mode
176+
if (apiToken && !dryRun) {
177+
if (!orgSlug) {
178+
const suggestion = await suggestOrgSlug()
179+
if (suggestion) orgSlug = suggestion
180+
updatedInput = true
181+
}
182+
183+
// (Don't bother asking for the rest if we didn't get an org slug above)
184+
if (orgSlug && !repoName) {
185+
const suggestion = await suggestRepoSlug(orgSlug)
186+
if (suggestion) {
187+
repoDefaultBranch = suggestion.defaultBranch
188+
repoName = suggestion.slug
189+
}
190+
updatedInput = true
191+
}
151192

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

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

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-
//
212+
if (!orgSlug || !repoName || !branchName || !targets.length) {
160213
// Use exit status of 2 to indicate incorrect usage, generally invalid
161214
// options or missing arguments.
162215
// https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
163216
process.exitCode = 2
164-
logger.fail(
165-
stripIndents`
217+
logger.fail(stripIndents`
166218
${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:
167219
168220
- Org name as the first argument ${!orgSlug ? colors.red('(missing!)') : colors.green('(ok)')}
@@ -171,30 +223,26 @@ async function run(
171223
172224
- Branch name using --branch ${!branchName ? colors.red('(missing!)') : colors.green('(ok)')}
173225
174-
- At least one TARGET (e.g. \`.\` or \`./package.json\`) ${!targets.length ? '(missing)' : colors.green('(ok)')}
226+
- At least one TARGET (e.g. \`.\` or \`./package.json\`) ${!targets.length ? colors.red('(missing)') : colors.green('(ok)')}
175227
176-
(Additionally, no API Token was set so we cannot auto-discover these details)
177-
`
178-
)
228+
${!apiToken ? 'Note: was unable to make suggestions because no API Token was found; this would make the command fail regardless' : ''}
229+
`)
179230
return
180231
}
181232

182233
// Note exiting earlier to skirt a hidden auth requirement
183-
if (cli.flags['dryRun']) {
234+
if (dryRun) {
184235
logger.log(DRY_RUN_BAIL_TEXT)
185236
return
186237
}
187238

188-
await createFullScan({
239+
await handleCreateNewScan({
189240
branchName: branchName as string,
190-
commitHash: (cli.flags['commitHash'] as string) ?? '',
191241
commitMessage: (cli.flags['commitMessage'] as string) ?? '',
192-
committers: (cli.flags['committers'] as string) ?? '',
193242
cwd,
194243
defaultBranch: Boolean(cli.flags['defaultBranch']),
195244
orgSlug,
196245
pendingHead: Boolean(cli.flags['pendingHead']),
197-
pullRequest: (cli.flags['pullRequest'] as number) ?? undefined,
198246
readOnly: Boolean(cli.flags['readOnly']),
199247
repoName: repoName as string,
200248
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)