Skip to content

Commit f2a504f

Browse files
committed
refactor: remove scopes enum from ZipUtil
1 parent e4a84a0 commit f2a504f

File tree

5 files changed

+45
-63
lines changed

5 files changed

+45
-63
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ export async function startSecurityScan(
156156
scanUuid,
157157
})
158158
}
159-
const zipMetadata = await zipUtil.generateZip(editor?.document.uri, scope)
159+
const zipType = scope === CodeAnalysisScope.PROJECT ? 'project' : 'file'
160+
const zipMetadata = await zipUtil.generateZip(editor?.document.uri, zipType)
160161
const projectPaths = getWorkspacePaths()
161162

162163
const contextTruncationStartTime = performance.now()
@@ -190,7 +191,7 @@ export async function startSecurityScan(
190191
try {
191192
artifactMap = await getPresignedUrlAndUpload(client, zipMetadata, scope, scanName)
192193
} finally {
193-
await zipUtil.removeTmpFiles(zipMetadata, scope)
194+
await zipUtil.removeTmpFiles(zipMetadata)
194195
codeScanTelemetryEntry.artifactsUploadDuration = performance.now() - uploadStartTime
195196
}
196197

packages/core/src/codewhisperer/service/securityScanHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ export async function uploadArtifactToS3(
422422
}
423423
}
424424

425-
// TODO: Refactor this
425+
// // TODO: Refactor this
426426
export function getLoggerForScope(scope?: CodeWhispererConstants.CodeAnalysisScope) {
427427
return scope === CodeWhispererConstants.CodeAnalysisScope.FILE_AUTO ? getNullLogger() : getLogger()
428428
}

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

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
import * as vscode from 'vscode'
66
import path from 'path'
77
import { tempDirPath, testGenerationLogsDir } from '../../shared/filesystemUtilities'
8-
import { getLogger } from '../../shared/logger/logger'
8+
import { getLogger, getNullLogger } from '../../shared/logger/logger'
99
import * as CodeWhispererConstants from '../models/constants'
10-
import { ToolkitError } from '../../shared/errors'
1110
import { fs } from '../../shared/fs/fs'
12-
import { getLoggerForScope } from '../service/securityScanHandler'
1311
import { runtimeLanguageContext } from './runtimeLanguageContext'
1412
import { CodewhispererLanguage } from '../../shared/telemetry/telemetry.gen'
1513
import {
@@ -43,6 +41,11 @@ export interface ZipMetadata {
4341
language: CodewhispererLanguage | undefined
4442
}
4543

44+
export interface GenerateZipOptions {
45+
includeGitDiffHeader?: boolean
46+
silent?: boolean
47+
}
48+
4649
export const ZipConstants = {
4750
newlineRegex: /\r?\n/,
4851
gitignoreFilename: '.gitignore',
@@ -110,7 +113,7 @@ export class ZipUtil {
110113
if (ZipUtil.aboveByteLimit(this._totalSize, 'file')) {
111114
throw new FileSizeExceededError()
112115
}
113-
const zipFilePath = this.getZipDirPath(FeatureUseCase.CODE_SCAN) + CodeWhispererConstants.codeScanZipExt
116+
const zipFilePath = this.getZipDirPath() + CodeWhispererConstants.codeScanZipExt
114117
await zip.finalizeToFile(zipFilePath)
115118
return zipFilePath
116119
}
@@ -209,7 +212,7 @@ export class ZipUtil {
209212
throw new NoSourceFilesError()
210213
}
211214
this._language = [...languageCount.entries()].reduce((a, b) => (b[1] > a[1] ? b : a))[0]
212-
const zipFilePath = this.getZipDirPath(useCase) + CodeWhispererConstants.codeScanZipExt
215+
const zipFilePath = this.getZipDirPath() + CodeWhispererConstants.codeScanZipExt
213216
await zip.finalizeToFile(zipFilePath)
214217
return zipFilePath
215218
}
@@ -231,7 +234,7 @@ export class ZipUtil {
231234
: vscode.workspace.workspaceFolders
232235
) as CurrentWsFolders
233236
}
234-
237+
// TODO: remove
235238
private getExcludePatterns(useCase: FeatureUseCase) {
236239
return useCase === FeatureUseCase.TEST_GENERATION
237240
? [...CodeWhispererConstants.testGenExcludePatterns, ...defaultExcludePatterns]
@@ -347,37 +350,27 @@ export class ZipUtil {
347350
return vscode.workspace.textDocuments.some((document) => document.uri.fsPath === uri.fsPath && document.isDirty)
348351
}
349352

350-
public getZipDirPath(useCase: FeatureUseCase): string {
353+
public getZipDirPath(): string {
351354
if (this._zipDir === '') {
352-
// const prefix =
353-
// useCase === FeatureUseCase.TEST_GENERATION
354-
// ? CodeWhispererConstants.TestGenerationTruncDirPrefix
355-
// : CodeWhispererConstants.codeScanTruncDirPrefix
356-
357355
this._zipDir = path.join(this._tmpDir, `${this._zipDirPrefix}_${this._timestamp}`)
358356
}
359357
return this._zipDir
360358
}
361359

362360
public async generateZip(
363361
uri: vscode.Uri | undefined,
364-
scope: CodeWhispererConstants.CodeAnalysisScope
362+
zipType: ZipType,
363+
options?: GenerateZipOptions
365364
): Promise<ZipMetadata> {
366365
try {
367-
const zipDirPath = this.getZipDirPath(FeatureUseCase.CODE_SCAN)
368-
let zipFilePath: string
369-
if (
370-
scope === CodeWhispererConstants.CodeAnalysisScope.FILE_AUTO ||
371-
scope === CodeWhispererConstants.CodeAnalysisScope.FILE_ON_DEMAND
372-
) {
373-
zipFilePath = await this.zipFile(uri, scope === CodeWhispererConstants.CodeAnalysisScope.FILE_AUTO)
374-
} else if (scope === CodeWhispererConstants.CodeAnalysisScope.PROJECT) {
375-
zipFilePath = await this.zipProject(FeatureUseCase.CODE_SCAN)
376-
} else {
377-
throw new ToolkitError(`Unknown code analysis scope: ${scope}`)
378-
}
366+
const zipDirPath = this.getZipDirPath()
367+
const zipFilePath = await (zipType === 'file'
368+
? this.zipFile(uri, options?.includeGitDiffHeader)
369+
: this.zipProject(FeatureUseCase.CODE_SCAN))
379370

380-
getLoggerForScope(scope).debug(`Picked source files: [${[...this._pickedSourceFiles].join(', ')}]`)
371+
if (!options?.silent) {
372+
getLogger().debug(`Picked source files: [${[...this._pickedSourceFiles].join(', ')}]`)
373+
}
381374
const zipFileSize = (await fs.stat(zipFilePath)).size
382375
return {
383376
rootDir: zipDirPath,
@@ -397,7 +390,7 @@ export class ZipUtil {
397390

398391
public async generateZipTestGen(projectPath: string, initialExecution: boolean): Promise<ZipMetadata> {
399392
try {
400-
const zipDirPath = this.getZipDirPath(FeatureUseCase.TEST_GENERATION)
393+
const zipDirPath = this.getZipDirPath()
401394

402395
const metadataDir = path.join(zipDirPath, 'utgRequiredArtifactsDir')
403396

@@ -440,8 +433,8 @@ export class ZipUtil {
440433
}
441434
}
442435
// TODO: Refactor this
443-
public async removeTmpFiles(zipMetadata: ZipMetadata, scope?: CodeWhispererConstants.CodeAnalysisScope) {
444-
const logger = getLoggerForScope(scope)
436+
public async removeTmpFiles(zipMetadata: ZipMetadata, silent?: boolean) {
437+
const logger = silent ? getNullLogger() : getLogger()
445438
logger.verbose(`Cleaning up temporary files...`)
446439
await fs.delete(zipMetadata.zipFilePath, { force: true })
447440
await fs.delete(zipMetadata.rootDir, { recursive: true, force: true })
@@ -454,31 +447,27 @@ interface GitDiffOptions {
454447
projectPath: string
455448
projectName: string
456449
filepath?: string
457-
scope?: CodeWhispererConstants.CodeAnalysisScope
450+
zipType?: ZipType
458451
}
459452

460-
async function getGitDiffContentForProjects(
461-
projectPaths: string[],
462-
filepath?: string,
463-
scope?: CodeWhispererConstants.CodeAnalysisScope
464-
) {
453+
async function getGitDiffContentForProjects(projectPaths: string[], filepath?: string, zipType?: ZipType) {
465454
let gitDiffContent = ''
466455
for (const projectPath of projectPaths) {
467456
const projectName = path.basename(projectPath)
468457
gitDiffContent += await getGitDiffContent({
469458
projectPath,
470459
projectName,
471460
filepath,
472-
scope,
461+
zipType,
473462
})
474463
}
475464
return gitDiffContent
476465
}
477466

478467
async function getGitDiffContent(options: GitDiffOptions): Promise<string> {
479-
const { projectPath, projectName, filepath: filePath, scope } = options
480-
const isProjectScope = scope === CodeWhispererConstants.CodeAnalysisScope.PROJECT
468+
const { projectPath, projectName, filepath: filePath } = options
481469

470+
const isProjectScope = options.zipType === 'project'
482471
const untrackedFilesString = await getGitUntrackedFiles(projectPath)
483472
const untrackedFilesArray = untrackedFilesString?.trim()?.split('\n')?.filter(Boolean)
484473

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

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import path from 'path'
1111
import JSZip from 'jszip'
1212
import { getTestWorkspaceFolder } from '../../testInteg/integrationTestsUtilities'
1313
import { ZipUtil } from '../../codewhisperer/util/zipUtil'
14-
import { CodeAnalysisScope, codeScanTruncDirPrefix } from '../../codewhisperer/models/constants'
14+
import { codeScanTruncDirPrefix } from '../../codewhisperer/models/constants'
1515
import { ToolkitError } from '../../shared/errors'
1616
import { fs } from '../../shared/fs/fs'
1717
import { tempDirPath } from '../../shared/filesystemUtilities'
@@ -68,7 +68,9 @@ describe('zipUtil', function () {
6868
})
6969

7070
it('Should generate zip for file scan and return expected metadata', async function () {
71-
const zipMetadata = await zipUtil.generateZip(vscode.Uri.file(appCodePath), CodeAnalysisScope.FILE_AUTO)
71+
const zipMetadata = await zipUtil.generateZip(vscode.Uri.file(appCodePath), 'file', {
72+
includeGitDiffHeader: true,
73+
})
7274
assert.strictEqual(zipMetadata.lines, 49)
7375
assert.ok(zipMetadata.rootDir.includes(codeScanTruncDirPrefix))
7476
assert.ok(zipMetadata.srcPayloadSizeInBytes > 0)
@@ -82,13 +84,13 @@ describe('zipUtil', function () {
8284
sinon.stub(ZipUtil, 'aboveByteLimit').returns(true)
8385

8486
await assert.rejects(
85-
() => zipUtil.generateZip(vscode.Uri.file(appCodePath), CodeAnalysisScope.FILE_AUTO),
87+
() => zipUtil.generateZip(vscode.Uri.file(appCodePath), 'file', { includeGitDiffHeader: true }),
8688
new ToolkitError(`Payload size limit reached`, { code: 'FileSizeExceeded' })
8789
)
8890
})
8991

9092
it('Should generate zip for project scan and return expected metadata', async function () {
91-
const zipMetadata = await zipUtil.generateZip(vscode.Uri.file(appCodePath), CodeAnalysisScope.PROJECT)
93+
const zipMetadata = await zipUtil.generateZip(vscode.Uri.file(appCodePath), 'project')
9294
assert.ok(zipMetadata.lines > 0)
9395
assert.ok(zipMetadata.rootDir.includes(codeScanTruncDirPrefix))
9496
assert.ok(zipMetadata.srcPayloadSizeInBytes > 0)
@@ -102,7 +104,7 @@ describe('zipUtil', function () {
102104
sinon.stub(ZipUtil, 'aboveByteLimit').returns(true)
103105

104106
await assert.rejects(
105-
() => zipUtil.generateZip(vscode.Uri.file(appCodePath), CodeAnalysisScope.PROJECT),
107+
() => zipUtil.generateZip(vscode.Uri.file(appCodePath), 'project'),
106108
new ToolkitError('Payload size limit reached', { code: 'ProjectSizeExceeded' })
107109
)
108110
})
@@ -111,20 +113,13 @@ describe('zipUtil', function () {
111113
sinon.stub(ZipUtil, 'aboveByteLimit').returns(true)
112114

113115
await assert.rejects(
114-
() => zipUtil.generateZip(vscode.Uri.file(appCodePath), CodeAnalysisScope.PROJECT),
116+
() => zipUtil.generateZip(vscode.Uri.file(appCodePath), 'project'),
115117
new ToolkitError('Payload size limit reached', { code: 'ProjectSizeExceeded' })
116118
)
117119
})
118120

119-
it('Should throw error if scan type is invalid', async function () {
120-
await assert.rejects(
121-
() => zipUtil.generateZip(vscode.Uri.file(appCodePath), 'unknown' as CodeAnalysisScope),
122-
new ToolkitError('Unknown code analysis scope: unknown')
123-
)
124-
})
125-
126121
it('Should read file content instead of from disk if file is dirty', async function () {
127-
const zipMetadata = await zipUtil.generateZip(vscode.Uri.file(appCodePath), CodeAnalysisScope.PROJECT)
122+
const zipMetadata = await zipUtil.generateZip(vscode.Uri.file(appCodePath), 'project')
128123

129124
const document = await vscode.workspace.openTextDocument(appCodePath)
130125
await vscode.window.showTextDocument(document)
@@ -134,16 +129,13 @@ describe('zipUtil', function () {
134129

135130
const zipMetadata2 = await new ZipUtil(CodeWhispererConstants.codeScanTruncDirPrefix).generateZip(
136131
vscode.Uri.file(appCodePath),
137-
CodeAnalysisScope.PROJECT
132+
'project'
138133
)
139134
assert.equal(zipMetadata2.lines, zipMetadata.lines + 1)
140135
})
141136

142137
it('should handle path with repeated project name for file scan', async function () {
143-
const zipMetadata = await zipUtil.generateZip(
144-
vscode.Uri.file(appCodePathWithRepeatedProjectName),
145-
CodeAnalysisScope.FILE_ON_DEMAND
146-
)
138+
const zipMetadata = await zipUtil.generateZip(vscode.Uri.file(appCodePathWithRepeatedProjectName), 'file')
147139

148140
const zipFileData = await fs.readFileBytes(zipMetadata.zipFilePath)
149141
const zip = await JSZip.loadAsync(zipFileData)
@@ -154,7 +146,7 @@ describe('zipUtil', function () {
154146
it('should handle path with repeated project name for project scan', async function () {
155147
const zipMetadata = await zipUtil.generateZip(
156148
vscode.Uri.file(appCodePathWithRepeatedProjectName),
157-
CodeAnalysisScope.PROJECT
149+
'project'
158150
)
159151

160152
const zipFileData = await fs.readFileBytes(zipMetadata.zipFilePath)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ describe('CodeWhisperer security scan', async function () {
9595

9696
const projectPaths = getWorkspacePaths()
9797
const scope = CodeWhispererConstants.CodeAnalysisScope.PROJECT
98-
const zipMetadata = await zipUtil.generateZip(uri, scope)
98+
const zipMetadata = await zipUtil.generateZip(uri, 'project')
9999
const codeScanName = randomUUID()
100100

101101
let artifactMap
102102
try {
103103
artifactMap = await getPresignedUrlAndUpload(client, zipMetadata, scope, codeScanName)
104104
} finally {
105-
await zipUtil.removeTmpFiles(zipMetadata, scope)
105+
await zipUtil.removeTmpFiles(zipMetadata)
106106
}
107107
return {
108108
artifactMap: artifactMap,

0 commit comments

Comments
 (0)