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 org name to snyk code test [NEBULA-195] #2764

Merged
merged 1 commit into from
Feb 23, 2022

Conversation

patricia-v
Copy link
Contributor

@patricia-v patricia-v commented Feb 17, 2022

What does this PR do?

When running snyk code test, the organization would appear as undefined.
This was solved by passing the org from the SastSettings API endpoint (which is the default org when not specified by the flag or config) and displaying it in the CLI.

Notes for the reviewer

Check the PR on the registry side as well for the API changes.

How should this be manually tested?

Run snyk code test in a repository and check the printed Organization line. It should not be undefined.

Notes:

  • Undefined organizations will assume the default organization.
  • Specify an org by using the flag --org=org.name or by setting the org in the config by running snyk config set org=org.name
  • Check if an org is in the config by running snyk config get org
  • To remove an assigned org in the config, run snyk config unset org

More information

Screenshots

Note: The default org for this example was patricia.vale. The config had no org specified and no flags were used.

When running snyk code test...

Before:
image

After:
image

@github-actions
Copy link
Contributor

github-actions bot commented Feb 17, 2022

Warnings
⚠️

Since the CLI is unifying on a standard and improved tooling, we're starting to migrate old-style imports and exports to ES6 ones.
A file you've modified is using either module.exports or require(). If you can, please update them to ES6 import syntax and export syntax.
Files found:

  • test/jest/unit/snyk-code/snyk-code-test.spec.ts

Generated by 🚫 dangerJS against 33097ce

@patricia-v patricia-v marked this pull request as ready for review February 17, 2022 16:58
@patricia-v patricia-v requested review from a team, pkey and ArturSnyk February 17, 2022 16:58
@@ -45,6 +45,9 @@ export const codePlugin: EcosystemPlugin = {
}
const numOfIssues = sarifTypedResult!.runs?.[0].results?.length || 0;
analytics.add('sast-issues-found', numOfIssues);
if (!options.org && sastSettings.org) {
options.org = sastSettings.org;
Copy link
Contributor

Choose a reason for hiding this comment

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

I would not mutate the option.org object. this might cause some side effects that we don't expect. Instead, we can in every place we check for options.org use options.org || sastSettings.org

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 agree that changing option.org is not the best thing. However, to solve this problem we will need the new org in the meta const in the next line. I didn't want to change how getMeta is called, so what would be the best solution here? I would have to change the arguments of getMeta to either include the options.org || sastSettings.org (or a new const with this result), or just the sastSettings/sastSettings.org. Do you think this is a good solution, or do I need a new approach here?

Copy link
Contributor

Choose a reason for hiding this comment

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

@ArturSnyk I think this is not a bad approach. It's basically saying - if no org defined in options let that org be the default one. Also, getMeta is the only consumer of options and all it does is extract the relevant information.

Copy link
Contributor

Choose a reason for hiding this comment

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

@patricia-v have you looked at how other snyk commands assign default org? Like snyk test. As far as I know, this issue is handled there so maybe we can actually use that solution?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pkey I have, it is done in a very different way, even in the formatting of the output in the CLI. snyk test requests from a few APIs that snyk code test doesn't, and one of those returns the org. Somewhere down the line the response of this request is used as an argument to format the output, including the Organization.

In this case I think it would be similar to passing the sastSettings in the arguments of getMeta.

Copy link
Contributor

Choose a reason for hiding this comment

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

@patricia-v sounds good then.

@ArturSnyk
Copy link
Contributor

[suggestion] let's also add tests for this case?

Copy link
Contributor

@pkey pkey left a comment

Choose a reason for hiding this comment

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

Agree with @ArturSnyk's comment, let's have a test to make sure we prevent this issue in the future.

@@ -45,6 +45,9 @@ export const codePlugin: EcosystemPlugin = {
}
const numOfIssues = sarifTypedResult!.runs?.[0].results?.length || 0;
analytics.add('sast-issues-found', numOfIssues);
if (!options.org && sastSettings.org) {
options.org = sastSettings.org;
Copy link
Contributor

Choose a reason for hiding this comment

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

@ArturSnyk I think this is not a bad approach. It's basically saying - if no org defined in options let that org be the default one. Also, getMeta is the only consumer of options and all it does is extract the relevant information.

@@ -45,6 +45,9 @@ export const codePlugin: EcosystemPlugin = {
}
const numOfIssues = sarifTypedResult!.runs?.[0].results?.length || 0;
analytics.add('sast-issues-found', numOfIssues);
if (!options.org && sastSettings.org) {
options.org = sastSettings.org;
Copy link
Contributor

Choose a reason for hiding this comment

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

@patricia-v have you looked at how other snyk commands assign default org? Like snyk test. As far as I know, this issue is handled there so maybe we can actually use that solution?

@pkey
Copy link
Contributor

pkey commented Feb 21, 2022

@patricia-v let me know if you get stuck with the tests we can look together!

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