@@ -92,13 +92,23 @@ func (a *Analyzer) AnalyzeOrg(ctx context.Context, org string, numberOfGoroutine
92
92
log .Debug ().Msgf ("Starting repository analysis for organization: %s on %s" , org , provider )
93
93
bar := a .progressBar (0 , "Analyzing repositories" )
94
94
95
- var wg sync.WaitGroup
95
+ var reposWg sync.WaitGroup
96
96
errChan := make (chan error , 1 )
97
97
maxGoroutines := 2
98
98
if numberOfGoroutines != nil {
99
99
maxGoroutines = * numberOfGoroutines
100
100
}
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
+ }()
102
112
103
113
for repoBatch := range orgReposBatches {
104
114
if repoBatch .Err != nil {
@@ -113,15 +123,15 @@ func (a *Analyzer) AnalyzeOrg(ctx context.Context, org string, numberOfGoroutine
113
123
bar .ChangeMax (repoBatch .TotalCount - 1 )
114
124
continue
115
125
}
116
- if err := sem .Acquire (ctx , 1 ); err != nil {
126
+ if err := goRoutineLimitSem .Acquire (ctx , 1 ); err != nil {
117
127
close (errChan )
118
128
return fmt .Errorf ("failed to acquire semaphore: %w" , err )
119
129
}
120
130
121
- wg .Add (1 )
131
+ reposWg .Add (1 )
122
132
go func (repo Repository ) {
123
- defer sem .Release (1 )
124
- defer wg .Done ()
133
+ defer goRoutineLimitSem .Release (1 )
134
+ defer reposWg .Done ()
125
135
repoNameWithOwner := repo .GetRepoIdentifier ()
126
136
tempDir , err := a .cloneRepoToTemp (ctx , repo .BuildGitURL (a .ScmClient .GetProviderBaseURL ()), a .ScmClient .GetToken (), "HEAD" )
127
137
if err != nil {
@@ -136,9 +146,16 @@ func (a *Analyzer) AnalyzeOrg(ctx context.Context, org string, numberOfGoroutine
136
146
return
137
147
}
138
148
139
- err = inventory .AddPackage (ctx , pkg , tempDir )
149
+ scannedPkg , err : = inventory .ScanPackage (ctx , pkg , tempDir )
140
150
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" )
142
159
return
143
160
}
144
161
_ = bar .Add (1 )
@@ -147,10 +164,13 @@ func (a *Analyzer) AnalyzeOrg(ctx context.Context, org string, numberOfGoroutine
147
164
}
148
165
149
166
go func () {
150
- wg .Wait ()
167
+ reposWg .Wait ()
168
+ close (pkgChan )
151
169
close (errChan )
152
170
}()
153
171
172
+ pkgWg .Wait ()
173
+
154
174
for err := range errChan {
155
175
if err != nil {
156
176
return err
0 commit comments