@@ -52,8 +52,8 @@ type AnalysisOrchestrator interface {
52
52
RunAnalysis (ctx context.Context , orgId string , rootPath string , workspaceId string ) (* sarif.SarifResponse , error )
53
53
RunIncrementalAnalysis (ctx context.Context , orgId string , rootPath string , workspaceId string , limitToFiles []string ) (* sarif.SarifResponse , error )
54
54
55
- RunTest (ctx context.Context , orgId string , b bundle.Bundle , target scan.Target , reportingOptions AnalysisConfig ) (* sarif.SarifResponse , error )
56
- RunTestRemote (ctx context.Context , orgId string , interactionId string , reportingOptions AnalysisConfig ) (* sarif.SarifResponse , error )
55
+ RunTest (ctx context.Context , orgId string , b bundle.Bundle , target scan.Target , reportingOptions AnalysisConfig ) (* sarif.SarifResponse , * scan. ResultMetaData , error )
56
+ RunTestRemote (ctx context.Context , orgId string , reportingOptions AnalysisConfig ) (* sarif.SarifResponse , * scan. ResultMetaData , error )
57
57
}
58
58
59
59
type AnalysisConfig struct {
@@ -74,6 +74,8 @@ type analysisOrchestrator struct {
74
74
testType testModels.Scan
75
75
}
76
76
77
+ var _ AnalysisOrchestrator = (* analysisOrchestrator )(nil )
78
+
77
79
type OptionFunc func (* analysisOrchestrator )
78
80
79
81
func WithInstrumentor (instrumentor observability.Instrumentor ) func (* analysisOrchestrator ) {
@@ -485,24 +487,24 @@ func (a *analysisOrchestrator) host(isHidden bool) string {
485
487
return fmt .Sprintf ("%s/%s" , apiUrl , path )
486
488
}
487
489
488
- func (a * analysisOrchestrator ) createTestAndGetResults (ctx context.Context , orgId string , body * testApi.CreateTestApplicationVndAPIPlusJSONRequestBody , progressString string ) (* sarif.SarifResponse , error ) {
490
+ func (a * analysisOrchestrator ) createTestAndGetResults (ctx context.Context , orgId string , body * testApi.CreateTestApplicationVndAPIPlusJSONRequestBody , progressString string ) (* sarif.SarifResponse , * scan. ResultMetaData , error ) {
489
491
tracker := a .trackerFactory .GenerateTracker ()
490
492
tracker .Begin (progressString , "Retrieving results..." )
491
493
492
- innerFunction := func () (* sarif.SarifResponse , error ) {
494
+ innerFunction := func () (* sarif.SarifResponse , * scan. ResultMetaData , error ) {
493
495
params := testApi.CreateTestParams {Version : testApi .ApiVersion }
494
496
orgUuid := uuid .MustParse (orgId )
495
497
host := a .host (true )
496
498
497
499
client , err := testApi .NewClient (host , testApi .WithHTTPClient (a .httpClient ))
498
500
if err != nil {
499
- return nil , err
501
+ return nil , nil , err
500
502
}
501
503
502
504
// create test
503
505
resp , err := client .CreateTestWithApplicationVndAPIPlusJSONBody (ctx , orgUuid , & params , * body )
504
506
if err != nil {
505
- return nil , err
507
+ return nil , nil , err
506
508
}
507
509
508
510
parsedResponse , err := testApi .ParseCreateTestResponse (resp )
@@ -514,28 +516,28 @@ func (a *analysisOrchestrator) createTestAndGetResults(ctx context.Context, orgI
514
516
}()
515
517
if err != nil {
516
518
a .logger .Debug ().Msg (err .Error ())
517
- return nil , err
519
+ return nil , nil , err
518
520
}
519
521
520
522
switch parsedResponse .StatusCode () {
521
523
case http .StatusCreated :
522
524
// poll results
523
525
return a .pollTestForFindings (ctx , client , orgUuid , parsedResponse .ApplicationvndApiJSON201 .Data .Id )
524
526
}
525
- return nil , nil
527
+ return nil , nil , nil
526
528
}
527
529
528
- result , err := innerFunction ()
530
+ result , metadata , err := innerFunction ()
529
531
if err != nil {
530
532
tracker .End ("Analysis failed." )
531
533
} else {
532
534
tracker .End ("Analysis completed." )
533
535
}
534
536
535
- return result , err
537
+ return result , metadata , err
536
538
}
537
539
538
- func (a * analysisOrchestrator ) RunTest (ctx context.Context , orgId string , b bundle.Bundle , target scan.Target , reportingConfig AnalysisConfig ) (* sarif.SarifResponse , error ) {
540
+ func (a * analysisOrchestrator ) RunTest (ctx context.Context , orgId string , b bundle.Bundle , target scan.Target , reportingConfig AnalysisConfig ) (* sarif.SarifResponse , * scan. ResultMetaData , error ) {
539
541
var repoUrl * string = nil
540
542
if repoTarget , ok := target .(* scan.RepositoryTarget ); ok {
541
543
tmp := repoTarget .GetRepositoryUrl ()
@@ -553,9 +555,9 @@ func (a *analysisOrchestrator) RunTest(ctx context.Context, orgId string, b bund
553
555
return a .createTestAndGetResults (ctx , orgId , body , "Snyk Code analysis for " + target .GetPath ())
554
556
}
555
557
556
- func (a * analysisOrchestrator ) RunTestRemote (ctx context.Context , orgId string , interactionId string , cfg AnalysisConfig ) (* sarif.SarifResponse , error ) {
558
+ func (a * analysisOrchestrator ) RunTestRemote (ctx context.Context , orgId string , cfg AnalysisConfig ) (* sarif.SarifResponse , * scan. ResultMetaData , error ) {
557
559
if cfg .ProjectId == nil || cfg .CommitId == nil {
558
- return nil , errors .New ("projectId and commitId are required" )
560
+ return nil , nil , errors .New ("projectId and commitId are required" )
559
561
}
560
562
561
563
legacyScmProject := testApi .NewTestInputLegacyScmProject (* cfg .ProjectId , * cfg .CommitId )
@@ -569,7 +571,7 @@ func (a *analysisOrchestrator) RunTestRemote(ctx context.Context, orgId string,
569
571
return a .createTestAndGetResults (ctx , orgId , body , "Snyk Code analysis for remote project" )
570
572
}
571
573
572
- func (a * analysisOrchestrator ) pollTestForFindings (ctx context.Context , client * testApi.Client , org uuid.UUID , testId openapi_types.UUID ) (* sarif.SarifResponse , error ) {
574
+ func (a * analysisOrchestrator ) pollTestForFindings (ctx context.Context , client * testApi.Client , org uuid.UUID , testId openapi_types.UUID ) (* sarif.SarifResponse , * scan. ResultMetaData , error ) {
573
575
method := "analysis.pollTestForFindings"
574
576
logger := a .logger .With ().Str ("method" , method ).Logger ()
575
577
@@ -582,24 +584,24 @@ func (a *analysisOrchestrator) pollTestForFindings(ctx context.Context, client *
582
584
case <- timeoutTimer .C :
583
585
msg := "Snyk Code analysis timed out"
584
586
logger .Error ().Str ("scanJobId" , testId .String ()).Msg (msg )
585
- return nil , errors .New (msg )
587
+ return nil , nil , errors .New (msg )
586
588
case <- pollingTicker .C :
587
- findingsUrl , complete , err := a .retrieveTestURL (ctx , client , org , testId )
589
+ resultMetaData , complete , err := a .retrieveTestURL (ctx , client , org , testId )
588
590
if err != nil {
589
- return nil , err
591
+ return nil , nil , err
590
592
}
591
593
if complete {
592
- findings , findingsErr := a .retrieveFindings (ctx , testId , findingsUrl )
594
+ findings , findingsErr := a .retrieveFindings (ctx , testId , resultMetaData . FindingsUrl )
593
595
if findingsErr != nil {
594
- return nil , findingsErr
596
+ return nil , nil , findingsErr
595
597
}
596
- return findings , nil
598
+ return findings , resultMetaData , nil
597
599
}
598
600
}
599
601
}
600
602
}
601
603
602
- func (a * analysisOrchestrator ) retrieveTestURL (ctx context.Context , client * testApi.Client , org uuid.UUID , testId openapi_types.UUID ) (url string , completed bool , err error ) {
604
+ func (a * analysisOrchestrator ) retrieveTestURL (ctx context.Context , client * testApi.Client , org uuid.UUID , testId openapi_types.UUID ) (resultMetaData * scan. ResultMetaData , completed bool , err error ) {
603
605
method := "analysis.retrieveTestURL"
604
606
logger := a .logger .With ().Str ("method" , method ).Logger ()
605
607
logger .Debug ().Msg ("retrieving Test URL" )
@@ -612,7 +614,7 @@ func (a *analysisOrchestrator) retrieveTestURL(ctx context.Context, client *test
612
614
)
613
615
if err != nil {
614
616
logger .Err (err ).Str ("testId" , testId .String ()).Msg ("error requesting the ScanJobResult" )
615
- return "" , false , err
617
+ return nil , false , err
616
618
}
617
619
defer func () {
618
620
closeErr := httpResponse .Body .Close ()
@@ -623,33 +625,42 @@ func (a *analysisOrchestrator) retrieveTestURL(ctx context.Context, client *test
623
625
624
626
parsedResponse , err := testApi .ParseGetTestResultResponse (httpResponse )
625
627
if err != nil {
626
- return "" , false , err
628
+ return nil , false , err
627
629
}
628
630
629
631
switch parsedResponse .StatusCode () {
630
632
case 200 :
631
633
stateDiscriminator , stateError := parsedResponse .ApplicationvndApiJSON200 .Data .Attributes .Discriminator ()
632
634
if stateError != nil {
633
- return "" , false , stateError
635
+ return nil , false , stateError
634
636
}
635
637
636
638
switch stateDiscriminator {
637
639
case string (testModels .TestAcceptedStateStatusAccepted ):
638
640
fallthrough
639
641
case string (testModels .TestInProgressStateStatusInProgress ):
640
- return "" , false , nil
642
+ return nil , false , nil
641
643
case string (testModels .TestCompletedStateStatusCompleted ):
642
644
testCompleted , stateCompleteError := parsedResponse .ApplicationvndApiJSON200 .Data .Attributes .AsTestCompletedState ()
643
645
if stateCompleteError != nil {
644
- return "" , false , stateCompleteError
646
+ return nil , false , stateCompleteError
645
647
}
646
648
649
+ tes := "/org/team-cli-testing/project/ff7a6ceb-fab5-4f68-bdbf-4dbc919e8074/history/65e25f20-af06-45ff-8515-40aea39af878"
650
+
647
651
findingsUrl := a .host (true ) + testCompleted .Documents .EnrichedSarif + "?version=" + testApi .DocumentApiVersion
648
- return findingsUrl , true , nil
652
+ result := & scan.ResultMetaData {
653
+ FindingsUrl : findingsUrl ,
654
+ WebUiUrl : tes ,
655
+ }
656
+ if testCompleted .Results .Webui != nil && testCompleted .Results .Webui .Link != nil {
657
+ result .WebUiUrl = * testCompleted .Results .Webui .Link
658
+ }
659
+ return result , true , nil
649
660
default :
650
- return "" , false , fmt .Errorf ("unexpected test status \" %s\" " , stateDiscriminator )
661
+ return nil , false , fmt .Errorf ("unexpected test status \" %s\" " , stateDiscriminator )
651
662
}
652
663
default :
653
- return "" , false , fmt .Errorf ("unexpected response status \" %d\" " , parsedResponse .StatusCode ())
664
+ return nil , false , fmt .Errorf ("unexpected response status \" %d\" " , parsedResponse .StatusCode ())
654
665
}
655
666
}
0 commit comments