Skip to content

Commit

Permalink
refactor: move the deepcode package
Browse files Browse the repository at this point in the history
  • Loading branch information
teodora-sandu committed Apr 8, 2024
1 parent 2e5a867 commit d782ad9
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 60 deletions.
3 changes: 2 additions & 1 deletion internal/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ package bundle

import (
"context"

"github.com/rs/zerolog"

"github.com/snyk/code-client-go/deepcode"
"github.com/snyk/code-client-go/internal/deepcode"
"github.com/snyk/code-client-go/observability"
)

Expand Down
20 changes: 10 additions & 10 deletions internal/bundle/bundle_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ package bundle

import (
"context"
deepcode2 "github.com/snyk/code-client-go/internal/deepcode"
"os"
"path/filepath"

"github.com/puzpuzpuz/xsync"
"github.com/rs/zerolog"

"github.com/snyk/code-client-go/deepcode"
"github.com/snyk/code-client-go/internal/util"
"github.com/snyk/code-client-go/observability"
)

// TODO: add progress tracker for percentage progress
type bundleManager struct {
SnykCode deepcode.SnykCodeClient
SnykCode deepcode2.SnykCodeClient
instrumentor observability.Instrumentor
errorReporter observability.ErrorReporter
logger *zerolog.Logger
Expand All @@ -52,13 +52,13 @@ type BundleManager interface {
ctx context.Context,
requestId string,
originalBundle Bundle,
files map[string]deepcode.BundleFile,
files map[string]deepcode2.BundleFile,
) (Bundle, error)
}

func NewBundleManager(
logger *zerolog.Logger,
SnykCode deepcode.SnykCodeClient,
SnykCode deepcode2.SnykCodeClient,
instrumentor observability.Instrumentor,
errorReporter observability.ErrorReporter,
) *bundleManager {
Expand All @@ -83,7 +83,7 @@ func (b *bundleManager) Create(ctx context.Context,

var limitToFiles []string
fileHashes := make(map[string]string)
bundleFiles := make(map[string]deepcode.BundleFile)
bundleFiles := make(map[string]deepcode2.BundleFile)
noFiles := true
for absoluteFilePath := range filePaths {
noFiles = false
Expand Down Expand Up @@ -116,7 +116,7 @@ func (b *bundleManager) Create(ctx context.Context,
}
relativePath = util.EncodePath(relativePath)

bundleFile := deepcode.BundleFileFrom(absoluteFilePath, fileContent)
bundleFile := deepcode2.BundleFileFrom(absoluteFilePath, fileContent)
bundleFiles[relativePath] = bundleFile
fileHashes[relativePath] = bundleFile.Hash

Expand Down Expand Up @@ -151,7 +151,7 @@ func (b *bundleManager) Upload(
ctx context.Context,
requestId string,
bundle Bundle,
files map[string]deepcode.BundleFile,
files map[string]deepcode2.BundleFile,
) (Bundle, error) {
method := "code.Batch"
s := b.instrumentor.StartSpan(ctx, method)
Expand Down Expand Up @@ -181,14 +181,14 @@ func (b *bundleManager) Upload(
func (b *bundleManager) groupInBatches(
ctx context.Context,
bundle Bundle,
files map[string]deepcode.BundleFile,
files map[string]deepcode2.BundleFile,
) []*Batch {
method := "code.groupInBatches"
s := b.instrumentor.StartSpan(ctx, method)
defer b.instrumentor.Finish(s)

var batches []*Batch
batch := NewBatch(map[string]deepcode.BundleFile{})
batch := NewBatch(map[string]deepcode2.BundleFile{})
for _, filePath := range bundle.GetMissingFiles() {
if len(batches) == 0 { // first batch added after first file found
batches = append(batches, batch)
Expand All @@ -201,7 +201,7 @@ func (b *bundleManager) groupInBatches(
batch.documents[filePath] = file
} else {
b.logger.Trace().Str("path", filePath).Int("size", len(fileContent)).Msgf("created new deepCodeBundle - %v bundles in this upload so far", len(batches))
newUploadBatch := NewBatch(map[string]deepcode.BundleFile{})
newUploadBatch := NewBatch(map[string]deepcode2.BundleFile{})
newUploadBatch.documents[filePath] = file
batches = append(batches, newUploadBatch)
batch = newUploadBatch
Expand Down
74 changes: 37 additions & 37 deletions internal/bundle/bundle_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package bundle_test
import (
"bytes"
"context"
bundle2 "github.com/snyk/code-client-go/internal/bundle"
"os"
"path/filepath"
"strings"
Expand All @@ -30,8 +29,9 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

deepcode2 "github.com/snyk/code-client-go/deepcode"
mocks2 "github.com/snyk/code-client-go/deepcode/mocks"
"github.com/snyk/code-client-go/internal/bundle"
"github.com/snyk/code-client-go/internal/deepcode"
deepcodeMocks "github.com/snyk/code-client-go/internal/deepcode/mocks"
"github.com/snyk/code-client-go/internal/util"
"github.com/snyk/code-client-go/observability/mocks"
)
Expand All @@ -42,8 +42,8 @@ func Test_Create(t *testing.T) {
ctrl := gomock.NewController(t)
mockSpan := mocks.NewMockSpan(ctrl)
mockSpan.EXPECT().Context().AnyTimes()
mockSnykCodeClient := mocks2.NewMockSnykCodeClient(ctrl)
mockSnykCodeClient.EXPECT().GetFilters(gomock.Any()).Return(deepcode2.FiltersResponse{
mockSnykCodeClient := deepcodeMocks.NewMockSnykCodeClient(ctrl)
mockSnykCodeClient.EXPECT().GetFilters(gomock.Any()).Return(deepcode.FiltersResponse{
ConfigFiles: []string{},
Extensions: []string{".java"},
}, nil)
Expand All @@ -61,7 +61,7 @@ func Test_Create(t *testing.T) {
err := os.WriteFile(file, []byte(data), 0600)
require.NoError(t, err)

var bundleManager = bundle2.NewBundleManager(newLogger(t), mockSnykCodeClient, mockInstrumentor, mockErrorReporter)
var bundleManager = bundle.NewBundleManager(newLogger(t), mockSnykCodeClient, mockInstrumentor, mockErrorReporter)
bundle, err := bundleManager.Create(context.Background(),
"testRequestId",
dir,
Expand All @@ -77,8 +77,8 @@ func Test_Create(t *testing.T) {
ctrl := gomock.NewController(t)
mockSpan := mocks.NewMockSpan(ctrl)
mockSpan.EXPECT().Context().AnyTimes()
mockSnykCodeClient := mocks2.NewMockSnykCodeClient(ctrl)
mockSnykCodeClient.EXPECT().GetFilters(gomock.Any()).Return(deepcode2.FiltersResponse{
mockSnykCodeClient := deepcodeMocks.NewMockSnykCodeClient(ctrl)
mockSnykCodeClient.EXPECT().GetFilters(gomock.Any()).Return(deepcode.FiltersResponse{
ConfigFiles: []string{},
Extensions: []string{".java"},
}, nil)
Expand All @@ -93,7 +93,7 @@ func Test_Create(t *testing.T) {
err := os.WriteFile(file, []byte(data), 0600)
require.NoError(t, err)

var bundleManager = bundle2.NewBundleManager(newLogger(t), mockSnykCodeClient, mockInstrumentor, mockErrorReporter)
var bundleManager = bundle.NewBundleManager(newLogger(t), mockSnykCodeClient, mockInstrumentor, mockErrorReporter)
bundle, err := bundleManager.Create(context.Background(),
"testRequestId",
dir,
Expand All @@ -110,8 +110,8 @@ func Test_Create(t *testing.T) {
ctrl := gomock.NewController(t)
mockSpan := mocks.NewMockSpan(ctrl)
mockSpan.EXPECT().Context().AnyTimes()
mockSnykCodeClient := mocks2.NewMockSnykCodeClient(ctrl)
mockSnykCodeClient.EXPECT().GetFilters(gomock.Any()).Return(deepcode2.FiltersResponse{
mockSnykCodeClient := deepcodeMocks.NewMockSnykCodeClient(ctrl)
mockSnykCodeClient.EXPECT().GetFilters(gomock.Any()).Return(deepcode.FiltersResponse{
ConfigFiles: []string{},
Extensions: []string{".java"},
}, nil)
Expand All @@ -130,7 +130,7 @@ func Test_Create(t *testing.T) {
)
require.NoError(t, err)

var bundleManager = bundle2.NewBundleManager(newLogger(t), mockSnykCodeClient, mockInstrumentor, mockErrorReporter)
var bundleManager = bundle.NewBundleManager(newLogger(t), mockSnykCodeClient, mockInstrumentor, mockErrorReporter)
bundle, err := bundleManager.Create(context.Background(),
"testRequestId",
dir,
Expand All @@ -147,8 +147,8 @@ func Test_Create(t *testing.T) {
ctrl := gomock.NewController(t)
mockSpan := mocks.NewMockSpan(ctrl)
mockSpan.EXPECT().Context().AnyTimes()
mockSnykCodeClient := mocks2.NewMockSnykCodeClient(ctrl)
mockSnykCodeClient.EXPECT().GetFilters(gomock.Any()).Return(deepcode2.FiltersResponse{
mockSnykCodeClient := deepcodeMocks.NewMockSnykCodeClient(ctrl)
mockSnykCodeClient.EXPECT().GetFilters(gomock.Any()).Return(deepcode.FiltersResponse{
ConfigFiles: []string{},
Extensions: []string{".java"},
}, nil)
Expand All @@ -166,7 +166,7 @@ func Test_Create(t *testing.T) {
},
)
require.NoError(t, err)
var bundleManager = bundle2.NewBundleManager(newLogger(t), mockSnykCodeClient, mockInstrumentor, mockErrorReporter)
var bundleManager = bundle.NewBundleManager(newLogger(t), mockSnykCodeClient, mockInstrumentor, mockErrorReporter)
bundle, err := bundleManager.Create(context.Background(),
"testRequestId",
dir,
Expand All @@ -181,8 +181,8 @@ func Test_Create(t *testing.T) {
ctrl := gomock.NewController(t)
mockSpan := mocks.NewMockSpan(ctrl)
mockSpan.EXPECT().Context().AnyTimes()
mockSnykCodeClient := mocks2.NewMockSnykCodeClient(ctrl)
mockSnykCodeClient.EXPECT().GetFilters(gomock.Any()).Return(deepcode2.FiltersResponse{
mockSnykCodeClient := deepcodeMocks.NewMockSnykCodeClient(ctrl)
mockSnykCodeClient.EXPECT().GetFilters(gomock.Any()).Return(deepcode.FiltersResponse{
ConfigFiles: []string{".test"},
Extensions: []string{},
}, nil)
Expand All @@ -199,7 +199,7 @@ func Test_Create(t *testing.T) {
err := os.WriteFile(file, []byte("some content so the file won't be skipped"), 0600)
assert.Nil(t, err)

var bundleManager = bundle2.NewBundleManager(newLogger(t), mockSnykCodeClient, mockInstrumentor, mockErrorReporter)
var bundleManager = bundle.NewBundleManager(newLogger(t), mockSnykCodeClient, mockInstrumentor, mockErrorReporter)
bundle, err := bundleManager.Create(context.Background(),
"testRequestId",
tempDir,
Expand All @@ -214,8 +214,8 @@ func Test_Create(t *testing.T) {
ctrl := gomock.NewController(t)
mockSpan := mocks.NewMockSpan(ctrl)
mockSpan.EXPECT().Context().AnyTimes()
mockSnykCodeClient := mocks2.NewMockSnykCodeClient(ctrl)
mockSnykCodeClient.EXPECT().GetFilters(gomock.Any()).Return(deepcode2.FiltersResponse{
mockSnykCodeClient := deepcodeMocks.NewMockSnykCodeClient(ctrl)
mockSnykCodeClient.EXPECT().GetFilters(gomock.Any()).Return(deepcode.FiltersResponse{
ConfigFiles: []string{},
Extensions: []string{".java"},
}, nil)
Expand Down Expand Up @@ -247,7 +247,7 @@ func Test_Create(t *testing.T) {
require.NoError(t, err)
}

var bundleManager = bundle2.NewBundleManager(newLogger(t), mockSnykCodeClient, mockInstrumentor, mockErrorReporter)
var bundleManager = bundle.NewBundleManager(newLogger(t), mockSnykCodeClient, mockInstrumentor, mockErrorReporter)
bundle, err := bundleManager.Create(context.Background(),
"testRequestId",
tempDir,
Expand All @@ -272,22 +272,22 @@ func Test_Upload(t *testing.T) {
ctrl := gomock.NewController(t)
mockSpan := mocks.NewMockSpan(ctrl)
mockSpan.EXPECT().Context().AnyTimes()
mockSnykCodeClient := mocks2.NewMockSnykCodeClient(ctrl)
mockSnykCodeClient := deepcodeMocks.NewMockSnykCodeClient(ctrl)
mockSnykCodeClient.EXPECT().ExtendBundle(gomock.Any(), "bundleHash", gomock.Len(1), []string{}).Times(1)
mockInstrumentor := mocks.NewMockInstrumentor(ctrl)
mockInstrumentor.EXPECT().StartSpan(gomock.Any(), gomock.Any()).Return(mockSpan).Times(2)
mockInstrumentor.EXPECT().Finish(gomock.Any()).Times(2)
mockErrorReporter := mocks.NewMockErrorReporter(ctrl)

var bundleManager = bundle2.NewBundleManager(newLogger(t), mockSnykCodeClient, mockInstrumentor, mockErrorReporter)
var bundleManager = bundle.NewBundleManager(newLogger(t), mockSnykCodeClient, mockInstrumentor, mockErrorReporter)
documentURI, bundleFile := createTempFileInDir(t, "bundleDoc.java", 10, temporaryDir)
bundleFileMap := map[string]deepcode2.BundleFile{}
bundleFileMap := map[string]deepcode.BundleFile{}
bundleFileMap[documentURI] = bundleFile

_, err := bundleManager.Upload(
context.Background(),
"testRequestId",
bundle2.NewBundle(mockSnykCodeClient, mockInstrumentor, mockErrorReporter, &logger, "bundleHash", bundleFileMap, []string{}, []string{documentURI}),
bundle.NewBundle(mockSnykCodeClient, mockInstrumentor, mockErrorReporter, &logger, "bundleHash", bundleFileMap, []string{}, []string{documentURI}),
bundleFileMap)
assert.NoError(t, err)
})
Expand All @@ -296,16 +296,16 @@ func Test_Upload(t *testing.T) {
ctrl := gomock.NewController(t)
mockSpan := mocks.NewMockSpan(ctrl)
mockSpan.EXPECT().Context().AnyTimes()
mockSnykCodeClient := mocks2.NewMockSnykCodeClient(ctrl)
mockSnykCodeClient := deepcodeMocks.NewMockSnykCodeClient(ctrl)
mockSnykCodeClient.EXPECT().ExtendBundle(gomock.Any(), "bundleHash", gomock.Len(3), []string{}).Return("newBundleHash", []string{}, nil).Times(1)
mockSnykCodeClient.EXPECT().ExtendBundle(gomock.Any(), "newBundleHash", gomock.Len(2), []string{}).Return("newerBundleHash", []string{}, nil).Times(1)
mockInstrumentor := mocks.NewMockInstrumentor(ctrl)
mockInstrumentor.EXPECT().StartSpan(gomock.Any(), gomock.Any()).Return(mockSpan).Times(2)
mockInstrumentor.EXPECT().Finish(gomock.Any()).Times(2)
mockErrorReporter := mocks.NewMockErrorReporter(ctrl)
var bundleManager = bundle2.NewBundleManager(newLogger(t), mockSnykCodeClient, mockInstrumentor, mockErrorReporter)
var bundleManager = bundle.NewBundleManager(newLogger(t), mockSnykCodeClient, mockInstrumentor, mockErrorReporter)

bundleFileMap := map[string]deepcode2.BundleFile{}
bundleFileMap := map[string]deepcode.BundleFile{}
var missingFiles []string
path, bundleFile := createTempFileInDir(t, "bundleDoc1.java", (1024*1024)-1, temporaryDir)
bundleFileMap[path] = bundleFile
Expand All @@ -326,29 +326,29 @@ func Test_Upload(t *testing.T) {
_, err := bundleManager.Upload(
context.Background(),
"testRequestId",
bundle2.NewBundle(mockSnykCodeClient, mockInstrumentor, mockErrorReporter, &logger, "bundleHash", bundleFileMap, []string{}, missingFiles),
bundle.NewBundle(mockSnykCodeClient, mockInstrumentor, mockErrorReporter, &logger, "bundleHash", bundleFileMap, []string{}, missingFiles),
bundleFileMap)
assert.Nil(t, err)
})
}

func createTempFileInDir(t *testing.T, name string, size int, temporaryDir string) (string, deepcode2.BundleFile) {
func createTempFileInDir(t *testing.T, name string, size int, temporaryDir string) (string, deepcode.BundleFile) {
t.Helper()

documentURI, fileContent := createFileOfSize(t, name, size, temporaryDir)
return documentURI, deepcode2.BundleFile{Hash: util.Hash(fileContent), Content: string(fileContent)}
return documentURI, deepcode.BundleFile{Hash: util.Hash(fileContent), Content: string(fileContent)}
}

func Test_IsSupported_Extensions(t *testing.T) {
ctrl := gomock.NewController(t)
mockSnykCodeClient := mocks2.NewMockSnykCodeClient(ctrl)
mockSnykCodeClient.EXPECT().GetFilters(gomock.Any()).Return(deepcode2.FiltersResponse{
mockSnykCodeClient := deepcodeMocks.NewMockSnykCodeClient(ctrl)
mockSnykCodeClient.EXPECT().GetFilters(gomock.Any()).Return(deepcode.FiltersResponse{
ConfigFiles: []string{},
Extensions: []string{".java"},
}, nil)
mockInstrumentor := mocks.NewMockInstrumentor(ctrl)
mockErrorReporter := mocks.NewMockErrorReporter(ctrl)
bundler := bundle2.NewBundleManager(newLogger(t), mockSnykCodeClient, mockInstrumentor, mockErrorReporter)
bundler := bundle.NewBundleManager(newLogger(t), mockSnykCodeClient, mockInstrumentor, mockErrorReporter)

t.Run("should return true for supported languages", func(t *testing.T) {
supported, _ := bundler.IsSupported(context.Background(), "C:\\some\\path\\Test.java")
Expand Down Expand Up @@ -380,14 +380,14 @@ func Test_IsSupported_ConfigFiles(t *testing.T) {
}

ctrl := gomock.NewController(t)
mockSnykCodeClient := mocks2.NewMockSnykCodeClient(ctrl)
mockSnykCodeClient.EXPECT().GetFilters(gomock.Any()).Return(deepcode2.FiltersResponse{
mockSnykCodeClient := deepcodeMocks.NewMockSnykCodeClient(ctrl)
mockSnykCodeClient.EXPECT().GetFilters(gomock.Any()).Return(deepcode.FiltersResponse{
ConfigFiles: configFilesFromFiltersEndpoint,
Extensions: []string{},
}, nil)
mockInstrumentor := mocks.NewMockInstrumentor(ctrl)
mockErrorReporter := mocks.NewMockErrorReporter(ctrl)
bundler := bundle2.NewBundleManager(newLogger(t), mockSnykCodeClient, mockInstrumentor, mockErrorReporter)
bundler := bundle.NewBundleManager(newLogger(t), mockSnykCodeClient, mockInstrumentor, mockErrorReporter)
dir, _ := os.Getwd()

t.Run("should return true for supported config files", func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions internal/bundle/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/snyk/code-client-go/deepcode"
deepcodeMocks "github.com/snyk/code-client-go/deepcode/mocks"
"github.com/snyk/code-client-go/internal/bundle"
"github.com/snyk/code-client-go/internal/deepcode"
deepcodeMocks "github.com/snyk/code-client-go/internal/deepcode/mocks"
"github.com/snyk/code-client-go/observability/mocks"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/bundle/mocks/bundle.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/bundle/mocks/bundle_manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
"github.com/stretchr/testify/assert"

confMocks "github.com/snyk/code-client-go/config/mocks"
"github.com/snyk/code-client-go/deepcode"
codeClientHTTP "github.com/snyk/code-client-go/http"
"github.com/snyk/code-client-go/internal/deepcode"
"github.com/snyk/code-client-go/internal/util"
"github.com/snyk/code-client-go/internal/util/testutil"
)
Expand Down
Loading

0 comments on commit d782ad9

Please sign in to comment.