Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /scripts /cleanup_security_alerts.py
| #!/usr/bin/env python3 | |
| """ | |
| Security Alerts Cleanup Helper | |
| Helps manage and cleanup GitHub Security alerts. | |
| Requires: pip install PyGithub | |
| Usage: | |
| export GITHUB_TOKEN=ghp_xxx | |
| python scripts/cleanup_security_alerts.py --list | |
| python scripts/cleanup_security_alerts.py --bulk-close --reason "false_positive" | |
| """ | |
| import argparse | |
| import os | |
| import sys | |
| from datetime import datetime | |
| def get_github_client(): | |
| """Initialize GitHub client.""" | |
| try: | |
| from github import Github | |
| except ImportError: | |
| print("Error: PyGithub not installed. Run: pip install PyGithub") | |
| sys.exit(1) | |
| token = os.environ.get("GITHUB_TOKEN") | |
| if not token: | |
| print("Error: GITHUB_TOKEN environment variable not set") | |
| sys.exit(1) | |
| return Github(token) | |
| def list_alerts(repo_name="SHAdd0WTAka/Zen-Ai-Pentest"): | |
| """List all open code scanning alerts.""" | |
| g = get_github_client() | |
| repo = g.get_repo(repo_name) | |
| alerts = repo.get_code_scanning_alerts(state="open") | |
| print(f"\n{'=' * 80}") | |
| print(f"Open Code Scanning Alerts for {repo_name}") | |
| print(f"{'=' * 80}\n") | |
| by_tool = {} | |
| by_severity = {"critical": 0, "high": 0, "medium": 0, "low": 0, "note": 0} | |
| for alert in alerts: | |
| tool = alert.tool.name if alert.tool else "Unknown" | |
| severity = alert.rule.severity if alert.rule else "unknown" | |
| by_tool[tool] = by_tool.get(tool, 0) + 1 | |
| by_severity[severity.lower()] = by_severity.get(severity.lower(), 0) + 1 | |
| print(f"Total open alerts: {alerts.totalCount}\n") | |
| print("By Tool:") | |
| for tool, count in sorted(by_tool.items(), key=lambda x: -x[1]): | |
| print(f" {tool}: {count}") | |
| print("\nBy Severity:") | |
| for sev, count in sorted(by_severity.items(), key=lambda x: -x[1]): | |
| if count > 0: | |
| print(f" {sev.upper()}: {count}") | |
| return alerts | |
| def close_alerts_by_tool(tool_name, reason="false_positive", repo_name="SHAdd0WTAka/Zen-Ai-Pentest"): | |
| """Close alerts from a specific tool.""" | |
| g = get_github_client() | |
| repo = g.get_repo(repo_name) | |
| alerts = repo.get_code_scanning_alerts(state="open") | |
| closed = 0 | |
| for alert in alerts: | |
| if alert.tool and alert.tool.name == tool_name: | |
| try: | |
| alert.dismiss(reason=reason, comment=f"Bulk closed via script on {datetime.now().isoformat()}") | |
| closed += 1 | |
| print(f"Closed: {alert.number} - {alert.rule.id if alert.rule else 'N/A'}") | |
| except Exception as e: | |
| print(f"Error closing alert {alert.number}: {e}") | |
| print(f"\nClosed {closed} alerts from {tool_name}") | |
| def close_old_alerts(days=30, reason="false_positive", repo_name="SHAdd0WTAka/Zen-Ai-Pentest"): | |
| """Close alerts older than specified days.""" | |
| from datetime import timedelta | |
| g = get_github_client() | |
| repo = g.get_repo(repo_name) | |
| alerts = repo.get_code_scanning_alerts(state="open") | |
| cutoff = datetime.now() - timedelta(days=days) | |
| closed = 0 | |
| for alert in alerts: | |
| created_at = alert.created_at.replace(tzinfo=None) | |
| if created_at < cutoff: | |
| try: | |
| alert.dismiss(reason=reason, comment=f"Closing old alert (> {days} days)") | |
| closed += 1 | |
| except Exception as e: | |
| print(f"Error closing alert {alert.number}: {e}") | |
| print(f"Closed {closed} alerts older than {days} days") | |
| def main(): | |
| parser = argparse.ArgumentParser(description="Manage GitHub Security alerts") | |
| parser.add_argument("--repo", default="SHAdd0WTAka/Zen-Ai-Pentest", help="Repository name (owner/repo)") | |
| parser.add_argument("--list", action="store_true", help="List all open alerts") | |
| parser.add_argument("--close-tool", help="Close all alerts from specified tool (e.g., 'Trivy')") | |
| parser.add_argument("--close-old", type=int, metavar="DAYS", help="Close alerts older than DAYS days") | |
| parser.add_argument( | |
| "--reason", default="false_positive", choices=["false_positive", "won't_fix", "used_in_tests"], help="Dismissal reason" | |
| ) | |
| args = parser.parse_args() | |
| if args.list: | |
| list_alerts(args.repo) | |
| elif args.close_tool: | |
| close_alerts_by_tool(args.close_tool, args.reason, args.repo) | |
| elif args.close_old: | |
| close_old_alerts(args.close_old, args.reason, args.repo) | |
| else: | |
| parser.print_help() | |
| if __name__ == "__main__": | |
| main() | |
Xet Storage Details
- Size:
- 4.44 kB
- Xet hash:
- 9e893dff8cb28e8e07f0649c3cc5a0e9747303fad3d737143411f7e236b5ea2d
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.