Skip to content

Commit 19b3238

Browse files
committed
refactor: move /test and /review specific logic to their respective files
1 parent 623e290 commit 19b3238

File tree

4 files changed

+128
-138
lines changed

4 files changed

+128
-138
lines changed

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import {
5050
import { SecurityIssueTreeViewProvider } from '../service/securityIssueTreeViewProvider'
5151
import { ChatSessionManager } from '../../amazonqScan/chat/storages/chatSession'
5252
import { TelemetryHelper } from '../util/telemetryHelper'
53-
import { getWorkspacePaths } from '../../shared/utilities/workspaceUtils'
53+
import { CurrentWsFolders, defaultExcludePatterns, getWorkspacePaths } from '../../shared/utilities/workspaceUtils'
5454

5555
const localize = nls.loadMessageBundle()
5656
export const stopScanButton = localize('aws.codewhisperer.stopscan', 'Stop Scan')
@@ -159,8 +159,12 @@ export async function startSecurityScan(
159159
const zipType = scope === CodeAnalysisScope.PROJECT ? 'project' : 'file'
160160
const zipMetadata =
161161
zipType === 'file'
162-
? await zipUtil.generateZipCodeScanForFile(editor?.document.uri)
163-
: await zipUtil.generateZipCodeScanForProject()
162+
? await zipUtil.zipFile(editor?.document.uri)
163+
: await zipUtil.zipProject(
164+
[...(vscode.workspace.workspaceFolders ?? [])] as CurrentWsFolders,
165+
defaultExcludePatterns,
166+
{ includeNonWorkspaceFiles: true }
167+
)
164168
const projectPaths = getWorkspacePaths()
165169

166170
const contextTruncationStartTime = performance.now()
@@ -546,3 +550,27 @@ function showScanCompletedNotification(total: number, scannedFiles: Set<string>)
546550
}
547551
})
548552
}
553+
554+
export async function generateZipCodeScanForProject(
555+
zipUtil: ZipUtil,
556+
options?: {
557+
includeGitDiff?: boolean
558+
silent?: boolean
559+
}
560+
): Promise<ZipMetadata> {
561+
try {
562+
// We assume there is at least one workspace open.
563+
return await zipUtil.zipProject(
564+
[...(vscode.workspace.workspaceFolders ?? [])] as CurrentWsFolders,
565+
defaultExcludePatterns,
566+
{
567+
includeGitDiff: options?.includeGitDiff,
568+
includeNonWorkspaceFiles: true,
569+
silent: options?.silent,
570+
}
571+
)
572+
} catch (error) {
573+
getLogger().error('Zip error caused by: %O', error)
574+
throw error
575+
}
576+
}

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

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import { getLogger } from '../../shared/logger/logger'
7-
import { ZipUtil } from '../util/zipUtil'
7+
import { ZipMetadata, ZipUtil } from '../util/zipUtil'
88
import { ArtifactMap } from '../client/codewhisperer'
99
import { testGenerationLogsDir } from '../../shared/filesystemUtilities'
1010
import {
@@ -14,15 +14,17 @@ import {
1414
pollTestJobStatus,
1515
throwIfCancelled,
1616
} from '../service/testGenHandler'
17+
import vscode from 'vscode'
1718
import path from 'path'
1819
import { testGenState } from '../models/model'
1920
import { ChatSessionManager } from '../../amazonqTest/chat/storages/chatSession'
2021
import { ChildProcess, spawn } from 'child_process' // eslint-disable-line no-restricted-imports
2122
import { BuildStatus } from '../../amazonqTest/chat/session/session'
2223
import { fs } from '../../shared/fs/fs'
2324
import { Range } from '../client/codewhispereruserclient'
24-
import { getWorkspaceForFile } from '../../shared/utilities/workspaceUtils'
25+
import { CurrentWsFolders, defaultExcludePatterns, getWorkspaceForFile } from '../../shared/utilities/workspaceUtils'
2526
import * as CodeWhispererConstants from '../models/constants'
27+
import { ProjectZipError } from '../../amazonqTest/error'
2628

2729
// eslint-disable-next-line unicorn/no-null
2830
let spawnResult: ChildProcess | null = null
@@ -58,7 +60,7 @@ export async function startTestGenerationProcess(
5860
session.projectRootPath = projectPath
5961
session.listOfTestGenerationJobId = []
6062
}
61-
const zipMetadata = await zipUtil.generateZipTestGen(session.projectRootPath, initialExecution)
63+
const zipMetadata = await generateZipTestGen(zipUtil, session.projectRootPath, initialExecution)
6264
session.srcPayloadSize = zipMetadata.buildPayloadSizeInBytes
6365
session.srcZipFileSize = zipMetadata.zipFileSizeInBytes
6466

@@ -254,3 +256,72 @@ export function cancelBuild() {
254256
getLogger().info('No active build to cancel')
255257
}
256258
}
259+
260+
export async function generateZipTestGen(
261+
zipUtil: ZipUtil,
262+
projectPath: string,
263+
initialExecution: boolean
264+
): Promise<ZipMetadata> {
265+
try {
266+
const zipDirPath = zipUtil.getZipDirPath()
267+
268+
const metadataDir = path.join(zipDirPath, 'utgRequiredArtifactsDir')
269+
270+
// Create directories
271+
const dirs = {
272+
metadata: metadataDir,
273+
buildAndExecuteLogDir: path.join(metadataDir, 'buildAndExecuteLogDir'),
274+
repoMapDir: path.join(metadataDir, 'repoMapData'),
275+
testCoverageDir: path.join(metadataDir, 'testCoverageDir'),
276+
}
277+
await Promise.all(Object.values(dirs).map((dir) => fs.mkdir(dir)))
278+
279+
if (!initialExecution) {
280+
await processTestCoverageFiles(dirs.testCoverageDir)
281+
282+
const sourcePath = path.join(testGenerationLogsDir, 'output.log')
283+
const targetPath = path.join(dirs.buildAndExecuteLogDir, 'output.log')
284+
if (await fs.exists(sourcePath)) {
285+
await fs.copy(sourcePath, targetPath)
286+
}
287+
}
288+
// We assume there is at least one workspace open.
289+
return await zipUtil.zipProject(
290+
[...(vscode.workspace.workspaceFolders ?? [])].sort(
291+
(a, b) => b.uri.fsPath.length - a.uri.fsPath.length
292+
) as CurrentWsFolders,
293+
[...CodeWhispererConstants.testGenExcludePatterns, ...defaultExcludePatterns],
294+
{
295+
metadataDir,
296+
projectPath,
297+
silent: true,
298+
}
299+
)
300+
} catch (error) {
301+
getLogger().error('Zip error caused by: %s', error)
302+
throw new ProjectZipError(
303+
error instanceof Error ? error.message : 'Unknown error occurred during zip operation'
304+
)
305+
}
306+
}
307+
308+
async function processTestCoverageFiles(targetPath: string) {
309+
// TODO: will be removed post release
310+
const coverageFilePatterns = ['**/coverage.xml', '**/coverage.json', '**/coverage.txt']
311+
let files: vscode.Uri[] = []
312+
313+
for (const pattern of coverageFilePatterns) {
314+
files = await vscode.workspace.findFiles(pattern)
315+
if (files.length > 0) {
316+
break
317+
}
318+
}
319+
320+
await Promise.all(
321+
files.map(async (file) => {
322+
const fileName = path.basename(file.path)
323+
const targetFilePath = path.join(targetPath, fileName)
324+
await fs.copy(file.path, targetFilePath)
325+
})
326+
)
327+
}

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

Lines changed: 7 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,19 @@
44
*/
55
import * as vscode from 'vscode'
66
import path from 'path'
7-
import { tempDirPath, testGenerationLogsDir } from '../../shared/filesystemUtilities'
7+
import { tempDirPath } from '../../shared/filesystemUtilities'
88
import { getLogger, getNullLogger } from '../../shared/logger/logger'
99
import * as CodeWhispererConstants from '../models/constants'
1010
import { fs } from '../../shared/fs/fs'
1111
import { runtimeLanguageContext } from './runtimeLanguageContext'
1212
import { CodewhispererLanguage } from '../../shared/telemetry/telemetry.gen'
13-
import {
14-
CurrentWsFolders,
15-
collectFiles,
16-
defaultExcludePatterns,
17-
getWorkspacePaths,
18-
} from '../../shared/utilities/workspaceUtils'
13+
import { CurrentWsFolders, collectFiles, getWorkspacePaths } from '../../shared/utilities/workspaceUtils'
1914
import {
2015
FileSizeExceededError,
2116
NoActiveFileError,
2217
NoSourceFilesError,
2318
ProjectSizeExceededError,
2419
} from '../models/errors'
25-
import { ProjectZipError } from '../../amazonqTest/error'
2620
import { normalize } from '../../shared/utilities/pathUtils'
2721
import { ZipStream } from '../../shared/utilities/zipStream'
2822
import { getTextContent } from '../../shared/utilities/textDocumentUtilities'
@@ -41,11 +35,6 @@ export interface ZipMetadata {
4135
language: CodewhispererLanguage | undefined
4236
}
4337

44-
export interface GenerateZipOptions {
45-
includeGitDiff?: boolean
46-
silent?: boolean
47-
}
48-
4938
export interface ZipProjectOptions {
5039
projectPath?: string
5140
includeGitDiff?: boolean
@@ -88,7 +77,7 @@ export class ZipUtil {
8877
return ZipUtil.aboveByteLimit(current + adding, 'project')
8978
}
9079

91-
protected async zipFile(uri: vscode.Uri | undefined, includeGitDiffHeader?: boolean) {
80+
public async zipFile(uri: vscode.Uri | undefined, includeGitDiffHeader?: boolean) {
9281
if (!uri) {
9382
throw new NoActiveFileError()
9483
}
@@ -121,9 +110,10 @@ export class ZipUtil {
121110
if (ZipUtil.aboveByteLimit(this._totalSize, 'file')) {
122111
throw new FileSizeExceededError()
123112
}
124-
const zipFilePath = this.getZipDirPath() + CodeWhispererConstants.codeScanZipExt
113+
const zipDirPath = this.getZipDirPath()
114+
const zipFilePath = zipDirPath + CodeWhispererConstants.codeScanZipExt
125115
await zip.finalizeToFile(zipFilePath)
126-
return zipFilePath
116+
return this.getGenerateZipResult(zipDirPath, zipFilePath)
127117
}
128118

129119
protected getZipEntryPath(projectName: string, relativePath: string) {
@@ -188,7 +178,7 @@ export class ZipUtil {
188178
await processDirectory(metadataDir)
189179
}
190180

191-
protected async zipProject(
181+
public async zipProject(
192182
workspaceFolders: CurrentWsFolders,
193183
excludePatterns: string[],
194184
options?: ZipProjectOptions
@@ -263,27 +253,6 @@ export class ZipUtil {
263253
}
264254
}
265255

266-
protected async processTestCoverageFiles(targetPath: string) {
267-
// TODO: will be removed post release
268-
const coverageFilePatterns = ['**/coverage.xml', '**/coverage.json', '**/coverage.txt']
269-
let files: vscode.Uri[] = []
270-
271-
for (const pattern of coverageFilePatterns) {
272-
files = await vscode.workspace.findFiles(pattern)
273-
if (files.length > 0) {
274-
break
275-
}
276-
}
277-
278-
await Promise.all(
279-
files.map(async (file) => {
280-
const fileName = path.basename(file.path)
281-
const targetFilePath = path.join(targetPath, fileName)
282-
await fs.copy(file.path, targetFilePath)
283-
})
284-
)
285-
}
286-
287256
protected processTextFile(
288257
zip: ZipStream,
289258
uri: vscode.Uri,
@@ -337,82 +306,6 @@ export class ZipUtil {
337306
return this._zipDir
338307
}
339308

340-
public async generateZipCodeScanForFile(
341-
uri: vscode.Uri | undefined,
342-
options?: GenerateZipOptions
343-
): Promise<ZipMetadata> {
344-
try {
345-
const zipDirPath = this.getZipDirPath()
346-
const zipFilePath = await this.zipFile(uri, options?.includeGitDiff)
347-
348-
return this.getGenerateZipResult(zipDirPath, zipFilePath, options?.silent)
349-
} catch (error) {
350-
getLogger().error('Zip error caused by: %O', error)
351-
throw error
352-
}
353-
}
354-
355-
public async generateZipCodeScanForProject(options?: GenerateZipOptions): Promise<ZipMetadata> {
356-
try {
357-
// We assume there is at least one workspace open.
358-
const workspaceFolders = [...(vscode.workspace.workspaceFolders ?? [])] as CurrentWsFolders
359-
360-
return await this.zipProject(workspaceFolders, defaultExcludePatterns, {
361-
includeGitDiff: options?.includeGitDiff,
362-
includeNonWorkspaceFiles: true,
363-
silent: options?.silent,
364-
})
365-
} catch (error) {
366-
getLogger().error('Zip error caused by: %O', error)
367-
throw error
368-
}
369-
}
370-
371-
public async generateZipTestGen(projectPath: string, initialExecution: boolean): Promise<ZipMetadata> {
372-
try {
373-
const zipDirPath = this.getZipDirPath()
374-
375-
const metadataDir = path.join(zipDirPath, 'utgRequiredArtifactsDir')
376-
377-
// Create directories
378-
const dirs = {
379-
metadata: metadataDir,
380-
buildAndExecuteLogDir: path.join(metadataDir, 'buildAndExecuteLogDir'),
381-
repoMapDir: path.join(metadataDir, 'repoMapData'),
382-
testCoverageDir: path.join(metadataDir, 'testCoverageDir'),
383-
}
384-
await Promise.all(Object.values(dirs).map((dir) => fs.mkdir(dir)))
385-
386-
if (!initialExecution) {
387-
await this.processTestCoverageFiles(dirs.testCoverageDir)
388-
389-
const sourcePath = path.join(testGenerationLogsDir, 'output.log')
390-
const targetPath = path.join(dirs.buildAndExecuteLogDir, 'output.log')
391-
if (await fs.exists(sourcePath)) {
392-
await fs.copy(sourcePath, targetPath)
393-
}
394-
}
395-
// We assume there is at least one workspace open.
396-
const workspaceFolders = [...(vscode.workspace.workspaceFolders ?? [])].sort(
397-
(a, b) => b.uri.fsPath.length - a.uri.fsPath.length
398-
) as CurrentWsFolders
399-
return await this.zipProject(
400-
workspaceFolders,
401-
[...CodeWhispererConstants.testGenExcludePatterns, ...defaultExcludePatterns],
402-
{
403-
metadataDir,
404-
projectPath,
405-
silent: true,
406-
}
407-
)
408-
} catch (error) {
409-
getLogger().error('Zip error caused by: %s', error)
410-
throw new ProjectZipError(
411-
error instanceof Error ? error.message : 'Unknown error occurred during zip operation'
412-
)
413-
}
414-
}
415-
416309
protected async getGenerateZipResult(
417310
zipDirpath: string,
418311
zipFilePath: string,

0 commit comments

Comments
 (0)