|
| 1 | +""" |
| 2 | +UCKN Issue Detection Rules Molecule |
| 3 | +
|
| 4 | +Implements a rule-based engine for identifying potential issues |
| 5 | +based on project configuration, dependencies, and common patterns. |
| 6 | +""" |
| 7 | + |
| 8 | +import logging |
| 9 | +from typing import Dict, Any, List |
| 10 | + |
| 11 | +from ..atoms.tech_stack_detector import TechStackDetector |
| 12 | + |
| 13 | +class IssueDetectionRules: |
| 14 | + """ |
| 15 | + Applies a set of predefined rules to detect potential issues in a project. |
| 16 | + """ |
| 17 | + |
| 18 | + def __init__(self, tech_stack_detector: TechStackDetector): |
| 19 | + self.tech_stack_detector = tech_stack_detector |
| 20 | + self._logger = logging.getLogger(__name__) |
| 21 | + |
| 22 | + def _detect_dependency_conflicts(self, project_stack: Dict[str, Any]) -> List[Dict[str, Any]]: |
| 23 | + """ |
| 24 | + Rule: Detect potential dependency conflicts. |
| 25 | + (Placeholder for more sophisticated logic) |
| 26 | + """ |
| 27 | + issues = [] |
| 28 | + if "Python" in project_stack.get("languages", []) and "pip" in project_stack.get("package_managers", []): |
| 29 | + # This is a simplified example. Real detection would involve parsing requirements.txt/pyproject.toml |
| 30 | + # and checking for known incompatible packages or version ranges. |
| 31 | + self._logger.info("Checking for Python dependency conflicts (rule-based).") |
| 32 | + # Example: if a project uses an old Python version with a new library |
| 33 | + # For demonstration, let's assume a rule: if Python is detected, and no specific lock file, |
| 34 | + # there's a *potential* for conflict. |
| 35 | + if not any(pm in ["poetry", "pixi"] for pm in project_stack.get("package_managers", [])): |
| 36 | + issues.append({ |
| 37 | + "type": "dependency_conflict", |
| 38 | + "description": "Potential dependency conflicts due to lack of strict dependency locking (e.g., poetry.lock, pixi.lock).", |
| 39 | + "severity": "medium", |
| 40 | + "confidence": 0.7, |
| 41 | + "preventive_measure": "Implement a dependency locking mechanism (e.g., Poetry, Pipenv, or strict requirements.txt with hashes)." |
| 42 | + }) |
| 43 | + if "JavaScript" in project_stack.get("languages", []) and "npm" in project_stack.get("package_managers", []): |
| 44 | + self._logger.info("Checking for JavaScript dependency conflicts (rule-based).") |
| 45 | + # Similar logic for package.json/package-lock.json |
| 46 | + if not (project_stack.get("project_path") and (project_stack["project_path"] / "package-lock.json").exists()): |
| 47 | + issues.append({ |
| 48 | + "type": "dependency_conflict", |
| 49 | + "description": "Potential JavaScript dependency conflicts due to missing 'package-lock.json' or 'yarn.lock'.", |
| 50 | + "severity": "medium", |
| 51 | + "confidence": 0.7, |
| 52 | + "preventive_measure": "Ensure 'package-lock.json' or 'yarn.lock' is committed to version control to guarantee consistent installations." |
| 53 | + }) |
| 54 | + return issues |
| 55 | + |
| 56 | + def _detect_build_failures(self, project_stack: Dict[str, Any]) -> List[Dict[str, Any]]: |
| 57 | + """ |
| 58 | + Rule: Detect potential build failures based on tech stack and common misconfigurations. |
| 59 | + (Placeholder for more sophisticated logic) |
| 60 | + """ |
| 61 | + issues = [] |
| 62 | + if "Python" in project_stack.get("languages", []): |
| 63 | + self._logger.info("Checking for Python build failure risks (rule-based).") |
| 64 | + # Example: Missing Dockerfile for a Python project intended for containerization |
| 65 | + if "Dockerfile" not in project_stack.get("files", []): # Assuming tech_stack_detector could list files |
| 66 | + issues.append({ |
| 67 | + "type": "build_failure_risk", |
| 68 | + "description": "No Dockerfile detected in a Python project, which might indicate a missing containerization strategy for deployment.", |
| 69 | + "severity": "low", |
| 70 | + "confidence": 0.6, |
| 71 | + "preventive_measure": "Consider adding a Dockerfile for consistent build and deployment environments." |
| 72 | + }) |
| 73 | + if "JavaScript" in project_stack.get("languages", []): |
| 74 | + self._logger.info("Checking for JavaScript build failure risks (rule-based).") |
| 75 | + # Example: Missing build script in package.json for a frontend project |
| 76 | + # This would require parsing package.json, which is beyond current TechStackDetector scope. |
| 77 | + # For now, a generic rule. |
| 78 | + issues.append({ |
| 79 | + "type": "build_failure_risk", |
| 80 | + "description": "Ensure 'build' scripts are properly configured in 'package.json' for production builds.", |
| 81 | + "severity": "low", |
| 82 | + "confidence": 0.5, |
| 83 | + "preventive_measure": "Verify 'scripts' section in 'package.json' includes a robust 'build' command." |
| 84 | + }) |
| 85 | + return issues |
| 86 | + |
| 87 | + def _detect_test_flakiness(self, project_stack: Dict[str, Any]) -> List[Dict[str, Any]]: |
| 88 | + """ |
| 89 | + Rule: Detect potential test flakiness indicators. |
| 90 | + (Placeholder for more sophisticated logic) |
| 91 | + """ |
| 92 | + issues = [] |
| 93 | + if "pytest" in project_stack.get("testing", []): |
| 94 | + self._logger.info("Checking for Pytest flakiness indicators (rule-based).") |
| 95 | + # Example: Presence of certain patterns in test files (e.g., reliance on global state, sleep calls) |
| 96 | + # This would require code analysis, which is not in scope for this molecule yet. |
| 97 | + issues.append({ |
| 98 | + "type": "test_flakiness_risk", |
| 99 | + "description": "Potential for test flakiness. Review tests for reliance on external state, timing issues, or non-deterministic behavior.", |
| 100 | + "severity": "medium", |
| 101 | + "confidence": 0.6, |
| 102 | + "preventive_measure": "Implement test isolation, use mocking/patching, and avoid `time.sleep()` in tests. Consider a flakiness detection tool." |
| 103 | + }) |
| 104 | + return issues |
| 105 | + |
| 106 | + def _detect_performance_bottlenecks(self, project_stack: Dict[str, Any]) -> List[Dict[str, Any]]: |
| 107 | + """ |
| 108 | + Rule: Detect potential performance bottlenecks based on tech stack. |
| 109 | + (Placeholder for more sophisticated logic) |
| 110 | + """ |
| 111 | + issues = [] |
| 112 | + if "Python" in project_stack.get("languages", []): |
| 113 | + self._logger.info("Checking for Python performance risks (rule-based).") |
| 114 | + issues.append({ |
| 115 | + "type": "performance_bottleneck_risk", |
| 116 | + "description": "Consider using asynchronous programming (asyncio) or optimizing database queries for I/O-bound Python applications.", |
| 117 | + "severity": "low", |
| 118 | + "confidence": 0.5, |
| 119 | + "preventive_measure": "Profile your application to identify hotspots. Optimize database interactions and consider caching strategies." |
| 120 | + }) |
| 121 | + if "JavaScript" in project_stack.get("languages", []): |
| 122 | + self._logger.info("Checking for JavaScript performance risks (rule-based).") |
| 123 | + issues.append({ |
| 124 | + "type": "performance_bottleneck_risk", |
| 125 | + "description": "Large bundle sizes or unoptimized image assets can lead to slow loading times in web applications.", |
| 126 | + "severity": "medium", |
| 127 | + "confidence": 0.6, |
| 128 | + "preventive_measure": "Implement code splitting, lazy loading, and image optimization techniques. Use Lighthouse or similar tools for auditing." |
| 129 | + }) |
| 130 | + return issues |
| 131 | + |
| 132 | + def _detect_security_vulnerabilities(self, project_stack: Dict[str, Any]) -> List[Dict[str, Any]]: |
| 133 | + """ |
| 134 | + Rule: Detect potential security vulnerabilities based on tech stack and common practices. |
| 135 | + (Placeholder for more sophisticated logic) |
| 136 | + """ |
| 137 | + issues = [] |
| 138 | + if "Python" in project_stack.get("languages", []): |
| 139 | + self._logger.info("Checking for Python security risks (rule-based).") |
| 140 | + issues.append({ |
| 141 | + "type": "security_vulnerability_risk", |
| 142 | + "description": "Ensure all dependencies are up-to-date to mitigate known vulnerabilities. Use tools like Bandit or Snyk.", |
| 143 | + "severity": "high", |
| 144 | + "confidence": 0.7, |
| 145 | + "preventive_measure": "Regularly audit dependencies for known CVEs. Implement secure coding practices (e.g., input validation, proper error handling)." |
| 146 | + }) |
| 147 | + if "JavaScript" in project_stack.get("languages", []): |
| 148 | + self._logger.info("Checking for JavaScript security risks (rule-based).") |
| 149 | + issues.append({ |
| 150 | + "type": "security_vulnerability_risk", |
| 151 | + "description": "Client-side JavaScript applications are susceptible to XSS and CSRF. Server-side Node.js apps need protection against injection attacks.", |
| 152 | + "severity": "high", |
| 153 | + "confidence": 0.7, |
| 154 | + "preventive_measure": "Sanitize all user inputs. Use Content Security Policy (CSP). Implement proper authentication and authorization. Keep Node.js dependencies updated." |
| 155 | + }) |
| 156 | + return issues |
| 157 | + |
| 158 | + def analyze_project_for_rules(self, project_path: str) -> List[Dict[str, Any]]: |
| 159 | + """ |
| 160 | + Analyzes a project using rule-based detection. |
| 161 | +
|
| 162 | + Args: |
| 163 | + project_path: The path to the project directory. |
| 164 | +
|
| 165 | + Returns: |
| 166 | + A list of dictionaries, each representing a detected issue. |
| 167 | + """ |
| 168 | + self._logger.info(f"Starting rule-based analysis for project: {project_path}") |
| 169 | + project_stack = self.tech_stack_detector.analyze_project(project_path) |
| 170 | + project_stack["project_path"] = project_path # Add path for potential file checks |
| 171 | + |
| 172 | + detected_issues = [] |
| 173 | + |
| 174 | + # Apply various rule sets |
| 175 | + detected_issues.extend(self._detect_dependency_conflicts(project_stack)) |
| 176 | + detected_issues.extend(self._detect_build_failures(project_stack)) |
| 177 | + detected_issues.extend(self._detect_test_flakiness(project_stack)) |
| 178 | + detected_issues.extend(self._detect_performance_bottlenecks(project_stack)) |
| 179 | + detected_issues.extend(self._detect_security_vulnerabilities(project_stack)) |
| 180 | + |
| 181 | + self._logger.info(f"Rule-based analysis complete. Found {len(detected_issues)} potential issues.") |
| 182 | + return detected_issues |
| 183 | + |
0 commit comments