Skip to content

Commit

Permalink
Chore: rename folderconfig (#590)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawkyZ authored Jul 18, 2024
1 parent 6924e04 commit 408e307
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ within `initializationOptions?: LSPAny;` we support the following settings:
"/a/trusted/path",
"/another/trusted/path"
], // An array of folder that should be trusted
"folderConfig": [{
"folderConfigs": [{
"baseBranch": "main",
"folderPath": "a/b/c",
}], // an array of folder configurations, defining the desired base branch of a workspaceFolder
Expand Down
2 changes: 1 addition & 1 deletion application/server/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func updateSnykOpenBrowserCodeActions(c *config.Config, settings types.Settings)
}

func updateFolderConfig(settings types.Settings) {
gitconfig.SetBaseBranch(settings.FolderConfig)
gitconfig.SetBaseBranch(settings.FolderConfigs)
}

func updateAuthenticationMethod(c *config.Config, settings types.Settings) {
Expand Down
2 changes: 1 addition & 1 deletion application/server/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func Test_UpdateSettings(t *testing.T) {
AuthenticationMethod: types.OAuthAuthentication,
SnykCodeApi: sampleSettings.SnykCodeApi,
EnableSnykOpenBrowserActions: "true",
FolderConfig: []types.FolderConfig{
FolderConfigs: []types.FolderConfig{
{
FolderPath: tempDir1,
BaseBranch: "testBaseBranch1",
Expand Down
27 changes: 20 additions & 7 deletions domain/snyk/persistence/git_persistence_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ const (
)

var (
_ ScanSnapshotPersister = (*GitPersistenceProvider)(nil)
ErrPathHashDoesntExist = errors.New("hashed folder path doesn't exist in cache")
ErrProductDoesntExist = errors.New("product doesn't exist in cache")
ErrCommitDoesntExist = errors.New("commit doesn't exist in cache")
_ ScanSnapshotPersister = (*GitPersistenceProvider)(nil)
)

type hashedFolderPath string
Expand Down Expand Up @@ -247,7 +250,7 @@ func (g *GitPersistenceProvider) Exists(folderPath, commitHash string, p product

existingCommitHash, err := g.getCommitHashForProduct(folderPath, p)

if err != nil || existingCommitHash != commitHash {
if err != nil || existingCommitHash != commitHash || existingCommitHash == "" {
return false
}

Expand All @@ -257,7 +260,17 @@ func (g *GitPersistenceProvider) Exists(folderPath, commitHash string, p product
}

exists := g.snapshotExistsOnDisk(cacheDir, hash, commitHash, p)
return exists
if exists {
return true
}

g.logger.Debug().Msg("entry exists in cache but not on disk. Maybe file was deleted? " + folderPath)

err = g.deleteFromCache(hash, commitHash, p)
if err != nil {
g.logger.Error().Err(err).Msg("failed to remove file from cache: " + folderPath)
}
return false
}

func (g *GitPersistenceProvider) deleteFile(fullPath string) error {
Expand All @@ -272,16 +285,16 @@ func (g *GitPersistenceProvider) deleteFile(fullPath string) error {
func (g *GitPersistenceProvider) deleteFromCache(hash hashedFolderPath, commitHash string, p product.Product) error {
pchm, exists := g.cache[hash]
if !exists {
return errors.New("hashed folder path doesn't exist in cache")
return ErrPathHashDoesntExist
}

currentCommitHash, pchExists := pchm[p]
if !pchExists {
return errors.New("product doesn't exist in cache")
return ErrProductDoesntExist
}

if currentCommitHash != commitHash {
return errors.New("commit hashes don't match")
return ErrCommitDoesntExist
}

delete(g.cache[hash], p)
Expand All @@ -304,7 +317,7 @@ func (g *GitPersistenceProvider) getCommitHashForProduct(folderPath string, p pr
}
pchMap, ok := g.cache[hash]
if !ok {
return "", nil
return "", ErrPathHashDoesntExist
}
commitHash = pchMap[p]
return commitHash, nil
Expand Down
26 changes: 26 additions & 0 deletions domain/snyk/persistence/git_persistence_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,32 @@ func TestEnsureCacheDirExists_DefaultCase(t *testing.T) {
assert.Equal(t, expectedCacheDir, actualCacheDir)
}

func TestExists_ExistsInCacheButNotInFs(t *testing.T) {
c := testutil.UnitTest(t)
existingCodeIssues := []snyk.Issue{
{
GlobalIdentity: uuid.New().String(),
},
}

appFs := afero.NewMemMapFs()
folderPath := "/home/myusr/testrepo"
mgo := getMockedGitOpsWithRepoConfig(folderPath)
cacheDir := filepath.Join(filepath.Join(folderPath, ".git"), CacheFolder)
commitHash := "eab0f18c4432b2a41e0f8e6c9831fe84be92b3db"
pc := product.ProductCode
cut := NewGitPersistenceProvider(c.Logger(), appFs, mgo)

err := cut.Add(folderPath, commitHash, existingCodeIssues, pc)
assert.NoError(t, err)
err = appFs.RemoveAll(cacheDir)
assert.NoError(t, err)

exists := cut.Exists(folderPath, commitHash, pc)

assert.False(t, exists)
}

func getMockedGitOpsWithRepoConfig(folderPath string) *vcs.MockGitOps {
storer := filesystem.NewStorage(osfs.New(folderPath), cache.NewObjectLRUDefault())
repo := &git.Repository{}
Expand Down
4 changes: 2 additions & 2 deletions internal/types/lsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ type WorkspaceFoldersChangeEvent struct {

// FolderConfig is exchanged between IDE and LS
// IDE sends this as part of the settings/initialization
// LS sends this via the $/snyk.FolderConfig notification
// LS sends this via the $/snyk.folderConfig notification
type FolderConfig struct {
FolderPath string `json:"folderPath"`
BaseBranch string `json:"baseBranch"`
Expand Down Expand Up @@ -564,7 +564,7 @@ type Settings struct {
AdditionalParams string `json:"additionalParams,omitempty"` // TODO make folder specific, move to folder config
AdditionalEnv string `json:"additionalEnv,omitempty"` // TODO make folder specific, move to folder config
TrustedFolders []string `json:"trustedFolders,omitempty"` // TODO make folder specific, move to folder config
FolderConfig []FolderConfig `json:"folderConfig,omitempty"`
FolderConfigs []FolderConfig `json:"folderConfigs,omitempty"`
// folder specific settings end
}

Expand Down
2 changes: 1 addition & 1 deletion internal/vcs/git_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func Clone(repoPath string, destinationPath string, branchName string, logger *z
})

if err != nil {
logger.Error().Err(err).Msg("Failed to clone base in temp repo with go-git " + destinationPath)
logger.Error().Err(err).Msgf("Failed to clone base branch: %s in temp repo with go-git: %s", baseBranchName, destinationPath)
return nil, err
}

Expand Down

0 comments on commit 408e307

Please sign in to comment.