Skip to content

Commit 73ab3a6

Browse files
committed
refactor: move use case enum one level up
1 parent f2a504f commit 73ab3a6

File tree

2 files changed

+37
-26
lines changed

2 files changed

+37
-26
lines changed

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

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,17 @@ export interface ZipMetadata {
4242
}
4343

4444
export interface GenerateZipOptions {
45-
includeGitDiffHeader?: boolean
45+
includeGitDiff?: boolean
4646
silent?: boolean
4747
}
4848

49+
export interface ZipProjectOptions {
50+
projectPath?: string
51+
includeGitDiff?: boolean
52+
metadataDir?: string
53+
includeNonWorkspaceFiles?: boolean
54+
}
55+
4956
export const ZipConstants = {
5057
newlineRegex: /\r?\n/,
5158
gitignoreFilename: '.gitignore',
@@ -180,31 +187,23 @@ export class ZipUtil {
180187
await processDirectory(metadataDir)
181188
}
182189

183-
protected async zipProject(useCase: FeatureUseCase, projectPath?: string, metadataDir?: string) {
190+
protected async zipProject(
191+
workspaceFolders: CurrentWsFolders,
192+
excludePatterns: string[],
193+
options?: ZipProjectOptions
194+
) {
184195
const zip = new ZipStream()
185-
let projectPaths = []
186-
if (useCase === FeatureUseCase.TEST_GENERATION && projectPath) {
187-
projectPaths.push(projectPath)
188-
} else {
189-
projectPaths = getWorkspacePaths()
190-
}
191-
if (useCase === FeatureUseCase.CODE_SCAN) {
196+
const projectPaths = options?.projectPath ? [options?.projectPath] : getWorkspacePaths()
197+
if (options?.includeGitDiff) {
192198
await this.processCombinedGitDiff(zip, projectPaths, '')
193199
}
194200
const languageCount = new Map<CodewhispererLanguage, number>()
195201

196-
await this.processSourceFiles(
197-
zip,
198-
languageCount,
199-
projectPaths,
200-
this.getWorkspaceFolders(useCase),
201-
this.getExcludePatterns(useCase),
202-
useCase === FeatureUseCase.TEST_GENERATION
203-
)
204-
if (metadataDir) {
205-
await this.processMetadataDir(zip, metadataDir)
202+
await this.processSourceFiles(zip, languageCount, projectPaths, workspaceFolders, excludePatterns)
203+
if (options?.metadataDir) {
204+
await this.processMetadataDir(zip, options?.metadataDir)
206205
}
207-
if (useCase !== FeatureUseCase.TEST_GENERATION) {
206+
if (options?.includeNonWorkspaceFiles) {
208207
this.processOtherFiles(zip, languageCount)
209208
}
210209

@@ -364,9 +363,14 @@ export class ZipUtil {
364363
): Promise<ZipMetadata> {
365364
try {
366365
const zipDirPath = this.getZipDirPath()
367-
const zipFilePath = await (zipType === 'file'
368-
? this.zipFile(uri, options?.includeGitDiffHeader)
369-
: this.zipProject(FeatureUseCase.CODE_SCAN))
366+
const zipFilePath =
367+
zipType === 'file'
368+
? await this.zipFile(uri, options?.includeGitDiff)
369+
: await this.zipProject(
370+
this.getWorkspaceFolders(FeatureUseCase.CODE_SCAN),
371+
this.getExcludePatterns(FeatureUseCase.CODE_SCAN),
372+
{ includeGitDiff: options?.includeGitDiff, includeNonWorkspaceFiles: true }
373+
)
370374

371375
if (!options?.silent) {
372376
getLogger().debug(`Picked source files: [${[...this._pickedSourceFiles].join(', ')}]`)
@@ -413,7 +417,14 @@ export class ZipUtil {
413417
}
414418
}
415419

416-
const zipFilePath: string = await this.zipProject(FeatureUseCase.TEST_GENERATION, projectPath, metadataDir)
420+
const zipFilePath: string = await this.zipProject(
421+
this.getWorkspaceFolders(FeatureUseCase.TEST_GENERATION),
422+
this.getExcludePatterns(FeatureUseCase.TEST_GENERATION),
423+
{
424+
metadataDir,
425+
projectPath,
426+
}
427+
)
417428
const zipFileSize = (await fs.stat(zipFilePath)).size
418429
return {
419430
rootDir: zipDirPath,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('zipUtil', function () {
6969

7070
it('Should generate zip for file scan and return expected metadata', async function () {
7171
const zipMetadata = await zipUtil.generateZip(vscode.Uri.file(appCodePath), 'file', {
72-
includeGitDiffHeader: true,
72+
includeGitDiff: true,
7373
})
7474
assert.strictEqual(zipMetadata.lines, 49)
7575
assert.ok(zipMetadata.rootDir.includes(codeScanTruncDirPrefix))
@@ -84,7 +84,7 @@ describe('zipUtil', function () {
8484
sinon.stub(ZipUtil, 'aboveByteLimit').returns(true)
8585

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

0 commit comments

Comments
 (0)