134 lines
3.8 KiB
Python
134 lines
3.8 KiB
Python
class ReviewPromptTemplates:
|
|
base_prompt: str = """You are an expert code reviewer analyzing staged changes in a Git repository.
|
|
|
|
Review the following code changes and provide detailed feedback on:
|
|
1. Potential bugs and security vulnerabilities
|
|
2. Code style and best practices violations
|
|
3. Performance concerns
|
|
4. Documentation issues
|
|
5. Suggestions for improvement
|
|
|
|
Respond in the following JSON format:
|
|
{{
|
|
"issues": [
|
|
{{
|
|
"file": "filename",
|
|
"line": line_number,
|
|
"severity": "critical|warning|info",
|
|
"category": "bug|security|style|performance|documentation",
|
|
"message": "description of the issue",
|
|
"suggestion": "suggested fix (if applicable)"
|
|
}}
|
|
],
|
|
"summary": {{
|
|
"critical_count": number,
|
|
"warning_count": number,
|
|
"info_count": number,
|
|
"overall_assessment": "brief summary"
|
|
}}
|
|
}}
|
|
|
|
Only include issues that match the strictness level: {strictness}
|
|
|
|
{strictness_settings}
|
|
|
|
Review the following diff:
|
|
```
|
|
{diff}
|
|
```
|
|
"""
|
|
|
|
permissive_settings: str = """Strictness: PERMISSIVE
|
|
- Only report critical security issues
|
|
- Only report definite bugs (not potential issues)
|
|
- Ignore style and formatting issues
|
|
- Ignore performance concerns
|
|
- Ignore documentation issues
|
|
"""
|
|
|
|
balanced_settings: str = """Strictness: BALANCED
|
|
- Report all security issues
|
|
- Report all definite bugs and potential bugs
|
|
- Report major style violations
|
|
- Ignore minor performance concerns
|
|
- Ignore documentation issues unless critical
|
|
"""
|
|
|
|
strict_settings: str = """Strictness: STRICT
|
|
- Report all security issues (even minor)
|
|
- Report all bugs (definite and potential)
|
|
- Report all style violations
|
|
- Report performance concerns
|
|
- Report documentation issues
|
|
- Suggest specific improvements
|
|
"""
|
|
|
|
@classmethod
|
|
def get_prompt(cls, diff: str, strictness: str = "balanced", language: str = "unknown") -> str:
|
|
settings_map = {
|
|
"permissive": cls.permissive_settings,
|
|
"balanced": cls.balanced_settings,
|
|
"strict": cls.strict_settings
|
|
}
|
|
|
|
settings = settings_map.get(strictness.lower(), cls.balanced_settings)
|
|
|
|
base = cls.base_prompt.format(
|
|
strictness=strictness.upper(),
|
|
strictness_settings=settings,
|
|
diff=diff
|
|
)
|
|
|
|
if language != "unknown":
|
|
base += f"\n\nNote: This code is in {language}. Apply {language}-specific best practices."
|
|
|
|
return base
|
|
|
|
@classmethod
|
|
def get_commit_review_prompt(cls, diff: str, commit_message: str, strictness: str = "balanced") -> str:
|
|
prompt = f"""Review the following commit with message: "{commit_message}"
|
|
|
|
Analyze whether the changes align with the commit message and provide feedback.
|
|
|
|
"""
|
|
prompt += cls.get_prompt(diff, strictness)
|
|
return prompt
|
|
|
|
@classmethod
|
|
def get_security_review_prompt(cls, diff: str) -> str:
|
|
template = """You are a security expert reviewing code changes for vulnerabilities.
|
|
|
|
Focus specifically on:
|
|
1. Injection vulnerabilities (SQL, command, code injection)
|
|
2. Authentication and authorization issues
|
|
3. Sensitive data exposure
|
|
4. Cryptographic weaknesses
|
|
5. Path traversal and file inclusion
|
|
6. Dependency security issues
|
|
|
|
Provide findings in JSON format:
|
|
```
|
|
{{
|
|
"vulnerabilities": [
|
|
{{
|
|
"file": "filename",
|
|
"line": line_number,
|
|
"severity": "critical|high|medium|low",
|
|
"type": "vulnerability type",
|
|
"description": "detailed description",
|
|
"exploit_scenario": "how it could be exploited",
|
|
"fix": "recommended fix"
|
|
}}
|
|
],
|
|
"secure_patterns": ["list of good security practices observed"],
|
|
"concerns": ["list of potential security concerns"]
|
|
}}
|
|
```
|
|
|
|
Review the following diff:
|
|
```
|
|
{diff}
|
|
```
|
|
"""
|
|
return template.format(diff=diff)
|