1- # services/semgrep_service.py
21from pathlib import Path
32import os
43import tempfile
1918# Email + CSV
2019from services .report_service import generate_csv_for_scan , send_csv_report_email
2120
22- # -------------------------------------------------------------------
23- # Logging / Config
24- # -------------------------------------------------------------------
21+
2522logging .basicConfig (
2623 level = logging .DEBUG ,
2724 format = '%(asctime)s %(levelname)s:%(name)s: %(message)s' ,
3633genai .configure (api_key = GEMINI_API_KEY )
3734
3835
39- # -------------------------------------------------------------------
40- # Helpers
41- # -------------------------------------------------------------------
4236def clean_path (full_path : str ) -> str :
4337 """Remove temp-dir prefixes from file paths for nicer display."""
4438 try :
@@ -73,9 +67,7 @@ def get_gemini_suggestion(finding: dict, max_retries: int = 3, delay: int = 2) -
7367 return "Review this finding and apply best practices."
7468
7569
76- # -------------------------------------------------------------------
77- # Semgrep execution
78- # -------------------------------------------------------------------
70+
7971def run_semgrep (target_path : str ) -> dict :
8072 """Run Semgrep and return normalized results (failed list + summary)."""
8173 cmd = ["semgrep" , "scan" , target_path , "--config=auto" , "--json" ]
@@ -133,9 +125,7 @@ def run_semgrep(target_path: str) -> dict:
133125 raise RuntimeError (f"Semgrep failed: { str (e )} " )
134126
135127
136- # -------------------------------------------------------------------
137- # Persistence + Email (one HTML email with CSV; English; no emojis; no scan id)
138- # -------------------------------------------------------------------
128+
139129def save_scan_history (user_id , result , input_type , repo_url = None ):
140130 """Persist Semgrep results and send one HTML email (with CSV) — English, no emojis, no scan id in subject."""
141131 try :
@@ -179,10 +169,10 @@ def save_scan_history(user_id, result, input_type, repo_url=None):
179169 send_csv_report_email (
180170 to_email = user .email ,
181171 subject = subject ,
182- body_text = body , # HTML-friendly; wrapped in blue template
172+ body_text = body ,
183173 csv_path = csv_path ,
184174 csv_filename = csv_filename ,
185- user_name = user .name , # greet by name
175+ user_name = user .name ,
186176 )
187177 except Exception as e :
188178 logger .warning (f"Combined email (finish + CSV) failed for Semgrep scan_id { scan_id } : { e } " )
@@ -194,10 +184,6 @@ def save_scan_history(user_id, result, input_type, repo_url=None):
194184 db .session .rollback ()
195185 raise RuntimeError (f"Failed to save scan: { str (e )} " )
196186
197-
198- # -------------------------------------------------------------------
199- # Entry point used by route
200- # -------------------------------------------------------------------
201187def validate_semgrep (user_id , input_type , file = None , repo_url = None , content = None , extension = "py" ):
202188 """
203189 Validate inputs and run Semgrep. Returns {"scan_id": id, **result}.
0 commit comments