@@ -474,6 +474,72 @@ func restoreCampaigns(ctx context.Context, client graphql.Client, db string, ass
474474 return nil
475475}
476476
477+ // validateLibraryTestCases checks if a list of library test case IDs exist in the target VECTR instance.
478+ // It performs a query and specifically handles the GraphQL error case where some IDs are not found,
479+ // returning a detailed error message.
480+ func validateLibraryTestCases (ctx context.Context , client graphql.Client , libraryTestCaseIDs []string , templateAssessmentName string ) error {
481+ if len (libraryTestCaseIDs ) == 0 {
482+ return nil
483+ }
484+ // first time, we never really need to check the response, if the missing ids remain none,
485+ // we don't need to do anything
486+ _ , err := dao .GetLibraryTestCases (ctx , client , libraryTestCaseIDs )
487+ if err == nil {
488+ return nil
489+ }
490+
491+ var missing_ids []string
492+ gqlerrlist , ok := err .(gqlerror.List )
493+ if ! ok {
494+ return fmt .Errorf ("could not fetch library test cases for %s: %w" , templateAssessmentName , err )
495+ }
496+
497+ // the error type we expect only has one entry for this path
498+ if ! (len (gqlerrlist ) == 1 && gqlerrlist [0 ].Path .String () == "libraryTestcasesByIds" ) {
499+ if gqlObject , ok := gqlErrParse (err ); ok {
500+ slog .ErrorContext (ctx , "detailed error" , "error" , gqlObject )
501+ }
502+ return fmt .Errorf ("could not fetch library test cases for %s: %w" , templateAssessmentName , err )
503+ }
504+ // there should be an `ids` field in the extensions object
505+ rawids , ok := gqlerrlist [0 ].Extensions ["ids" ]
506+ if ! ok {
507+ if gqlObject , ok := gqlErrParse (err ); ok {
508+ slog .ErrorContext (ctx , "detailed error" , "error" , gqlObject )
509+ }
510+ return fmt .Errorf ("could not fetch library test cases for %s: %w" , templateAssessmentName , err )
511+ }
512+ // the `ids` filed should only have one entry
513+ ids , ok := rawids .([]any )
514+ if ! (ok && len (ids ) == 1 ) {
515+ if gqlObject , ok := gqlErrParse (err ); ok {
516+ slog .ErrorContext (ctx , "detailed error" , "error" , gqlObject )
517+ }
518+ return fmt .Errorf ("could not fetch library test cases for %s: %w" , templateAssessmentName , err )
519+ }
520+
521+ id := ids [0 ].(string )
522+ if ! strings .HasPrefix (id , "The following IDs were not valid" ) {
523+ if gqlObject , ok := gqlErrParse (err ); ok {
524+ slog .ErrorContext (ctx , "detailed error" , "error" , gqlObject )
525+ }
526+ return fmt .Errorf ("could not fetch library test cases for %s: %w" , templateAssessmentName , err )
527+ }
528+ // this is a case where we got an error back for an otherwise valid query, one or more of the ids are not valid
529+ mids , err := ParseLibraryTestcasesByIdsError (id )
530+ if err != nil {
531+ return fmt .Errorf ("could not fetch library test cases for %s: %w" , templateAssessmentName , err )
532+ }
533+ missing_ids = append (missing_ids , mids ... )
534+
535+ if len (missing_ids ) > 0 {
536+ slog .ErrorContext (ctx , "could not find all the ids in the instance" , "missing-ids" , missing_ids )
537+ return fmt .Errorf ("could not find all the ids in the instance, override templates to insert, missing id count: %d" , len (missing_ids ))
538+ }
539+
540+ return nil
541+ }
542+
477543func RestoreAssessment (ctx context.Context , client graphql.Client , db string , ad * AssessmentData , optionalParams * RestoreOptionalParams ) error {
478544 slog .InfoContext (ctx , "Starting RestoreAssessment" , "db" , db , "assessment_name" , ad .Assessment .Name )
479545
@@ -569,61 +635,8 @@ func RestoreAssessment(ctx context.Context, client graphql.Client, db string, ad
569635 }
570636 // now let's check the actual data
571637 ids := slices .Collect (maps .Keys (ad .LibraryTestCases ))
572- if len (ids ) > 0 {
573- missing_ids := []string {}
574- // first time, we never really need to check the response, if the missing ids remain none,
575- // we don't need to do anything
576- _ , err := dao .GetLibraryTestCases (ctx , client , ids )
577- if err != nil {
578- gqlerrlist , ok := err .(gqlerror.List )
579- if ! ok {
580- return fmt .Errorf ("could not fetch library test cases for %s: %w" , ad .TemplateAssessment , err )
581- }
582-
583- // the error type we expect only has one entry for this path
584- if ! (len (gqlerrlist ) == 1 && gqlerrlist [0 ].Path .String () == "libraryTestcasesByIds" ) {
585- if gqlObject , ok := gqlErrParse (err ); ok {
586- slog .ErrorContext (ctx , "detailed error" , "error" , gqlObject )
587- }
588- return fmt .Errorf ("could not fetch library test cases for %s: %w" , ad .TemplateAssessment , err )
589- }
590- // there should be an `ids` field in the extensions object
591- rawids , ok := gqlerrlist [0 ].Extensions ["ids" ]
592- if ! ok {
593- if gqlObject , ok := gqlErrParse (err ); ok {
594- slog .ErrorContext (ctx , "detailed error" , "error" , gqlObject )
595- }
596- return fmt .Errorf ("could not fetch library test cases for %s: %w" , ad .TemplateAssessment , err )
597- }
598- // the `ids` filed should only have one entry
599- ids , ok := rawids .([]any )
600- if ! (ok && len (ids ) == 1 ) {
601- if gqlObject , ok := gqlErrParse (err ); ok {
602- slog .ErrorContext (ctx , "detailed error" , "error" , gqlObject )
603- }
604- return fmt .Errorf ("could not fetch library test cases for %s: %w" , ad .TemplateAssessment , err )
605- }
606-
607- id := ids [0 ].(string )
608- if ! strings .HasPrefix (id , "The following IDs were not valid" ) {
609- if gqlObject , ok := gqlErrParse (err ); ok {
610- slog .ErrorContext (ctx , "detailed error" , "error" , gqlObject )
611- }
612- return fmt .Errorf ("could not fetch library test cases for %s: %w" , ad .TemplateAssessment , err )
613- }
614- // this is a case where we got an error back for an otherwise valid query, one or more of the ids are not valid
615- mids , err := ParseLibraryTestcasesByIdsError (id )
616- if err != nil {
617- return fmt .Errorf ("could not fetch library test cases for %s: %w" , ad .TemplateAssessment , err )
618- }
619- missing_ids = append (missing_ids , mids ... )
620- }
621- if len (missing_ids ) > 0 {
622- slog .ErrorContext (ctx , "could not find all the ids in the instance" , "missing-ids" , missing_ids )
623- return fmt .Errorf ("could not find all the ids in the instance, override templates to insert, missing id count: %d" , len (missing_ids ))
624-
625- }
626-
638+ if err := validateLibraryTestCases (ctx , client , ids , ad .TemplateAssessment ); err != nil {
639+ return err
627640 }
628641
629642 }
@@ -696,6 +709,18 @@ func RestoreCampaign(ctx context.Context, client graphql.Client, db string, ad *
696709 }
697710 targetAssessmentId := targetAssessment .Assessments .Nodes [0 ].Id
698711
712+ // Collect and validate library test case IDs for the specific campaign
713+ libraryTestCaseIDs := []string {}
714+ for _ , tc := range campaignToRestore .TestCases {
715+ if tc .LibraryTestCaseId != "" && tc .LibraryTestCaseId != "null" {
716+ libraryTestCaseIDs = append (libraryTestCaseIDs , tc .LibraryTestCaseId )
717+ }
718+ }
719+
720+ if err := validateLibraryTestCases (ctx , client , libraryTestCaseIDs , ad .TemplateAssessment ); err != nil {
721+ return err
722+ }
723+
699724 // Collect organizations for the specific campaign
700725 campaignOrgNames := make ([]string , 0 , len (campaignToRestore .Organizations ))
701726 for _ , org := range campaignToRestore .Organizations {
0 commit comments