Skip to content

Commit d916785

Browse files
fix: reinstate default bundle create behaviour (#116)
Signed-off-by: Andrew Robinson Hodges <[email protected]>
1 parent f69203f commit d916785

File tree

8 files changed

+102
-19
lines changed

8 files changed

+102
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Implement the `config.Config` interface to configure the Snyk Code API client fr
8484

8585
### Code Scanner
8686

87-
Use the Code Scanner to trigger a scan for a Snyk Code workspace using the Bundle Manager created above.
87+
Use the Code Scanner to trigger a scan for a Snyk Code workspace using the Bundle Manager.
8888

8989
The Code Scanner exposes a `UploadAndAnalyze` function, which can be used like this:
9090

bundle/bundle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func NewBatchFromRawContent(documents map[string][]byte) (*Batch, error) {
145145
bundleFiles := make(map[string]deepcode.BundleFile)
146146

147147
for key, rawData := range documents {
148-
bundleFile, err := deepcode.BundleFileFrom(rawData)
148+
bundleFile, err := deepcode.BundleFileFrom(rawData, true)
149149
if err != nil {
150150
return nil, fmt.Errorf("failed to create file from raw data: %v", err)
151151
}

bundle/bundle_manager.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ type BundleManager interface {
5050
changedFiles map[string]bool,
5151
) (bundle Bundle, err error)
5252

53+
// CreateEmpty does not include the file contents in the bundle.
54+
CreateEmpty(ctx context.Context,
55+
rootPath string,
56+
filePaths <-chan string,
57+
changedFiles map[string]bool,
58+
) (bundle Bundle, err error)
59+
5360
Upload(
5461
ctx context.Context,
5562
requestId string,
@@ -76,11 +83,31 @@ func NewBundleManager(
7683
}
7784
}
7885

79-
func (b *bundleManager) Create(ctx context.Context,
86+
func (b *bundleManager) Create(
87+
ctx context.Context,
8088
requestId string,
8189
rootPath string,
8290
filePaths <-chan string,
8391
changedFiles map[string]bool,
92+
) (bundle Bundle, err error) {
93+
return b.create(ctx, rootPath, filePaths, changedFiles, true)
94+
}
95+
96+
func (b *bundleManager) CreateEmpty(
97+
ctx context.Context,
98+
rootPath string,
99+
filePaths <-chan string,
100+
changedFiles map[string]bool,
101+
) (bundle Bundle, err error) {
102+
return b.create(ctx, rootPath, filePaths, changedFiles, false)
103+
}
104+
105+
func (b *bundleManager) create(
106+
ctx context.Context,
107+
rootPath string,
108+
filePaths <-chan string,
109+
changedFiles map[string]bool,
110+
includeFileContents bool,
84111
) (bundle Bundle, err error) {
85112
span := b.instrumentor.StartSpan(ctx, "code.createBundle")
86113
defer b.instrumentor.Finish(span)
@@ -129,7 +156,7 @@ func (b *bundleManager) Create(ctx context.Context,
129156
}
130157
relativePath = util.EncodePath(relativePath)
131158

132-
bundleFile, fileErr := deepcode.BundleFileFrom(fileContent)
159+
bundleFile, fileErr := deepcode.BundleFileFrom(fileContent, includeFileContents)
133160
if fileErr != nil {
134161
b.logger.Error().Err(err).Str("filePath", absoluteFilePath).Msg("Error creating bundle file")
135162
}

bundle/bundle_test.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,18 @@ import (
2525
"os"
2626
"testing"
2727

28+
"golang.org/x/net/html/charset"
29+
2830
"github.com/golang/mock/gomock"
2931
"github.com/rs/zerolog"
30-
"github.com/stretchr/testify/assert"
31-
"github.com/stretchr/testify/require"
32-
"golang.org/x/net/html/charset"
3332

3433
"github.com/snyk/code-client-go/bundle"
3534
"github.com/snyk/code-client-go/internal/deepcode"
3635
deepcodeMocks "github.com/snyk/code-client-go/internal/deepcode/mocks"
36+
"github.com/snyk/code-client-go/internal/util"
3737
"github.com/snyk/code-client-go/observability/mocks"
38+
"github.com/stretchr/testify/assert"
39+
"github.com/stretchr/testify/require"
3840
)
3941

4042
var bundleWithFiles = bundle.NewBatch(map[string]deepcode.BundleFile{"file": {}})
@@ -140,7 +142,7 @@ func Test_RawContentBatch(t *testing.T) {
140142
t.Run("create a batch from raw content and upload the bundle", func(t *testing.T) {
141143
ctrl := gomock.NewController(t)
142144
mockSnykCodeClient := deepcodeMocks.NewMockDeepcodeClient(ctrl)
143-
mockSnykCodeClient.EXPECT().ExtendBundle(gomock.Any(), "testBundleHash", bundleFilePartialMatcher{expectedKey: "hello", expectedContent: ""}, []string{}).Return("newBundleHash", []string{}, nil).Times(1)
145+
mockSnykCodeClient.EXPECT().ExtendBundle(gomock.Any(), "testBundleHash", bundleFilePartialMatcher{expectedKey: "hello", expectedContent: "world"}, []string{}).Return("newBundleHash", []string{}, nil).Times(1)
144146

145147
mockSpan := mocks.NewMockSpan(ctrl)
146148
mockSpan.EXPECT().Context().AnyTimes()
@@ -162,7 +164,7 @@ func Test_RawContentBatch(t *testing.T) {
162164
func Test_BundleEncoding(t *testing.T) {
163165
t.Run("utf-8 encoded content", func(t *testing.T) {
164166
content := []byte("hello")
165-
bundleFile, err := deepcode.BundleFileFrom(content)
167+
bundleFile, err := deepcode.BundleFileFrom(content, false)
166168
assert.NoError(t, err)
167169

168170
ExpectedShaSum := sha256.Sum256(content)
@@ -173,7 +175,7 @@ func Test_BundleEncoding(t *testing.T) {
173175
content, err := os.ReadFile("testdata/rshell_font.php")
174176
assert.NoError(t, err)
175177

176-
bundleFile, err := deepcode.BundleFileFrom(content)
178+
bundleFile, err := deepcode.BundleFileFrom(content, false)
177179
assert.NoError(t, err)
178180

179181
byteReader := bytes.NewReader(content)
@@ -183,3 +185,27 @@ func Test_BundleEncoding(t *testing.T) {
183185
assert.Equal(t, hex.EncodeToString(ExpectedShaSum[:]), bundleFile.Hash)
184186
})
185187
}
188+
189+
func Test_BundleFileContent(t *testing.T) {
190+
t.Run("include file contents", func(t *testing.T) {
191+
content := []byte("hello")
192+
bundleFile, err := deepcode.BundleFileFrom(content, true)
193+
assert.NoError(t, err)
194+
195+
utf8Content, err := util.ConvertToUTF8(content)
196+
assert.NoError(t, err)
197+
198+
assert.Equal(t, string(utf8Content), bundleFile.Content)
199+
assert.Equal(t, len(content), bundleFile.ContentSize)
200+
})
201+
202+
t.Run("exclude file contents", func(t *testing.T) {
203+
content := []byte("hello")
204+
bundleFile, err := deepcode.BundleFileFrom(content, false)
205+
assert.NoError(t, err)
206+
207+
assert.Equal(t, "", bundleFile.Content)
208+
// Note that we still expect the bundle to indicate the expected final size.
209+
assert.Equal(t, len(content), bundleFile.ContentSize)
210+
})
211+
}

bundle/mocks/bundle_manager.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/deepcode/helpers.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,26 @@ type BundleFile struct {
2525
ContentSize int `json:"-"`
2626
}
2727

28-
func BundleFileFrom(content []byte) (BundleFile, error) {
28+
func BundleFileFrom(content []byte, includeContent bool) (BundleFile, error) {
2929
hash, err := util.Hash(content)
30+
31+
// We can either create the bundleFile empty and enrich it with content later, or include the content now.
32+
// Creating empty avoids keeping the file contents in memory, so improves performance if we don't need access to the
33+
// contents right away.
34+
bundleFileContent := ""
35+
if includeContent {
36+
utf8Content, convertErr := util.ConvertToUTF8(content)
37+
if convertErr == nil {
38+
bundleFileContent = string(utf8Content)
39+
} else {
40+
bundleFileContent = string(content)
41+
err = convertErr
42+
}
43+
}
44+
3045
file := BundleFile{
3146
Hash: hash,
32-
Content: "", // We create the bundleFile empty, and enrich with content later.
47+
Content: bundleFileContent,
3348
ContentSize: len(content),
3449
}
3550
return file, err

scan.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func (c *codeScanner) Upload(
215215
return nil, err
216216
}
217217

218-
originalBundle, err := c.bundleManager.Create(ctx, requestId, target.GetPath(), files, changedFiles)
218+
originalBundle, err := c.bundleManager.CreateEmpty(ctx, target.GetPath(), files, changedFiles)
219219
err = c.checkCancellationOrLogError(ctx, target.GetPath(), err, "error creating bundle...")
220220
if err != nil {
221221
return nil, err

scan_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ import (
4444
func Test_UploadAndAnalyze(t *testing.T) {
4545
baseDir, firstDocPath, secondDocPath, firstDocContent, secondDocContent := setupDocs(t)
4646
docs := sliceToChannel([]string{firstDocPath, secondDocPath})
47-
firstBundle, err := deepcode.BundleFileFrom(firstDocContent)
47+
firstBundle, err := deepcode.BundleFileFrom(firstDocContent, false)
4848
assert.NoError(t, err)
49-
secondBundle, err := deepcode.BundleFileFrom(secondDocContent)
49+
secondBundle, err := deepcode.BundleFileFrom(secondDocContent, false)
5050
assert.NoError(t, err)
5151

5252
files := map[string]deepcode.BundleFile{
@@ -80,7 +80,7 @@ func Test_UploadAndAnalyze(t *testing.T) {
8080
requestId := uuid.NewString()
8181
mockBundle := bundle.NewBundle(deepcodeMocks.NewMockDeepcodeClient(ctrl), mockInstrumentor, mockErrorReporter, &logger, "testRootPath", "", files, []string{}, []string{})
8282
mockBundleManager := bundleMocks.NewMockBundleManager(ctrl)
83-
mockBundleManager.EXPECT().Create(gomock.Any(), requestId, baseDir, gomock.Any(), map[string]bool{}).Return(mockBundle, nil)
83+
mockBundleManager.EXPECT().CreateEmpty(gomock.Any(), baseDir, gomock.Any(), map[string]bool{}).Return(mockBundle, nil)
8484
mockBundleManager.EXPECT().Upload(gomock.Any(), requestId, mockBundle, files).Return(mockBundle, nil)
8585

8686
codeScanner := codeclient.NewCodeScanner(
@@ -104,7 +104,7 @@ func Test_UploadAndAnalyze(t *testing.T) {
104104
requestId := uuid.NewString()
105105
mockBundle := bundle.NewBundle(deepcodeMocks.NewMockDeepcodeClient(ctrl), mockInstrumentor, mockErrorReporter, &logger, "testRootPath", uuid.NewString(), files, []string{}, []string{})
106106
mockBundleManager := bundleMocks.NewMockBundleManager(ctrl)
107-
mockBundleManager.EXPECT().Create(gomock.Any(), requestId, baseDir, gomock.Any(), map[string]bool{}).Return(mockBundle, nil)
107+
mockBundleManager.EXPECT().CreateEmpty(gomock.Any(), baseDir, gomock.Any(), map[string]bool{}).Return(mockBundle, nil)
108108
mockBundleManager.EXPECT().Upload(gomock.Any(), requestId, mockBundle, files).Return(mockBundle, nil)
109109

110110
codeScanner := codeclient.NewCodeScanner(
@@ -129,7 +129,7 @@ func Test_UploadAndAnalyze(t *testing.T) {
129129
requestId := uuid.NewString()
130130
mockBundle := bundle.NewBundle(deepcodeMocks.NewMockDeepcodeClient(ctrl), mockInstrumentor, mockErrorReporter, &logger, "testRootPath", uuid.NewString(), files, []string{}, []string{})
131131
mockBundleManager := bundleMocks.NewMockBundleManager(ctrl)
132-
mockBundleManager.EXPECT().Create(gomock.Any(), requestId, baseDir, gomock.Any(), map[string]bool{}).Return(mockBundle, nil)
132+
mockBundleManager.EXPECT().CreateEmpty(gomock.Any(), baseDir, gomock.Any(), map[string]bool{}).Return(mockBundle, nil)
133133
mockBundleManager.EXPECT().Upload(gomock.Any(), requestId, mockBundle, files).Return(mockBundle, nil)
134134

135135
mockAnalysisOrchestrator := mockAnalysis.NewMockAnalysisOrchestrator(ctrl)
@@ -166,7 +166,7 @@ func Test_UploadAndAnalyze(t *testing.T) {
166166
requestId := uuid.NewString()
167167
mockBundle := bundle.NewBundle(deepcodeMocks.NewMockDeepcodeClient(ctrl), mockInstrumentor, mockErrorReporter, &logger, "testRootPath", uuid.NewString(), files, []string{relativeChangedFile}, []string{})
168168
mockBundleManager := bundleMocks.NewMockBundleManager(ctrl)
169-
mockBundleManager.EXPECT().Create(gomock.Any(), requestId, baseDir, gomock.Any(), map[string]bool{}).Return(mockBundle, nil)
169+
mockBundleManager.EXPECT().CreateEmpty(gomock.Any(), baseDir, gomock.Any(), map[string]bool{}).Return(mockBundle, nil)
170170
mockBundleManager.EXPECT().Upload(gomock.Any(), requestId, mockBundle, files).Return(mockBundle, nil)
171171

172172
mockAnalysisOrchestrator := mockAnalysis.NewMockAnalysisOrchestrator(ctrl)

0 commit comments

Comments
 (0)