|
3 | 3 | from datetime import datetime |
4 | 4 |
|
5 | 5 | def generate_report(results, config): |
6 | | - report_path = config['reporting']['save_path'] |
7 | | - os.makedirs(report_path, exist_ok=True) |
8 | | - # Adjust timestamp format to be compatible with Windows file paths |
| 6 | + """Generates a report in JSON format and saves it to the specified file.""" |
| 7 | + |
| 8 | + # Check if malicious content is detected |
| 9 | + if results["malicious"]: |
| 10 | + results["message"] = ( |
| 11 | + "⚠️ WARNING: Malicious code detected! Please review the findings and address potential security issues in your code to ensure safety." |
| 12 | + ) |
| 13 | + |
| 14 | + # Check if no issues were found to add a success message |
| 15 | + elif not results["insecure"] and not results["dependencies"]: |
| 16 | + results["message"] = ( |
| 17 | + "🎉 SUCCESS! No security issues found! Your code is secure, clean, and ready for use." |
| 18 | + ) |
| 19 | + |
| 20 | + # Retrieve report directory from config or default to "reports" |
| 21 | + report_dir = config.get("reporting", {}).get("report_directory", "reports") |
| 22 | + os.makedirs(report_dir, exist_ok=True) |
| 23 | + |
| 24 | + # Generate a filename with a timestamp |
9 | 25 | timestamp = datetime.now().strftime("%Y-%m-%dT%H-%M-%S") |
10 | | - filename = os.path.join(report_path, f"scan_report_{timestamp}.json") |
| 26 | + filename = os.path.join(report_dir, f"scan_report_{timestamp}.json") |
| 27 | + |
| 28 | + # Write the JSON report to the file |
11 | 29 | with open(filename, "w") as file: |
12 | 30 | json.dump(results, file, indent=4) |
13 | | - print(f"Report saved to {filename}") |
| 31 | + |
| 32 | + # Return the path to the generated report for logging and CLI output |
| 33 | + return filename |
0 commit comments