Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
65 changes: 46 additions & 19 deletions src/commands/scan/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const create: CliSubcommand = {
}
const spinnerText = 'Creating a scan... \n'
const spinner = new Spinner({ text: spinnerText }).start()
await createFullScan(input, spinner, apiToken)
await createFullScan(input, spinner, apiToken, input.cwd)
}
}
}
Expand Down Expand Up @@ -59,6 +59,10 @@ const createFullScanFlags: { [key: string]: any } = {
default: '',
description: 'Commit hash'
},
cwd: {
type: 'string',
description: 'working directory, defaults to process.cwd()'
},
pullRequest: {
type: 'number',
shortFlag: 'pr',
Expand Down Expand Up @@ -93,6 +97,7 @@ const createFullScanFlags: { [key: string]: any } = {
// Internal functions

type CommandContext = {
cwd: string
orgSlug: string
repoName: string
branchName: string
Expand All @@ -118,13 +123,18 @@ async function setupCommand(
const cli = meow(
`
Usage
$ ${name} [...options]
$ ${name} [...options] <org> <TARGET> [TARGET ...]

Where TARGET is a FILE or DIR that _must_ be inside the CWD.

When a FILE is given only that FILE is targeted. Otherwise any eligible
files in the given DIR will be considered.

Options
${getFlagListOutput(flags, 6)}

Examples
$ ${name} --org=FakeOrg --repo=test-repo --branch=main ./package.json
$ ${name} --repo=test-repo --branch=main FakeOrg ./package.json
`,
{
argv,
Expand All @@ -133,16 +143,9 @@ async function setupCommand(
flags
}
)

let showHelp = cli.flags['help']
if (!cli.input[0]) {
showHelp = true
}
if (showHelp) {
cli.showHelp()
return
}
const { 0: orgSlug = '' } = cli.input
const cwd = process.cwd()

const socketSdk = await setupSdk()
const supportedFiles = await socketSdk
.getReportSupportedFiles()
Expand All @@ -164,24 +167,46 @@ async function setupCommand(
}
)

// TODO: I think the cwd should be set to the DIR|FILE arg of this command and the DIR/FILE be either '.' or the filename() of the arg
const cwd =
cli.flags['cwd'] && cli.flags['cwd'] !== 'process.cwd()'
? String(cli.flags['cwd'])
: process.cwd()

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

const packagePaths = await getPackageFilesFullScans(
cwd,
cli.input,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would have sent the org as part of potential folders (which probably would lead to an error? or accidental include maybe?). Not sure how blocking this was but it's fixed now.

targets,
supportedFiles
)

const { branch: branchName, repo: repoName } = cli.flags
if (!repoName || !branchName || !packagePaths.length) {
if (!orgSlug || !repoName || !branchName || !packagePaths.length) {
showHelp = true
console.error(`${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:\n
- Repository name using --repo ${!repoName ? colors.red('(missing!)') : colors.green('(ok)')}\n
- Branch name using --branch ${!branchName ? colors.red('(missing!)') : colors.green('(ok)')}\n
- At least one file path (e.g. ./package.json) ${!packagePaths.length ? colors.red('(missing or no matching/supported files found!)') : colors.green('(ok)')}`)
- Org name as first arg ${!orgSlug ? colors.red('(missing!)') : colors.green('(ok)')}\n
- Repository name using --repo ${!repoName ? colors.red('(missing!)') : colors.green('(ok)')}\n
- Branch name using --branch ${!branchName ? colors.red('(missing!)') : colors.green('(ok)')}\n
- At least one file path (e.g. ./package.json) ${
!packagePaths.length
? colors.red(
targets.length > 0
? '(TARGET' +
(targets.length ? 's' : '') +
' contained no matching/supported files!)'
: '(missing)'
)
: colors.green('(ok)')
}`)
}
if (showHelp) {
cli.showHelp()
return
}

return <CommandContext>{
cwd,
orgSlug,
repoName,
branchName,
Expand All @@ -199,7 +224,8 @@ async function setupCommand(
async function createFullScan(
input: CommandContext,
spinner: Spinner,
apiToken: string
apiToken: string,
cwd: string = process.cwd()
): Promise<void> {
const socketSdk = await setupSdk(apiToken)
const {
Expand All @@ -223,7 +249,8 @@ async function createFullScan(
set_as_pending_head: pendingHead,
tmp
},
packagePaths
packagePaths,
cwd
),
'Creating scan'
)
Expand Down
22 changes: 15 additions & 7 deletions src/utils/path-resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ async function filterGlobResultToSupportedFiles(
entries: string[],
supportedFiles: SocketSdkReturnType<'getReportSupportedFiles'>['data']
): Promise<string[]> {
const patterns = ['golang', NPM, 'pypi'].reduce((r: string[], n: string) => {
const supported = supportedFiles[n]
r.push(
...(supported ? Object.values(supported).map(p => `**/${p.pattern}`) : [])
)
return r
}, [])
const patterns = ['golang', NPM, 'maven', 'pypi'].reduce(
(r: string[], n: string) => {
const supported = supportedFiles[n]
r.push(
...(supported
? Object.values(supported).map(p => `**/${p.pattern}`)
: [])
)
return r
},
[]
)
return entries.filter(p => micromatch.some(p, patterns))
}

Expand Down Expand Up @@ -84,6 +89,9 @@ async function globWithGitIgnore(
return result
}
const { absolute } = globOptions

// Note: the input files must be INSIDE the cwd. If you get strange looking
// relative path errors here, most likely your path is outside the given cwd.
const filtered = ignore()
.add(ignores)
.filter(absolute ? result.map(p => path.relative(cwd, p)) : result)
Expand Down
Loading