Skip to content

Commit 9b4ad3d

Browse files
authored
Now using supported files SDK endpoint for file globs (#60)
* Now using supported files SDK endpoint for file globs
1 parent d94da02 commit 9b4ad3d

File tree

3 files changed

+67
-30
lines changed

3 files changed

+67
-30
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ dependencies = [
1313
'GitPython',
1414
'packaging',
1515
'python-dotenv',
16-
'socket-sdk-python>=2.0.8'
16+
'socket-sdk-python>=2.0.9'
1717
]
1818
readme = "README.md"
1919
description = "Socket Security CLI for CI/CD"

socketsecurity/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
__author__ = 'socket.dev'
2-
__version__ = '2.0.10'
2+
__version__ = '2.0.11'

socketsecurity/core/__init__.py

Lines changed: 65 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ def create_sbom_output(self, diff: Diff) -> dict:
123123
log.error(result.get("message", "No error message provided"))
124124
return {}
125125

126-
@staticmethod
127-
def find_files(path: str) -> List[str]:
126+
def find_files(self, path: str) -> List[str]:
128127
"""
129128
Finds supported manifest files in the given path.
130129
@@ -138,10 +137,19 @@ def find_files(path: str) -> List[str]:
138137
start_time = time.time()
139138
files = set()
140139

141-
for ecosystem in socket_globs:
142-
patterns = socket_globs[ecosystem]
143-
for file_name in patterns:
144-
pattern = Core.to_case_insensitive_regex(patterns[file_name]["pattern"])
140+
# Get supported patterns from the API
141+
try:
142+
patterns = self.get_supported_patterns()
143+
except Exception as e:
144+
log.error(f"Error getting supported patterns from API: {e}")
145+
log.warning("Falling back to local patterns")
146+
from .utils import socket_globs as fallback_patterns
147+
patterns = fallback_patterns
148+
149+
for ecosystem in patterns:
150+
ecosystem_patterns = patterns[ecosystem]
151+
for file_name in ecosystem_patterns:
152+
pattern = Core.to_case_insensitive_regex(ecosystem_patterns[file_name]["pattern"])
145153
file_path = f"{path}/**/{pattern}"
146154
#log.debug(f"Globbing {file_path}")
147155
glob_start = time.time()
@@ -164,6 +172,57 @@ def find_files(path: str) -> List[str]:
164172
log.debug(f"{len(files_list)} Files found ({total_time:.2f}s): {', '.join(files_list)}")
165173
return list(files)
166174

175+
def get_supported_patterns(self) -> Dict:
176+
"""
177+
Gets supported file patterns from the Socket API.
178+
179+
Returns:
180+
Dictionary of supported file patterns with 'general' key removed
181+
"""
182+
response = self.sdk.report.supported()
183+
if not response:
184+
log.error("Failed to get supported patterns from API")
185+
# Import the old patterns as fallback
186+
from .utils import socket_globs
187+
return socket_globs
188+
189+
# Remove the 'general' key if it exists
190+
if 'general' in response:
191+
response.pop('general')
192+
193+
# The response is already in the format we need
194+
return response
195+
196+
def has_manifest_files(self, files: list) -> bool:
197+
"""
198+
Checks if any files in the list are supported manifest files.
199+
200+
Args:
201+
files: List of file paths to check
202+
203+
Returns:
204+
True if any files match manifest patterns, False otherwise
205+
"""
206+
# Get supported patterns
207+
try:
208+
patterns = self.get_supported_patterns()
209+
except Exception as e:
210+
log.error(f"Error getting supported patterns from API: {e}")
211+
log.warning("Falling back to local patterns")
212+
from .utils import socket_globs as fallback_patterns
213+
patterns = fallback_patterns
214+
215+
for ecosystem in patterns:
216+
ecosystem_patterns = patterns[ecosystem]
217+
for file_name in ecosystem_patterns:
218+
pattern_str = ecosystem_patterns[file_name]["pattern"]
219+
for file in files:
220+
if "\\" in file:
221+
file = file.replace("\\", "/")
222+
if PurePath(file).match(pattern_str):
223+
return True
224+
return False
225+
167226
@staticmethod
168227
def to_case_insensitive_regex(input_string: str) -> str:
169228
"""
@@ -740,28 +799,6 @@ def save_file(file_name: str, content: str) -> None:
740799
log.error(f"Failed to save file {file_name}: {e}")
741800
raise
742801

743-
@staticmethod
744-
def has_manifest_files(files: list) -> bool:
745-
"""
746-
Checks if any files in the list are supported manifest files.
747-
748-
Args:
749-
files: List of file paths to check
750-
751-
Returns:
752-
True if any files match manifest patterns, False otherwise
753-
"""
754-
for ecosystem in socket_globs:
755-
patterns = socket_globs[ecosystem]
756-
for file_name in patterns:
757-
pattern = patterns[file_name]["pattern"]
758-
for file in files:
759-
if "\\" in file:
760-
file = file.replace("\\", "/")
761-
if PurePath(file).match(pattern):
762-
return True
763-
return False
764-
765802
@staticmethod
766803
def get_capabilities_for_added_packages(added_packages: Dict[str, Package]) -> Dict[str, List[str]]:
767804
"""

0 commit comments

Comments
 (0)