@@ -5,11 +5,15 @@ import colors from 'yoctocolors-cjs'
55
66import { 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'
913import constants from '../../constants'
1014import { meowOrExit } from '../../utils/meow-with-subcommands'
1115import { getFlagListOutput } from '../../utils/output-formatting'
12- import { getDefaultToken } from '../../utils/sdk'
16+ import { getDefaultToken , setupSdk } from '../../utils/sdk'
1317
1418import 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,
0 commit comments