Skip to content

Commit 312dcf9

Browse files
committed
refactor(cli): reuse sub-path discovery results and clarify scan routing
The --sub-path routing pre-check walked every selected path to decide whether any manifests existed, then discarded the result so scan creation walked the same paths again. Retain and reuse it. Apply --excluded-ecosystems before the pre-check rather than after, so every find_files() call in a run sees the same ecosystem filter. Add an INFO duration for CLI run registration, and replace the "No Manifest files changed" line with wording that describes the decision being made: no supported manifest was detected in the changed-file set, so a full report is created. Scan-routing semantics are unchanged. Ref: CE-379
1 parent 33d616c commit 312dcf9

3 files changed

Lines changed: 45 additions & 18 deletions

File tree

socketsecurity/core/streaming.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"""
1414

1515
import logging
16+
import time
1617
from typing import Optional
1718

1819
from .cli_client import CliClient
@@ -49,12 +50,17 @@ def set_report_run_id(self, report_run_id: Optional[str]) -> None:
4950
self._report_run_id = report_run_id
5051

5152
def __enter__(self) -> "StreamingLogs":
53+
registration_start = time.perf_counter()
5254
self._run_id = register_cli_run(
5355
self._client,
5456
client_version=self._client_version,
5557
upload_logs=self._upload_logs,
5658
)
5759
cli_logger = self._loggers[0]
60+
cli_logger.info(
61+
"CLI run registration completed in "
62+
f"{time.perf_counter() - registration_start:.2f}s"
63+
)
5864
if not self._run_id:
5965
cli_logger.debug("server log streaming not active for this run")
6066
return self

socketsecurity/socketcli.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ def main_code():
207207
if dirs_to_include:
208208
core.config.excluded_dirs = set(core.config.excluded_dirs) - dirs_to_include
209209
log.debug(f"Re-including normally-excluded directories in scan: {sorted(dirs_to_include)}")
210+
if config.excluded_ecosystems:
211+
core.config.excluded_ecosystems = list(config.excluded_ecosystems)
210212

211213
# Check for required dependencies if reachability analysis is enabled
212214
if config.reach:
@@ -292,6 +294,9 @@ def main_code():
292294
facts_file_to_submit = None
293295
# Variable to track SBOM files to submit when using --reach-use-only-pregenerated-sboms
294296
sbom_files_to_submit = None
297+
# Manifest results retained from the --sub-path routing pre-check. Reusing
298+
# these avoids walking every selected sub-path again during scan creation.
299+
discovered_scan_files = None
295300

296301
# Git setup
297302
is_repo = False
@@ -534,14 +539,18 @@ def main_code():
534539
# Override file checking to look in the scan paths instead
535540
# Get manifest files from all scan paths
536541
try:
537-
all_scan_files = []
542+
discovered_scan_files = []
538543
for scan_path in scan_paths:
539544
scan_files = core.find_files(scan_path)
540-
all_scan_files.extend(scan_files)
541-
has_supported_files = len(all_scan_files) > 0
542-
log.debug(f"Found {len(all_scan_files)} manifest files across {len(scan_paths)} scan paths")
545+
discovered_scan_files.extend(scan_files)
546+
has_supported_files = len(discovered_scan_files) > 0
547+
log.debug(
548+
f"Found {len(discovered_scan_files)} manifest files across "
549+
f"{len(scan_paths)} scan paths"
550+
)
543551
except Exception as e:
544552
log.debug(f"Error finding files in scan paths: {e}")
553+
discovered_scan_files = None
545554
has_supported_files = False
546555

547556
# Case 3: If no supported files or files are empty, force API mode (no PR comments)
@@ -564,8 +573,6 @@ def main_code():
564573
org_slug = core.config.org_slug
565574
if config.repo_is_public:
566575
core.config.repo_visibility = "public"
567-
if config.excluded_ecosystems and len(config.excluded_ecosystems) > 0:
568-
core.config.excluded_ecosystems = config.excluded_ecosystems
569576
integration_type = config.integration_type
570577
integration_org_slug = config.integration_org_slug or org_slug
571578
try:
@@ -613,6 +620,12 @@ def main_code():
613620
diff.diff_url = ""
614621
diff.report_url = ""
615622

623+
scan_explicit_files = (
624+
sbom_files_to_submit
625+
if sbom_files_to_submit is not None
626+
else discovered_scan_files
627+
)
628+
616629
# Handle SCM-specific flows
617630
log.debug(f"Flow decision: scm={scm is not None}, force_diff_mode={force_diff_mode}, force_api_mode={force_api_mode}, enable_diff={config.enable_diff}")
618631

@@ -684,7 +697,7 @@ def _is_unprocessed(c):
684697
log.info("Push initiated flow")
685698
if scm.check_event_type() == "diff":
686699
log.info("Starting comment logic for PR/MR event")
687-
diff = core.create_new_diff(scan_paths, params, no_change=should_skip_scan, save_files_list_path=config.save_submitted_files_list, save_manifest_tar_path=config.save_manifest_tar, base_paths=base_paths, explicit_files=sbom_files_to_submit)
700+
diff = core.create_new_diff(scan_paths, params, no_change=should_skip_scan, save_files_list_path=config.save_submitted_files_list, save_manifest_tar_path=config.save_manifest_tar, base_paths=base_paths, explicit_files=scan_explicit_files)
688701
comments = scm.get_comments_for_pr()
689702

690703
# FIXME: this overwrites diff.new_alerts, which was previously populated by Core.create_issue_alerts
@@ -807,14 +820,14 @@ def _is_unprocessed(c):
807820
)
808821
else:
809822
log.info("Starting non-PR/MR flow")
810-
diff = core.create_new_diff(scan_paths, params, no_change=should_skip_scan, save_files_list_path=config.save_submitted_files_list, save_manifest_tar_path=config.save_manifest_tar, base_paths=base_paths, explicit_files=sbom_files_to_submit)
823+
diff = core.create_new_diff(scan_paths, params, no_change=should_skip_scan, save_files_list_path=config.save_submitted_files_list, save_manifest_tar_path=config.save_manifest_tar, base_paths=base_paths, explicit_files=scan_explicit_files)
811824

812825
output_handler.handle_output(diff)
813826

814827
elif (config.enable_diff or force_diff_mode) and not force_api_mode:
815828
# New logic: --enable-diff or force_diff_mode (from --ignore-commit-files in git repos) forces diff mode
816829
log.info("Diff mode enabled without SCM integration")
817-
diff = core.create_new_diff(scan_paths, params, no_change=should_skip_scan, save_files_list_path=config.save_submitted_files_list, save_manifest_tar_path=config.save_manifest_tar, base_paths=base_paths, explicit_files=sbom_files_to_submit)
830+
diff = core.create_new_diff(scan_paths, params, no_change=should_skip_scan, save_files_list_path=config.save_submitted_files_list, save_manifest_tar_path=config.save_manifest_tar, base_paths=base_paths, explicit_files=scan_explicit_files)
818831
output_handler.handle_output(diff)
819832

820833
elif (config.enable_diff or force_diff_mode) and force_api_mode:
@@ -834,15 +847,18 @@ def _is_unprocessed(c):
834847
save_files_list_path=config.save_submitted_files_list,
835848
save_manifest_tar_path=config.save_manifest_tar,
836849
base_paths=base_paths,
837-
explicit_files=sbom_files_to_submit
850+
explicit_files=scan_explicit_files
838851
)
839852
log.info(f"Full scan created with ID: {diff.id}")
840853
log.info(f"Full scan report URL: {diff.report_url}")
841854
output_handler.handle_output(diff)
842855

843856
else:
844857
if force_api_mode:
845-
log.info("No Manifest files changed, creating Socket Report")
858+
log.info(
859+
"No supported manifest detected in the changed-file set; "
860+
"creating a full Socket report"
861+
)
846862
serializable_params = {
847863
key: value if isinstance(value, (int, float, str, list, dict, bool, type(None))) else str(value)
848864
for key, value in params.__dict__.items()
@@ -855,7 +871,7 @@ def _is_unprocessed(c):
855871
save_files_list_path=config.save_submitted_files_list,
856872
save_manifest_tar_path=config.save_manifest_tar,
857873
base_paths=base_paths,
858-
explicit_files=sbom_files_to_submit
874+
explicit_files=scan_explicit_files
859875
)
860876
log.info(f"Full scan created with ID: {diff.id}")
861877
log.info(f"Full scan report URL: {diff.report_url}")
@@ -868,7 +884,7 @@ def _is_unprocessed(c):
868884
save_files_list_path=config.save_submitted_files_list,
869885
save_manifest_tar_path=config.save_manifest_tar,
870886
base_paths=base_paths,
871-
explicit_files=sbom_files_to_submit
887+
explicit_files=scan_explicit_files
872888
)
873889
output_handler.handle_output(diff)
874890

tests/unit/test_streaming.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,19 @@ def _make(**overrides):
1818
return StreamingLogs(**kwargs)
1919

2020

21-
def test_setup_streaming_is_noop_when_register_fails():
21+
def test_setup_streaming_is_noop_when_register_fails(caplog):
2222
finalize_calls = []
23-
with patch("socketsecurity.core.streaming.register_cli_run", return_value=None), \
24-
patch("socketsecurity.core.streaming.finalize_cli_run", side_effect=lambda *a, **k: finalize_calls.append(k)):
25-
with _make(cli_name="t-fail-cli", sdk_name="t-fail-sdk") as streaming:
26-
assert isinstance(streaming, StreamingLogs)
23+
with caplog.at_level(logging.INFO, logger="t-fail-cli"):
24+
with patch("socketsecurity.core.streaming.register_cli_run", return_value=None), \
25+
patch("socketsecurity.core.streaming.finalize_cli_run", side_effect=lambda *a, **k: finalize_calls.append(k)):
26+
with _make(cli_name="t-fail-cli", sdk_name="t-fail-sdk") as streaming:
27+
assert isinstance(streaming, StreamingLogs)
2728
# No run was registered → finalize must not be called.
2829
assert finalize_calls == []
30+
assert any(
31+
"CLI run registration completed" in record.message
32+
for record in caplog.records
33+
)
2934

3035

3136
def test_clean_exit_reports_success():

0 commit comments

Comments
 (0)