Skip to content

Commit d9f009b

Browse files
committed
Minor reachability cleanup
1 parent 6e00665 commit d9f009b

4 files changed

Lines changed: 65 additions & 88 deletions

File tree

src/commands/scan/finalize-tier1-scan.mts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ export type FinalizeTier1ScanOptions = {
1313
* - Sets the tier1 reachability scan to "finalized" state.
1414
*/
1515
export async function finalizeTier1Scan(
16-
tier1_reachability_scan_id: string,
17-
report_run_id: string,
16+
tier1ReachabilityScanId: string,
17+
scanId: string,
1818
): Promise<CResult<unknown>> {
1919
// we do not use the SDK here because the tier1-reachability-scan/finalize is a hidden
2020
// endpoint that is not part of the OpenAPI specification.
2121
return await sendApiRequest('tier1-reachability-scan/finalize', {
2222
method: 'POST',
2323
body: {
24-
tier1_reachability_scan_id,
25-
report_run_id,
24+
tier1_reachability_scan_id: tier1ReachabilityScanId,
25+
report_run_id: scanId,
2626
},
2727
})
2828
}

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

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { generateAutoManifest } from '../manifest/generate_auto_manifest.mts'
1717

1818
import type { ReachabilityOptions } from './perform-reachability-analysis.mts'
1919
import type { OutputKind } from '../../types.mts'
20+
import type { Remap } from '@socketsecurity/registry/lib/objects'
2021

2122
export async function handleCreateNewScan({
2223
autoManifest,
@@ -50,9 +51,11 @@ export async function handleCreateNewScan({
5051
pendingHead: boolean
5152
pullRequest: number
5253
outputKind: OutputKind
53-
reach: {
54-
runReachabilityAnalysis: boolean
55-
} & ReachabilityOptions
54+
reach: Remap<
55+
ReachabilityOptions & {
56+
runReachabilityAnalysis: boolean
57+
}
58+
>
5659
readOnly: boolean
5760
repoName: string
5861
report: boolean
@@ -125,17 +128,15 @@ export async function handleCreateNewScan({
125128

126129
spinner.start()
127130

128-
const reachResult = await performReachabilityAnalysis(
129-
{
130-
packagePaths,
131-
orgSlug,
132-
cwd,
133-
repoName,
134-
branchName,
135-
reachabilityOptions: reach,
136-
},
137-
{ spinner },
138-
)
131+
const reachResult = await performReachabilityAnalysis({
132+
branchName,
133+
cwd,
134+
orgSlug,
135+
packagePaths,
136+
reachabilityOptions: reach,
137+
repoName,
138+
spinner,
139+
})
139140

140141
spinner.stop()
141142

@@ -171,25 +172,22 @@ export async function handleCreateNewScan({
171172
},
172173
)
173174

174-
if (
175-
reach &&
176-
tier1ReachabilityScanId &&
177-
fullScanCResult.ok &&
178-
fullScanCResult.data?.id
179-
) {
180-
await finalizeTier1Scan(tier1ReachabilityScanId, fullScanCResult.data.id)
175+
const scanId = fullScanCResult.ok ? fullScanCResult.data?.id : undefined
176+
177+
if (reach && scanId && tier1ReachabilityScanId) {
178+
await finalizeTier1Scan(tier1ReachabilityScanId, scanId)
181179
}
182180

183181
if (report && fullScanCResult.ok) {
184-
if (fullScanCResult.data?.id) {
182+
if (scanId) {
185183
await handleScanReport({
186184
filePath: '-',
187185
fold: 'version',
188186
includeLicensePolicy: true,
189187
orgSlug,
190188
outputKind,
191189
reportLevel: 'error',
192-
scanId: fullScanCResult.data.id,
190+
scanId,
193191
short: false,
194192
})
195193
} else {
@@ -208,7 +206,6 @@ export async function handleCreateNewScan({
208206
}
209207
} else {
210208
spinner.stop()
211-
spinner.clear()
212209

213210
await outputCreateNewScan(fullScanCResult, { interactive, outputKind })
214211
}

src/commands/scan/handle-scan-reach.mts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,14 @@ export async function handleScanReach({
6666

6767
spinner.start('Running reachability analysis...')
6868

69-
const result = await performReachabilityAnalysis(
70-
{
71-
cwd,
72-
orgSlug,
73-
packagePaths,
74-
reachabilityOptions,
75-
uploadManifests: true,
76-
},
77-
{ spinner },
78-
)
69+
const result = await performReachabilityAnalysis({
70+
cwd,
71+
orgSlug,
72+
packagePaths,
73+
reachabilityOptions,
74+
spinner,
75+
uploadManifests: true,
76+
})
7977

8078
spinner.stop()
8179

src/commands/scan/perform-reachability-analysis.mts

Lines changed: 32 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,22 @@ import type { PURL_Type } from '../../utils/ecosystem.mts'
1111
import type { Spinner } from '@socketsecurity/registry/lib/spinner'
1212

1313
export type ReachabilityOptions = {
14-
reachDisableAnalytics: boolean
1514
reachAnalysisTimeout: number
1615
reachAnalysisMemoryLimit: number
16+
reachDisableAnalytics: boolean
1717
reachEcosystems: PURL_Type[]
1818
reachExcludePaths: string[]
1919
}
2020

21-
export type ReachabilityAnalysisConfig = {
22-
branchName?: string
23-
cwd: string
24-
orgSlug?: string
25-
packagePaths?: string[]
26-
reachabilityOptions: ReachabilityOptions
27-
repoName?: string
28-
uploadManifests?: boolean
29-
}
30-
3121
export type ReachabilityAnalysisOptions = {
22+
branchName?: string | undefined
23+
cwd?: string | undefined
24+
orgSlug?: string | undefined
25+
packagePaths?: string[] | undefined
26+
reachabilityOptions: ReachabilityOptions
27+
repoName?: string | undefined
3228
spinner?: Spinner | undefined
29+
uploadManifests?: boolean | undefined
3330
}
3431

3532
export type ReachabilityAnalysisResult = {
@@ -38,22 +35,18 @@ export type ReachabilityAnalysisResult = {
3835
}
3936

4037
export async function performReachabilityAnalysis(
41-
{
38+
options?: ReachabilityAnalysisOptions | undefined,
39+
): Promise<CResult<ReachabilityAnalysisResult>> {
40+
const {
4241
branchName,
43-
cwd,
42+
cwd = process.cwd(),
4443
orgSlug,
4544
packagePaths,
4645
reachabilityOptions,
4746
repoName,
47+
spinner,
4848
uploadManifests = true,
49-
}: ReachabilityAnalysisConfig,
50-
options?: ReachabilityAnalysisOptions | undefined,
51-
): Promise<CResult<ReachabilityAnalysisResult>> {
52-
const { spinner } = {
53-
__proto__: null,
54-
...options,
55-
} as ReachabilityAnalysisOptions
56-
49+
} = { __proto__: null, ...options } as ReachabilityAnalysisOptions
5750
let tarHash: string | undefined
5851

5952
if (uploadManifests && orgSlug && packagePaths) {
@@ -67,15 +60,15 @@ export async function performReachabilityAnalysis(
6760

6861
const wasSpinning = !!spinner?.isSpinning
6962

70-
// Upload manifests to get tar hash
71-
spinner?.start('Uploading manifests for reachability analysis...')
72-
73-
// Exclude DOT_SOCKET_DOT_FACTS_JSON if it was created in previous runs.
74-
const filteredPackagePaths = packagePaths.filter(
63+
// Exclude .socket.facts.json from upload.
64+
const filepathsToUpload = packagePaths.filter(
7565
p => !p.endsWith(constants.DOT_SOCKET_DOT_FACTS_JSON),
7666
)
67+
68+
spinner?.start('Uploading manifests for reachability analysis...')
69+
7770
const uploadCResult = await handleApiCall(
78-
sockSdk.uploadManifestFiles(orgSlug, filteredPackagePaths),
71+
sockSdk.uploadManifestFiles(orgSlug, filepathsToUpload),
7972
{
8073
desc: 'upload manifests',
8174
spinner,
@@ -105,16 +98,12 @@ export async function performReachabilityAnalysis(
10598

10699
spinner?.start()
107100
spinner?.success(`Manifests uploaded successfully. Tar hash: ${tarHash}`)
108-
spinner?.infoAndStop('Running reachability analysis with Coana...')
109-
} else {
110-
const wasSpinning = !!spinner?.isSpinning
111-
spinner?.start('Running reachability analysis with Coana...')
112-
if (!wasSpinning) {
113-
spinner?.stop()
114-
}
115101
}
116102

117-
// Build Coana arguments
103+
spinner?.start()
104+
spinner?.infoAndStop('Running reachability analysis with Coana...')
105+
106+
// Build Coana arguments.
118107
const coanaArgs = [
119108
'run',
120109
cwd,
@@ -124,33 +113,27 @@ export async function performReachabilityAnalysis(
124113
constants.DOT_SOCKET_DOT_FACTS_JSON,
125114
'--disable-report-submission',
126115
...(reachabilityOptions.reachAnalysisTimeout
127-
? [
128-
'--analysis-timeout',
129-
reachabilityOptions.reachAnalysisTimeout.toString(),
130-
]
116+
? ['--analysis-timeout', `${reachabilityOptions.reachAnalysisTimeout}`]
131117
: []),
132118
...(reachabilityOptions.reachAnalysisMemoryLimit
133-
? [
134-
'--memory-limit',
135-
reachabilityOptions.reachAnalysisMemoryLimit.toString(),
136-
]
119+
? ['--memory-limit', `${reachabilityOptions.reachAnalysisMemoryLimit}`]
137120
: []),
138121
...(reachabilityOptions.reachDisableAnalytics
139122
? ['--disable-analytics-sharing']
140123
: []),
141-
// empty reachEcosystems implies scan all ecosystems
124+
...(tarHash
125+
? ['--run-without-docker', '--manifests-tar-hash', tarHash]
126+
: []),
127+
// Empty reachEcosystems implies scan all ecosystems.
142128
...(reachabilityOptions.reachEcosystems.length
143129
? ['--purl-types', ...reachabilityOptions.reachEcosystems]
144130
: []),
145131
...(reachabilityOptions.reachExcludePaths.length
146-
? ['--exclude-dirs', reachabilityOptions.reachExcludePaths.join(' ')]
147-
: []),
148-
...(tarHash
149-
? ['--manifests-tar-hash', tarHash, '--run-without-docker']
132+
? ['--exclude-dirs', ...reachabilityOptions.reachExcludePaths]
150133
: []),
151134
]
152135

153-
// Build environment variables
136+
// Build environment variables.
154137
const env: NodeJS.ProcessEnv = {
155138
...process.env,
156139
}
@@ -161,7 +144,6 @@ export async function performReachabilityAnalysis(
161144
env['SOCKET_BRANCH_NAME'] = branchName
162145
}
163146

164-
// Run Coana with the manifests tar hash.
165147
const coanaResult = await spawnCoana(coanaArgs, {
166148
cwd,
167149
env,

0 commit comments

Comments
 (0)