Skip to content

Commit

Permalink
fix: send folderConfigs after init (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawkyZ authored Jul 18, 2024
1 parent 408e307 commit 478a6d1
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 23 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,18 @@ Right now the language server supports the following actions:

### Custom additions to Language Server Protocol
- Folder Config Notification
- method: `$/snyk.folderConfig`
- method: `$/snyk.folderConfigs`
- payload:
```json5
{
"folderPath": "the/folder/path",
"baseBranch": "the-base-branch", // e.g. main
"localBranches": [ "branch1", "branch2" ]
"folderConfigs":
[
{
"folderPath": "the/folder/path",
"baseBranch": "the-base-branch", // e.g. main
"localBranches": [ "branch1", "branch2" ]
}
]
}
```
- Authentication Notification
Expand Down
4 changes: 2 additions & 2 deletions application/server/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ func registerNotifier(c *config.Config, srv types.Server) {
logger := c.Logger().With().Str("method", "registerNotifier").Logger()
callbackFunction := func(params any) {
switch params := params.(type) {
case types.FolderConfig:
notifier(c, srv, "$/snyk.folderConfig", params)
case types.FolderConfigsParam:
notifier(c, srv, "$/snyk.folderConfigs", params)
logger.Info().Any("folderConfig", params).Msg("sending folderConfig to client")
case types.AuthenticationParams:
notifier(c, srv, "$/snyk.hasAuthenticated", params)
Expand Down
4 changes: 2 additions & 2 deletions application/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func workspaceDidChangeWorkspaceFoldersHandler(srv *jrpc2.Server) jrpc2.Handler
logger.Info().Msg("RECEIVING")
defer logger.Info().Msg("SENDING")
workspace.Get().ChangeWorkspaceFolders(bgCtx, params)
command.HandleUntrustedFolders(bgCtx, srv)
command.HandleFolders(bgCtx, srv, di.Notifier())
return nil, nil
})
}
Expand Down Expand Up @@ -369,7 +369,7 @@ func initializedHandler(srv *jrpc2.Server) handler.Func {
}

logger.Debug().Msg("trying to get trusted status for untrusted folders")
go command.HandleUntrustedFolders(context.Background(), srv)
go command.HandleFolders(context.Background(), srv, di.Notifier())
return nil, nil
})
}
Expand Down
13 changes: 7 additions & 6 deletions application/server/server_smoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,14 +425,15 @@ func runSmokeTest(t *testing.T, repo string, commit string, file1 string, file2

waitForScan(t, cloneTargetDir)

notifications := jsonRPCRecorder.FindNotificationsByMethod("$/snyk.folderConfig")
notifications := jsonRPCRecorder.FindNotificationsByMethod("$/snyk.folderConfigs")
assert.Len(t, notifications, 1)
var folderConfig types.FolderConfig
err := notifications[0].UnmarshalParams(&folderConfig)
var folderConfigsParam types.FolderConfigsParam
err := notifications[0].UnmarshalParams(&folderConfigsParam)
assert.NoError(t, err)
assert.Equal(t, cloneTargetDir, folderConfig.FolderPath)
assert.NotEmpty(t, folderConfig.BaseBranch)
assert.NotEmpty(t, folderConfig.LocalBranches)
assert.Len(t, folderConfigsParam.FolderConfigs, 1)
assert.Equal(t, cloneTargetDir, folderConfigsParam.FolderConfigs[0].FolderPath)
assert.NotEmpty(t, folderConfigsParam.FolderConfigs[0].BaseBranch)
assert.NotEmpty(t, folderConfigsParam.FolderConfigs[0].LocalBranches)

jsonRPCRecorder.ClearNotifications()
var testPath string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package command
import (
"context"
"fmt"
gitconfig "github.com/snyk/snyk-ls/internal/git_config"
noti "github.com/snyk/snyk-ls/internal/notification"

"github.com/pkg/errors"

Expand All @@ -30,6 +32,27 @@ import (
const DoTrust = "Trust folders and continue"
const DontTrust = "Don't trust folders"

func HandleFolders(ctx context.Context, srv types.Server, notifier noti.Notifier) {
go sendFolderConfigsNotification(notifier)
HandleUntrustedFolders(ctx, srv)
}

func sendFolderConfigsNotification(notifier noti.Notifier) {
logger := config.CurrentConfig().Logger().With().Str("method", "HandleFolders").Logger()
ws := workspace.Get()
var folderConfigs []types.FolderConfig
for _, f := range ws.Folders() {
folderConfig, err := gitconfig.GetOrCreateFolderConfig(f.Path())
if err != nil {
logger.Warn().Err(err).Msg("error determining folder config")
continue
}
folderConfigs = append(folderConfigs, *folderConfig)
}
folderConfigsParam := types.FolderConfigsParam{FolderConfigs: folderConfigs}
notifier.Send(folderConfigsParam)
}

func HandleUntrustedFolders(ctx context.Context, srv types.Server) {
w := workspace.Get()
// debounce requests from overzealous clients (Eclipse, I'm looking at you)
Expand Down
9 changes: 0 additions & 9 deletions domain/ide/workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/snyk/snyk-ls/application/config"
"github.com/snyk/snyk-ls/domain/ide/hover"
"github.com/snyk/snyk-ls/domain/snyk"
gitconfig "github.com/snyk/snyk-ls/internal/git_config"
noti "github.com/snyk/snyk-ls/internal/notification"
"github.com/snyk/snyk-ls/internal/observability/performance"
"github.com/snyk/snyk-ls/internal/product"
Expand Down Expand Up @@ -127,14 +126,6 @@ func (w *Workspace) AddFolder(f *Folder) {
w.folders = map[string]*Folder{}
}
w.folders[f.Path()] = f
// get & send folder config to client
folderConfig, err := gitconfig.GetOrCreateFolderConfig(f.path)
if err != nil {
w.c.Logger().Warn().Err(err).Msg("error determining folder config")
return
}

w.notifier.Send(*folderConfig)
}

func (w *Workspace) IssuesForFile(path string) []snyk.Issue {
Expand Down
4 changes: 4 additions & 0 deletions internal/types/lsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,10 @@ type FolderConfig struct {
LocalBranches []string `json:"localBranches,omitempty"`
}

type FolderConfigsParam struct {
FolderConfigs []FolderConfig `json:"folderConfigs"`
}

// Settings is the struct that is parsed from the InitializationParams.InitializationOptions field
type Settings struct {
// global settings start
Expand Down

0 comments on commit 478a6d1

Please sign in to comment.