Skip to content

Commit 22b00dd

Browse files
committed
CM-48119 - Add detection sorting by line number in addition to severity
1 parent 11acd50 commit 22b00dd

16 files changed

+43
-34
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
## [v1.16.0]
6+
7+
- Add detection sorting by line number in addition to severity
8+
59
## [v1.15.0]
610

711
- Add proper support for disabled modules
@@ -141,6 +145,8 @@
141145

142146
The first stable release with the support of Secrets, SCA, TreeView, Violation Card, and more.
143147

148+
[v1.16.0]: https://github.com/cycodehq/vscode-extension/releases/tag/v1.16.0
149+
144150
[v1.15.0]: https://github.com/cycodehq/vscode-extension/releases/tag/v1.15.0
145151

146152
[v1.14.0]: https://github.com/cycodehq/vscode-extension/releases/tag/v1.14.0

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "cycode",
33
"displayName": "Cycode",
4-
"version": "1.15.0",
4+
"version": "1.16.0",
55
"publisher": "cycode",
66
"description": "Boost security in your dev lifecycle via SAST, SCA, Secrets & IaC scanning.",
77
"repository": {

src/cli/models/scan-result/iac/iac-detection-details.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ export class IacDetectionDetails extends ScanDetectionDetailsBase {
1717
getFilepath(): string {
1818
return this.fileName;
1919
}
20+
21+
getLineInFile(): number {
22+
return this.lineInFile;
23+
}
2024
}

src/cli/models/scan-result/iac/iac-detection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ export class IacDetection extends DetectionBase {
2222
}
2323

2424
getFormattedNodeTitle(): string {
25-
return `line ${this.detectionDetails.lineInFile + 1}: ${this.getFormattedMessage()}`;
25+
return `line ${this.detectionDetails.getLineInFile()}: ${this.getFormattedMessage()}`;
2626
}
2727
}

src/cli/models/scan-result/sast/sast-detection-details.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ export class SastDetectionDetails extends ScanDetectionDetailsBase {
1919
getFilepath(): string {
2020
return this.filePath.startsWith('/') ? this.filePath : `/${this.filePath}`;
2121
}
22+
23+
getLineInFile(): number {
24+
return this.lineInFile;
25+
}
2226
}

src/cli/models/scan-result/sast/sast-detection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ export class SastDetection extends DetectionBase {
2222
}
2323

2424
getFormattedNodeTitle(): string {
25-
return `line ${this.detectionDetails.lineInFile}: ${this.getFormattedMessage()}`;
25+
return `line ${this.detectionDetails.getLineInFile()}: ${this.getFormattedMessage()}`;
2626
}
2727
}

src/cli/models/scan-result/sca/sca-detection-details.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ export class ScaDetectionDetails extends ScanDetectionDetailsBase {
2525
getFilepath(): string {
2626
return this.fileName;
2727
}
28+
29+
getLineInFile(): number {
30+
return this.lineInFile;
31+
}
2832
}

src/cli/models/scan-result/sca/sca-detection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ export class ScaDetection extends DetectionBase {
2323
}
2424

2525
getFormattedNodeTitle(): string {
26-
return `line ${this.detectionDetails.lineInFile}: ${this.getFormattedTitle()}`;
26+
return `line ${this.detectionDetails.getLineInFile()}: ${this.getFormattedTitle()}`;
2727
}
2828
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export abstract class ScanDetectionDetailsBase {
22
public abstract getFilepath(): string;
3+
public abstract getLineInFile(): number;
34
}

src/cli/models/scan-result/secret/secret-detection-details.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Exclude } from 'class-transformer';
22
import { ScanDetectionDetailsBase } from '../scan-detection-details-base';
33

4+
const IDE_ENTRY_LINE_NUMBER = 1;
5+
46
export class SecretDetectionDetails extends ScanDetectionDetailsBase {
57
sha512: string;
68
provider: string;
@@ -22,4 +24,8 @@ export class SecretDetectionDetails extends ScanDetectionDetailsBase {
2224
public getFilepath(): string {
2325
return `${this.filePath}${this.fileName}`;
2426
}
27+
28+
public getLineInFile(): number {
29+
return this.line + IDE_ENTRY_LINE_NUMBER;
30+
}
2531
}

src/cli/models/scan-result/secret/secret-detection.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { SecretDetectionDetails } from './secret-detection-details';
22
import { DetectionBase } from '../detection-base';
33
import { Type } from 'class-transformer';
44

5-
const IDE_ENTRY_LINE_NUMBER = 1;
6-
75
export class SecretDetection extends DetectionBase {
86
id: string;
97
message: string;
@@ -25,6 +23,6 @@ export class SecretDetection extends DetectionBase {
2523
}
2624

2725
public getFormattedNodeTitle(): string {
28-
return `line ${this.detectionDetails.line + IDE_ENTRY_LINE_NUMBER}: a hardcoded ${this.type} is used`;
26+
return `line ${this.detectionDetails.getLineInFile()}: a hardcoded ${this.type} is used`;
2927
}
3028
}

src/commands/open-violation-in-file-command.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,10 @@
11
import * as vscode from 'vscode';
22
import { DetectionBase } from '../cli/models/scan-result/detection-base';
3-
import { SecretDetection } from '../cli/models/scan-result/secret/secret-detection';
4-
import { ScaDetection } from '../cli/models/scan-result/sca/sca-detection';
5-
import { IacDetection } from '../cli/models/scan-result/iac/iac-detection';
6-
import { SastDetection } from '../cli/models/scan-result/sast/sast-detection';
73

84
const VSCODE_LINE_NUMBER_DIFF = 1; // CLI starts counting from 0, although vscode starts from line 1.
95

106
export default async (detection: DetectionBase) => {
11-
let vscodeLineNumber = 0;
12-
if (detection instanceof SecretDetection) {
13-
// secret detection line is 0-based
14-
vscodeLineNumber = detection.detectionDetails.line + 1;
15-
} else if (detection instanceof ScaDetection) {
16-
vscodeLineNumber = detection.detectionDetails.lineInFile;
17-
} else if (detection instanceof IacDetection) {
18-
vscodeLineNumber = detection.detectionDetails.lineInFile;
19-
} else if (detection instanceof SastDetection) {
20-
vscodeLineNumber = detection.detectionDetails.lineInFile;
21-
}
22-
23-
vscodeLineNumber -= VSCODE_LINE_NUMBER_DIFF;
7+
const vscodeLineNumber = detection.detectionDetails.getLineInFile() - VSCODE_LINE_NUMBER_DIFF;
248

259
const uri = vscode.Uri.file(detection.detectionDetails.getFilepath());
2610
await vscode.window.showTextDocument(uri, {

src/providers/diagnostics/iac-diagnostics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const createDiagnostics = async (
2727
message += `In file: ${fileName}\n`;
2828

2929
const diagnostic = new vscode.Diagnostic(
30-
document.lineAt(detectionDetails.lineInFile - 1).range,
30+
document.lineAt(detectionDetails.getLineInFile() - 1).range,
3131
message,
3232
vscode.DiagnosticSeverity.Error,
3333
);

src/providers/diagnostics/sast-diagnostics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const createDiagnostics = async (
2222
message += `In file: ${detection.detectionDetails.fileName}\n`;
2323

2424
const diagnostic = new vscode.Diagnostic(
25-
document.lineAt(detectionDetails.lineInFile - 1).range,
25+
document.lineAt(detectionDetails.getLineInFile() - 1).range,
2626
message,
2727
vscode.DiagnosticSeverity.Error,
2828
);

src/providers/diagnostics/sca-diagnostics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const createDiagnostics = async (
3232

3333
const diagnostic = new vscode.Diagnostic(
3434
// BE of SCA counts lines from 1, while VSCode counts from 0
35-
document.lineAt(detectionDetails.lineInFile - 1).range,
35+
document.lineAt(detectionDetails.getLineInFile() - 1).range,
3636
message,
3737
vscode.DiagnosticSeverity.Error,
3838
);

src/providers/tree-data/provider.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,7 @@ export class TreeDataProvider implements vscode.TreeDataProvider<BaseNode> {
103103
return !enabledSeverityFilters.has(detection.severity.toLowerCase());
104104
});
105105

106-
const severitySortedDetections = severityFilteredDetections.sort((a, b) => {
107-
return this.getSeverityWeight(b.severity) - this.getSeverityWeight(a.severity);
108-
});
109-
110-
const groupedByFilepathDetection = severitySortedDetections
106+
const groupedByFilepathDetection = severityFilteredDetections
111107
.reduce<Map<string, DetectionBase[]>>((acc, detection) => {
112108
const filepath = detection.detectionDetails.getFilepath();
113109
if (!acc.has(filepath)) {
@@ -117,7 +113,7 @@ export class TreeDataProvider implements vscode.TreeDataProvider<BaseNode> {
117113
return acc;
118114
}, new Map());
119115

120-
const scanTypeNode = new ScanTypeNode(scanType, this.getScanTypeNodeSummary(severitySortedDetections));
116+
const scanTypeNode = new ScanTypeNode(scanType, this.getScanTypeNodeSummary(severityFilteredDetections));
121117
this._createdRootNodes.push(scanTypeNode);
122118
this._createdNodesToChildren.set(scanTypeNode, []);
123119

@@ -127,7 +123,13 @@ export class TreeDataProvider implements vscode.TreeDataProvider<BaseNode> {
127123
this._createdNodesToChildren.set(fileNode, []);
128124
this._createdChildToParentNode.set(fileNode, scanTypeNode);
129125

130-
for (const detection of detections) {
126+
const sortedDetections = detections.sort((a, b) => {
127+
const severityDiff = this.getSeverityWeight(b.severity) - this.getSeverityWeight(a.severity);
128+
const lineDiff = a.detectionDetails.getLineInFile() - b.detectionDetails.getLineInFile();
129+
return severityDiff !== 0 ? severityDiff : lineDiff;
130+
});
131+
132+
for (const detection of sortedDetections) {
131133
const detectionNode = new DetectionNode(scanType, detection);
132134
this._createdNodesToChildren.get(fileNode)?.push(detectionNode);
133135
this._createdChildToParentNode.set(detectionNode, fileNode);

0 commit comments

Comments
 (0)