Skip to content

Commit b81a7c9

Browse files
committed
refactor: split file and project zipping into seperate functions
1 parent 01400a7 commit b81a7c9

File tree

5 files changed

+50
-34
lines changed

5 files changed

+50
-34
lines changed

packages/core/src/codewhisperer/commands/startSecurityScan.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,10 @@ export async function startSecurityScan(
157157
})
158158
}
159159
const zipType = scope === CodeAnalysisScope.PROJECT ? 'project' : 'file'
160-
const zipMetadata = await zipUtil.generateZipCodeScan(editor?.document.uri, zipType)
160+
const zipMetadata =
161+
zipType === 'file'
162+
? await zipUtil.generateZipCodeScanForFile(editor?.document.uri)
163+
: await zipUtil.generateZipCodeScanForProject()
161164
const projectPaths = getWorkspacePaths()
162165

163166
const contextTruncationStartTime = performance.now()

packages/core/src/codewhisperer/util/zipUtil.ts

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -335,34 +335,53 @@ export class ZipUtil {
335335
return this._zipDir
336336
}
337337

338-
public async generateZipCodeScan(
338+
public async generateZipCodeScanForFile(
339339
uri: vscode.Uri | undefined,
340-
zipType: ZipType,
341340
options?: GenerateZipOptions
342341
): Promise<ZipMetadata> {
342+
try {
343+
const zipDirPath = this.getZipDirPath()
344+
const zipFilePath = await this.zipFile(uri, options?.includeGitDiff)
345+
346+
if (!options?.silent) {
347+
getLogger().debug(`Picked source files: [${[...this._pickedSourceFiles].join(', ')}]`)
348+
}
349+
return {
350+
rootDir: zipDirPath,
351+
zipFilePath: zipFilePath,
352+
srcPayloadSizeInBytes: this._totalSize,
353+
scannedFiles: new Set([...this._pickedSourceFiles, ...this._pickedBuildFiles]),
354+
zipFileSizeInBytes: (await fs.stat(zipFilePath)).size,
355+
buildPayloadSizeInBytes: this._totalBuildSize,
356+
lines: this._totalLines,
357+
language: this._language,
358+
}
359+
} catch (error) {
360+
getLogger().error('Zip error caused by: %O', error)
361+
throw error
362+
}
363+
}
364+
365+
public async generateZipCodeScanForProject(options?: GenerateZipOptions): Promise<ZipMetadata> {
343366
try {
344367
const zipDirPath = this.getZipDirPath()
345368
// We assume there is at least one workspace open.
346369
const workspaceFolders = [...(vscode.workspace.workspaceFolders ?? [])] as CurrentWsFolders
347370

348-
const zipFilePath =
349-
zipType === 'file'
350-
? await this.zipFile(uri, options?.includeGitDiff)
351-
: await this.zipProject(workspaceFolders, defaultExcludePatterns, {
352-
includeGitDiff: options?.includeGitDiff,
353-
includeNonWorkspaceFiles: true,
354-
})
371+
const zipFilePath = await this.zipProject(workspaceFolders, defaultExcludePatterns, {
372+
includeGitDiff: options?.includeGitDiff,
373+
includeNonWorkspaceFiles: true,
374+
})
355375

356376
if (!options?.silent) {
357377
getLogger().debug(`Picked source files: [${[...this._pickedSourceFiles].join(', ')}]`)
358378
}
359-
const zipFileSize = (await fs.stat(zipFilePath)).size
360379
return {
361380
rootDir: zipDirPath,
362381
zipFilePath: zipFilePath,
363382
srcPayloadSizeInBytes: this._totalSize,
364383
scannedFiles: new Set([...this._pickedSourceFiles, ...this._pickedBuildFiles]),
365-
zipFileSizeInBytes: zipFileSize,
384+
zipFileSizeInBytes: (await fs.stat(zipFilePath)).size,
366385
buildPayloadSizeInBytes: this._totalBuildSize,
367386
lines: this._totalLines,
368387
language: this._language,
@@ -409,13 +428,13 @@ export class ZipUtil {
409428
projectPath,
410429
}
411430
)
412-
const zipFileSize = (await fs.stat(zipFilePath)).size
431+
413432
return {
414433
rootDir: zipDirPath,
415434
zipFilePath: zipFilePath,
416435
srcPayloadSizeInBytes: this._totalSize,
417436
scannedFiles: new Set(this._pickedSourceFiles),
418-
zipFileSizeInBytes: zipFileSize,
437+
zipFileSizeInBytes: (await fs.stat(zipFilePath)).size,
419438
buildPayloadSizeInBytes: this._totalBuildSize,
420439
lines: this._totalLines,
421440
language: this._language,

packages/core/src/test/codewhisperer/zipUtil.test.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ describe('zipUtil', function () {
6868
})
6969

7070
it('Should generate zip for file scan and return expected metadata', async function () {
71-
const zipMetadata = await zipUtil.generateZipCodeScan(vscode.Uri.file(appCodePath), 'file', {
71+
const zipMetadata = await zipUtil.generateZipCodeScanForFile(vscode.Uri.file(appCodePath), {
7272
includeGitDiff: true,
7373
})
7474
assert.strictEqual(zipMetadata.lines, 49)
@@ -84,13 +84,13 @@ describe('zipUtil', function () {
8484
sinon.stub(ZipUtil, 'aboveByteLimit').returns(true)
8585

8686
await assert.rejects(
87-
() => zipUtil.generateZipCodeScan(vscode.Uri.file(appCodePath), 'file', { includeGitDiff: true }),
87+
() => zipUtil.generateZipCodeScanForFile(vscode.Uri.file(appCodePath), { includeGitDiff: true }),
8888
new ToolkitError(`Payload size limit reached`, { code: 'FileSizeExceeded' })
8989
)
9090
})
9191

9292
it('Should generate zip for project scan and return expected metadata', async function () {
93-
const zipMetadata = await zipUtil.generateZipCodeScan(vscode.Uri.file(appCodePath), 'project')
93+
const zipMetadata = await zipUtil.generateZipCodeScanForProject()
9494
assert.ok(zipMetadata.lines > 0)
9595
assert.ok(zipMetadata.rootDir.includes(codeScanTruncDirPrefix))
9696
assert.ok(zipMetadata.srcPayloadSizeInBytes > 0)
@@ -104,7 +104,7 @@ describe('zipUtil', function () {
104104
sinon.stub(ZipUtil, 'aboveByteLimit').returns(true)
105105

106106
await assert.rejects(
107-
() => zipUtil.generateZipCodeScan(vscode.Uri.file(appCodePath), 'project'),
107+
() => zipUtil.generateZipCodeScanForProject(),
108108
new ToolkitError('Payload size limit reached', { code: 'ProjectSizeExceeded' })
109109
)
110110
})
@@ -113,31 +113,29 @@ describe('zipUtil', function () {
113113
sinon.stub(ZipUtil, 'aboveByteLimit').returns(true)
114114

115115
await assert.rejects(
116-
() => zipUtil.generateZipCodeScan(vscode.Uri.file(appCodePath), 'project'),
116+
() => zipUtil.generateZipCodeScanForProject(),
117117
new ToolkitError('Payload size limit reached', { code: 'ProjectSizeExceeded' })
118118
)
119119
})
120120

121121
it('Should read file content instead of from disk if file is dirty', async function () {
122-
const zipMetadata = await zipUtil.generateZipCodeScan(vscode.Uri.file(appCodePath), 'project')
122+
const zipMetadata = await zipUtil.generateZipCodeScanForProject()
123123

124124
const document = await vscode.workspace.openTextDocument(appCodePath)
125125
await vscode.window.showTextDocument(document)
126126
void vscode.window.activeTextEditor?.edit((editBuilder) => {
127127
editBuilder.insert(new vscode.Position(0, 0), '// a comment\n')
128128
})
129129

130-
const zipMetadata2 = await new ZipUtil(CodeWhispererConstants.codeScanTruncDirPrefix).generateZipCodeScan(
131-
vscode.Uri.file(appCodePath),
132-
'project'
133-
)
130+
const zipMetadata2 = await new ZipUtil(
131+
CodeWhispererConstants.codeScanTruncDirPrefix
132+
).generateZipCodeScanForProject()
134133
assert.equal(zipMetadata2.lines, zipMetadata.lines + 1)
135134
})
136135

137136
it('should handle path with repeated project name for file scan', async function () {
138-
const zipMetadata = await zipUtil.generateZipCodeScan(
139-
vscode.Uri.file(appCodePathWithRepeatedProjectName),
140-
'file'
137+
const zipMetadata = await zipUtil.generateZipCodeScanForFile(
138+
vscode.Uri.file(appCodePathWithRepeatedProjectName)
141139
)
142140

143141
const zipFileData = await fs.readFileBytes(zipMetadata.zipFilePath)
@@ -147,10 +145,7 @@ describe('zipUtil', function () {
147145
})
148146

149147
it('should handle path with repeated project name for project scan', async function () {
150-
const zipMetadata = await zipUtil.generateZipCodeScan(
151-
vscode.Uri.file(appCodePathWithRepeatedProjectName),
152-
'project'
153-
)
148+
const zipMetadata = await zipUtil.generateZipCodeScanForProject()
154149

155150
const zipFileData = await fs.readFileBytes(zipMetadata.zipFilePath)
156151
const zip = await JSZip.loadAsync(zipFileData)

packages/core/src/testE2E/codewhisperer/securityScan.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,10 @@ describe('CodeWhisperer security scan', async function () {
9191
async function securityJobSetup(editor: vscode.TextEditor) {
9292
const codeScanStartTime = performance.now()
9393
const zipUtil = new ZipUtil(CodeWhispererConstants.codeScanTruncDirPrefix)
94-
const uri = editor.document.uri
9594

9695
const projectPaths = getWorkspacePaths()
9796
const scope = CodeWhispererConstants.CodeAnalysisScope.PROJECT
98-
const zipMetadata = await zipUtil.generateZipCodeScan(uri, 'project')
97+
const zipMetadata = await zipUtil.generateZipCodeScanForProject()
9998
const codeScanName = randomUUID()
10099

101100
let artifactMap

packages/core/src/testInteg/perf/startSecurityScan.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ describe('startSecurityScanPerformanceTest', function () {
102102
assert.ok(setup.commandSpy.neverCalledWith('workbench.action.problems.focus'))
103103
assert.ok(setup.securityScanRenderSpy.calledOnce)
104104

105-
assert.ok(setup.zipSpy.generateZipCodeScan.calledOnce)
105+
assert.ok(setup.zipSpy.generateZipCodeScanForProject.calledOnce)
106106
assert.ok(setup.zipSpy.removeTmpFiles.calledOnce)
107107
assert.ok(
108108
getFsCallsUpperBound(setup.fsSpy) <= 5,

0 commit comments

Comments
 (0)