66from glob import glob
77from pathlib import PurePath
88from typing import BinaryIO , Dict , List , Optional , Tuple
9+ from itertools import chain
910
1011from socketdev import socketdev
1112from socketdev .fullscans import (
@@ -405,41 +406,26 @@ def get_added_and_removed_packages(self, head_full_scan_id: Optional[str], new_f
405406 log .info (f"Replaced: { len (diff_report .artifacts .replaced )} " )
406407 log .info (f"Updated: { len (diff_report .artifacts .updated )} " )
407408
408- added_artifacts = diff_report .artifacts .added + diff_report .artifacts .updated
409- removed_artifacts = diff_report .artifacts .removed + diff_report .artifacts .replaced
410-
411409 added_packages : Dict [str , Package ] = {}
412410 removed_packages : Dict [str , Package ] = {}
413411
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 ):
415414 try :
416- pkg = Package .from_diff_artifact (artifact )
415+ pkg = Package .from_socket_artifact (artifact )
417416 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 } " )
428420
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 ):
430423 try :
431- pkg = Package .from_diff_artifact (asdict ( artifact ) )
424+ pkg = Package .from_diff_artifact (artifact )
432425 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 } " )
443429
444430 return added_packages , removed_packages
445431
@@ -518,32 +504,38 @@ def create_diff_report(
518504 seen_new_packages = set ()
519505 seen_removed_packages = set ()
520506
507+ # Process added packages
521508 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 )
528518
529519 self .add_package_alerts_to_collection (
530520 package = package ,
531- alerts_collection = alerts_in_added_packages ,
532- packages = added_packages
521+ alerts_collection = alerts_in_added_packages
533522 )
534523
524+ # Process removed packages
535525 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 )
542535
543536 self .add_package_alerts_to_collection (
544537 package = package ,
545- alerts_collection = alerts_in_removed_packages ,
546- packages = removed_packages
538+ alerts_collection = alerts_in_removed_packages
547539 )
548540
549541 diff .new_alerts = Core .get_new_alerts (
@@ -552,7 +544,6 @@ def create_diff_report(
552544 )
553545
554546 diff .new_capabilities = Core .get_capabilities_for_added_packages (added_packages )
555-
556547 Core .add_purl_capabilities (diff )
557548
558549 return diff
@@ -647,29 +638,20 @@ def add_purl_capabilities(diff: Diff) -> None:
647638
648639 diff .new_packages = new_packages
649640
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."""
662643 default_props = type ('EmptyProps' , (), {
663644 'description' : "" ,
664645 'title' : "" ,
665646 'suggestion' : "" ,
666647 'nextStepTitle' : ""
667648 })()
668649
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+
671654 props = getattr (self .config .all_issues , alert .type , default_props )
672- introduced_by = self .get_source_data (package , packages )
673655
674656 issue_alert = Issue (
675657 pkg_type = package .type ,
@@ -684,7 +666,7 @@ def add_package_alerts_to_collection(self, package: Package, alerts_collection:
684666 title = props .title ,
685667 suggestion = props .suggestion ,
686668 next_step_title = props .nextStepTitle ,
687- introduced_by = introduced_by ,
669+ introduced_by = package . introduced_by ,
688670 purl = package .purl ,
689671 url = package .url
690672 )
@@ -693,13 +675,10 @@ def add_package_alerts_to_collection(self, package: Package, alerts_collection:
693675 action = self .config .security_policy [alert .type ]['action' ]
694676 setattr (issue_alert , action , True )
695677
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 )
703682
704683 @staticmethod
705684 def save_file (file_name : str , content : str ) -> None :
0 commit comments