Skip to content

Commit 9beb7fa

Browse files
fix: send the correct limitToFiles to the incremental scan [IDE-402] (#58)
1 parent 48f53df commit 9beb7fa

File tree

7 files changed

+121
-8
lines changed

7 files changed

+121
-8
lines changed

internal/analysis/analysis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func (a *analysisOrchestrator) RunIncrementalAnalysis(ctx context.Context, orgId
241241
org := uuid.MustParse(orgId)
242242

243243
host := a.host(false)
244-
a.logger.Debug().Str("host", host).Str("workspaceId", workspaceId).Msg("starting scan")
244+
a.logger.Debug().Str("host", host).Str("workspaceId", workspaceId).Interface("limitToFiles", limitToFiles).Msg("starting scan")
245245

246246
client, err := orchestrationClient.NewClientWithResponses(host, orchestrationClient.WithHTTPClient(a.httpClient))
247247
if err != nil {

internal/bundle/bundle.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Bundle interface {
3131
GetBundleHash() string
3232
GetFiles() map[string]deepcode.BundleFile
3333
GetMissingFiles() []string
34+
GetLimitToFiles() []string
3435
GetRootPath() string
3536
}
3637

@@ -84,6 +85,10 @@ func (b *deepCodeBundle) GetMissingFiles() []string {
8485
return b.missingFiles
8586
}
8687

88+
func (b *deepCodeBundle) GetLimitToFiles() []string {
89+
return b.limitToFiles
90+
}
91+
8792
func (b *deepCodeBundle) GetRootPath() string {
8893
return b.rootPath
8994
}

internal/bundle/bundle_manager_test.go

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func Test_Create(t *testing.T) {
236236
assert.Contains(t, bundle.GetFiles(), relativePath)
237237
})
238238

239-
t.Run("url-encodes files", func(t *testing.T) {
239+
t.Run("url-encoded files", func(t *testing.T) {
240240
ctrl := gomock.NewController(t)
241241
mockSpan := mocks.NewMockSpan(ctrl)
242242
mockSpan.EXPECT().Context().AnyTimes()
@@ -289,6 +289,64 @@ func Test_Create(t *testing.T) {
289289
assert.Contains(t, bundle.GetFiles(), expectedPath)
290290
}
291291
})
292+
293+
t.Run("includes changed files into limit to files", func(t *testing.T) {
294+
ctrl := gomock.NewController(t)
295+
mockSpan := mocks.NewMockSpan(ctrl)
296+
mockSpan.EXPECT().Context().AnyTimes()
297+
mockSnykCodeClient := deepcodeMocks.NewMockDeepcodeClient(ctrl)
298+
mockSnykCodeClient.EXPECT().GetFilters(gomock.Any()).Return(deepcode.FiltersResponse{
299+
ConfigFiles: []string{},
300+
Extensions: []string{".java"},
301+
}, nil)
302+
mockSnykCodeClient.EXPECT().CreateBundle(gomock.Any(), map[string]string{
303+
"path/to/file1.java": "9c05690c5b8e22df259431c95df33d01267f799de6810382ada1a9ff1b89710e",
304+
"path/with%20spaces/file2.java": "9c05690c5b8e22df259431c95df33d01267f799de6810382ada1a9ff1b89710e",
305+
}).Times(1)
306+
mockInstrumentor := mocks.NewMockInstrumentor(ctrl)
307+
mockInstrumentor.EXPECT().StartSpan(gomock.Any(), gomock.Any()).Return(mockSpan).AnyTimes()
308+
mockInstrumentor.EXPECT().Finish(gomock.Any()).AnyTimes()
309+
mockErrorReporter := mocks.NewMockErrorReporter(ctrl)
310+
mockTracker := trackerMocks.NewMockTracker(ctrl)
311+
mockTracker.EXPECT().Begin(gomock.Eq("Creating file bundle"), gomock.Eq("Checking and adding files for analysis")).Return()
312+
mockTracker.EXPECT().End(gomock.Eq("")).Return()
313+
mockTrackerFactory := trackerMocks.NewMockTrackerFactory(ctrl)
314+
mockTrackerFactory.EXPECT().GenerateTracker().Return(mockTracker)
315+
316+
filesRelPaths := []string{
317+
"path/to/file1.java",
318+
"path/with spaces/file2.java",
319+
}
320+
expectedPaths := []string{
321+
"path/to/file1.java",
322+
}
323+
324+
tempDir := t.TempDir()
325+
var filesFullPaths []string
326+
for _, fileRelPath := range filesRelPaths {
327+
file := filepath.Join(tempDir, fileRelPath)
328+
filesFullPaths = append(filesFullPaths, file)
329+
_ = os.MkdirAll(filepath.Dir(file), 0700)
330+
err := os.WriteFile(file, []byte("some content so the file won't be skipped"), 0600)
331+
require.NoError(t, err)
332+
}
333+
334+
var bundleManager = bundle.NewBundleManager(mockSnykCodeClient, newLogger(t), mockInstrumentor, mockErrorReporter, mockTrackerFactory)
335+
bundle, err := bundleManager.Create(context.Background(),
336+
"testRequestId",
337+
tempDir,
338+
sliceToChannel(filesFullPaths),
339+
map[string]bool{
340+
filesFullPaths[0]: true,
341+
})
342+
require.NoError(t, err)
343+
for _, expectedPath := range expectedPaths {
344+
assert.Contains(t, bundle.GetFiles(), expectedPath)
345+
}
346+
for _, expectedPath := range expectedPaths {
347+
assert.Contains(t, bundle.GetLimitToFiles(), expectedPath)
348+
}
349+
})
292350
}
293351

294352
func Test_Upload(t *testing.T) {

internal/bundle/mocks/bundle.go

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

internal/workspace/2024-05-14/client.go

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

scan.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ func NewCodeScanner(
125125
analysis.WithInstrumentor(scanner.instrumentor),
126126
analysis.WithErrorReporter(scanner.errorReporter),
127127
analysis.WithTrackerFactory(scanner.trackerFactory),
128+
analysis.WithLogger(scanner.logger),
128129
analysis.WithFlow(scanner.flow),
129130
)
130131
scanner.analysisOrchestrator = analysisOrchestrator
@@ -214,11 +215,8 @@ func (c *codeScanner) UploadAndAnalyze(
214215
return nil, bundleHash, nil
215216
}
216217
}
217-
var limitToFiles []string
218-
for file := range changedFiles {
219-
limitToFiles = append(limitToFiles, file)
220-
}
221-
response, err := c.analysisOrchestrator.RunIncrementalAnalysis(ctx, c.config.Organization(), b.GetRootPath(), workspaceId, limitToFiles)
218+
219+
response, err := c.analysisOrchestrator.RunIncrementalAnalysis(ctx, c.config.Organization(), b.GetRootPath(), workspaceId, b.GetLimitToFiles())
222220

223221
if ctx.Err() != nil {
224222
c.logger.Info().Msg("Canceling Code scan - Code scanner received cancellation signal")

scan_test.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func Test_UploadAndAnalyze(t *testing.T) {
105105
"4a72d1db-b465-4764-99e1-ecedad03b06a",
106106
"testRootPath",
107107
"c172d1db-b465-4764-99e1-ecedad03b06a",
108-
gomock.Any(),
108+
[]string{},
109109
).Return(&sarif.SarifResponse{Status: "COMPLETE"}, nil)
110110

111111
codeScanner := codeclient.NewCodeScanner(
@@ -126,6 +126,43 @@ func Test_UploadAndAnalyze(t *testing.T) {
126126
assert.Equal(t, "testBundleHash", bundleHash)
127127
},
128128
)
129+
130+
t.Run(
131+
"should send the changed files to the analysis", func(t *testing.T) {
132+
relativeChangedFile := "./nested/folder/nested/file.ts"
133+
134+
mockBundle := bundle.NewBundle(deepcodeMocks.NewMockDeepcodeClient(ctrl), mockInstrumentor, mockErrorReporter, &logger, "testRootPath", "testBundleHash", files, []string{relativeChangedFile}, []string{})
135+
mockBundleManager := bundleMocks.NewMockBundleManager(ctrl)
136+
mockBundleManager.EXPECT().Create(gomock.Any(), "b372d1db-b465-4764-99e1-ecedad03b06a", baseDir, gomock.Any(), map[string]bool{}).Return(mockBundle, nil)
137+
mockBundleManager.EXPECT().Upload(gomock.Any(), "b372d1db-b465-4764-99e1-ecedad03b06a", mockBundle, files).Return(mockBundle, nil)
138+
139+
mockAnalysisOrchestrator := mockAnalysis.NewMockAnalysisOrchestrator(ctrl)
140+
mockAnalysisOrchestrator.EXPECT().CreateWorkspace(gomock.Any(), "4a72d1db-b465-4764-99e1-ecedad03b06a", "b372d1db-b465-4764-99e1-ecedad03b06a", target, "testBundleHash").Return("c172d1db-b465-4764-99e1-ecedad03b06a", nil)
141+
mockAnalysisOrchestrator.EXPECT().RunIncrementalAnalysis(
142+
gomock.Any(),
143+
"4a72d1db-b465-4764-99e1-ecedad03b06a",
144+
"testRootPath",
145+
"c172d1db-b465-4764-99e1-ecedad03b06a",
146+
[]string{relativeChangedFile},
147+
).Return(&sarif.SarifResponse{Status: "COMPLETE"}, nil)
148+
149+
codeScanner := codeclient.NewCodeScanner(
150+
mockConfig,
151+
mockHTTPClient,
152+
codeclient.WithTrackerFactory(mockTrackerFactory),
153+
codeclient.WithInstrumentor(mockInstrumentor),
154+
codeclient.WithErrorReporter(mockErrorReporter),
155+
codeclient.WithLogger(&logger),
156+
)
157+
158+
response, _, err := codeScanner.
159+
WithBundleManager(mockBundleManager).
160+
WithAnalysisOrchestrator(mockAnalysisOrchestrator).
161+
UploadAndAnalyze(context.Background(), "b372d1db-b465-4764-99e1-ecedad03b06a", target, docs, map[string]bool{})
162+
require.NoError(t, err)
163+
assert.Equal(t, "COMPLETE", response.Status)
164+
},
165+
)
129166
}
130167

131168
func setupDocs(t *testing.T) (string, string, string, []byte, []byte) {

0 commit comments

Comments
 (0)