Skip to content

Commit 05599f6

Browse files
authored
scan create: allow maven files, include cwd option (#319)
1 parent 7ab93cc commit 05599f6

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed

src/commands/scan/cmd-create.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ const config: CliCommandConfig = {
4343
default: '',
4444
description: 'Commit hash'
4545
},
46+
cwd: {
47+
type: 'string',
48+
description: 'working directory, defaults to process.cwd()'
49+
},
4650
pullRequest: {
4751
type: 'number',
4852
shortFlag: 'pr',
@@ -76,13 +80,18 @@ const config: CliCommandConfig = {
7680
},
7781
help: (parentName, config) => `
7882
Usage
79-
$ ${parentName} ${config.commandName} [...options] <org>
83+
$ ${parentName} ${config.commandName} [...options] <org> <TARGET> [TARGET...]
84+
85+
Where TARGET is a FILE or DIR that _must_ be inside the CWD.
86+
87+
When a FILE is given only that FILE is targeted. Otherwise any eligible
88+
files in the given DIR will be considered.
8089
8190
Options
8291
${getFlagListOutput(config.flags, 6)}
8392
8493
Examples
85-
$ ${parentName} ${config.commandName} --org=FakeOrg --repo=test-repo --branch=main ./package.json
94+
$ ${parentName} ${config.commandName} --repo=test-repo --branch=main FakeOrg ./package.json
8695
`
8796
}
8897

@@ -104,8 +113,12 @@ async function run(
104113
flags: config.flags
105114
})
106115

107-
const orgSlug = cli.input[0] ?? '' // TODO: if nobody uses this then get rid of it in favor of --org
108-
const cwd = process.cwd()
116+
const [orgSlug = '', ...targets] = cli.input
117+
118+
const cwd =
119+
cli.flags['cwd'] && cli.flags['cwd'] !== 'process.cwd()'
120+
? String(cli.flags['cwd'])
121+
: process.cwd()
109122

110123
const socketSdk = await setupSdk()
111124
const supportedFiles = await socketSdk
@@ -126,18 +139,28 @@ async function run(
126139

127140
const packagePaths = await getPackageFilesFullScans(
128141
cwd,
129-
cli.input,
142+
targets,
130143
supportedFiles
131144
)
132145

133146
const { branch: branchName, repo: repoName } = cli.flags
134147

135148
if (!orgSlug || !repoName || !branchName || !packagePaths.length) {
136149
console.error(`${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:\n
137-
- Org name as the argument ${!orgSlug ? colors.red('(missing!)') : colors.green('(ok)')}\n
150+
- Org name as the first argument ${!orgSlug ? colors.red('(missing!)') : colors.green('(ok)')}\n
138151
- Repository name using --repo ${!repoName ? colors.red('(missing!)') : colors.green('(ok)')}\n
139152
- Branch name using --branch ${!branchName ? colors.red('(missing!)') : colors.green('(ok)')}\n
140-
- At least one file path (e.g. ./package.json) ${!packagePaths.length ? colors.red('(missing or no matching/supported files found!)') : colors.green('(ok)')}`)
153+
- At least one TARGET (e.g. \`.\` or \`./package.json\`) ${
154+
!packagePaths.length
155+
? colors.red(
156+
targets.length > 0
157+
? '(TARGET' +
158+
(targets.length ? 's' : '') +
159+
' contained no matching/supported files!)'
160+
: '(missing)'
161+
)
162+
: colors.green('(ok)')
163+
}`)
141164
config.help(parentName, config)
142165
return
143166
}
@@ -159,6 +182,7 @@ async function run(
159182
pendingHead: Boolean(cli.flags['pendingHead']),
160183
tmp: Boolean(cli.flags['tmp']),
161184
packagePaths,
185+
cwd,
162186
commitHash: (cli.flags['commitHash'] as string) ?? '',
163187
committers: (cli.flags['committers'] as string) ?? '',
164188
pullRequest: (cli.flags['pullRequest'] as number) ?? undefined

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export async function createFullScan({
1515
commitHash: _commitHash,
1616
commitMessage,
1717
committers: _committers,
18+
cwd,
1819
defaultBranch,
1920
orgSlug,
2021
packagePaths,
@@ -35,6 +36,7 @@ export async function createFullScan({
3536
pendingHead: boolean
3637
tmp: boolean
3738
packagePaths: string[]
39+
cwd: string
3840
}): Promise<void> {
3941
const spinnerText = 'Creating a scan... \n'
4042
const spinner = new Spinner({ text: spinnerText }).start()
@@ -51,7 +53,8 @@ export async function createFullScan({
5153
set_as_pending_head: pendingHead,
5254
tmp
5355
},
54-
packagePaths
56+
packagePaths,
57+
cwd
5558
),
5659
'Creating scan'
5760
)

src/utils/path-resolve.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,18 @@ async function filterGlobResultToSupportedFiles(
2525
entries: string[],
2626
supportedFiles: SocketSdkReturnType<'getReportSupportedFiles'>['data']
2727
): Promise<string[]> {
28-
const patterns = ['golang', NPM, 'pypi'].reduce((r: string[], n: string) => {
29-
const supported = supportedFiles[n]
30-
r.push(
31-
...(supported ? Object.values(supported).map(p => `**/${p.pattern}`) : [])
32-
)
33-
return r
34-
}, [])
28+
const patterns = ['golang', NPM, 'maven', 'pypi'].reduce(
29+
(r: string[], n: string) => {
30+
const supported = supportedFiles[n]
31+
r.push(
32+
...(supported
33+
? Object.values(supported).map(p => `**/${p.pattern}`)
34+
: [])
35+
)
36+
return r
37+
},
38+
[]
39+
)
3540
return entries.filter(p => micromatch.some(p, patterns))
3641
}
3742

@@ -84,6 +89,9 @@ async function globWithGitIgnore(
8489
return result
8590
}
8691
const { absolute } = globOptions
92+
93+
// Note: the input files must be INSIDE the cwd. If you get strange looking
94+
// relative path errors here, most likely your path is outside the given cwd.
8795
const filtered = ignore()
8896
.add(ignores)
8997
.filter(absolute ? result.map(p => path.relative(cwd, p)) : result)

0 commit comments

Comments
 (0)