@@ -87,7 +87,7 @@ type Analyzer struct {
87
87
Opa * opa.Opa
88
88
}
89
89
90
- func (a * Analyzer ) AnalyzeOrg (ctx context.Context , org string , numberOfGoroutines * int ) error {
90
+ func (a * Analyzer ) AnalyzeOrg (ctx context.Context , org string , numberOfGoroutines * int ) ([] * models. PackageInsights , error ) {
91
91
provider := a .ScmClient .GetProviderName ()
92
92
93
93
providerVersion , err := a .ScmClient .GetProviderVersion (ctx )
@@ -114,19 +114,21 @@ func (a *Analyzer) AnalyzeOrg(ctx context.Context, org string, numberOfGoroutine
114
114
}
115
115
goRoutineLimitSem := semaphore .NewWeighted (int64 (maxGoroutines ))
116
116
117
+ scannedPackages := make ([]* models.PackageInsights , 0 )
118
+
117
119
pkgChan := make (chan * models.PackageInsights )
118
120
pkgWg := sync.WaitGroup {}
119
121
pkgWg .Add (1 )
120
122
go func () {
121
123
defer pkgWg .Done ()
122
124
for pkg := range pkgChan {
123
- inventory . Packages = append (inventory . Packages , pkg )
125
+ scannedPackages = append (scannedPackages , pkg )
124
126
}
125
127
}()
126
128
127
129
for repoBatch := range orgReposBatches {
128
130
if repoBatch .Err != nil {
129
- return fmt .Errorf ("failed to get batch of repos: %w" , repoBatch .Err )
131
+ return scannedPackages , fmt .Errorf ("failed to get batch of repos: %w" , repoBatch .Err )
130
132
}
131
133
if repoBatch .TotalCount != 0 {
132
134
bar .ChangeMax (repoBatch .TotalCount )
@@ -144,7 +146,7 @@ func (a *Analyzer) AnalyzeOrg(ctx context.Context, org string, numberOfGoroutine
144
146
}
145
147
if err := goRoutineLimitSem .Acquire (ctx , 1 ); err != nil {
146
148
close (errChan )
147
- return fmt .Errorf ("failed to acquire semaphore: %w" , err )
149
+ return scannedPackages , fmt .Errorf ("failed to acquire semaphore: %w" , err )
148
150
}
149
151
150
152
reposWg .Add (1 )
@@ -165,7 +167,7 @@ func (a *Analyzer) AnalyzeOrg(ctx context.Context, org string, numberOfGoroutine
165
167
return
166
168
}
167
169
168
- scannedPkg , err := inventory .ScanPackage (ctx , pkg , tempDir )
170
+ scannedPkg , err := inventory .ScanPackage (ctx , * pkg , tempDir )
169
171
if err != nil {
170
172
log .Error ().Err (err ).Str ("repo" , repoNameWithOwner ).Msg ("failed to scan package" )
171
173
return
@@ -192,23 +194,28 @@ func (a *Analyzer) AnalyzeOrg(ctx context.Context, org string, numberOfGoroutine
192
194
193
195
for err := range errChan {
194
196
if err != nil {
195
- return err
197
+ return scannedPackages , err
196
198
}
197
199
}
198
200
199
201
_ = bar .Finish ()
200
202
201
- return a .finalizeAnalysis (ctx , inventory )
203
+ err = a .finalizeAnalysis (ctx , scannedPackages )
204
+ if err != nil {
205
+ return scannedPackages , err
206
+ }
207
+
208
+ return scannedPackages , nil
202
209
}
203
210
204
- func (a * Analyzer ) AnalyzeRepo (ctx context.Context , repoString string , ref string ) error {
211
+ func (a * Analyzer ) AnalyzeRepo (ctx context.Context , repoString string , ref string ) ( * models. PackageInsights , error ) {
205
212
org , repoName , err := a .ScmClient .ParseRepoAndOrg (repoString )
206
213
if err != nil {
207
- return fmt .Errorf ("failed to parse repository: %w" , err )
214
+ return nil , fmt .Errorf ("failed to parse repository: %w" , err )
208
215
}
209
216
repo , err := a .ScmClient .GetRepo (ctx , org , repoName )
210
217
if err != nil {
211
- return fmt .Errorf ("failed to get repo: %w" , err )
218
+ return nil , fmt .Errorf ("failed to get repo: %w" , err )
212
219
}
213
220
provider := repo .GetProviderName ()
214
221
@@ -228,7 +235,7 @@ func (a *Analyzer) AnalyzeRepo(ctx context.Context, repoString string, ref strin
228
235
229
236
tempDir , err := a .cloneRepoToTemp (ctx , repo .BuildGitURL (a .ScmClient .GetProviderBaseURL ()), a .ScmClient .GetToken (), ref )
230
237
if err != nil {
231
- return err
238
+ return nil , err
232
239
}
233
240
defer os .RemoveAll (tempDir )
234
241
@@ -237,26 +244,31 @@ func (a *Analyzer) AnalyzeRepo(ctx context.Context, repoString string, ref strin
237
244
238
245
pkg , err := a .generatePackageInsights (ctx , tempDir , repo , ref )
239
246
if err != nil {
240
- return err
247
+ return nil , err
241
248
}
242
249
243
- err = inventory .AddPackage (ctx , pkg , tempDir )
250
+ scannedPackage , err : = inventory .ScanPackage (ctx , * pkg , tempDir )
244
251
if err != nil {
245
- return err
252
+ return nil , err
246
253
}
247
254
_ = bar .Finish ()
248
255
249
- return a .finalizeAnalysis (ctx , inventory )
256
+ err = a .finalizeAnalysis (ctx , []* models.PackageInsights {scannedPackage })
257
+ if err != nil {
258
+ return nil , err
259
+ }
260
+
261
+ return scannedPackage , nil
250
262
}
251
263
252
- func (a * Analyzer ) AnalyzeLocalRepo (ctx context.Context , repoPath string ) error {
264
+ func (a * Analyzer ) AnalyzeLocalRepo (ctx context.Context , repoPath string ) ( * models. PackageInsights , error ) {
253
265
org , repoName , err := a .ScmClient .ParseRepoAndOrg (repoPath )
254
266
if err != nil {
255
- return fmt .Errorf ("failed to parse repository: %w" , err )
267
+ return nil , fmt .Errorf ("failed to parse repository: %w" , err )
256
268
}
257
269
repo , err := a .ScmClient .GetRepo (ctx , org , repoName )
258
270
if err != nil {
259
- return fmt .Errorf ("failed to get repo: %w" , err )
271
+ return nil , fmt .Errorf ("failed to get repo: %w" , err )
260
272
}
261
273
provider := repo .GetProviderName ()
262
274
@@ -274,28 +286,28 @@ func (a *Analyzer) AnalyzeLocalRepo(ctx context.Context, repoPath string) error
274
286
275
287
pkg , err := a .generatePackageInsights (ctx , repoPath , repo , "" )
276
288
if err != nil {
277
- return err
289
+ return nil , err
278
290
}
279
291
280
- err = inventory .AddPackage (ctx , pkg , repoPath )
292
+ scannedPackage , err : = inventory .ScanPackage (ctx , * pkg , repoPath )
281
293
if err != nil {
282
- return err
294
+ return nil , err
295
+ }
296
+
297
+ err = a .finalizeAnalysis (ctx , []* models.PackageInsights {scannedPackage })
298
+ if err != nil {
299
+ return nil , err
283
300
}
284
301
285
- return a . finalizeAnalysis ( ctx , inventory )
302
+ return scannedPackage , nil
286
303
}
287
304
288
305
type Formatter interface {
289
- Format (ctx context.Context , report * opa. FindingsResult , packages []* models.PackageInsights ) error
306
+ Format (ctx context.Context , packages []* models.PackageInsights ) error
290
307
}
291
308
292
- func (a * Analyzer ) finalizeAnalysis (ctx context.Context , inventory * scanner.Inventory ) error {
293
- report , err := inventory .Findings (ctx )
294
- if err != nil {
295
- return err
296
- }
297
-
298
- err = a .Formatter .Format (ctx , report , inventory .Packages )
309
+ func (a * Analyzer ) finalizeAnalysis (ctx context.Context , scannedPackages []* models.PackageInsights ) error {
310
+ err := a .Formatter .Format (ctx , scannedPackages )
299
311
if err != nil {
300
312
return err
301
313
}
0 commit comments