6
6
from glob import glob
7
7
from pathlib import PurePath
8
8
from typing import BinaryIO , Dict , List , Optional , Tuple
9
+ from itertools import chain
9
10
10
11
from socketdev import socketdev
11
12
from socketdev .fullscans import (
@@ -405,41 +406,26 @@ def get_added_and_removed_packages(self, head_full_scan_id: Optional[str], new_f
405
406
log .info (f"Replaced: { len (diff_report .artifacts .replaced )} " )
406
407
log .info (f"Updated: { len (diff_report .artifacts .updated )} " )
407
408
408
- added_artifacts = diff_report .artifacts .added + diff_report .artifacts .updated
409
- removed_artifacts = diff_report .artifacts .removed + diff_report .artifacts .replaced
410
-
411
409
added_packages : Dict [str , Package ] = {}
412
410
removed_packages : Dict [str , Package ] = {}
413
411
414
- for artifact in added_artifacts :
412
+ # Process added and updated artifacts
413
+ for artifact in chain (diff_report .artifacts .added , diff_report .artifacts .updated ):
415
414
try :
416
- pkg = Package .from_diff_artifact (artifact )
415
+ pkg = Package .from_socket_artifact (artifact )
417
416
added_packages [artifact .id ] = pkg
418
- except KeyError :
419
- log .error (f"KeyError: Could not create package from added artifact { artifact .id } " )
420
- log .error (f"Artifact details - name: { artifact .name } , version: { artifact .version } " )
421
- matches = [p for p in added_artifacts .values () if p .name == artifact .name and p .version == artifact .version ]
422
- if matches :
423
- log .error (f"Found { len (matches )} packages with matching name/version:" )
424
- for m in matches :
425
- log .error (f" ID: { m .id } , name: { m .name } , version: { m .version } " )
426
- else :
427
- log .error ("No matching packages found in new_full_scan" )
417
+ except KeyError as e :
418
+ log .error (f"KeyError creating package from added artifact { artifact .id } : { e } " )
419
+ log .error (f"Artifact: name={ artifact .name } , version={ artifact .version } " )
428
420
429
- for artifact in removed_artifacts :
421
+ # Process removed and replaced artifacts
422
+ for artifact in chain (diff_report .artifacts .removed , diff_report .artifacts .replaced ):
430
423
try :
431
- pkg = Package .from_diff_artifact (asdict ( artifact ) )
424
+ pkg = Package .from_diff_artifact (artifact )
432
425
removed_packages [artifact .id ] = pkg
433
- except KeyError :
434
- log .error (f"KeyError: Could not create package from removed artifact { artifact .id } " )
435
- log .error (f"Artifact details - name: { artifact .name } , version: { artifact .version } " )
436
- matches = [p for p in removed_artifacts .values () if p .name == artifact .name and p .version == artifact .version ]
437
- if matches :
438
- log .error (f"Found { len (matches )} packages with matching name/version:" )
439
- for m in matches :
440
- log .error (f" ID: { m .id } , name: { m .name } , version: { m .version } " )
441
- else :
442
- log .error ("No matching packages found in head_full_scan" )
426
+ except KeyError as e :
427
+ log .error (f"KeyError creating package from removed artifact { artifact .id } : { e } " )
428
+ log .error (f"Artifact: name={ artifact .name } , version={ artifact .version } " )
443
429
444
430
return added_packages , removed_packages
445
431
@@ -518,32 +504,38 @@ def create_diff_report(
518
504
seen_new_packages = set ()
519
505
seen_removed_packages = set ()
520
506
507
+ # Process added packages
521
508
for package_id , package in added_packages .items ():
522
- purl = Core .create_purl (package_id , added_packages )
523
- base_purl = f"{ purl .ecosystem } /{ purl .name } @{ purl .version } "
524
-
525
- if (not direct_only or package .direct ) and base_purl not in seen_new_packages :
526
- diff .new_packages .append (purl )
527
- seen_new_packages .add (base_purl )
509
+ # Calculate source data once per package
510
+ package .introduced_by = self .get_source_data (package , added_packages )
511
+
512
+ if not direct_only or package .direct :
513
+ base_purl = f"{ package .type } /{ package .name } @{ package .version } "
514
+ if base_purl not in seen_new_packages :
515
+ purl = Core .create_purl (package_id , added_packages )
516
+ diff .new_packages .append (purl )
517
+ seen_new_packages .add (base_purl )
528
518
529
519
self .add_package_alerts_to_collection (
530
520
package = package ,
531
- alerts_collection = alerts_in_added_packages ,
532
- packages = added_packages
521
+ alerts_collection = alerts_in_added_packages
533
522
)
534
523
524
+ # Process removed packages
535
525
for package_id , package in removed_packages .items ():
536
- purl = Core .create_purl (package_id , removed_packages )
537
- base_purl = f"{ purl .ecosystem } /{ purl .name } @{ purl .version } "
538
-
539
- if (not direct_only or package .direct ) and base_purl not in seen_removed_packages :
540
- diff .removed_packages .append (purl )
541
- seen_removed_packages .add (base_purl )
526
+ # Calculate source data once per package
527
+ package .introduced_by = self .get_source_data (package , removed_packages )
528
+
529
+ if not direct_only or package .direct :
530
+ base_purl = f"{ package .type } /{ package .name } @{ package .version } "
531
+ if base_purl not in seen_removed_packages :
532
+ purl = Core .create_purl (package_id , removed_packages )
533
+ diff .removed_packages .append (purl )
534
+ seen_removed_packages .add (base_purl )
542
535
543
536
self .add_package_alerts_to_collection (
544
537
package = package ,
545
- alerts_collection = alerts_in_removed_packages ,
546
- packages = removed_packages
538
+ alerts_collection = alerts_in_removed_packages
547
539
)
548
540
549
541
diff .new_alerts = Core .get_new_alerts (
@@ -552,7 +544,6 @@ def create_diff_report(
552
544
)
553
545
554
546
diff .new_capabilities = Core .get_capabilities_for_added_packages (added_packages )
555
-
556
547
Core .add_purl_capabilities (diff )
557
548
558
549
return diff
@@ -647,29 +638,20 @@ def add_purl_capabilities(diff: Diff) -> None:
647
638
648
639
diff .new_packages = new_packages
649
640
650
- def add_package_alerts_to_collection (self , package : Package , alerts_collection : dict , packages : dict ) -> dict :
651
- """
652
- Processes alerts from a package and adds them to a shared alerts collection.
653
-
654
- Args:
655
- package: Package to process alerts from
656
- alerts_collection: Dictionary to store processed alerts
657
- packages: Dictionary of all packages for dependency lookup
658
-
659
- Returns:
660
- Updated alerts collection dictionary
661
- """
641
+ def add_package_alerts_to_collection (self , package : Package , alerts_collection : dict ) -> None :
642
+ """Processes alerts from a package and adds them to a shared alerts collection."""
662
643
default_props = type ('EmptyProps' , (), {
663
644
'description' : "" ,
664
645
'title' : "" ,
665
646
'suggestion' : "" ,
666
647
'nextStepTitle' : ""
667
648
})()
668
649
669
- for alert_item in package .alerts :
670
- alert = Alert (** alert_item )
650
+ for alert in package .alerts :
651
+ if alert .type == 'licenseSpdxDisj' :
652
+ continue
653
+
671
654
props = getattr (self .config .all_issues , alert .type , default_props )
672
- introduced_by = self .get_source_data (package , packages )
673
655
674
656
issue_alert = Issue (
675
657
pkg_type = package .type ,
@@ -684,7 +666,7 @@ def add_package_alerts_to_collection(self, package: Package, alerts_collection:
684
666
title = props .title ,
685
667
suggestion = props .suggestion ,
686
668
next_step_title = props .nextStepTitle ,
687
- introduced_by = introduced_by ,
669
+ introduced_by = package . introduced_by ,
688
670
purl = package .purl ,
689
671
url = package .url
690
672
)
@@ -693,13 +675,10 @@ def add_package_alerts_to_collection(self, package: Package, alerts_collection:
693
675
action = self .config .security_policy [alert .type ]['action' ]
694
676
setattr (issue_alert , action , True )
695
677
696
- if issue_alert .type != 'licenseSpdxDisj' :
697
- if issue_alert .key not in alerts_collection :
698
- alerts_collection [issue_alert .key ] = [issue_alert ]
699
- else :
700
- alerts_collection [issue_alert .key ].append (issue_alert )
701
-
702
- return alerts_collection
678
+ if alert .key not in alerts_collection :
679
+ alerts_collection [alert .key ] = [issue_alert ]
680
+ else :
681
+ alerts_collection [alert .key ].append (issue_alert )
703
682
704
683
@staticmethod
705
684
def save_file (file_name : str , content : str ) -> None :
0 commit comments