Skip to content

Commit bc3702a

Browse files
authored
Add socket scan setup for flag defaults (#621)
* Add `socket scan setup` for flag defaults * ok * what will it find next
1 parent ef04e03 commit bc3702a

14 files changed

+724
-79
lines changed

src/commands/manifest/setup-manifest-config.mts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,11 @@ async function askForStdout(
365365
{
366366
name: 'no',
367367
value: 'no',
368-
selected: defaultValue === false,
369368
description: 'Write output to a file, not stdout',
370369
},
371370
{
372371
name: 'yes',
373372
value: 'yes',
374-
selected: defaultValue === true,
375373
description: 'Print in stdout (this will supersede --out)',
376374
},
377375
{
@@ -394,13 +392,11 @@ async function askForEnabled(
394392
{
395393
name: 'Enable',
396394
value: true,
397-
selected: defaultValue === true,
398395
description: 'Generate manifest files for this language when detected',
399396
},
400397
{
401398
name: 'Disable',
402399
value: false,
403-
selected: defaultValue === false,
404400
description:
405401
'Do not generate manifest files for this language when detected, unless explicitly asking for it',
406402
},
@@ -461,13 +457,11 @@ async function askForVerboseFlag(
461457
{
462458
name: 'no',
463459
value: 'no',
464-
selected: current === false,
465460
description: 'Do not run this manifest in verbose mode',
466461
},
467462
{
468463
name: 'yes',
469464
value: 'yes',
470-
selected: current === true,
471465
description: 'Run this manifest in verbose mode',
472466
},
473467
{

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

Lines changed: 73 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import path from 'node:path'
2+
13
import { logger } from '@socketsecurity/registry/lib/logger'
24

35
import { handleCreateNewScan } from './handle-create-new-scan.mts'
@@ -28,14 +30,12 @@ const config: CliCommandConfig = {
2830
...outputFlags,
2931
autoManifest: {
3032
type: 'boolean',
31-
default: false,
3233
description:
3334
'Run `socket manifest auto` before collecting manifest files? This would be necessary for languages like Scala, Gradle, and Kotlin, See `socket manifest auto --help`.',
3435
},
3536
branch: {
3637
type: 'string',
3738
shortFlag: 'b',
38-
default: 'socket-default-branch',
3939
description: 'Branch name',
4040
},
4141
commitMessage: {
@@ -91,12 +91,10 @@ const config: CliCommandConfig = {
9191
repo: {
9292
type: 'string',
9393
shortFlag: 'r',
94-
default: 'socket-default-repository',
9594
description: 'Repository name',
9695
},
9796
report: {
9897
type: 'boolean',
99-
default: false,
10098
description:
10199
'Wait for the scan creation to complete, then basically run `socket scan report` on it',
102100
},
@@ -118,25 +116,29 @@ const config: CliCommandConfig = {
118116
// TODO: your project's "socket.yml" file's "projectIgnorePaths"
119117
help: (command, config) => `
120118
Usage
121-
$ ${command} [...options]${isTestingV1() ? '' : ' <org>'} <TARGET> [TARGET...]
119+
$ ${command} [options]${isTestingV1() ? '' : ' <org>'} [TARGET...]
122120
123121
API Token Requirements
124122
- Quota: 1 unit
125123
- Permissions: full-scans:create
126124
127-
Uploads the specified "package.json" and lock files for JavaScript, Python,
128-
Go, Scala, Gradle, and Kotlin dependency manifests.
129-
If any folder is specified, the ones found in there recursively are uploaded.
130-
131-
Supports globbing such as "**/package.json", "**/requirements.txt", etc.
125+
Options
126+
${getFlagListOutput(config.flags, 6)}
132127
133-
Ignores any file specified in your project's ".gitignore" and also has a
134-
sensible set of default ignores from the "ignore-by-default" module.
128+
Uploads the specified dependency manifest files for Go, Gradle, JavaScript,
129+
Kotlin, Python, and Scala. Files like "package.json" and "requirements.txt".
130+
If any folder is specified, the ones found in there recursively are uploaded.
135131
136-
TARGET should be a FILE or DIR that _must_ be inside the CWD.
132+
Details on TARGET:
137133
138-
When a FILE is given only that FILE is targeted. Otherwise any eligible
139-
files in the given DIR will be considered.
134+
- Defaults to the current dir (cwd) if none given
135+
- Multiple targets can be specified
136+
- If a target is a file, only that file is checked
137+
- If it is a dir, the dir is scanned for any supported manifest files
138+
- Dirs MUST be within the current dir (cwd), you can use --cwd to change it
139+
- Supports globbing such as "**/package.json", "**/requirements.txt", etc.
140+
- Ignores any file specified in your project's ".gitignore"
141+
- Also a sensible set of default ignores from the "ignore-by-default" module
140142
141143
The --repo and --branch flags tell Socket to associate this Scan with that
142144
repo/branch. The names will show up on your dashboard on the Socket website.
@@ -151,11 +153,10 @@ const config: CliCommandConfig = {
151153
this by using --no-setAsAlertsPage. This flag is ignored for any branch that
152154
is not designated as the "default branch". It is disabled when using --tmp.
153155
154-
Options
155-
${getFlagListOutput(config.flags, 6)}
156+
You can use \`socket scan setup\` to configure certain repo flag defaults.
156157
157158
Examples
158-
$ ${command}${isTestingV1() ? '' : ' FakeOrg'} .
159+
$ ${command}${isTestingV1() ? '' : ' FakeOrg'}
159160
$ ${command} --repo=test-repo --branch=main${isTestingV1() ? '' : ' FakeOrg'} ./package.json
160161
`,
161162
}
@@ -179,8 +180,6 @@ async function run(
179180
})
180181

181182
const {
182-
autoManifest = false,
183-
branch: branchName = 'socket-default-branch',
184183
commitHash,
185184
commitMessage,
186185
committers,
@@ -193,13 +192,9 @@ async function run(
193192
org: orgFlag,
194193
pullRequest,
195194
readOnly,
196-
repo: repoName = 'socket-default-repository',
197-
report,
198195
setAsAlertsPage: pendingHeadFlag,
199196
tmp,
200197
} = cli.flags as {
201-
autoManifest: boolean
202-
branch: string
203198
cwd: string
204199
commitHash: string
205200
commitMessage: string
@@ -212,11 +207,20 @@ async function run(
212207
org: string
213208
pullRequest: number
214209
readOnly: boolean
215-
repo: string
216-
report: boolean
217210
setAsAlertsPage: boolean
218211
tmp: boolean
219212
}
213+
let {
214+
autoManifest,
215+
branch: branchName,
216+
repo: repoName,
217+
report,
218+
} = cli.flags as {
219+
autoManifest?: boolean
220+
branch: string
221+
repo: string
222+
report?: boolean
223+
}
220224
const outputKind = getOutputKind(json, markdown)
221225

222226
const pendingHead = tmp ? false : pendingHeadFlag
@@ -232,13 +236,54 @@ async function run(
232236
defaultOrgSlug = ''
233237
}
234238

235-
let targets = cli.input.slice(isTestingV1() || defaultOrgSlug ? 0 : 1)
239+
// Accept zero or more paths. Default to cwd() if none given.
240+
let targets =
241+
cli.input.slice(isTestingV1() || defaultOrgSlug ? 0 : 1) || process.cwd()
236242

237243
const cwd =
238244
cwdOverride && cwdOverride !== 'process.cwd()'
239-
? String(cwdOverride)
245+
? path.resolve(process.cwd(), String(cwdOverride))
240246
: process.cwd()
241247

248+
const socketJson = await readOrDefaultSocketJson(cwd)
249+
250+
// Note: This needs meow booleanDefault=undefined
251+
if (typeof autoManifest !== 'boolean') {
252+
if (socketJson.defaults?.scan?.create?.autoManifest !== undefined) {
253+
autoManifest = socketJson.defaults.scan.create.autoManifest
254+
logger.info(
255+
'Using default --autoManifest from socket.json:',
256+
autoManifest,
257+
)
258+
} else {
259+
autoManifest = false
260+
}
261+
}
262+
if (!branchName) {
263+
if (socketJson.defaults?.scan?.create?.branch) {
264+
branchName = socketJson.defaults.scan.create.branch
265+
logger.info('Using default --branch from socket.json:', branchName)
266+
} else {
267+
branchName = 'socket-default-branch'
268+
}
269+
}
270+
if (!repoName) {
271+
if (socketJson.defaults?.scan?.create?.repo) {
272+
repoName = socketJson.defaults.scan.create.repo
273+
logger.info('Using default --repo from socket.json:', repoName)
274+
} else {
275+
repoName = 'socket-default-repository'
276+
}
277+
}
278+
if (typeof report !== 'boolean') {
279+
if (socketJson.defaults?.scan?.create?.report !== undefined) {
280+
report = socketJson.defaults.scan.create.report
281+
logger.info('Using default --report from socket.json:', report)
282+
} else {
283+
report = false
284+
}
285+
}
286+
242287
// We're going to need an api token to suggest data because those suggestions
243288
// must come from data we already know. Don't error on missing api token yet.
244289
// If the api-token is not set, ignore it for the sake of suggestions.
@@ -267,8 +312,6 @@ async function run(
267312
}
268313
}
269314

270-
const socketJson = await readOrDefaultSocketJson(cwd)
271-
272315
const detected = await detectManifestActions(socketJson, cwd)
273316
if (detected.count > 0 && !autoManifest) {
274317
logger.info(

src/commands/scan/cmd-scan-create.test.mts

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,12 @@ describe('socket scan create', async () => {
1818
"Create a scan
1919
2020
Usage
21-
$ socket scan create [...options] <org> <TARGET> [TARGET...]
21+
$ socket scan create [options] <org> [TARGET...]
2222
2323
API Token Requirements
2424
- Quota: 1 unit
2525
- Permissions: full-scans:create
2626
27-
Uploads the specified "package.json" and lock files for JavaScript, Python,
28-
Go, Scala, Gradle, and Kotlin dependency manifests.
29-
If any folder is specified, the ones found in there recursively are uploaded.
30-
31-
Supports globbing such as "**/package.json", "**/requirements.txt", etc.
32-
33-
Ignores any file specified in your project's ".gitignore" and also has a
34-
sensible set of default ignores from the "ignore-by-default" module.
35-
36-
TARGET should be a FILE or DIR that _must_ be inside the CWD.
37-
38-
When a FILE is given only that FILE is targeted. Otherwise any eligible
39-
files in the given DIR will be considered.
40-
41-
The --repo and --branch flags tell Socket to associate this Scan with that
42-
repo/branch. The names will show up on your dashboard on the Socket website.
43-
44-
Note: for a first run you probably want to set --defaultBranch to indicate
45-
the default branch name, like "main" or "master".
46-
47-
The "alerts page" (https://socket.dev/dashboard/org/YOURORG/alerts) will show
48-
the results from the last scan designated as the "pending head" on the branch
49-
configured on Socket to be the "default branch". When creating a scan the
50-
--setAsAlertsPage flag will default to true to update this. You can prevent
51-
this by using --no-setAsAlertsPage. This flag is ignored for any branch that
52-
is not designated as the "default branch". It is disabled when using --tmp.
53-
5427
Options
5528
--autoManifest Run \`socket manifest auto\` before collecting manifest files? This would be necessary for languages like Scala, Gradle, and Kotlin, See \`socket manifest auto --help\`.
5629
--branch Branch name
@@ -71,8 +44,38 @@ describe('socket scan create', async () => {
7144
--setAsAlertsPage When true and if this is the "default branch" then this Scan will be the one reflected on your alerts page. See help for details. Defaults to true.
7245
--tmp Set the visibility (true/false) of the scan in your dashboard.
7346
47+
Uploads the specified dependency manifest files for Go, Gradle, JavaScript,
48+
Kotlin, Python, and Scala. Files like "package.json" and "requirements.txt".
49+
If any folder is specified, the ones found in there recursively are uploaded.
50+
51+
Details on TARGET:
52+
53+
- Defaults to the current dir (cwd) if none given
54+
- Multiple targets can be specified
55+
- If a target is a file, only that file is checked
56+
- If it is a dir, the dir is scanned for any supported manifest files
57+
- Dirs MUST be within the current dir (cwd), you can use --cwd to change it
58+
- Supports globbing such as "**/package.json", "**/requirements.txt", etc.
59+
- Ignores any file specified in your project's ".gitignore"
60+
- Also a sensible set of default ignores from the "ignore-by-default" module
61+
62+
The --repo and --branch flags tell Socket to associate this Scan with that
63+
repo/branch. The names will show up on your dashboard on the Socket website.
64+
65+
Note: for a first run you probably want to set --defaultBranch to indicate
66+
the default branch name, like "main" or "master".
67+
68+
The "alerts page" (https://socket.dev/dashboard/org/YOURORG/alerts) will show
69+
the results from the last scan designated as the "pending head" on the branch
70+
configured on Socket to be the "default branch". When creating a scan the
71+
--setAsAlertsPage flag will default to true to update this. You can prevent
72+
this by using --no-setAsAlertsPage. This flag is ignored for any branch that
73+
is not designated as the "default branch". It is disabled when using --tmp.
74+
75+
You can use \`socket scan setup\` to configure certain repo flag defaults.
76+
7477
Examples
75-
$ socket scan create FakeOrg .
78+
$ socket scan create FakeOrg
7679
$ socket scan create --repo=test-repo --branch=main FakeOrg ./package.json"
7780
`)
7881
expect(`\n ${stderr}`).toMatchInlineSnapshot(`

0 commit comments

Comments
 (0)