@@ -92,13 +92,23 @@ func (a *Analyzer) AnalyzeOrg(ctx context.Context, org string, numberOfGoroutine
9292 log .Debug ().Msgf ("Starting repository analysis for organization: %s on %s" , org , provider )
9393 bar := a .progressBar (0 , "Analyzing repositories" )
9494
95- var wg sync.WaitGroup
95+ var reposWg sync.WaitGroup
9696 errChan := make (chan error , 1 )
9797 maxGoroutines := 2
9898 if numberOfGoroutines != nil {
9999 maxGoroutines = * numberOfGoroutines
100100 }
101- sem := semaphore .NewWeighted (int64 (maxGoroutines ))
101+ goRoutineLimitSem := semaphore .NewWeighted (int64 (maxGoroutines ))
102+
103+ pkgChan := make (chan * models.PackageInsights )
104+ pkgWg := sync.WaitGroup {}
105+ pkgWg .Add (1 )
106+ go func () {
107+ defer pkgWg .Done ()
108+ for pkg := range pkgChan {
109+ inventory .Packages = append (inventory .Packages , pkg )
110+ }
111+ }()
102112
103113 for repoBatch := range orgReposBatches {
104114 if repoBatch .Err != nil {
@@ -113,15 +123,15 @@ func (a *Analyzer) AnalyzeOrg(ctx context.Context, org string, numberOfGoroutine
113123 bar .ChangeMax (repoBatch .TotalCount - 1 )
114124 continue
115125 }
116- if err := sem .Acquire (ctx , 1 ); err != nil {
126+ if err := goRoutineLimitSem .Acquire (ctx , 1 ); err != nil {
117127 close (errChan )
118128 return fmt .Errorf ("failed to acquire semaphore: %w" , err )
119129 }
120130
121- wg .Add (1 )
131+ reposWg .Add (1 )
122132 go func (repo Repository ) {
123- defer sem .Release (1 )
124- defer wg .Done ()
133+ defer goRoutineLimitSem .Release (1 )
134+ defer reposWg .Done ()
125135 repoNameWithOwner := repo .GetRepoIdentifier ()
126136 tempDir , err := a .cloneRepoToTemp (ctx , repo .BuildGitURL (a .ScmClient .GetProviderBaseURL ()), a .ScmClient .GetToken (), "HEAD" )
127137 if err != nil {
@@ -136,9 +146,16 @@ func (a *Analyzer) AnalyzeOrg(ctx context.Context, org string, numberOfGoroutine
136146 return
137147 }
138148
139- err = inventory .AddPackage (ctx , pkg , tempDir )
149+ scannedPkg , err : = inventory .ScanPackage (ctx , pkg , tempDir )
140150 if err != nil {
141- log .Error ().Err (err ).Str ("repo" , repoNameWithOwner ).Msg ("failed to add package to inventory" )
151+ log .Error ().Err (err ).Str ("repo" , repoNameWithOwner ).Msg ("failed to scan package" )
152+ return
153+ }
154+
155+ select {
156+ case pkgChan <- scannedPkg :
157+ case <- ctx .Done ():
158+ log .Error ().Msg ("Context canceled while sending package to channel" )
142159 return
143160 }
144161 _ = bar .Add (1 )
@@ -147,10 +164,13 @@ func (a *Analyzer) AnalyzeOrg(ctx context.Context, org string, numberOfGoroutine
147164 }
148165
149166 go func () {
150- wg .Wait ()
167+ reposWg .Wait ()
168+ close (pkgChan )
151169 close (errChan )
152170 }()
153171
172+ pkgWg .Wait ()
173+
154174 for err := range errChan {
155175 if err != nil {
156176 return err
0 commit comments