Skip to content

Commit

Permalink
fix: Snyk Learn link missing [HEAD-558] (#370)
Browse files Browse the repository at this point in the history
* fix: restore state directly in the script

* refactor: use actual types
  • Loading branch information
Asaf Agami authored Aug 15, 2023
1 parent b902bb4 commit 925b2e7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 23 deletions.
6 changes: 3 additions & 3 deletions src/snyk/common/views/webviewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ export abstract class WebviewProvider<ViewModel> implements IWebViewProvider<Vie
}
}

protected async checkVisibility(): Promise<void> {
protected checkVisibility(): void {
if (this.panel && this.panel.visible) {
try {
await this.panel.webview.postMessage({ type: 'get' });
await this.panel.webview.postMessage({ type: 'getLesson' });
void this.panel.webview.postMessage({ type: 'get' });
void this.panel.webview.postMessage({ type: 'getLesson' });
} catch (e) {
if (!this.panel) return; // can happen due to asynchronicity, ignore such cases
Logger.error(`Failed to restore the '${this.panel.title}' webview.`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ErrorHandler } from '../../../common/error/errorHandler';
import { CodeIssueData, ExampleCommitFix, Issue, Marker, Point } from '../../../common/languageServer/types';
import { ILog } from '../../../common/logger/interfaces';
import { messages as learnMessages } from '../../../common/messages/learn';
import { LearnService } from '../../../common/services/learnService';
import { getNonce } from '../../../common/views/nonce';
import { WebviewPanelSerializer } from '../../../common/views/webviewPanelSerializer';
import { WebviewProvider } from '../../../common/views/webviewProvider';
Expand All @@ -22,7 +23,6 @@ import { messages as errorMessages } from '../../messages/error';
import { getAbsoluteMarkerFilePath } from '../../utils/analysisUtils';
import { IssueUtils } from '../../utils/issueUtils';
import { ICodeSuggestionWebviewProvider } from '../interfaces';
import { LearnService } from '../../../common/services/learnService';

type Suggestion = {
id: string;
Expand Down Expand Up @@ -114,7 +114,7 @@ export class CodeSuggestionWebviewProvider

this.panel.webview.html = this.getHtmlForWebview(this.panel.webview);

await this.panel.webview.postMessage({ type: 'set', args: this.mapToModel(issue) });
void this.panel.webview.postMessage({ type: 'set', args: this.mapToModel(issue) });
void this.postLearnLessonMessage(issue);

this.issue = issue;
Expand Down
85 changes: 67 additions & 18 deletions src/snyk/snykCode/views/suggestion/codeSuggestionWebviewScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,45 @@
title: string;
};

let lesson: Lesson | null;
type ExampleCommitFix = {
commitURL: string;
lines: CommitChangeLine[];
};
type CommitChangeLine = {
line: string;
lineNumber: number;
lineChange: 'removed' | 'added' | 'none';
};
type Marker = {
msg: Point;
pos: MarkerPosition[];
};
type MarkerPosition = {
cols: Point;
rows: Point;
file: string;
};
type Point = [number, number];
type Suggestion = {
id: string;
message: string;
severity: string;
leadURL?: string;
rule: string;
repoDatasetSize: number;
exampleCommitFixes: ExampleCommitFix[];
cwe: string[];
title: string;
text: string;
isSecurityType: boolean;
uri: string;
markers?: Marker[];
cols: Point;
rows: Point;
};

const vscode = acquireVsCodeApi();

function navigateToUrl(url: string) {
sendMessage({
type: 'openBrowser',
Expand All @@ -27,30 +63,40 @@
}

let exampleCount = 0;
let suggestion = {} as any;

// Try to restore the previous state
let lesson: Lesson | null = vscode.getState()?.lesson || null;
fillLearnLink();
let suggestion: Suggestion | null = vscode.getState()?.suggestion || null;
showCurrentSuggestion();

function navigateToLeadURL() {
if (!suggestion.leadURL) return;
if (!suggestion?.leadURL) return;
navigateToUrl(suggestion.leadURL);
}
function navigateToIssue(_e: any, range: any) {
if (!suggestion) return;
sendMessage({
type: 'openLocal',
args: getSuggestionPosition(range),
args: getSuggestionPosition(suggestion, range),
});
}
function navigateToCurrentExample() {
if (!suggestion?.exampleCommitFixes) return;

const url = suggestion.exampleCommitFixes[exampleCount].commitURL;
sendMessage({
type: 'openBrowser',
args: { url },
});
}
function ignoreIssue(lineOnly: boolean) {
if (!suggestion) return;

sendMessage({
type: 'ignoreIssue',
args: {
...getSuggestionPosition(),
...getSuggestionPosition(suggestion),
message: suggestion.message,
rule: suggestion.rule,
id: suggestion.id,
Expand All @@ -67,12 +113,12 @@
},
});
}
function getSuggestionPosition(position?: { file: string; rows: any; cols: any }) {
function getSuggestionPosition(suggestionParam: Suggestion, position?: { file: string; rows: any; cols: any }) {
return {
uri: position?.file ?? suggestion.uri,
rows: position ? position.rows : suggestion.rows,
cols: position ? position.cols : suggestion.cols,
suggestionUri: suggestion.uri,
uri: position?.file ?? suggestionParam.uri,
rows: position ? position.rows : suggestionParam.rows,
cols: position ? position.cols : suggestionParam.cols,
suggestionUri: suggestionParam.uri,
};
}
function previousExample() {
Expand All @@ -88,8 +134,7 @@
}
function showCurrentExample() {
if (
!suggestion ||
!suggestion.exampleCommitFixes.length ||
!suggestion?.exampleCommitFixes?.length ||
exampleCount < 0 ||
exampleCount >= suggestion.exampleCommitFixes.length
)
Expand Down Expand Up @@ -141,6 +186,10 @@
}

function showCurrentSuggestion() {
if (!suggestion) {
return;
}

exampleCount = 0;
const currentSeverity = getCurrentSeverity();
const severity = document.getElementById('severity')!;
Expand Down Expand Up @@ -185,7 +234,7 @@
};
title.appendChild(mark);
const markMsg = document.createElement('span');
markMsg.innerHTML = suggestion.message.substring(m.msg[0], (m.msg[1] as number) + 1);
markMsg.innerHTML = suggestion.message.substring(m.msg[0], m.msg[1] + 1);
mark.appendChild(markMsg);
let markLineText = ' [';
let first = true;
Expand All @@ -199,7 +248,7 @@
markLine.innerHTML = markLineText;
markLine.className = 'mark-position';
mark.appendChild(markLine);
i = (m.msg[1] as number) + 1;
i = m.msg[1] + 1;
}
const postText = suggestion.message.substring(i);
const postMark = document.createTextNode(postText);
Expand All @@ -219,7 +268,7 @@
const dataset = document.getElementById('dataset-number')!;
const infoTop = document.getElementById('info-top')!;
if (suggestion.repoDatasetSize) {
dataset.innerHTML = suggestion.repoDatasetSize;
dataset.innerHTML = suggestion.repoDatasetSize.toString();
infoTop.className = 'font-light';
} else {
infoTop.className = 'font-light hidden';
Expand All @@ -228,13 +277,13 @@
const exampleTop = document.getElementById('example-top')!;
const example = document.getElementById('example')!;
const noExamples = document.getElementById('info-no-examples')!;
if (suggestion.exampleCommitFixes.length) {
if (suggestion?.exampleCommitFixes?.length) {
exampleTop.className = 'row between';
example.className = '';
const exNum = document.getElementById('example-number')!;
exNum.innerHTML = suggestion.exampleCommitFixes.length;
exNum.innerHTML = suggestion.exampleCommitFixes.length.toString();
const exNum2 = document.getElementById('example-number2')!;
exNum2.innerHTML = suggestion.exampleCommitFixes.length;
exNum2.innerHTML = suggestion.exampleCommitFixes.length.toString();
noExamples.className = 'hidden';
showCurrentExample();
} else {
Expand Down

0 comments on commit 925b2e7

Please sign in to comment.