@@ -467,7 +467,7 @@ def get_package_license_text(self, package: Package) -> str:
467
467
468
468
license_raw = package .license
469
469
data = self .sdk .licensemetadata .post ([license_raw ], {'includetext' : 'true' })
470
- license_str = data . data [0 ].license if data and len (data ) == 1 else ""
470
+ license_str = data [0 ].get ( 'text' ) if data and len (data ) == 1 else ""
471
471
return license_str
472
472
473
473
def get_repo_info (self , repo_slug : str , default_branch : str = "socket-default-branch" ) -> RepositoryInfo :
@@ -543,13 +543,41 @@ def update_package_values(pkg: Package) -> Package:
543
543
pkg .url += f"/{ pkg .name } /overview/{ pkg .version } "
544
544
return pkg
545
545
546
- def get_added_and_removed_packages (self , head_full_scan_id : str , new_full_scan_id : str ) -> Tuple [Dict [str , Package ], Dict [str , Package ]]:
546
+ def get_license_text_via_purl (self , packages : dict [str , Package ]) -> dict :
547
+ components = []
548
+ for purl in packages :
549
+ full_purl = f"pkg:/{ purl } "
550
+ components .append ({"purl" : full_purl })
551
+ results = self .sdk .purl .post (
552
+ license = True ,
553
+ components = components ,
554
+ licenseattrib = True ,
555
+ licensedetails = True
556
+ )
557
+ purl_packages = []
558
+ for result in results :
559
+ ecosystem = result ["type" ]
560
+ name = result ["name" ]
561
+ package_version = result ["version" ]
562
+ licenseDetails = result .get ("licenseDetails" )
563
+ licenseAttrib = result .get ("licenseAttrib" )
564
+ purl = f"{ ecosystem } /{ name } @{ package_version } "
565
+ if purl not in purl_packages :
566
+ packages [purl ].licenseAttrib = licenseAttrib
567
+ packages [purl ].licenseDetails = licenseDetails
568
+ return packages
569
+
570
+ def get_added_and_removed_packages (
571
+ self ,
572
+ head_full_scan_id : str ,
573
+ new_full_scan_id : str
574
+ ) -> Tuple [Dict [str , Package ], Dict [str , Package ], Dict [str , Package ]]:
547
575
"""
548
576
Get packages that were added and removed between scans.
549
577
550
578
Args:
551
- head_full_scan : Previous scan (may be None if first scan)
552
- head_full_scan_id : New scan just created
579
+ head_full_scan_id : Previous scan (maybe None if first scan)
580
+ new_full_scan_id : New scan just created
553
581
554
582
Returns:
555
583
Tuple of (added_packages, removed_packages) dictionaries
@@ -579,20 +607,36 @@ def get_added_and_removed_packages(self, head_full_scan_id: str, new_full_scan_i
579
607
580
608
added_artifacts = diff_report .artifacts .added + diff_report .artifacts .updated
581
609
removed_artifacts = diff_report .artifacts .removed + diff_report .artifacts .replaced
610
+ unchanged_artifacts = diff_report .artifacts .unchanged
582
611
583
612
added_packages : Dict [str , Package ] = {}
584
613
removed_packages : Dict [str , Package ] = {}
585
-
614
+ packages : Dict [ str , Package ] = {}
586
615
for artifact in added_artifacts :
587
616
try :
588
617
pkg = Package .from_diff_artifact (asdict (artifact ))
589
618
pkg = Core .update_package_values (pkg )
590
619
added_packages [artifact .id ] = pkg
620
+ full_purl = f"{ pkg .type } /{ pkg .purl } "
621
+ if full_purl not in packages :
622
+ packages [full_purl ] = pkg
591
623
except KeyError :
592
624
log .error (f"KeyError: Could not create package from added artifact { artifact .id } " )
593
625
log .error (f"Artifact details - name: { artifact .name } , version: { artifact .version } " )
594
626
log .error ("No matching packages found in new_full_scan" )
595
627
628
+ for artifact in unchanged_artifacts :
629
+ try :
630
+ pkg = Package .from_diff_artifact (asdict (artifact ))
631
+ pkg = Core .update_package_values (pkg )
632
+ full_purl = f"{ pkg .type } /{ pkg .purl } "
633
+ if full_purl not in packages :
634
+ packages [full_purl ] = pkg
635
+ except KeyError :
636
+ log .error (f"KeyError: Could not create package from unchanged artifact { artifact .id } " )
637
+ log .error (f"Artifact details - name: { artifact .name } , version: { artifact .version } " )
638
+ log .error ("No matching packages found in new_full_scan" )
639
+
596
640
for artifact in removed_artifacts :
597
641
try :
598
642
pkg = Package .from_diff_artifact (asdict (artifact ))
@@ -605,7 +649,8 @@ def get_added_and_removed_packages(self, head_full_scan_id: str, new_full_scan_i
605
649
log .error (f"Artifact details - name: { artifact .name } , version: { artifact .version } " )
606
650
log .error ("No matching packages found in head_full_scan" )
607
651
608
- return added_packages , removed_packages
652
+ packages = self .get_license_text_via_purl (packages )
653
+ return added_packages , removed_packages , packages
609
654
610
655
def create_new_diff (
611
656
self ,
@@ -665,9 +710,14 @@ def create_new_diff(
665
710
scans_ready = self .check_full_scans_status (head_full_scan_id , new_full_scan .id )
666
711
if scans_ready is False :
667
712
log .error (f"Full scans did not complete within { self .config .timeout } seconds" )
668
- added_packages , removed_packages = self .get_added_and_removed_packages (head_full_scan_id , new_full_scan .id )
713
+ (
714
+ added_packages ,
715
+ removed_packages ,
716
+ packages
717
+ ) = self .get_added_and_removed_packages (head_full_scan_id , new_full_scan .id )
669
718
670
719
diff = self .create_diff_report (added_packages , removed_packages )
720
+ diff .packages = packages
671
721
672
722
base_socket = "https://socket.dev/dashboard/org"
673
723
diff .id = new_full_scan .id
@@ -676,6 +726,7 @@ def create_new_diff(
676
726
if not params .include_license_details :
677
727
report_url += "?include_license_details=false"
678
728
diff .report_url = report_url
729
+ diff .new_scan_id = new_full_scan .id
679
730
680
731
if head_full_scan_id is not None :
681
732
diff .diff_url = f"{ base_socket } /{ self .config .org_slug } /diff/{ head_full_scan_id } /{ diff .id } "
0 commit comments