GeezerVison is showing github.com — fix: bump Socket SDK version, handle missing diff scores (#193) · SocketDev/socket-python-cli@206efe9
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: pyproject.toml +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,7 +6,7 @@ build-backend = "hatchling.build" | 6 | 6 |
| 7 | 7 | [project] | 8 | 8 | name = "socketsecurity" | 9 | | -version = "2.2.85" | | 9 | +version = "2.2.86" | 10 | 10 | requires-python = ">= 3.11" | 11 | 11 | license = {"file" = "LICENSE"} | 12 | 12 | dependencies = [ | @@ -16,7 +16,7 @@ dependencies = [ | 16 | 16 | 'GitPython', | 17 | 17 | 'packaging', | 18 | 18 | 'python-dotenv', | 19 | | - "socketdev>=3.0.32,<4.0.0", | | 19 | + "socketdev>=3.0.33,<4.0.0", | 20 | 20 | "bs4>=0.0.2", | 21 | 21 | "markdown>=3.10", | 22 | 22 | ] | |
Collapse file Copy file name to clipboard +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | __author__ = 'socket.dev' | 2 | | -__version__ = '2.2.85' | | 2 | +__version__ = '2.2.86' | 3 | 3 | USER_AGENT = f'SocketPythonCLI/{__version__}' |
Collapse file Copy file name to clipboardExpand all lines: socketsecurity/core/classes.py +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -207,7 +207,7 @@ def from_diff_artifact(cls, data: dict) -> "Package": | 207 | 207 | name=data["name"], | 208 | 208 | version=data["version"], | 209 | 209 | type=data["type"], | 210 | | - score=data.get("score", data.get("scores", {})), | | 210 | + score=data.get("score") or data.get("scores") or {}, | 211 | 211 | alerts=data.get("alerts", []), | 212 | 212 | author=data.get("author", []), | 213 | 213 | size=data.get("size"), | @@ -236,7 +236,7 @@ def from_diff_artifact(cls, data: dict) -> "Package": | 236 | 236 | name=data["name"], | 237 | 237 | version=data["version"], | 238 | 238 | type=data["type"], | 239 | | - score=data.get("score", data.get("scores", {})), | | 239 | + score=data.get("score") or data.get("scores") or {}, | 240 | 240 | alerts=data.get("alerts", []), | 241 | 241 | author=data.get("author", []), | 242 | 242 | size=data.get("size"), | @@ -448,6 +448,8 @@ def __init__(self, **kwargs): | 448 | 448 | self.capabilities = [] | 449 | 449 | if not hasattr(self, "is_new"): | 450 | 450 | self.is_new = False | | 451 | + if not hasattr(self, "scores") or self.scores is None: | | 452 | + self.scores = {} | 451 | 453 | self.author_url = Purl.generate_author_data(self.author, self.ecosystem) | 452 | 454 |
| 453 | 455 | @staticmethod | |
Collapse file Copy file name to clipboardExpand all lines: socketsecurity/core/messages.py +20-5Lines changed: 20 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1179,12 +1179,27 @@ def score_to_badge(score): | 1179 | 1179 | score_percent = int(score * 100) # Convert to integer percentage | 1180 | 1180 | return f"[]({added.url})" | 1181 | 1181 |
| | 1182 | + def get_score_for_badge(score_name: str) -> float: | | 1183 | + scores = getattr(added, "scores", None) | | 1184 | + if isinstance(scores, dict): | | 1185 | + raw_score = scores.get(score_name) | | 1186 | + else: | | 1187 | + raw_score = getattr(scores, score_name, None) if scores is not None else None | | 1188 | + | | 1189 | + if raw_score is None: | | 1190 | + return 1.0 | | 1191 | + | | 1192 | + score = float(raw_score) | | 1193 | + if score > 1: | | 1194 | + score = score / 100 | | 1195 | + return max(0.0, min(score, 1.0)) | | 1196 | + | 1182 | 1197 | # Generate badges for each score type | 1183 | | - supply_chain_risk_badge = score_to_badge(added.scores.get("supplyChain", 100)) | 1184 | | - vulnerability_badge = score_to_badge(added.scores.get("vulnerability", 100)) | 1185 | | - quality_badge = score_to_badge(added.scores.get("quality", 100)) | 1186 | | - maintenance_badge = score_to_badge(added.scores.get("maintenance", 100)) | 1187 | | - license_badge = score_to_badge(added.scores.get("license", 100)) | | 1198 | + supply_chain_risk_badge = score_to_badge(get_score_for_badge("supplyChain")) | | 1199 | + vulnerability_badge = score_to_badge(get_score_for_badge("vulnerability")) | | 1200 | + quality_badge = score_to_badge(get_score_for_badge("quality")) | | 1201 | + maintenance_badge = score_to_badge(get_score_for_badge("maintenance")) | | 1202 | + license_badge = score_to_badge(get_score_for_badge("license")) | 1188 | 1203 |
| 1189 | 1204 | # Add the row for this package | 1190 | 1205 | row = [ | |
Collapse file Copy file name to clipboard +67Lines changed: 67 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +from socketsecurity.core.classes import Diff, Package, Purl | | 2 | +from socketsecurity.core.messages import Messages | | 3 | + | | 4 | + | | 5 | +def _make_purl(name: str, scores) -> Purl: | | 6 | + return Purl( | | 7 | + id=f"pkg:npm/{name}@1.0.0", | | 8 | + name=name, | | 9 | + version="1.0.0", | | 10 | + ecosystem="npm", | | 11 | + direct=True, | | 12 | + introduced_by=[("direct", "package.json")], | | 13 | + author=["test-author"], | | 14 | + size=1000, | | 15 | + transitives=0, | | 16 | + url=f"https://socket.dev/npm/package/{name}/overview/1.0.0", | | 17 | + purl=f"pkg:npm/{name}@1.0.0", | | 18 | + scores=scores, | | 19 | + ) | | 20 | + | | 21 | + | | 22 | +def test_package_from_diff_artifact_normalizes_null_score(): | | 23 | + package = Package.from_diff_artifact( | | 24 | + { | | 25 | + | | 26 | + "name": "example", | | 27 | + "version": "1.0.0", | | 28 | + "type": "npm", | | 29 | + "diffType": "added", | | 30 | + "score": None, | | 31 | + "alerts": [], | | 32 | + "author": [], | | 33 | + "topLevelAncestors": [], | | 34 | + "direct": True, | | 35 | + "manifestFiles": [], | | 36 | + } | | 37 | + ) | | 38 | + | | 39 | + assert package.score == {} | | 40 | + | | 41 | + | | 42 | +def test_dependency_overview_template_defaults_missing_or_null_scores(tmp_path, monkeypatch): | | 43 | + monkeypatch.chdir(tmp_path) | | 44 | + | | 45 | + diff = Diff( | | 46 | + id="test-diff", | | 47 | + diff_url="https://socket.dev/test-diff", | | 48 | + new_packages=[ | | 49 | + _make_purl("missing-scores", None), | | 50 | + _make_purl( | | 51 | + "partial-scores", | | 52 | + { | | 53 | + "supplyChain": 0.42, | | 54 | + "vulnerability": None, | | 55 | + }, | | 56 | + ), | | 57 | + ], | | 58 | + removed_packages=[], | | 59 | + new_alerts=[], | | 60 | + ) | | 61 | + | | 62 | + comment = Messages.dependency_overview_template(diff) | | 63 | + | | 64 | + assert "Socket Security: Dependency Overview" in comment | | 65 | + assert "score-42.svg" in comment | | 66 | + assert "score-100.svg" in comment | | 67 | + assert "score-10000.svg" not in comment |
Collapse file Copy file name to clipboardExpand all lines: uv.lock +5-5Lines changed: 5 additions & 5 deletions
|