-
Notifications
You must be signed in to change notification settings - Fork 0
CM-35961 - Leave "Open violation card" as only one quick fix action #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MarshalX
merged 1 commit into
main
from
CM-35961-leave-open-violation-card-as-only-one-quick-fix-action
May 21, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,95 +1,93 @@ | ||
| import * as vscode from 'vscode'; | ||
| import {DiagnosticCode} from '../../services/common'; | ||
| import {VscodeCommands} from '../../utils/commands'; | ||
| import {CommandParameters} from '../../cli-wrapper/constants'; | ||
| import {IgnoreCommandConfig} from '../../types/commands'; | ||
| import {scanResultsService} from '../../services/ScanResultsService'; | ||
| import {AnyDetection, IacDetection, SastDetection, ScaDetection, SecretDetection} from '../../types/detection'; | ||
| import {ScanType} from '../../constants'; | ||
|
|
||
| export const createIgnoreRuleAction = ( | ||
| diagnostics: vscode.Diagnostic[], diagnosticCode: DiagnosticCode, document: vscode.TextDocument | ||
| ): vscode.CodeAction => { | ||
| const detection = scanResultsService.getDetectionById(diagnosticCode.uniqueDetectionId); | ||
| const ruleId = detection?.detection_rule_id; | ||
| const _getOpenViolationCardActionSastTitle = (detection: SastDetection) => { | ||
| return detection?.detection_details.policy_display_name; | ||
| }; | ||
|
|
||
| const ignoreRuleAction = new vscode.CodeAction( | ||
| `ignore rule ${ruleId}`, | ||
| vscode.CodeActionKind.QuickFix | ||
| ); | ||
| ignoreRuleAction.command = { | ||
| command: VscodeCommands.IgnoreCommandId, | ||
| title: `Ignore rule ID: ${ruleId}`, | ||
| tooltip: 'This will always ignore this rule type', | ||
| arguments: [ | ||
| { | ||
| scanType: diagnosticCode.scanType, | ||
| ignoreBy: CommandParameters.ByRule, | ||
| param: ruleId, | ||
| filePath: document.fileName, | ||
| } as IgnoreCommandConfig, | ||
| ], | ||
| }; | ||
| ignoreRuleAction.diagnostics = diagnostics; | ||
| ignoreRuleAction.isPreferred = false; | ||
| const _getOpenViolationCardActionIacTitle = (detection: IacDetection) => { | ||
| return detection?.message; | ||
| }; | ||
|
|
||
| return ignoreRuleAction; | ||
| const _getOpenViolationCardActionScaTitle = (detection: ScaDetection) => { | ||
| let description = detection.detection_details.vulnerability_description; | ||
| if (!description) { | ||
| // if detection is about non-premise licence | ||
| description = detection.message; | ||
| } | ||
|
|
||
| return description; | ||
| }; | ||
|
|
||
| export const createIgnorePathAction = ( | ||
| diagnostics: vscode.Diagnostic[], diagnosticCode: DiagnosticCode, document: vscode.TextDocument | ||
| ): vscode.CodeAction => { | ||
| const ignorePathAction = new vscode.CodeAction( | ||
| `ignore path ${document.uri.fsPath}`, | ||
| vscode.CodeActionKind.QuickFix | ||
| ); | ||
| ignorePathAction.command = { | ||
| command: VscodeCommands.IgnoreCommandId, | ||
| title: `Ignore path: ${document.uri.fsPath}`, | ||
| tooltip: 'This will always ignore this path', | ||
| arguments: [ | ||
| { | ||
| scanType: diagnosticCode.scanType, | ||
| ignoreBy: CommandParameters.ByPath, | ||
| param: document.uri.fsPath, | ||
| filePath: document.fileName, | ||
| } as IgnoreCommandConfig, | ||
| ], | ||
| }; | ||
| ignorePathAction.diagnostics = diagnostics; | ||
| ignorePathAction.isPreferred = false; | ||
| const _getOpenViolationCardActionSecretTitle = (detection: SecretDetection) => { | ||
| return `a hardcoded ${detection.type} is used`; | ||
| }; | ||
|
|
||
| const _getOpenViolationCardActionDetectionSpecificTitle = ( | ||
| detection: AnyDetection, diagnosticCode: DiagnosticCode | ||
| ): string => { | ||
| switch (diagnosticCode.scanType) { | ||
| case ScanType.Sast: | ||
| return _getOpenViolationCardActionSastTitle(detection as SastDetection); | ||
| case ScanType.Secrets: | ||
| return _getOpenViolationCardActionSecretTitle(detection as SecretDetection); | ||
| case ScanType.Sca: | ||
| return _getOpenViolationCardActionScaTitle(detection as ScaDetection); | ||
| case ScanType.Iac: | ||
| return _getOpenViolationCardActionIacTitle(detection as IacDetection); | ||
| default: | ||
| return detection?.message; | ||
| } | ||
| }; | ||
|
|
||
| const _getOpenViolationCardActionTitle = ( | ||
| detection: AnyDetection, diagnosticCode: DiagnosticCode | ||
| ): string => { | ||
| let title = _getOpenViolationCardActionDetectionSpecificTitle(detection, diagnosticCode); | ||
|
|
||
| return ignorePathAction; | ||
| // cut too long messages | ||
| if (title && title.length > 50) { | ||
| title = title.slice(0, 50) + '...'; | ||
| } | ||
|
|
||
| // Cut too long ID. | ||
| // The original unique ID is 2 ** 64 combinations (16 characters). | ||
| // We cut it to 6 characters to make it more readable. | ||
| // It gives as 2 ** 24 combinations that are still enough to be collision-free. | ||
| // Because it's super rare to have the same detections in the same file in the same text range. | ||
| const uniqueDetectionId = diagnosticCode.uniqueDetectionId.slice(0, 6); | ||
|
|
||
| return `Cycode: ${title} (${uniqueDetectionId})`; | ||
| }; | ||
|
|
||
| export const createOpenViolationCardAction = ( | ||
| diagnostics: vscode.Diagnostic[], diagnosticCode: DiagnosticCode | ||
| ): vscode.CodeAction => { | ||
| const detection = scanResultsService.getDetectionById(diagnosticCode.uniqueDetectionId); | ||
|
|
||
| let message = detection?.message; | ||
| if (detection?.type === 'SAST') { | ||
| message = detection?.detection_details.policy_display_name; | ||
| const scanResult = scanResultsService.getDetectionById(diagnosticCode.uniqueDetectionId); | ||
| if (!scanResult) { | ||
| throw new Error(`Detection with id ${diagnosticCode.uniqueDetectionId} not found`); | ||
| } | ||
|
|
||
| if (message && message.length > 50) { | ||
| message = message.slice(0, 50) + '...'; | ||
| } | ||
| const title = _getOpenViolationCardActionTitle(scanResult.detection, diagnosticCode); | ||
|
|
||
| const openViolationCardAction = new vscode.CodeAction( | ||
| `open violation card for ${message}`, | ||
| vscode.CodeActionKind.QuickFix | ||
| title, vscode.CodeActionKind.QuickFix | ||
| ); | ||
| openViolationCardAction.command = { | ||
| command: VscodeCommands.OpenViolationPanel, | ||
| title: `Open Violation Card: ${message}`, | ||
| title: title, | ||
| tooltip: 'This will open violation card for this detection', | ||
| arguments: [ | ||
| diagnosticCode.scanType, | ||
| detection, | ||
| scanResult.detection, | ||
| ], | ||
| }; | ||
| openViolationCardAction.diagnostics = diagnostics; | ||
| openViolationCardAction.isPreferred = true; | ||
|
|
||
| return openViolationCardAction; | ||
| }; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,12 @@ | ||
| import * as vscode from 'vscode'; | ||
| import {DiagnosticCode} from '../../services/common'; | ||
| import {createIgnorePathAction, createIgnoreRuleAction, createOpenViolationCardAction} from './commonActions'; | ||
| import {createOpenViolationCardAction} from './commonActions'; | ||
|
|
||
| export const createCommandCodeActions = ( | ||
| document: vscode.TextDocument, | ||
| diagnostics: vscode.Diagnostic[], | ||
| diagnosticCode: DiagnosticCode, | ||
| ): vscode.CodeAction[] => { | ||
| return [ | ||
| createOpenViolationCardAction(diagnostics, diagnosticCode), | ||
| createIgnoreRuleAction(diagnostics, diagnosticCode, document), | ||
| createIgnorePathAction(diagnostics, diagnosticCode, document), | ||
| ]; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,12 @@ | ||
| import * as vscode from 'vscode'; | ||
| import {DiagnosticCode} from '../../services/common'; | ||
| import {createIgnorePathAction, createIgnoreRuleAction, createOpenViolationCardAction} from './commonActions'; | ||
| import {createOpenViolationCardAction} from './commonActions'; | ||
|
|
||
| export const createCommandCodeActions = ( | ||
| document: vscode.TextDocument, | ||
| diagnostics: vscode.Diagnostic[], | ||
| diagnosticCode: DiagnosticCode, | ||
| ): vscode.CodeAction[] => { | ||
| return [ | ||
| createOpenViolationCardAction(diagnostics, diagnosticCode), | ||
| createIgnoreRuleAction(diagnostics, diagnosticCode, document), | ||
| createIgnorePathAction(diagnostics, diagnosticCode, document), | ||
| ]; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,12 @@ | ||
| import * as vscode from 'vscode'; | ||
| import {DiagnosticCode} from '../../services/common'; | ||
| import {createIgnorePathAction, createIgnoreRuleAction, createOpenViolationCardAction} from './commonActions'; | ||
| import {createOpenViolationCardAction} from './commonActions'; | ||
|
|
||
| export const createCommandCodeActions = ( | ||
| document: vscode.TextDocument, | ||
| diagnostics: vscode.Diagnostic[], | ||
| diagnosticCode: DiagnosticCode, | ||
| ): vscode.CodeAction[] => { | ||
| return [ | ||
| createOpenViolationCardAction(diagnostics, diagnosticCode), | ||
| createIgnoreRuleAction(diagnostics, diagnosticCode, document), | ||
| createIgnorePathAction(diagnostics, diagnosticCode, document), | ||
| ]; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,49 +1,12 @@ | ||
| import * as vscode from 'vscode'; | ||
| import {DiagnosticCode} from '../../services/common'; | ||
| import {VscodeCommands} from '../../utils/commands'; | ||
| import {CommandParameters} from '../../cli-wrapper/constants'; | ||
| import {IgnoreCommandConfig} from '../../types/commands'; | ||
| import {createIgnorePathAction, createIgnoreRuleAction, createOpenViolationCardAction} from './commonActions'; | ||
| import {ScanType} from '../../constants'; | ||
|
|
||
| const createIgnoreValueAction = ( | ||
| diagnostics: vscode.Diagnostic[], range: vscode.Range | vscode.Selection, document: vscode.TextDocument | ||
| ): vscode.CodeAction => { | ||
| const value = document.getText(range); | ||
|
|
||
| const ignoreValueAction = new vscode.CodeAction( | ||
| `ignore value ${value}`, | ||
| vscode.CodeActionKind.QuickFix | ||
| ); | ||
| ignoreValueAction.command = { | ||
| command: VscodeCommands.IgnoreCommandId, | ||
| title: `Ignore value: ${value}`, | ||
| tooltip: 'This will always ignore this value', | ||
| arguments: [ | ||
| { | ||
| scanType: ScanType.Secrets, | ||
| ignoreBy: CommandParameters.ByValue, | ||
| param: value, | ||
| filePath: document.fileName, | ||
| } as IgnoreCommandConfig, | ||
| ], | ||
| }; | ||
| ignoreValueAction.diagnostics = diagnostics; | ||
| ignoreValueAction.isPreferred = true; | ||
|
|
||
| return ignoreValueAction; | ||
| }; | ||
| import {createOpenViolationCardAction} from './commonActions'; | ||
|
|
||
| export const createCommandCodeActions = ( | ||
| document: vscode.TextDocument, | ||
| range: vscode.Range | vscode.Selection, | ||
| diagnostics: vscode.Diagnostic[], | ||
| diagnosticCode: DiagnosticCode, | ||
| ): vscode.CodeAction[] => { | ||
| return [ | ||
| createOpenViolationCardAction(diagnostics, diagnosticCode), | ||
| createIgnoreValueAction(diagnostics, range, document), | ||
| createIgnoreRuleAction(diagnostics, diagnosticCode, document), | ||
| createIgnorePathAction(diagnostics, diagnosticCode, document), | ||
| ]; | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.