Skip to content

Commit 53e5b2a

Browse files
committed
Fixing compatibility drift between CLI <> SDK surfaced by test failures
Signed-off-by: lelia <lelia@socket.dev>
1 parent b6d31a1 commit 53e5b2a

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

socketsecurity/core/__init__.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,16 @@ def get_org_id_slug(self) -> Tuple[str, str]:
8888
return org_id, organizations[org_id]['slug']
8989
return None, None
9090

91-
def get_sbom_data(self, full_scan_id: str) -> List[SocketArtifact]:
92-
"""Returns the list of SBOM artifacts for a full scan."""
91+
def get_sbom_data(self, full_scan_id: str) -> Dict[str, SocketArtifact]:
92+
"""Returns SBOM artifacts for a full scan keyed by artifact ID."""
9393
response = self.sdk.fullscans.stream(self.config.org_slug, full_scan_id, use_types=True)
94-
artifacts: List[SocketArtifact] = []
9594
if not response.success:
9695
log.debug(f"Failed to get SBOM data for full-scan {full_scan_id}")
9796
log.debug(response.message)
9897
return {}
9998
if not hasattr(response, "artifacts") or not response.artifacts:
100-
return artifacts
101-
for artifact_id in response.artifacts:
102-
artifacts.append(response.artifacts[artifact_id])
103-
return artifacts
99+
return {}
100+
return response.artifacts
104101

105102
def get_sbom_data_list(self, artifacts_dict: Dict[str, SocketArtifact]) -> list[SocketArtifact]:
106103
"""Converts artifacts dictionary to a list."""

socketsecurity/core/classes.py

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import json
22
from dataclasses import dataclass, field
3-
from typing import Dict, List, TypedDict, Any, Optional
3+
from typing import Dict, List, Optional, TypedDict
44

5-
from socketdev.fullscans import FullScanMetadata, SocketArtifact, SocketArtifactLink, DiffType, SocketManifestReference, SocketScore, SocketAlert
5+
from socketdev.fullscans import (
6+
FullScanMetadata,
7+
SocketAlert,
8+
SocketArtifact,
9+
SocketArtifactLink,
10+
SocketManifestReference,
11+
SocketScore,
12+
)
613

714
__all__ = [
815
"Report",
@@ -109,8 +116,8 @@ class Package():
109116
type: str
110117
name: str
111118
version: str
112-
release: str
113-
diffType: str
119+
release: Optional[str] = None
120+
diffType: Optional[str] = None
114121
id: str
115122
author: List[str] = field(default_factory=list)
116123
score: SocketScore
@@ -158,6 +165,8 @@ def from_socket_artifact(cls, data: dict) -> "Package":
158165
name=data["name"],
159166
version=data["version"],
160167
type=data["type"],
168+
release=data.get("release"),
169+
diffType=data.get("diffType"),
161170
score=data["score"],
162171
alerts=data["alerts"],
163172
author=data.get("author", []),
@@ -187,10 +196,36 @@ def from_diff_artifact(cls, data: dict) -> "Package":
187196
Raises:
188197
ValueError: If reference data cannot be found in DiffArtifact
189198
"""
199+
diff_type = data.get("diffType")
200+
if hasattr(diff_type, "value"):
201+
diff_type = diff_type.value
202+
203+
# Newer API responses may provide flattened diff artifacts without refs.
204+
if "topLevelAncestors" in data or (not data.get("head") and not data.get("base")):
205+
return cls(
206+
id=data["id"],
207+
name=data["name"],
208+
version=data["version"],
209+
type=data["type"],
210+
score=data.get("score", data.get("scores", {})),
211+
alerts=data.get("alerts", []),
212+
author=data.get("author", []),
213+
size=data.get("size"),
214+
license=data.get("license"),
215+
topLevelAncestors=data.get("topLevelAncestors", []),
216+
direct=data.get("direct", True),
217+
manifestFiles=data.get("manifestFiles", []),
218+
dependencies=data.get("dependencies"),
219+
artifact=data.get("artifact"),
220+
namespace=data.get("namespace"),
221+
release=data.get("release"),
222+
diffType=diff_type,
223+
)
224+
190225
ref = None
191-
if data["diffType"] in ["added", "updated", "unchanged"] and data.get("head"):
226+
if diff_type in ["added", "updated", "unchanged"] and data.get("head"):
192227
ref = data["head"][0]
193-
elif data["diffType"] in ["removed", "replaced"] and data.get("base"):
228+
elif diff_type in ["removed", "replaced"] and data.get("base"):
194229
ref = data["base"][0]
195230

196231
if not ref:
@@ -201,8 +236,8 @@ def from_diff_artifact(cls, data: dict) -> "Package":
201236
name=data["name"],
202237
version=data["version"],
203238
type=data["type"],
204-
score=data["score"],
205-
alerts=data["alerts"],
239+
score=data.get("score", data.get("scores", {})),
240+
alerts=data.get("alerts", []),
206241
author=data.get("author", []),
207242
size=data.get("size"),
208243
license=data.get("license"),
@@ -213,7 +248,7 @@ def from_diff_artifact(cls, data: dict) -> "Package":
213248
artifact=ref.get("artifact"),
214249
namespace=data.get('namespace', None),
215250
release=ref.get("release", None),
216-
diffType=ref.get("diffType", None),
251+
diffType=ref.get("diffType", diff_type),
217252
)
218253

219254
class Issue:

0 commit comments

Comments
 (0)