@@ -87,7 +87,7 @@ type Analyzer struct {
8787 Opa * opa.Opa
8888}
8989
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 ) {
9191 provider := a .ScmClient .GetProviderName ()
9292
9393 providerVersion , err := a .ScmClient .GetProviderVersion (ctx )
@@ -114,19 +114,21 @@ func (a *Analyzer) AnalyzeOrg(ctx context.Context, org string, numberOfGoroutine
114114 }
115115 goRoutineLimitSem := semaphore .NewWeighted (int64 (maxGoroutines ))
116116
117+ scannedPackages := make ([]* models.PackageInsights , 0 )
118+
117119 pkgChan := make (chan * models.PackageInsights )
118120 pkgWg := sync.WaitGroup {}
119121 pkgWg .Add (1 )
120122 go func () {
121123 defer pkgWg .Done ()
122124 for pkg := range pkgChan {
123- inventory . Packages = append (inventory . Packages , pkg )
125+ scannedPackages = append (scannedPackages , pkg )
124126 }
125127 }()
126128
127129 for repoBatch := range orgReposBatches {
128130 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 )
130132 }
131133 if repoBatch .TotalCount != 0 {
132134 bar .ChangeMax (repoBatch .TotalCount )
@@ -144,7 +146,7 @@ func (a *Analyzer) AnalyzeOrg(ctx context.Context, org string, numberOfGoroutine
144146 }
145147 if err := goRoutineLimitSem .Acquire (ctx , 1 ); err != nil {
146148 close (errChan )
147- return fmt .Errorf ("failed to acquire semaphore: %w" , err )
149+ return scannedPackages , fmt .Errorf ("failed to acquire semaphore: %w" , err )
148150 }
149151
150152 reposWg .Add (1 )
@@ -165,7 +167,7 @@ func (a *Analyzer) AnalyzeOrg(ctx context.Context, org string, numberOfGoroutine
165167 return
166168 }
167169
168- scannedPkg , err := inventory .ScanPackage (ctx , pkg , tempDir )
170+ scannedPkg , err := inventory .ScanPackage (ctx , * pkg , tempDir )
169171 if err != nil {
170172 log .Error ().Err (err ).Str ("repo" , repoNameWithOwner ).Msg ("failed to scan package" )
171173 return
@@ -192,23 +194,28 @@ func (a *Analyzer) AnalyzeOrg(ctx context.Context, org string, numberOfGoroutine
192194
193195 for err := range errChan {
194196 if err != nil {
195- return err
197+ return scannedPackages , err
196198 }
197199 }
198200
199201 _ = bar .Finish ()
200202
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
202209}
203210
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 ) {
205212 org , repoName , err := a .ScmClient .ParseRepoAndOrg (repoString )
206213 if err != nil {
207- return fmt .Errorf ("failed to parse repository: %w" , err )
214+ return nil , fmt .Errorf ("failed to parse repository: %w" , err )
208215 }
209216 repo , err := a .ScmClient .GetRepo (ctx , org , repoName )
210217 if err != nil {
211- return fmt .Errorf ("failed to get repo: %w" , err )
218+ return nil , fmt .Errorf ("failed to get repo: %w" , err )
212219 }
213220 provider := repo .GetProviderName ()
214221
@@ -228,7 +235,7 @@ func (a *Analyzer) AnalyzeRepo(ctx context.Context, repoString string, ref strin
228235
229236 tempDir , err := a .cloneRepoToTemp (ctx , repo .BuildGitURL (a .ScmClient .GetProviderBaseURL ()), a .ScmClient .GetToken (), ref )
230237 if err != nil {
231- return err
238+ return nil , err
232239 }
233240 defer os .RemoveAll (tempDir )
234241
@@ -237,26 +244,31 @@ func (a *Analyzer) AnalyzeRepo(ctx context.Context, repoString string, ref strin
237244
238245 pkg , err := a .generatePackageInsights (ctx , tempDir , repo , ref )
239246 if err != nil {
240- return err
247+ return nil , err
241248 }
242249
243- err = inventory .AddPackage (ctx , pkg , tempDir )
250+ scannedPackage , err : = inventory .ScanPackage (ctx , * pkg , tempDir )
244251 if err != nil {
245- return err
252+ return nil , err
246253 }
247254 _ = bar .Finish ()
248255
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
250262}
251263
252- func (a * Analyzer ) AnalyzeLocalRepo (ctx context.Context , repoPath string ) error {
264+ func (a * Analyzer ) AnalyzeLocalRepo (ctx context.Context , repoPath string ) ( * models. PackageInsights , error ) {
253265 org , repoName , err := a .ScmClient .ParseRepoAndOrg (repoPath )
254266 if err != nil {
255- return fmt .Errorf ("failed to parse repository: %w" , err )
267+ return nil , fmt .Errorf ("failed to parse repository: %w" , err )
256268 }
257269 repo , err := a .ScmClient .GetRepo (ctx , org , repoName )
258270 if err != nil {
259- return fmt .Errorf ("failed to get repo: %w" , err )
271+ return nil , fmt .Errorf ("failed to get repo: %w" , err )
260272 }
261273 provider := repo .GetProviderName ()
262274
@@ -274,28 +286,28 @@ func (a *Analyzer) AnalyzeLocalRepo(ctx context.Context, repoPath string) error
274286
275287 pkg , err := a .generatePackageInsights (ctx , repoPath , repo , "" )
276288 if err != nil {
277- return err
289+ return nil , err
278290 }
279291
280- err = inventory .AddPackage (ctx , pkg , repoPath )
292+ scannedPackage , err : = inventory .ScanPackage (ctx , * pkg , repoPath )
281293 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
283300 }
284301
285- return a . finalizeAnalysis ( ctx , inventory )
302+ return scannedPackage , nil
286303}
287304
288305type Formatter interface {
289- Format (ctx context.Context , report * opa. FindingsResult , packages []* models.PackageInsights ) error
306+ Format (ctx context.Context , packages []* models.PackageInsights ) error
290307}
291308
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 )
299311 if err != nil {
300312 return err
301313 }
0 commit comments