-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: adapt vuln count code action provider
- Loading branch information
1 parent
b97b472
commit cc73834
Showing
2 changed files
with
94 additions
and
13 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
src/snyk/snykOss/codeActions/vulnerabilityCodeActionProviderLS.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { IAnalytics } from '../../common/analytics/itly'; | ||
import { OpenCommandIssueType, OpenIssueCommandArg } from '../../common/commands/types'; | ||
import { SNYK_OPEN_ISSUE_COMMAND } from '../../common/constants/commands'; | ||
import { IDE_NAME } from '../../common/constants/general'; | ||
import { ICodeActionKindAdapter } from '../../common/vscode/codeAction'; | ||
import { | ||
CodeAction, | ||
CodeActionContext, | ||
CodeActionKind, | ||
CodeActionProvider, | ||
Command, | ||
ProviderResult, | ||
Range, | ||
Selection, | ||
TextDocument, | ||
} from '../../common/vscode/types'; | ||
import { DIAGNOSTICS_OSS_COLLECTION_NAME } from '../../snykCode/constants/analysis'; | ||
import { messages } from '../messages/vulnerabilityCount'; | ||
import { isResultCliError } from '../ossResult'; | ||
import { OssService } from '../services/ossService'; | ||
import { ModuleVulnerabilityCountProviderLS } from '../services/vulnerabilityCount/vulnerabilityCountProviderLS'; | ||
|
||
export class VulnerabilityCodeActionProviderLS implements CodeActionProvider { | ||
public codeActionKinds: ReadonlyArray<CodeActionKind> = [this.codeActionKindProvider.getQuickFix()]; | ||
|
||
constructor( | ||
private readonly vulnerabilityCountProvider: ModuleVulnerabilityCountProviderLS, | ||
private readonly codeActionKindProvider: ICodeActionKindAdapter, | ||
private readonly analytics: IAnalytics, | ||
) {} | ||
|
||
async provideCodeActions( | ||
document: TextDocument, | ||
_: Range | Selection, | ||
context: CodeActionContext, | ||
): Promise<ProviderResult<(CodeAction | Command)[]>> { | ||
const ossDiagnostics = context.diagnostics.filter(d => d.source === DIAGNOSTICS_OSS_COLLECTION_NAME); | ||
if (!ossDiagnostics.length) { | ||
return; | ||
} | ||
|
||
const ossResult = this.vulnerabilityCountProvider.getResultArray(); | ||
if (!ossResult) { | ||
return; | ||
} | ||
|
||
const fileResult = ossResult.find( | ||
res => !isResultCliError(res) && this.vulnerabilityCountProvider.isFilePartOfOssTest(document.fileName, res), | ||
); | ||
|
||
if (!fileResult || isResultCliError(fileResult)) { | ||
return; | ||
} | ||
|
||
for (const diagnostic of ossDiagnostics) { | ||
const vulnerability = fileResult.vulnerabilities.find(vuln => vuln.id === diagnostic.code); | ||
if (!vulnerability) { | ||
continue; | ||
} | ||
|
||
const command: Command = { | ||
command: SNYK_OPEN_ISSUE_COMMAND, | ||
title: messages.showMostSevereVulnerability, | ||
arguments: [ | ||
{ | ||
issueType: OpenCommandIssueType.OssVulnerability, | ||
issue: await OssService.getOssIssueCommandArg(vulnerability, fileResult.vulnerabilities), | ||
} as OpenIssueCommandArg, | ||
], | ||
}; | ||
|
||
this.analytics.logQuickFixIsDisplayed({ | ||
quickFixType: ['Show Most Severe Vulnerability'], | ||
ide: IDE_NAME, | ||
}); | ||
|
||
return [command]; | ||
} | ||
} | ||
} |
This file contains 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