Skip to content

Commit 2ee545f

Browse files
committed
Cleanup ecosystems and reachability-flags
1 parent 33fd4f8 commit 2ee545f

11 files changed

Lines changed: 407 additions & 243 deletions

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

Lines changed: 121 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@ import { reachabilityFlags } from './reachability-flags.mts'
88
import { suggestOrgSlug } from './suggest-org-slug.mts'
99
import { suggestTarget } from './suggest_target.mts'
1010
import constants from '../../constants.mts'
11-
import { type MeowFlags, commonFlags, outputFlags } from '../../flags.mts'
11+
import { commonFlags, outputFlags } from '../../flags.mts'
1212
import { checkCommandInput } from '../../utils/check-input.mts'
1313
import { cmdFlagValueToArray } from '../../utils/cmd.mts'
1414
import { determineOrgSlug } from '../../utils/determine-org-slug.mts'
15-
import {
16-
type EcosystemString,
17-
getEcosystemChoicesForMeow,
18-
} from '../../utils/ecosystem.mts'
15+
import { getEcosystemChoicesForMeow } from '../../utils/ecosystem.mts'
1916
import { getOutputKind } from '../../utils/get-output-kind.mts'
2017
import { getRepoName, gitBranch } from '../../utils/git.mts'
2118
import { meowOrExit } from '../../utils/meow-with-subcommands.mts'
@@ -24,6 +21,8 @@ import { hasDefaultToken } from '../../utils/sdk.mts'
2421
import { readOrDefaultSocketJson } from '../../utils/socket-json.mts'
2522
import { detectManifestActions } from '../manifest/detect-manifest-actions.mts'
2623

24+
import type { MeowFlags } from '../../flags.mts'
25+
import type { PURL_Type } from '../../utils/ecosystem.mts'
2726
import type { CliCommandConfig } from '../../utils/meow-with-subcommands.mts'
2827

2928
const {
@@ -32,119 +31,110 @@ const {
3231
SOCKET_DEFAULT_REPOSITORY,
3332
} = constants
3433

34+
const generalFlags: MeowFlags = {
35+
...commonFlags,
36+
...outputFlags,
37+
autoManifest: {
38+
type: 'boolean',
39+
description:
40+
'Run `socket manifest auto` before collecting manifest files. This is necessary for languages like Scala, Gradle, and Kotlin, See `socket manifest auto --help`.',
41+
},
42+
branch: {
43+
type: 'string',
44+
shortFlag: 'b',
45+
description: 'Branch name',
46+
},
47+
commitHash: {
48+
type: 'string',
49+
shortFlag: 'ch',
50+
default: '',
51+
description: 'Commit hash',
52+
},
53+
commitMessage: {
54+
type: 'string',
55+
shortFlag: 'm',
56+
default: '',
57+
description: 'Commit message',
58+
},
59+
committers: {
60+
type: 'string',
61+
shortFlag: 'c',
62+
default: '',
63+
description: 'Committers',
64+
},
65+
cwd: {
66+
type: 'string',
67+
description: 'working directory, defaults to process.cwd()',
68+
},
69+
defaultBranch: {
70+
type: 'boolean',
71+
default: false,
72+
description:
73+
'Set the default branch of the repository to the branch of this full-scan. Should only need to be done once, for example for the "main" or "master" branch.',
74+
},
75+
interactive: {
76+
type: 'boolean',
77+
default: true,
78+
description:
79+
'Allow for interactive elements, asking for input. Use --no-interactive to prevent any input questions, defaulting them to cancel/no.',
80+
},
81+
pullRequest: {
82+
type: 'number',
83+
shortFlag: 'pr',
84+
description: 'Commit hash',
85+
},
86+
org: {
87+
type: 'string',
88+
description:
89+
'Force override the organization slug, overrides the default org from config',
90+
},
91+
reach: {
92+
type: 'boolean',
93+
default: false,
94+
description: 'Run tier 1 full application reachability analysis',
95+
},
96+
readOnly: {
97+
type: 'boolean',
98+
default: false,
99+
description:
100+
'Similar to --dry-run except it can read from remote, stops before it would create an actual report',
101+
},
102+
repo: {
103+
type: 'string',
104+
shortFlag: 'r',
105+
description: 'Repository name',
106+
},
107+
report: {
108+
type: 'boolean',
109+
description:
110+
'Wait for the scan creation to complete, then basically run `socket scan report` on it',
111+
},
112+
setAsAlertsPage: {
113+
type: 'boolean',
114+
default: true,
115+
aliases: ['pendingHead'],
116+
description:
117+
'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.',
118+
},
119+
tmp: {
120+
type: 'boolean',
121+
shortFlag: 't',
122+
default: false,
123+
description:
124+
'Set the visibility (true/false) of the scan in your dashboard.',
125+
},
126+
}
127+
35128
const config: CliCommandConfig = {
36129
commandName: 'create',
37130
description: 'Create a new Socket scan and report',
38131
hidden: false,
39132
flags: {
40-
...commonFlags,
41-
...outputFlags,
42-
autoManifest: {
43-
type: 'boolean',
44-
description:
45-
'Run `socket manifest auto` before collecting manifest files. This is necessary for languages like Scala, Gradle, and Kotlin, See `socket manifest auto --help`.',
46-
},
47-
branch: {
48-
type: 'string',
49-
shortFlag: 'b',
50-
description: 'Branch name',
51-
},
52-
commitMessage: {
53-
type: 'string',
54-
shortFlag: 'm',
55-
default: '',
56-
description: 'Commit message',
57-
},
58-
commitHash: {
59-
type: 'string',
60-
shortFlag: 'ch',
61-
default: '',
62-
description: 'Commit hash',
63-
},
64-
committers: {
65-
type: 'string',
66-
shortFlag: 'c',
67-
default: '',
68-
description: 'Committers',
69-
},
70-
cwd: {
71-
type: 'string',
72-
description: 'working directory, defaults to process.cwd()',
73-
},
74-
defaultBranch: {
75-
type: 'boolean',
76-
default: false,
77-
description:
78-
'Set the default branch of the repository to the branch of this full-scan. Should only need to be done once, for example for the "main" or "master" branch.',
79-
},
80-
interactive: {
81-
type: 'boolean',
82-
default: true,
83-
description:
84-
'Allow for interactive elements, asking for input. Use --no-interactive to prevent any input questions, defaulting them to cancel/no.',
85-
},
86-
pullRequest: {
87-
type: 'number',
88-
shortFlag: 'pr',
89-
description: 'Commit hash',
90-
},
91-
org: {
92-
type: 'string',
93-
description:
94-
'Force override the organization slug, overrides the default org from config',
95-
},
96-
reach: {
97-
type: 'boolean',
98-
default: false,
99-
description: 'Run tier 1 full application reachability analysis',
100-
},
101-
readOnly: {
102-
type: 'boolean',
103-
default: false,
104-
description:
105-
'Similar to --dry-run except it can read from remote, stops before it would create an actual report',
106-
},
107-
repo: {
108-
type: 'string',
109-
shortFlag: 'r',
110-
description: 'Repository name',
111-
},
112-
report: {
113-
type: 'boolean',
114-
description:
115-
'Wait for the scan creation to complete, then basically run `socket scan report` on it',
116-
},
117-
setAsAlertsPage: {
118-
type: 'boolean',
119-
default: true,
120-
aliases: ['pendingHead'],
121-
description:
122-
'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.',
123-
},
124-
tmp: {
125-
type: 'boolean',
126-
shortFlag: 't',
127-
default: false,
128-
description:
129-
'Set the visibility (true/false) of the scan in your dashboard.',
130-
},
131-
132-
// Reachability scan flags
133+
...generalFlags,
133134
...reachabilityFlags,
134135
},
135136
// TODO: Your project's "socket.yml" file's "projectIgnorePaths".
136-
help: (command, config) => {
137-
const allFlags = config.flags || {}
138-
const generalFlags: MeowFlags = {}
139-
140-
// Separate general flags from reachability flags
141-
for (const [key, value] of Object.entries(allFlags)) {
142-
if (!reachabilityFlags[key]) {
143-
generalFlags[key] = value
144-
}
145-
}
146-
147-
return `
137+
help: command => `
148138
Usage
149139
$ ${command} [options] [TARGET...]
150140
@@ -192,8 +182,7 @@ const config: CliCommandConfig = {
192182
$ ${command}
193183
$ ${command} ./proj --json
194184
$ ${command} --repo=test-repo --branch=main ./package.json
195-
`
196-
},
185+
`,
197186
}
198187

199188
export const cmdScanCreate = {
@@ -249,29 +238,28 @@ async function run(
249238
readOnly: boolean
250239
setAsAlertsPage: boolean
251240
tmp: boolean
252-
253-
// reachability flags
241+
// Reachability flags.
254242
reach: boolean
255-
reachAnalysisTimeout?: number
256-
reachAnalysisMemoryLimit?: number
243+
reachAnalysisTimeout: number
244+
reachAnalysisMemoryLimit: number
257245
reachContinueOnFailingProjects: boolean
258246
reachDisableAnalytics: boolean
259247
}
260248

261-
// Process comma-separated values for isMultiple flags
249+
// Process comma-separated values for isMultiple flags.
262250
const reachEcosystemsRaw = cmdFlagValueToArray(cli.flags['reachEcosystems'])
263251
const reachExcludePaths = cmdFlagValueToArray(cli.flags['reachExcludePaths'])
264252

265-
// Validate ecosystem values
253+
// Validate ecosystem values.
254+
const reachEcosystems: PURL_Type[] = []
266255
const validEcosystems = getEcosystemChoicesForMeow()
267-
const reachEcosystems: EcosystemString[] = []
268256
for (const ecosystem of reachEcosystemsRaw) {
269257
if (!validEcosystems.includes(ecosystem)) {
270258
throw new Error(
271259
`Invalid ecosystem: "${ecosystem}". Valid values are: ${validEcosystems.join(', ')}`,
272260
)
273261
}
274-
reachEcosystems.push(ecosystem as EcosystemString)
262+
reachEcosystems.push(ecosystem as PURL_Type)
275263
}
276264

277265
let {
@@ -285,6 +273,7 @@ async function run(
285273
repo: string
286274
report?: boolean
287275
}
276+
288277
let [orgSlug] = await determineOrgSlug(
289278
String(orgFlag || ''),
290279
interactive,
@@ -301,7 +290,7 @@ async function run(
301290

302291
const sockJson = readOrDefaultSocketJson(cwd)
303292

304-
// Note: This needs meow booleanDefault=undefined
293+
// Note: This needs meow booleanDefault=undefined.
305294
if (typeof autoManifest !== 'boolean') {
306295
if (sockJson.defaults?.scan?.create?.autoManifest !== undefined) {
307296
autoManifest = sockJson.defaults.scan.create.autoManifest
@@ -445,7 +434,10 @@ async function run(
445434
},
446435
{
447436
nook: true,
448-
test: reach || !reachDisableAnalytics,
437+
test:
438+
reach ||
439+
reachDisableAnalytics ===
440+
reachabilityFlags['reachDisableAnalytics']?.default,
449441
message: 'The --reachDisableAnalytics flag requires --reach to be set',
450442
pass: 'ok',
451443
fail: 'missing --reach flag',
@@ -454,15 +446,18 @@ async function run(
454446
nook: true,
455447
test:
456448
reach ||
457-
reachAnalysisMemoryLimit === undefined ||
458-
reachAnalysisMemoryLimit === 8192,
449+
reachAnalysisMemoryLimit ===
450+
reachabilityFlags['reachAnalysisMemoryLimit']?.default,
459451
message: 'The --reachAnalysisMemoryLimit flag requires --reach to be set',
460452
pass: 'ok',
461453
fail: 'missing --reach flag',
462454
},
463455
{
464456
nook: true,
465-
test: reach || !reachAnalysisTimeout,
457+
test:
458+
reach ||
459+
reachAnalysisTimeout ===
460+
reachabilityFlags['reachAnalysisTimeout']?.default,
466461
message: 'The --reachAnalysisTimeout flag requires --reach to be set',
467462
pass: 'ok',
468463
fail: 'missing --reach flag',
@@ -494,7 +489,7 @@ async function run(
494489
return
495490
}
496491

497-
// Note exiting earlier to skirt a hidden auth requirement
492+
// Note: Exiting earlier to skirt a hidden auth requirement.
498493
if (dryRun) {
499494
logger.log(DRY_RUN_BAILING_NOW)
500495
return

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ describe('socket scan create', async () => {
181181
],
182182
'should succeed when --reachAnalysisMemoryLimit is used with default value without --reach',
183183
async cmd => {
184-
const { code, stderr, stdout } = await invokeNpm(binCliPath, cmd)
184+
const { code, stdout } = await invokeNpm(binCliPath, cmd)
185185
expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`)
186186
expect(code, 'should exit with code 0 when using default value').toBe(0)
187187
},

0 commit comments

Comments
 (0)