@@ -47,14 +47,15 @@ import (
47
47
//go:generate mockgen -destination=mocks/analysis.go -source=analysis.go -package mocks
48
48
type AnalysisOrchestrator interface {
49
49
CreateWorkspace (ctx context.Context , orgId string , requestId string , path scan.Target , bundleHash string ) (string , error )
50
- RunAnalysis (ctx context.Context , orgId string , workspaceId string ) (* sarif.SarifResponse , error )
50
+ RunAnalysis (ctx context.Context , orgId string , rootPath string , workspaceId string ) (* sarif.SarifResponse , error )
51
51
}
52
52
53
53
type analysisOrchestrator struct {
54
54
httpClient codeClientHTTP.HTTPClient
55
55
instrumentor observability.Instrumentor
56
56
errorReporter observability.ErrorReporter
57
57
logger * zerolog.Logger
58
+ trackerFactory scan.TrackerFactory
58
59
config config.Config
59
60
timeoutInSeconds time.Duration
60
61
}
@@ -73,13 +74,15 @@ func NewAnalysisOrchestrator(
73
74
httpClient codeClientHTTP.HTTPClient ,
74
75
instrumentor observability.Instrumentor ,
75
76
errorReporter observability.ErrorReporter ,
77
+ trackerFactory scan.TrackerFactory ,
76
78
options ... OptionFunc ,
77
79
) AnalysisOrchestrator {
78
80
a := & analysisOrchestrator {
79
81
httpClient : httpClient ,
80
82
instrumentor : instrumentor ,
81
83
errorReporter : errorReporter ,
82
84
logger : logger ,
85
+ trackerFactory : trackerFactory ,
83
86
config : config ,
84
87
timeoutInSeconds : 120 * time .Second ,
85
88
}
@@ -98,6 +101,10 @@ func (a *analysisOrchestrator) CreateWorkspace(ctx context.Context, orgId string
98
101
span := a .instrumentor .StartSpan (ctx , method )
99
102
defer a .instrumentor .Finish (span )
100
103
104
+ tracker := a .trackerFactory .GenerateTracker ()
105
+ tracker .Begin ("Creating file bundle workspace" , "" )
106
+ defer tracker .End ("" )
107
+
101
108
orgUUID := uuid .MustParse (orgId )
102
109
103
110
if target == nil {
@@ -181,23 +188,44 @@ func (a *analysisOrchestrator) CreateWorkspace(ctx context.Context, orgId string
181
188
return workspaceId , nil
182
189
}
183
190
184
- func (a * analysisOrchestrator ) RunAnalysis (ctx context.Context , orgId string , workspaceId string ) (* sarif.SarifResponse , error ) {
191
+ func (a * analysisOrchestrator ) RunAnalysis (ctx context.Context , orgId string , rootPath string , workspaceId string ) (* sarif.SarifResponse , error ) {
185
192
method := "analysis.RunAnalysis"
186
193
logger := a .logger .With ().Str ("method" , method ).Logger ()
187
194
logger .Debug ().Msg ("API: Creating the scan" )
195
+
196
+ tracker := a .trackerFactory .GenerateTracker ()
197
+ tracker .Begin ("Snyk Code analysis for " + rootPath , "Retrieving results..." )
198
+
188
199
org := uuid .MustParse (orgId )
189
200
190
201
host := a .host (false )
191
202
a .logger .Debug ().Str ("host" , host ).Str ("workspaceId" , workspaceId ).Msg ("starting scan" )
192
203
193
204
client , err := orchestrationClient .NewClientWithResponses (host , orchestrationClient .WithHTTPClient (a .httpClient ))
194
-
195
205
if err != nil {
206
+ tracker .End (fmt .Sprintf ("Analysis failed: %v" , err ))
196
207
return nil , fmt .Errorf ("failed to create orchestrationClient: %w" , err )
197
208
}
198
209
210
+ scanJobId , err := a .triggerScan (ctx , client , org , workspaceId )
211
+ if err != nil {
212
+ tracker .End (fmt .Sprintf ("Analysis failed: %v" , err ))
213
+ return nil , err
214
+ }
215
+
216
+ response , err := a .pollScanForFindings (ctx , client , org , * scanJobId )
217
+ if err != nil {
218
+ tracker .End (fmt .Sprintf ("Analysis failed: %v" , err ))
219
+ return nil , err
220
+ }
221
+
222
+ tracker .End ("Analysis complete." )
223
+ return response , nil
224
+ }
225
+
226
+ func (a * analysisOrchestrator ) triggerScan (ctx context.Context , client * orchestrationClient.ClientWithResponses , org uuid.UUID , workspaceId string ) (* openapi_types.UUID , error ) {
199
227
flow := scans.Flow {}
200
- err = flow .UnmarshalJSON ([]byte (`{"name": "cli_test"}` ))
228
+ err : = flow .UnmarshalJSON ([]byte (`{"name": "cli_test"}` ))
201
229
if err != nil {
202
230
return nil , fmt .Errorf ("failed to create scan request: %w" , err )
203
231
}
@@ -242,7 +270,7 @@ func (a *analysisOrchestrator) RunAnalysis(ctx context.Context, orgId string, wo
242
270
switch createScanResponse .StatusCode () {
243
271
case 201 :
244
272
scanJobId = createScanResponse .ApplicationvndApiJSON201 .Data .Id
245
- a .logger .Debug ().Str ("host" , host ). Str ( " workspaceId" , workspaceId ).Msg ("starting scan" )
273
+ a .logger .Debug ().Str ("workspaceId" , workspaceId ).Msg ("starting scan" )
246
274
case 400 :
247
275
msg = createScanResponse .ApplicationvndApiJSON400 .Errors [0 ].Detail
248
276
case 401 :
@@ -260,12 +288,7 @@ func (a *analysisOrchestrator) RunAnalysis(ctx context.Context, orgId string, wo
260
288
return nil , errors .New (msg )
261
289
}
262
290
263
- response , err := a .pollScanForFindings (ctx , client , org , scanJobId )
264
- if err != nil {
265
- return nil , err
266
- }
267
-
268
- return response , nil
291
+ return & scanJobId , nil
269
292
}
270
293
271
294
func (a * analysisOrchestrator ) pollScanForFindings (ctx context.Context , client * orchestrationClient.ClientWithResponses , org uuid.UUID , scanJobId openapi_types.UUID ) (* sarif.SarifResponse , error ) {
@@ -279,7 +302,7 @@ func (a *analysisOrchestrator) pollScanForFindings(ctx context.Context, client *
279
302
for {
280
303
select {
281
304
case <- timeoutTimer .C :
282
- msg := "timeout requesting the ScanJobResult "
305
+ msg := "Snyk Code analysis timed out "
283
306
logger .Error ().Str ("scanJobId" , scanJobId .String ()).Msg (msg )
284
307
return nil , errors .New (msg )
285
308
case <- pollingTicker .C :
0 commit comments