Skip to content

Commit f4be9e2

Browse files
committed
CM-64295 changed behavior of starting all scans to pop up one message and later show finished message per scanner
1 parent f709704 commit f4be9e2

2 files changed

Lines changed: 52 additions & 23 deletions

File tree

src/commands/run-all-scans-command.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,12 @@ export default getCommonCommand(async () => {
99
const cycodeService = container.resolve<ICycodeService>(CycodeService);
1010
const stateService = container.resolve<IStateService>(StateServiceSymbol);
1111

12-
const scanPromises = [];
12+
const enabledScanTypes: CliScanType[] = [];
1313

14-
if (stateService.tempState.IsSecretScanningEnabled) {
15-
scanPromises.push(cycodeService.startScanForCurrentProject(CliScanType.Secret));
16-
}
17-
if (stateService.tempState.IsScaScanningEnabled) {
18-
scanPromises.push(cycodeService.startScanForCurrentProject(CliScanType.Sca));
19-
}
20-
if (stateService.tempState.IsIacScanningEnabled) {
21-
scanPromises.push(cycodeService.startScanForCurrentProject(CliScanType.Iac));
22-
}
23-
if (stateService.tempState.IsSastScanningEnabled) {
24-
scanPromises.push(cycodeService.startScanForCurrentProject(CliScanType.Sast));
25-
}
14+
if (stateService.tempState.IsSecretScanningEnabled) enabledScanTypes.push(CliScanType.Secret);
15+
if (stateService.tempState.IsScaScanningEnabled) enabledScanTypes.push(CliScanType.Sca);
16+
if (stateService.tempState.IsIacScanningEnabled) enabledScanTypes.push(CliScanType.Iac);
17+
if (stateService.tempState.IsSastScanningEnabled) enabledScanTypes.push(CliScanType.Sast);
2618

27-
await Promise.all(scanPromises);
19+
await cycodeService.startAllScansForCurrentProject(enabledScanTypes);
2820
});

src/services/cycode-service.ts

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface ICycodeService {
2323
startAuth(): Promise<void>;
2424
startScan(scanType: CliScanType, paths: string[], onDemand: boolean): Promise<void>;
2525
startScanForCurrentProject(scanType: CliScanType): Promise<void>;
26+
startAllScansForCurrentProject(scanTypes: CliScanType[]): Promise<void>;
2627
applyDetectionIgnore(scanType: CliScanType, ignoreType: CliIgnoreType, value: string): Promise<void>;
2728
getAiRemediation(detectionId: string): Promise<AiRemediationResultData | null>;
2829
}
@@ -99,17 +100,22 @@ export class CycodeService implements ICycodeService {
99100
await this.startScan(scanType, [projectRoot], true); // onDemand = true
100101
}
101102

102-
public async startScan(scanType: CliScanType, paths: string[], onDemand = false) {
103-
const scanMethods = {
104-
[CliScanType.Secret]: (
105-
token: vscode.CancellationToken,
106-
) => this.cliService.scanPathsSecrets(paths, onDemand, token),
107-
[CliScanType.Sca]: (token: vscode.CancellationToken) => this.cliService.scanPathsSca(paths, onDemand, token),
108-
[CliScanType.Iac]: (token: vscode.CancellationToken) => this.cliService.scanPathsIac(paths, onDemand, token),
109-
[CliScanType.Sast]: (token: vscode.CancellationToken) => this.cliService.scanPathsSast(paths, onDemand, token),
103+
private getScanMethod(
104+
scanType: CliScanType,
105+
paths: string[],
106+
onDemand: boolean,
107+
): ((token: vscode.CancellationToken) => Promise<void>) | undefined {
108+
const scanMethods: Record<CliScanType, (token: vscode.CancellationToken) => Promise<void>> = {
109+
[CliScanType.Secret]: (token) => this.cliService.scanPathsSecrets(paths, onDemand, token),
110+
[CliScanType.Sca]: (token) => this.cliService.scanPathsSca(paths, onDemand, token),
111+
[CliScanType.Iac]: (token) => this.cliService.scanPathsIac(paths, onDemand, token),
112+
[CliScanType.Sast]: (token) => this.cliService.scanPathsSast(paths, onDemand, token),
110113
};
114+
return scanMethods[scanType];
115+
}
111116

112-
const scanMethod = scanMethods[scanType];
117+
public async startScan(scanType: CliScanType, paths: string[], onDemand = false) {
118+
const scanMethod = this.getScanMethod(scanType, paths, onDemand);
113119
if (!scanMethod) {
114120
this.logger.error(`Unknown scan type: ${scanType}`);
115121
return;
@@ -128,6 +134,37 @@ export class CycodeService implements ICycodeService {
128134
);
129135
}
130136

137+
public async startAllScansForCurrentProject(scanTypes: CliScanType[]) {
138+
const projectRoot = this.cliService.getProjectRootDirectory();
139+
if (!projectRoot) {
140+
vscode.window.showErrorMessage(
141+
'Cycode scans the project that is currently opened. Please open a project and try again',
142+
);
143+
return;
144+
}
145+
146+
await this.withProgressBar(
147+
'Cycode is scanning files...',
148+
async (cancellationToken: vscode.CancellationToken) => {
149+
this.logger.debug(`[RunAll] Start scanning paths: ${projectRoot}`);
150+
statusBar.showScanningInProgress();
151+
await Promise.all(
152+
scanTypes.map((scanType) => {
153+
const scanMethod = this.getScanMethod(scanType, [projectRoot], true);
154+
if (!scanMethod) {
155+
this.logger.error(`Unknown scan type: ${scanType}`);
156+
return Promise.resolve();
157+
}
158+
return scanMethod(cancellationToken);
159+
}),
160+
);
161+
statusBar.showScanComplete();
162+
this.logger.debug(`[RunAll] Finish scanning paths: ${projectRoot}`);
163+
},
164+
{ cancellable: true, location: vscode.ProgressLocation.Notification },
165+
);
166+
}
167+
131168
private async applyDetectionIgnoreInUi(ignoreType: CliIgnoreType, value: string) {
132169
if (ignoreType !== CliIgnoreType.Value) {
133170
return;

0 commit comments

Comments
 (0)