-
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.
OWL-884 feat: add learn jit to webpanel (#187)
- Loading branch information
1 parent
3f11305
commit 6daa113
Showing
20 changed files
with
498 additions
and
22 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
* @snyk/road-runner | ||
src/snyk/common/services/learnService.ts @snyk/owl | ||
src/test/unit/common/services/learnService.test.ts @snyk/owl |
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,23 @@ | ||
.learn { | ||
opacity: 0; | ||
height: 0; | ||
margin-top: 0; | ||
|
||
&.show { | ||
margin-top: 6px; | ||
opacity: 1; | ||
height: auto; | ||
transition-duration: 500ms; | ||
transition-property: height, opacity, margin-top; | ||
} | ||
|
||
&--link { | ||
margin-left: 3px; | ||
} | ||
|
||
&__code { | ||
.learn--link { | ||
color: var(--vscode-foreground); | ||
} | ||
} | ||
} |
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
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
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
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,4 @@ | ||
export const messages = { | ||
getLessonError: 'Failed to get Snyk Learn lesson', | ||
lessonButtonTitle: 'Learn about this vulnerability', | ||
}; |
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,125 @@ | ||
import axios from 'axios'; | ||
import { isCodeIssue, isOssIssue, OpenCommandIssueType } from '../../common/commands/types'; | ||
import { SNYK_LEARN_API_CACHE_DURATION_IN_MS } from '../../common/constants/general'; | ||
import type { completeFileSuggestionType } from '../../snykCode/interfaces'; | ||
import { OssIssueCommandArg } from '../../snykOss/views/ossVulnerabilityTreeProvider'; | ||
import { IConfiguration } from '../configuration/configuration'; | ||
import { ErrorHandler } from '../error/errorHandler'; | ||
import { ILog } from '../logger/interfaces'; | ||
|
||
export type Lesson = { | ||
title: string; | ||
lessonId: string; | ||
ecosystem: string; | ||
url: string; | ||
}; | ||
|
||
interface LessonLookupParams { | ||
rule: string; | ||
ecosystem: string; | ||
cwes?: string[]; | ||
cves?: string[]; | ||
} | ||
|
||
export class LearnService { | ||
private lessonsCache = new Map< | ||
string, | ||
{ | ||
lessons: Lesson[]; | ||
expiry: number; | ||
} | ||
>(); | ||
|
||
constructor( | ||
private readonly configuration: IConfiguration, | ||
private readonly logger: ILog, | ||
private readonly shouldCacheRequests = true, | ||
) {} | ||
|
||
static getCodeIssueParams(issue: completeFileSuggestionType): LessonLookupParams { | ||
const idParts = issue.id.split(/\/|%2F/g); | ||
|
||
return { | ||
rule: idParts[idParts.length - 1], | ||
ecosystem: idParts[0], | ||
cwes: issue.cwe, | ||
}; | ||
} | ||
|
||
static getOSSIssueParams(issue: OssIssueCommandArg): LessonLookupParams { | ||
return { | ||
rule: issue.id, | ||
ecosystem: issue.packageManager, | ||
cwes: issue.identifiers?.CWE, | ||
cves: issue.identifiers?.CVE, | ||
}; | ||
} | ||
|
||
async requestLessons(params: LessonLookupParams) { | ||
const cacheResult = this.lessonsCache.get(params.rule); | ||
|
||
if (this.shouldCacheRequests && cacheResult && cacheResult?.expiry > Date.now()) { | ||
return cacheResult.lessons; | ||
} else { | ||
const res = await axios.get<{ lessons: Lesson[] }>('/lessons/lookup-for-cta', { | ||
baseURL: this.snykLearnEndpoint, | ||
params: { | ||
source: 'ide', | ||
rule: params.rule, | ||
ecosystem: params.ecosystem, | ||
cwe: params.cwes?.[0], | ||
cve: params.cves?.[0], | ||
}, | ||
}); | ||
|
||
const lessons = res.data.lessons; | ||
|
||
this.lessonsCache.set(params.rule, { | ||
lessons, | ||
expiry: Date.now() + SNYK_LEARN_API_CACHE_DURATION_IN_MS, | ||
}); | ||
|
||
return lessons; | ||
} | ||
} | ||
|
||
async getLesson( | ||
issue: OssIssueCommandArg | completeFileSuggestionType, | ||
issueType: OpenCommandIssueType, | ||
): Promise<Lesson | null> { | ||
try { | ||
let params: LessonLookupParams | null = null; | ||
|
||
if (isCodeIssue(issue, issueType)) { | ||
if (!issue.isSecurityType) return null; | ||
|
||
params = LearnService.getCodeIssueParams(issue); | ||
} else if (isOssIssue(issue, issueType)) { | ||
// Snyk Learn does not currently deal with licensing issues. | ||
if (issue.license) return null; | ||
|
||
params = LearnService.getOSSIssueParams(issue); | ||
} else { | ||
ErrorHandler.handle(new Error(`Issue type "${issueType}" not supported`), this.logger); | ||
return null; | ||
} | ||
|
||
console.log(params); | ||
|
||
if (!params) { | ||
return null; | ||
} | ||
|
||
const lessons = await this.requestLessons(params); | ||
|
||
return lessons.length > 0 ? lessons[0] : null; | ||
} catch (err) { | ||
ErrorHandler.handle(err, this.logger, 'Error getting Snyk Learn Lesson'); | ||
return null; | ||
} | ||
} | ||
|
||
get snykLearnEndpoint(): string { | ||
return `${this.configuration.baseApiUrl}/v1/learn`; | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.