Skip to content

Commit 1511848

Browse files
leliajdalton
andauthored
Add workspace support for full scans (v1.x) (#1096)
* Add --workspace flag for full scan association Signed-off-by: lelia <lelia@socket.dev> * Update scan create help snapshot --------- Signed-off-by: lelia <lelia@socket.dev> Co-authored-by: John-David Dalton <jdalton@users.noreply.github.com>
1 parent 0d98aa7 commit 1511848

File tree

6 files changed

+43
-1
lines changed

6 files changed

+43
-1
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ const generalFlags: MeowFlags = {
122122
shortFlag: 'r',
123123
description: 'Repository name',
124124
},
125+
workspace: {
126+
type: 'string',
127+
default: '',
128+
description:
129+
'The workspace in the Socket Organization that the repository is in to associate with the full scan.',
130+
},
125131
report: {
126132
type: 'boolean',
127133
description:
@@ -306,11 +312,13 @@ async function run(
306312
branch: branchName,
307313
repo: repoName,
308314
report,
315+
workspace,
309316
} = cli.flags as {
310317
autoManifest?: boolean | undefined
311318
branch: string
312319
repo: string
313320
report?: boolean | undefined
321+
workspace: string
314322
}
315323

316324
let { 0: orgSlug } = await determineOrgSlug(
@@ -355,6 +363,10 @@ async function run(
355363
repoName = await getRepoName(cwd)
356364
}
357365
}
366+
if (!workspace && sockJson.defaults?.scan?.create?.workspace) {
367+
workspace = sockJson.defaults.scan.create.workspace
368+
logger.info(`Using default --workspace from ${SOCKET_JSON}:`, workspace)
369+
}
358370
if (typeof report !== 'boolean') {
359371
if (sockJson.defaults?.scan?.create?.report !== undefined) {
360372
report = sockJson.defaults.scan.create.report
@@ -598,5 +610,6 @@ async function run(
598610
reportLevel,
599611
targets,
600612
tmp: Boolean(tmp),
613+
workspace: (workspace && String(workspace)) || '',
601614
})
602615
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ describe('socket scan create', async () => {
5252
--report-level Which policy level alerts should be reported (default 'error')
5353
--set-as-alerts-page 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.
5454
--tmp Set the visibility (true/false) of the scan in your dashboard.
55+
--workspace The workspace in the Socket Organization that the repository is in to associate with the full scan.
5556
5657
Reachability Options (when --reach is used)
5758
--reach-analysis-memory-limit The maximum memory in MB to use for the reachability analysis. The default is 8192MB.

src/commands/scan/fetch-create-org-full-scan.mts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export type FetchCreateOrgFullScanConfigs = {
1919
pullRequest: number
2020
repoName: string
2121
scanType: string | undefined
22+
workspace?: string | undefined
2223
}
2324

2425
export type FetchCreateOrgFullScanOptions = {
@@ -43,6 +44,7 @@ export async function fetchCreateOrgFullScan(
4344
pullRequest,
4445
repoName,
4546
scanType,
47+
workspace,
4648
} = { __proto__: null, ...config } as FetchCreateOrgFullScanConfigs
4749

4850
const {
@@ -82,6 +84,7 @@ export async function fetchCreateOrgFullScan(
8284
...(pullRequest ? { pull_request: String(pullRequest) } : {}),
8385
scan_type: scanType,
8486
repo: repoName,
87+
...(workspace ? { workspace } : {}),
8588
set_as_pending_head: String(pendingHead),
8689
tmp: String(tmp),
8790
}),

src/commands/scan/handle-create-new-scan.mts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export type HandleCreateNewScanConfig = {
8484
reportLevel: REPORT_LEVEL
8585
targets: string[]
8686
tmp: boolean
87+
workspace?: string | undefined
8788
}
8889

8990
export async function handleCreateNewScan({
@@ -106,8 +107,12 @@ export async function handleCreateNewScan({
106107
reportLevel,
107108
targets,
108109
tmp,
110+
workspace,
109111
}: HandleCreateNewScanConfig): Promise<void> {
110-
debugFn('notice', `Creating new scan for ${orgSlug}/${repoName}`)
112+
debugFn(
113+
'notice',
114+
`Creating new scan for ${orgSlug}/${workspace ? `${workspace}/` : ''}${repoName}`,
115+
)
111116
debugDir('inspect', {
112117
autoManifest,
113118
branchName,
@@ -121,6 +126,7 @@ export async function handleCreateNewScan({
121126
reportLevel,
122127
targets,
123128
tmp,
129+
workspace,
124130
})
125131

126132
if (autoManifest) {
@@ -257,6 +263,7 @@ export async function handleCreateNewScan({
257263
scanType: reach.runReachabilityAnalysis
258264
? constants.SCAN_TYPE_SOCKET_TIER1
259265
: constants.SCAN_TYPE_SOCKET,
266+
workspace,
260267
},
261268
{
262269
cwd,

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import constants, { SOCKET_JSON } from '../../constants.mts'
88
import {
99
detectDefaultBranch,
1010
getRepoName,
11+
getRepoOwner,
1112
gitBranch,
1213
} from '../../utils/git.mts'
1314
import {
@@ -154,6 +155,22 @@ async function configureScan(
154155
delete config.repo
155156
}
156157

158+
const defaultWorkspace = await input({
159+
message:
160+
'(--workspace) The workspace in the Socket Organization that the repository is in to associate with the full scan.',
161+
default: config.workspace || (await getRepoOwner(cwd)) || '',
162+
required: false,
163+
// validate: async string => bool
164+
})
165+
if (defaultWorkspace === undefined) {
166+
return canceledByUser()
167+
}
168+
if (defaultWorkspace) {
169+
config.workspace = defaultWorkspace
170+
} else {
171+
delete config.workspace
172+
}
173+
157174
const defaultBranchName = await input({
158175
message:
159176
'(--branch) What branch name (slug) should be reported to Socket for this dir?',

src/utils/socket-json.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export interface SocketJson {
7171
repo?: string | undefined
7272
report?: boolean | undefined
7373
branch?: string | undefined
74+
workspace?: string | undefined
7475
}
7576
github?: {
7677
all?: boolean | undefined

0 commit comments

Comments
 (0)