Skip to content
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

feat: add advisor preview #127

Merged
merged 1 commit into from
Apr 6, 2022
Merged

feat: add advisor preview #127

merged 1 commit into from
Apr 6, 2022

Conversation

sangress
Copy link
Contributor

@sangress sangress commented Jan 5, 2022

Add advisor score to vscode extension

Score will show for packages with vulnerabilities, as shown in the following image:

image

To see this feature in action, use a package.json file with a package that known to have a score lower then 70, then hover with your mouse over the Advisor Score text with the mouse to see the advisor score details.
Example dependencies:

    "axios": "^0.20.0",
    "express": "^4.17.1",
    "firebase": "^5.8.2",
    "lodash": "^4.17.11",
    "numeral": "^2.0.6", # lower then 70
    "vue": "^2.5.21",
    "vue-router": "^3.0.6",
    "chino": "0.0.0", # lower then 70
    "vuejs": "3.0.1" # lower then 70

Screenshot from 2022-03-16 15-08-17

@sangress sangress requested a review from michelkaporin January 5, 2022 08:51
@sangress sangress requested a review from a team as a code owner January 5, 2022 08:51
@CLAassistant
Copy link

CLAassistant commented Jan 5, 2022

CLA assistant check
All committers have signed the CLA.

@sangress sangress marked this pull request as draft January 27, 2022 08:56
@sangress sangress marked this pull request as ready for review February 2, 2022 15:06
@@ -0,0 +1,13 @@
export type AdvisorScoreLabel = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Names of the files should follow camel casing - see other files.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean files, not types :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh, okay.

@@ -0,0 +1,75 @@
import * as vscode from 'vscode';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should rely on vscode abstractions instead, for testing purposes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please eleborate.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you write unit tests, "vscode" module cannot be resolved since tests run not within VS Code. To overcome this, we invert our dependency on vscode module through dependency inversion mechanisms. You can see inversions in src/snyk/common/vscode folder.

@@ -25,3 +25,28 @@ export type DecorationOptions = vscode.DecorationOptions;
export type ThemeColor = vscode.ThemeColor;
export type ThemableDecorationInstanceRenderOptions = vscode.ThemableDecorationInstanceRenderOptions;
export type CodeActionProviderMetadata = vscode.CodeActionProviderMetadata;
export enum Language {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file contains mappings between local types and vscode API ones.
The following types are unrelated to vscode, thus shouldn't be part of /vscode/types.ts.

hoverMessageMarkdown.appendMarkdown(`| ${label}: | | | ${score?.labels[label]} |`);
hoverMessageMarkdown.appendMarkdown('\n');
});
hoverMessageMarkdown.appendMarkdown(`[More Details](http://snyk.io/advisor/npm-package/${score.name})`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make use of existing configuration variables instead of hardcoding the link here? E.g. defaultAuthHost from configuration.ts

if (supportedLanguage) {
const modules = getModules(fileName, document.getText(), supportedLanguage).filter(isValidModuleName);
const promises = modules
.map(module => this.vulnerabilityCountProvider.getVulnerabilityCount(module, supportedLanguage))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need this map? Vulnerability Count doesn't relate to advisor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we only show advisor score for packages that has vulnerabilities.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it the case in IntelliJ Advisor integration as well? I didn't see the feature plan and tbh this wouldn't be my expectation 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the requirements that I got from Oren - @snyk/advisor

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please verify if we're aligned on that requirement with IntelliJ plugin.

}

const result = new CliError(err, '');
console.error('Failed to get scores', result.error.toString());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Errors should be reported via Logger class. See logger injection in other classes.

post<T = unknown, R = AxiosResponse<T>>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<R> {
this.http.interceptors.request.use(req => {
if (req.method === 'post') {
req.baseURL = this.configuration.baseApiUrl;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class should work with the same base Url for API. If there's a need to work with different API url, you either create another class or introduce type parameter here indicating what type of Snyk Api we want to POST to.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want to create a new class just for a single method? I think it's a bit overkill and duplication of classes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is important to follow single responsibility principle. Use another suggested option then, if you don't like another class. But first determine what's the difference between these two APIs and only then make a decision please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand and I agree on that, it looks like a duplication now that I look at it. But I think this is not the case because apiClient is common and generic.
Another solution can be that we'll use a helper function/class that will return the right url according to the request method (in this case). What do you think about it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you can keep single responsibility principle with a new function/class, then sure :) Sometimes it's better to duplicate rather than making a thing less comprehensible when glancing at the code.

@@ -0,0 +1,44 @@
export default [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't duplicate the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved it to common, how is that a duplication?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

snyk/vscode-extension/src/snyk/snykOss/constants/nativeModules.ts

src/snyk/common/vscode/languageConsts.ts Outdated Show resolved Hide resolved
@michelkaporin
Copy link
Contributor

Issues found from quick glance:

  1. When opening and closing tab, advisor score is not getting reported inline. It seems to show the score only if the extension is started when manifest file is opened.
  2. Score is prefixed with pipe when no vuln count is shown.

Screenshot 2022-02-03 at 16 19 35

3. Hover doesn't show where does the Advisor info come from. User can be misled and not know that it's actually Snyk. Please run this by UX.

Screenshot 2022-02-03 at 16 20 44

package.json Show resolved Hide resolved
src/snyk/advisor/editor/editorDecorator.ts Outdated Show resolved Hide resolved
src/snyk/common/vscode/editorDecorator.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@michelkaporin michelkaporin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's quite many comments I've left but it's much better after the last iteration 👍. Let's have a call when you will go through them. There are few bits that I still want to discuss.

Something you shouldn't also forget is testing the implemented bits 😉

src/snyk/advisor/services/AdvisorDisposable.ts Outdated Show resolved Hide resolved
src/snyk/advisor/services/AdvisorApiClient.ts Outdated Show resolved Hide resolved
src/snyk/advisor/services/AdvisorDisposable.ts Outdated Show resolved Hide resolved
src/snyk/advisor/services/AdvisorDisposable.ts Outdated Show resolved Hide resolved
src/snyk/advisor/services/AdvisorDisposable.ts Outdated Show resolved Hide resolved
src/snyk/common/vscode/parsing.ts Outdated Show resolved Hide resolved
src/snyk/common/vscode/parsing.ts Outdated Show resolved Hide resolved
src/snyk/common/vscode/parsing.ts Outdated Show resolved Hide resolved
src/snyk/common/vscode/types.ts Outdated Show resolved Hide resolved
src/snyk/common/vscode/languageConsts.ts Outdated Show resolved Hide resolved
src/snyk/advisor/messages.ts Outdated Show resolved Hide resolved
src/snyk/advisor/services/AdvisorApiClient.ts Outdated Show resolved Hide resolved
src/snyk/base/modules/snykLib.ts Outdated Show resolved Hide resolved
src/snyk/common/editor/types.ts Outdated Show resolved Hide resolved
src/snyk/advisor/services/advisorApiClient.ts Outdated Show resolved Hide resolved
src/snyk/common/vscode/parsing.ts Outdated Show resolved Hide resolved
src/snyk/common/vscode/parsing.ts Outdated Show resolved Hide resolved
src/snyk/common/vscode/parsing.ts Outdated Show resolved Hide resolved
src/snyk/common/vscode/parsing.ts Outdated Show resolved Hide resolved
src/snyk/advisor/editor/editorDecorator.ts Outdated Show resolved Hide resolved
src/snyk/advisor/services/advisorService.ts Outdated Show resolved Hide resolved
src/snyk/advisor/services/advisorService.ts Show resolved Hide resolved
src/snyk/advisor/services/advisorService.ts Outdated Show resolved Hide resolved
@@ -141,7 +142,8 @@ export default class SnykLib extends BaseSnykModule implements ISnykLib {

try {
const oldResult = this.ossService.getResult();
const result = await this.ossService.test(manual, reportTriggeredEvent);
const result: OssResult = await this.ossService.test(manual, reportTriggeredEvent);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you revert file to the original? There's no need to write type for the variable, let's have changes clean for git blame to work fine when needed.

src/snyk/common/messages/advisorMessages.ts Outdated Show resolved Hide resolved
src/snyk/common/vscode/parsing.ts Outdated Show resolved Hide resolved
@michelkaporin
Copy link
Contributor

As discussed offline, I'll be taking this PR over and merging it afterwards.

@michelkaporin michelkaporin changed the title Feat/advisor score feat: add advisor preview Apr 6, 2022
michelkaporin
michelkaporin previously approved these changes Apr 6, 2022
Copy link
Contributor

@michelkaporin michelkaporin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bless me for self approving it.

@michelkaporin michelkaporin merged commit 213c781 into main Apr 6, 2022
@michelkaporin michelkaporin deleted the feat/advisor_score branch April 6, 2022 14:53
@michelkaporin
Copy link
Contributor

Thank you for your work on it @sangress 👏

@sangress
Copy link
Contributor Author

sangress commented Jun 8, 2022

Thank you for your work on it @sangress 👏

Thanks @michelkaporin for mentioning.
It's good to see it live; it was my longest PR ever, but I did enjoy working on it.

Good luck with future features :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants