Skip to content

Commit

Permalink
fix: remove open browser action by default, make it configurable (#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
bastiandoetsch authored Jul 18, 2024
1 parent f3a964d commit 6924e04
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 179 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ within `initializationOptions?: LSPAny;` we support the following settings:
"snykCodeApi": "https://deeproxy.snyk.io", // Specifies the Snyk Code API endpoint to use. Default is https://deeproxy.snyk.io
"enableSnykLearnCodeActions": "true", // show snyk learns code actions
"enableSnykOSSQuickFixCodeActions": "true", // show quickfixes for supported OSS package manager issues
"enableSnykOpenBrowserActions": "false", // show code actions to open issue descriptions
"enableDeltaFindings": "false", // only display issues that are not new and thus not on the base branch
"requiredProtocolVersion": "11",
"additionalParams": "--all-projects", // Any extra params for Open Source scans using the Snyk CLI, separated by spaces
Expand Down
2 changes: 2 additions & 0 deletions application/codeaction/codeaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ func (c *CodeActionsService) GetCodeActions(params types.CodeActionParams) []typ
func (c *CodeActionsService) updateIssuesWithQuickFix(quickFixGroupables []types.Groupable, issues []snyk.Issue) []snyk.Issue {
// we only allow one quickfix, so it needs to be grouped
quickFix := c.getQuickFixAction(quickFixGroupables)
// update title with number of issues
quickFix.Title = fmt.Sprintf("%s and fix %d issues", quickFix.Title, len(quickFixGroupables))

var updatedIssues []snyk.Issue
for _, issue := range issues {
Expand Down
11 changes: 10 additions & 1 deletion application/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ type Config struct {
storage storage.StorageWithCallbacks
m sync.Mutex
clientProtocolVersion string
isOpenBrowserActionEnabled bool
}

func CurrentConfig() *Config {
Expand Down Expand Up @@ -849,7 +850,7 @@ func (c *Config) SetSnykLearnCodeActionsEnabled(enabled bool) {
c.enableSnykLearnCodeActions = enabled
}

func (c *Config) IsSnyOSSQuickFixCodeActionsEnabled() bool {
func (c *Config) IsSnykOSSQuickFixCodeActionsEnabled() bool {
return c.enableSnykOSSQuickFixCodeActions
}

Expand Down Expand Up @@ -942,3 +943,11 @@ func (c *Config) AuthenticationMethod() types.AuthenticationMethod {
func (c *Config) SetAuthenticationMethod(authMethod types.AuthenticationMethod) {
c.authenticationMethod = authMethod
}

func (c *Config) IsSnykOpenBrowserActionEnabled() bool {
return c.isOpenBrowserActionEnabled
}

func (c *Config) SetSnykOpenBrowserActionsEnabled(enable bool) {
c.isOpenBrowserActionEnabled = enable
}
10 changes: 10 additions & 0 deletions application/server/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,20 @@ func writeSettings(c *config.Config, settings types.Settings, initialize bool) {
updateAutoScan(c, settings)
updateSnykLearnCodeActions(c, settings)
updateSnykOSSQuickFixCodeActions(c, settings)
updateSnykOpenBrowserCodeActions(c, settings)
updateDeltaFindings(c, settings)
updateFolderConfig(settings)
}

func updateSnykOpenBrowserCodeActions(c *config.Config, settings types.Settings) {
enable := false
if settings.EnableSnykOpenBrowserActions == "true" {
enable = true
}

c.SetSnykOpenBrowserActionsEnabled(enable)
}

func updateFolderConfig(settings types.Settings) {
gitconfig.SetBaseBranch(settings.FolderConfig)
}
Expand Down
46 changes: 24 additions & 22 deletions application/server/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,28 +176,29 @@ func Test_UpdateSettings(t *testing.T) {
tempDir1 := filepath.Join(t.TempDir(), "tempDir1")
tempDir2 := filepath.Join(t.TempDir(), "tempDir2")
settings := types.Settings{
ActivateSnykOpenSource: "false",
ActivateSnykCode: "false",
ActivateSnykIac: "false",
Insecure: "true",
Endpoint: "https://api.snyk.io",
AdditionalParams: "--all-projects -d",
AdditionalEnv: "a=b;c=d",
Path: "addPath",
SendErrorReports: "true",
Organization: expectedOrgId,
ManageBinariesAutomatically: "false",
CliPath: "C:\\Users\\CliPath\\snyk-ls.exe",
Token: "a fancy token",
FilterSeverity: types.DefaultSeverityFilter(),
TrustedFolders: []string{"trustedPath1", "trustedPath2"},
OsPlatform: "windows",
OsArch: "amd64",
RuntimeName: "java",
RuntimeVersion: "1.8.0_275",
ScanningMode: "manual",
AuthenticationMethod: types.OAuthAuthentication,
SnykCodeApi: sampleSettings.SnykCodeApi,
ActivateSnykOpenSource: "false",
ActivateSnykCode: "false",
ActivateSnykIac: "false",
Insecure: "true",
Endpoint: "https://api.snyk.io",
AdditionalParams: "--all-projects -d",
AdditionalEnv: "a=b;c=d",
Path: "addPath",
SendErrorReports: "true",
Organization: expectedOrgId,
ManageBinariesAutomatically: "false",
CliPath: "C:\\Users\\CliPath\\snyk-ls.exe",
Token: "a fancy token",
FilterSeverity: types.DefaultSeverityFilter(),
TrustedFolders: []string{"trustedPath1", "trustedPath2"},
OsPlatform: "windows",
OsArch: "amd64",
RuntimeName: "java",
RuntimeVersion: "1.8.0_275",
ScanningMode: "manual",
AuthenticationMethod: types.OAuthAuthentication,
SnykCodeApi: sampleSettings.SnykCodeApi,
EnableSnykOpenBrowserActions: "true",
FolderConfig: []types.FolderConfig{
{
FolderPath: tempDir1,
Expand Down Expand Up @@ -234,6 +235,7 @@ func Test_UpdateSettings(t *testing.T) {
assert.Equal(t, settings.RuntimeVersion, c.RuntimeVersion())
assert.False(t, c.IsAutoScanEnabled())
assert.Equal(t, sampleSettings.SnykCodeApi, c.SnykCodeApi())
assert.Equal(t, true, c.IsSnykOpenBrowserActionEnabled())

err := initTestRepo(t, tempDir1)
assert.NoError(t, err)
Expand Down
31 changes: 22 additions & 9 deletions domain/ide/codelens/codelens.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package codelens

import (
"fmt"

sglsp "github.com/sourcegraph/go-lsp"

"github.com/snyk/snyk-ls/domain/ide/converter"
Expand All @@ -25,6 +27,11 @@ import (
"github.com/snyk/snyk-ls/internal/types"
)

type lensesWithIssueCount struct {
lensCommands []types.CommandData
issueCount int
}

func GetFor(filePath string) (lenses []sglsp.CodeLens) {
f := workspace.Get().GetFolderContaining(filePath)
if f == nil {
Expand All @@ -34,31 +41,36 @@ func GetFor(filePath string) (lenses []sglsp.CodeLens) {
issues := f.IssuesForFile(filePath)

// group by range first
lensesByRange := make(map[snyk.Range][]types.CommandData)
lensesByRange := make(map[snyk.Range]*lensesWithIssueCount)
for _, issue := range issues {
for _, lens := range issue.CodelensCommands {
commands := lensesByRange[issue.Range]
if commands == nil {
commands = []types.CommandData{}
lensesWithIssueCountsForRange := lensesByRange[issue.Range]
if lensesWithIssueCountsForRange == nil {
lensesWithIssueCountsForRange = &lensesWithIssueCount{
lensCommands: []types.CommandData{},
issueCount: 0,
}
}
commands = append(commands, lens)
lensesByRange[issue.Range] = commands
lensesWithIssueCountsForRange.lensCommands = append(lensesWithIssueCountsForRange.lensCommands, lens)
lensesWithIssueCountsForRange.issueCount++
lensesByRange[issue.Range] = lensesWithIssueCountsForRange
}
}

for r, commands := range lensesByRange {
lensCommands := getLensCommands(commands)
for _, command := range lensCommands {
lenses = append(lenses, getCodeLensFromCommand(r, command))
lens := getCodeLensFromCommand(r, command)
lenses = append(lenses, lens)
}
}

return lenses
}

func getLensCommands(inputCommands []types.CommandData) []types.CommandData {
func getLensCommands(lensesWithIssueCount *lensesWithIssueCount) []types.CommandData {
groupableByType := map[types.GroupingType][]types.Groupable{}
for _, groupable := range inputCommands {
for _, groupable := range lensesWithIssueCount.lensCommands {
commands := groupableByType[groupable.GetGroupingType()]
if commands == nil {
commands = []types.Groupable{}
Expand All @@ -73,6 +85,7 @@ func getLensCommands(inputCommands []types.CommandData) []types.CommandData {
// right now we can always group by max semver version, as
// code only has one quickfix available, and iac none at all
qf, ok := types.MaxSemver()(lensCommands).(types.CommandData)
qf.Title = fmt.Sprintf("%s and fix %d issues", qf.Title, lensesWithIssueCount.issueCount)
if ok {
lenses = append(lenses, qf)
}
Expand Down
31 changes: 19 additions & 12 deletions infrastructure/oss/code_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,32 @@ import (
)

func (i *ossIssue) AddCodeActions(learnService learn.Service, ep error_reporting.ErrorReporter, affectedFilePath string, issueRange snyk.Range) (actions []snyk.CodeAction) {
c := config.CurrentConfig()
if reflect.DeepEqual(issueRange, snyk.Range{}) {
config.CurrentConfig().Logger().Debug().Str("issue", i.Id).Msg("skipping adding code action, as issueRange is empty")
return
c.Logger().Debug().Str("issue", i.Id).Msg("skipping adding code action, as issueRange is empty")
return actions
}

quickFixAction := i.AddQuickFixAction(affectedFilePath, issueRange)
if quickFixAction != nil {
actions = append(actions, *quickFixAction)
}

title := fmt.Sprintf("Open description of '%s affecting package %s' in browser (Snyk)", i.Title, i.PackageName)
command := &types.CommandData{
Title: title,
CommandId: types.OpenBrowserCommand,
Arguments: []any{i.CreateIssueURL().String()},
}
if c.IsSnykOpenBrowserActionEnabled() {
title := fmt.Sprintf("Open description of '%s affecting package %s' in browser (Snyk)", i.Title, i.PackageName)
command := &types.CommandData{
Title: title,
CommandId: types.OpenBrowserCommand,
Arguments: []any{i.CreateIssueURL().String()},
}

action, _ := snyk.NewCodeAction(title, nil, command)
actions = append(actions, action)
action, err := snyk.NewCodeAction(title, nil, command)
if err != nil {
c.Logger().Err(err).Msgf("could not create code action %s", title)
} else {
actions = append(actions, action)
}
}

codeAction := i.AddSnykLearnAction(learnService, ep)
if codeAction != nil {
Expand Down Expand Up @@ -90,15 +97,15 @@ func (i *ossIssue) AddSnykLearnAction(learnService learn.Service, ep error_repor

func (i *ossIssue) AddQuickFixAction(affectedFilePath string, issueRange snyk.Range) *snyk.CodeAction {
logger := config.CurrentConfig().Logger().With().Str("method", "oss.AddQuickFixAction").Logger()
if !config.CurrentConfig().IsSnyOSSQuickFixCodeActionsEnabled() {
if !config.CurrentConfig().IsSnykOSSQuickFixCodeActionsEnabled() {
return nil
}
logger.Debug().Msg("create deferred quickfix code action")
quickfixEdit := i.getQuickfixEdit(affectedFilePath)
if quickfixEdit == "" {
return nil
}
upgradeMessage := "Upgrade to " + quickfixEdit + " (Snyk)"
upgradeMessage := "⚡️ Upgrade to " + quickfixEdit
autofixEditCallback := func() *snyk.WorkspaceEdit {
edit := &snyk.WorkspaceEdit{}
singleTextEdit := snyk.TextEdit{
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/oss/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func toIssue(
for _, codeAction := range codeActions {
if strings.Contains(codeAction.Title, "Upgrade to") {
codelensCommands = append(codelensCommands, types.CommandData{
Title: "⚡ Fix this issue: " + codeAction.Title,
Title: codeAction.Title,
CommandId: types.CodeFixCommand,
Arguments: []any{
codeAction.Uuid,
Expand Down
Loading

0 comments on commit 6924e04

Please sign in to comment.